comparison perl-5.22.2/sv.c @ 8045:a16537d2fe07

<xfix> tar xf perl-5.22.2.tar.gz # Ah, whatever, I\'m doing it anyway
author HackBot
date Sat, 14 May 2016 14:54:38 +0000
parents
children
comparison
equal deleted inserted replaced
8044:711c038a7dce 8045:a16537d2fe07
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 #ifdef __VMS
35 # include <rms.h>
36 #endif
37
38 #ifdef __Lynx__
39 /* Missing proto on LynxOS */
40 char *gconvert(double, int, int, char *);
41 #endif
42
43 #ifdef USE_QUADMATH
44 # define SNPRINTF_G(nv, buffer, size, ndig) \
45 quadmath_snprintf(buffer, size, "%.*Qg", (int)ndig, (NV)(nv))
46 #else
47 # define SNPRINTF_G(nv, buffer, size, ndig) \
48 PERL_UNUSED_RESULT(Gconvert((NV)(nv), (int)ndig, 0, buffer))
49 #endif
50
51 #ifndef SV_COW_THRESHOLD
52 # define SV_COW_THRESHOLD 0 /* COW iff len > K */
53 #endif
54 #ifndef SV_COWBUF_THRESHOLD
55 # define SV_COWBUF_THRESHOLD 1250 /* COW iff len > K */
56 #endif
57 #ifndef SV_COW_MAX_WASTE_THRESHOLD
58 # define SV_COW_MAX_WASTE_THRESHOLD 80 /* COW iff (len - cur) < K */
59 #endif
60 #ifndef SV_COWBUF_WASTE_THRESHOLD
61 # define SV_COWBUF_WASTE_THRESHOLD 80 /* COW iff (len - cur) < K */
62 #endif
63 #ifndef SV_COW_MAX_WASTE_FACTOR_THRESHOLD
64 # define SV_COW_MAX_WASTE_FACTOR_THRESHOLD 2 /* COW iff len < (cur * K) */
65 #endif
66 #ifndef SV_COWBUF_WASTE_FACTOR_THRESHOLD
67 # define SV_COWBUF_WASTE_FACTOR_THRESHOLD 2 /* COW iff len < (cur * K) */
68 #endif
69 /* Work around compiler warnings about unsigned >= THRESHOLD when thres-
70 hold is 0. */
71 #if SV_COW_THRESHOLD
72 # define GE_COW_THRESHOLD(cur) ((cur) >= SV_COW_THRESHOLD)
73 #else
74 # define GE_COW_THRESHOLD(cur) 1
75 #endif
76 #if SV_COWBUF_THRESHOLD
77 # define GE_COWBUF_THRESHOLD(cur) ((cur) >= SV_COWBUF_THRESHOLD)
78 #else
79 # define GE_COWBUF_THRESHOLD(cur) 1
80 #endif
81 #if SV_COW_MAX_WASTE_THRESHOLD
82 # define GE_COW_MAX_WASTE_THRESHOLD(cur,len) (((len)-(cur)) < SV_COW_MAX_WASTE_THRESHOLD)
83 #else
84 # define GE_COW_MAX_WASTE_THRESHOLD(cur,len) 1
85 #endif
86 #if SV_COWBUF_WASTE_THRESHOLD
87 # define GE_COWBUF_WASTE_THRESHOLD(cur,len) (((len)-(cur)) < SV_COWBUF_WASTE_THRESHOLD)
88 #else
89 # define GE_COWBUF_WASTE_THRESHOLD(cur,len) 1
90 #endif
91 #if SV_COW_MAX_WASTE_FACTOR_THRESHOLD
92 # define GE_COW_MAX_WASTE_FACTOR_THRESHOLD(cur,len) ((len) < SV_COW_MAX_WASTE_FACTOR_THRESHOLD * (cur))
93 #else
94 # define GE_COW_MAX_WASTE_FACTOR_THRESHOLD(cur,len) 1
95 #endif
96 #if SV_COWBUF_WASTE_FACTOR_THRESHOLD
97 # define GE_COWBUF_WASTE_FACTOR_THRESHOLD(cur,len) ((len) < SV_COWBUF_WASTE_FACTOR_THRESHOLD * (cur))
98 #else
99 # define GE_COWBUF_WASTE_FACTOR_THRESHOLD(cur,len) 1
100 #endif
101
102 #define CHECK_COW_THRESHOLD(cur,len) (\
103 GE_COW_THRESHOLD((cur)) && \
104 GE_COW_MAX_WASTE_THRESHOLD((cur),(len)) && \
105 GE_COW_MAX_WASTE_FACTOR_THRESHOLD((cur),(len)) \
106 )
107 #define CHECK_COWBUF_THRESHOLD(cur,len) (\
108 GE_COWBUF_THRESHOLD((cur)) && \
109 GE_COWBUF_WASTE_THRESHOLD((cur),(len)) && \
110 GE_COWBUF_WASTE_FACTOR_THRESHOLD((cur),(len)) \
111 )
112
113 #ifdef PERL_UTF8_CACHE_ASSERT
114 /* if adding more checks watch out for the following tests:
115 * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
116 * lib/utf8.t lib/Unicode/Collate/t/index.t
117 * --jhi
118 */
119 # define ASSERT_UTF8_CACHE(cache) \
120 STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); \
121 assert((cache)[2] <= (cache)[3]); \
122 assert((cache)[3] <= (cache)[1]);} \
123 } STMT_END
124 #else
125 # define ASSERT_UTF8_CACHE(cache) NOOP
126 #endif
127
128 #ifdef PERL_OLD_COPY_ON_WRITE
129 #define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
130 #define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next))
131 #endif
132
133 /* ============================================================================
134
135 =head1 Allocation and deallocation of SVs.
136 An SV (or AV, HV, etc.) is allocated in two parts: the head (struct
137 sv, av, hv...) contains type and reference count information, and for
138 many types, a pointer to the body (struct xrv, xpv, xpviv...), which
139 contains fields specific to each type. Some types store all they need
140 in the head, so don't have a body.
141
142 In all but the most memory-paranoid configurations (ex: PURIFY), heads
143 and bodies are allocated out of arenas, which by default are
144 approximately 4K chunks of memory parcelled up into N heads or bodies.
145 Sv-bodies are allocated by their sv-type, guaranteeing size
146 consistency needed to allocate safely from arrays.
147
148 For SV-heads, the first slot in each arena is reserved, and holds a
149 link to the next arena, some flags, and a note of the number of slots.
150 Snaked through each arena chain is a linked list of free items; when
151 this becomes empty, an extra arena is allocated and divided up into N
152 items which are threaded into the free list.
153
154 SV-bodies are similar, but they use arena-sets by default, which
155 separate the link and info from the arena itself, and reclaim the 1st
156 slot in the arena. SV-bodies are further described later.
157
158 The following global variables are associated with arenas:
159
160 PL_sv_arenaroot pointer to list of SV arenas
161 PL_sv_root pointer to list of free SV structures
162
163 PL_body_arenas head of linked-list of body arenas
164 PL_body_roots[] array of pointers to list of free bodies of svtype
165 arrays are indexed by the svtype needed
166
167 A few special SV heads are not allocated from an arena, but are
168 instead directly created in the interpreter structure, eg PL_sv_undef.
169 The size of arenas can be changed from the default by setting
170 PERL_ARENA_SIZE appropriately at compile time.
171
172 The SV arena serves the secondary purpose of allowing still-live SVs
173 to be located and destroyed during final cleanup.
174
175 At the lowest level, the macros new_SV() and del_SV() grab and free
176 an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
177 to return the SV to the free list with error checking.) new_SV() calls
178 more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
179 SVs in the free list have their SvTYPE field set to all ones.
180
181 At the time of very final cleanup, sv_free_arenas() is called from
182 perl_destruct() to physically free all the arenas allocated since the
183 start of the interpreter.
184
185 The function visit() scans the SV arenas list, and calls a specified
186 function for each SV it finds which is still live - ie which has an SvTYPE
187 other than all 1's, and a non-zero SvREFCNT. visit() is used by the
188 following functions (specified as [function that calls visit()] / [function
189 called by visit() for each SV]):
190
191 sv_report_used() / do_report_used()
192 dump all remaining SVs (debugging aid)
193
194 sv_clean_objs() / do_clean_objs(),do_clean_named_objs(),
195 do_clean_named_io_objs(),do_curse()
196 Attempt to free all objects pointed to by RVs,
197 try to do the same for all objects indir-
198 ectly referenced by typeglobs too, and
199 then do a final sweep, cursing any
200 objects that remain. Called once from
201 perl_destruct(), prior to calling sv_clean_all()
202 below.
203
204 sv_clean_all() / do_clean_all()
205 SvREFCNT_dec(sv) each remaining SV, possibly
206 triggering an sv_free(). It also sets the
207 SVf_BREAK flag on the SV to indicate that the
208 refcnt has been artificially lowered, and thus
209 stopping sv_free() from giving spurious warnings
210 about SVs which unexpectedly have a refcnt
211 of zero. called repeatedly from perl_destruct()
212 until there are no SVs left.
213
214 =head2 Arena allocator API Summary
215
216 Private API to rest of sv.c
217
218 new_SV(), del_SV(),
219
220 new_XPVNV(), del_XPVGV(),
221 etc
222
223 Public API:
224
225 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
226
227 =cut
228
229 * ========================================================================= */
230
231 /*
232 * "A time to plant, and a time to uproot what was planted..."
233 */
234
235 #ifdef PERL_MEM_LOG
236 # define MEM_LOG_NEW_SV(sv, file, line, func) \
237 Perl_mem_log_new_sv(sv, file, line, func)
238 # define MEM_LOG_DEL_SV(sv, file, line, func) \
239 Perl_mem_log_del_sv(sv, file, line, func)
240 #else
241 # define MEM_LOG_NEW_SV(sv, file, line, func) NOOP
242 # define MEM_LOG_DEL_SV(sv, file, line, func) NOOP
243 #endif
244
245 #ifdef DEBUG_LEAKING_SCALARS
246 # define FREE_SV_DEBUG_FILE(sv) STMT_START { \
247 if ((sv)->sv_debug_file) PerlMemShared_free((sv)->sv_debug_file); \
248 } STMT_END
249 # define DEBUG_SV_SERIAL(sv) \
250 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) del_SV\n", \
251 PTR2UV(sv), (long)(sv)->sv_debug_serial))
252 #else
253 # define FREE_SV_DEBUG_FILE(sv)
254 # define DEBUG_SV_SERIAL(sv) NOOP
255 #endif
256
257 #ifdef PERL_POISON
258 # define SvARENA_CHAIN(sv) ((sv)->sv_u.svu_rv)
259 # define SvARENA_CHAIN_SET(sv,val) (sv)->sv_u.svu_rv = MUTABLE_SV((val))
260 /* Whilst I'd love to do this, it seems that things like to check on
261 unreferenced scalars
262 # define POISON_SV_HEAD(sv) PoisonNew(sv, 1, struct STRUCT_SV)
263 */
264 # define POISON_SV_HEAD(sv) PoisonNew(&SvANY(sv), 1, void *), \
265 PoisonNew(&SvREFCNT(sv), 1, U32)
266 #else
267 # define SvARENA_CHAIN(sv) SvANY(sv)
268 # define SvARENA_CHAIN_SET(sv,val) SvANY(sv) = (void *)(val)
269 # define POISON_SV_HEAD(sv)
270 #endif
271
272 /* Mark an SV head as unused, and add to free list.
273 *
274 * If SVf_BREAK is set, skip adding it to the free list, as this SV had
275 * its refcount artificially decremented during global destruction, so
276 * there may be dangling pointers to it. The last thing we want in that
277 * case is for it to be reused. */
278
279 #define plant_SV(p) \
280 STMT_START { \
281 const U32 old_flags = SvFLAGS(p); \
282 MEM_LOG_DEL_SV(p, __FILE__, __LINE__, FUNCTION__); \
283 DEBUG_SV_SERIAL(p); \
284 FREE_SV_DEBUG_FILE(p); \
285 POISON_SV_HEAD(p); \
286 SvFLAGS(p) = SVTYPEMASK; \
287 if (!(old_flags & SVf_BREAK)) { \
288 SvARENA_CHAIN_SET(p, PL_sv_root); \
289 PL_sv_root = (p); \
290 } \
291 --PL_sv_count; \
292 } STMT_END
293
294 #define uproot_SV(p) \
295 STMT_START { \
296 (p) = PL_sv_root; \
297 PL_sv_root = MUTABLE_SV(SvARENA_CHAIN(p)); \
298 ++PL_sv_count; \
299 } STMT_END
300
301
302 /* make some more SVs by adding another arena */
303
304 STATIC SV*
305 S_more_sv(pTHX)
306 {
307 SV* sv;
308 char *chunk; /* must use New here to match call to */
309 Newx(chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
310 sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
311 uproot_SV(sv);
312 return sv;
313 }
314
315 /* new_SV(): return a new, empty SV head */
316
317 #ifdef DEBUG_LEAKING_SCALARS
318 /* provide a real function for a debugger to play with */
319 STATIC SV*
320 S_new_SV(pTHX_ const char *file, int line, const char *func)
321 {
322 SV* sv;
323
324 if (PL_sv_root)
325 uproot_SV(sv);
326 else
327 sv = S_more_sv(aTHX);
328 SvANY(sv) = 0;
329 SvREFCNT(sv) = 1;
330 SvFLAGS(sv) = 0;
331 sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
332 sv->sv_debug_line = (U16) (PL_parser && PL_parser->copline != NOLINE
333 ? PL_parser->copline
334 : PL_curcop
335 ? CopLINE(PL_curcop)
336 : 0
337 );
338 sv->sv_debug_inpad = 0;
339 sv->sv_debug_parent = NULL;
340 sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL;
341
342 sv->sv_debug_serial = PL_sv_serial++;
343
344 MEM_LOG_NEW_SV(sv, file, line, func);
345 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) new_SV (from %s:%d [%s])\n",
346 PTR2UV(sv), (long)sv->sv_debug_serial, file, line, func));
347
348 return sv;
349 }
350 # define new_SV(p) (p)=S_new_SV(aTHX_ __FILE__, __LINE__, FUNCTION__)
351
352 #else
353 # define new_SV(p) \
354 STMT_START { \
355 if (PL_sv_root) \
356 uproot_SV(p); \
357 else \
358 (p) = S_more_sv(aTHX); \
359 SvANY(p) = 0; \
360 SvREFCNT(p) = 1; \
361 SvFLAGS(p) = 0; \
362 MEM_LOG_NEW_SV(p, __FILE__, __LINE__, FUNCTION__); \
363 } STMT_END
364 #endif
365
366
367 /* del_SV(): return an empty SV head to the free list */
368
369 #ifdef DEBUGGING
370
371 #define del_SV(p) \
372 STMT_START { \
373 if (DEBUG_D_TEST) \
374 del_sv(p); \
375 else \
376 plant_SV(p); \
377 } STMT_END
378
379 STATIC void
380 S_del_sv(pTHX_ SV *p)
381 {
382 PERL_ARGS_ASSERT_DEL_SV;
383
384 if (DEBUG_D_TEST) {
385 SV* sva;
386 bool ok = 0;
387 for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
388 const SV * const sv = sva + 1;
389 const SV * const svend = &sva[SvREFCNT(sva)];
390 if (p >= sv && p < svend) {
391 ok = 1;
392 break;
393 }
394 }
395 if (!ok) {
396 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
397 "Attempt to free non-arena SV: 0x%"UVxf
398 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
399 return;
400 }
401 }
402 plant_SV(p);
403 }
404
405 #else /* ! DEBUGGING */
406
407 #define del_SV(p) plant_SV(p)
408
409 #endif /* DEBUGGING */
410
411 /*
412 * Bodyless IVs and NVs!
413 *
414 * Since 5.9.2, we can avoid allocating a body for SVt_IV-type SVs.
415 * Since the larger IV-holding variants of SVs store their integer
416 * values in their respective bodies, the family of SvIV() accessor
417 * macros would naively have to branch on the SV type to find the
418 * integer value either in the HEAD or BODY. In order to avoid this
419 * expensive branch, a clever soul has deployed a great hack:
420 * We set up the SvANY pointer such that instead of pointing to a
421 * real body, it points into the memory before the location of the
422 * head. We compute this pointer such that the location of
423 * the integer member of the hypothetical body struct happens to
424 * be the same as the location of the integer member of the bodyless
425 * SV head. This now means that the SvIV() family of accessors can
426 * always read from the (hypothetical or real) body via SvANY.
427 *
428 * Since the 5.21 dev series, we employ the same trick for NVs
429 * if the architecture can support it (NVSIZE <= IVSIZE).
430 */
431
432 /* The following two macros compute the necessary offsets for the above
433 * trick and store them in SvANY for SvIV() (and friends) to use. */
434 #define SET_SVANY_FOR_BODYLESS_IV(sv) \
435 SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv))
436
437 #define SET_SVANY_FOR_BODYLESS_NV(sv) \
438 SvANY(sv) = (XPVNV*)((char*)&(sv->sv_u.svu_nv) - STRUCT_OFFSET(XPVNV, xnv_u.xnv_nv))
439
440 /*
441 =head1 SV Manipulation Functions
442
443 =for apidoc sv_add_arena
444
445 Given a chunk of memory, link it to the head of the list of arenas,
446 and split it into a list of free SVs.
447
448 =cut
449 */
450
451 static void
452 S_sv_add_arena(pTHX_ char *const ptr, const U32 size, const U32 flags)
453 {
454 SV *const sva = MUTABLE_SV(ptr);
455 SV* sv;
456 SV* svend;
457
458 PERL_ARGS_ASSERT_SV_ADD_ARENA;
459
460 /* The first SV in an arena isn't an SV. */
461 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
462 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
463 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
464
465 PL_sv_arenaroot = sva;
466 PL_sv_root = sva + 1;
467
468 svend = &sva[SvREFCNT(sva) - 1];
469 sv = sva + 1;
470 while (sv < svend) {
471 SvARENA_CHAIN_SET(sv, (sv + 1));
472 #ifdef DEBUGGING
473 SvREFCNT(sv) = 0;
474 #endif
475 /* Must always set typemask because it's always checked in on cleanup
476 when the arenas are walked looking for objects. */
477 SvFLAGS(sv) = SVTYPEMASK;
478 sv++;
479 }
480 SvARENA_CHAIN_SET(sv, 0);
481 #ifdef DEBUGGING
482 SvREFCNT(sv) = 0;
483 #endif
484 SvFLAGS(sv) = SVTYPEMASK;
485 }
486
487 /* visit(): call the named function for each non-free SV in the arenas
488 * whose flags field matches the flags/mask args. */
489
490 STATIC I32
491 S_visit(pTHX_ SVFUNC_t f, const U32 flags, const U32 mask)
492 {
493 SV* sva;
494 I32 visited = 0;
495
496 PERL_ARGS_ASSERT_VISIT;
497
498 for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
499 const SV * const svend = &sva[SvREFCNT(sva)];
500 SV* sv;
501 for (sv = sva + 1; sv < svend; ++sv) {
502 if (SvTYPE(sv) != (svtype)SVTYPEMASK
503 && (sv->sv_flags & mask) == flags
504 && SvREFCNT(sv))
505 {
506 (*f)(aTHX_ sv);
507 ++visited;
508 }
509 }
510 }
511 return visited;
512 }
513
514 #ifdef DEBUGGING
515
516 /* called by sv_report_used() for each live SV */
517
518 static void
519 do_report_used(pTHX_ SV *const sv)
520 {
521 if (SvTYPE(sv) != (svtype)SVTYPEMASK) {
522 PerlIO_printf(Perl_debug_log, "****\n");
523 sv_dump(sv);
524 }
525 }
526 #endif
527
528 /*
529 =for apidoc sv_report_used
530
531 Dump the contents of all SVs not yet freed (debugging aid).
532
533 =cut
534 */
535
536 void
537 Perl_sv_report_used(pTHX)
538 {
539 #ifdef DEBUGGING
540 visit(do_report_used, 0, 0);
541 #else
542 PERL_UNUSED_CONTEXT;
543 #endif
544 }
545
546 /* called by sv_clean_objs() for each live SV */
547
548 static void
549 do_clean_objs(pTHX_ SV *const ref)
550 {
551 assert (SvROK(ref));
552 {
553 SV * const target = SvRV(ref);
554 if (SvOBJECT(target)) {
555 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(ref)));
556 if (SvWEAKREF(ref)) {
557 sv_del_backref(target, ref);
558 SvWEAKREF_off(ref);
559 SvRV_set(ref, NULL);
560 } else {
561 SvROK_off(ref);
562 SvRV_set(ref, NULL);
563 SvREFCNT_dec_NN(target);
564 }
565 }
566 }
567 }
568
569
570 /* clear any slots in a GV which hold objects - except IO;
571 * called by sv_clean_objs() for each live GV */
572
573 static void
574 do_clean_named_objs(pTHX_ SV *const sv)
575 {
576 SV *obj;
577 assert(SvTYPE(sv) == SVt_PVGV);
578 assert(isGV_with_GP(sv));
579 if (!GvGP(sv))
580 return;
581
582 /* freeing GP entries may indirectly free the current GV;
583 * hold onto it while we mess with the GP slots */
584 SvREFCNT_inc(sv);
585
586 if ( ((obj = GvSV(sv) )) && SvOBJECT(obj)) {
587 DEBUG_D((PerlIO_printf(Perl_debug_log,
588 "Cleaning named glob SV object:\n "), sv_dump(obj)));
589 GvSV(sv) = NULL;
590 SvREFCNT_dec_NN(obj);
591 }
592 if ( ((obj = MUTABLE_SV(GvAV(sv)) )) && SvOBJECT(obj)) {
593 DEBUG_D((PerlIO_printf(Perl_debug_log,
594 "Cleaning named glob AV object:\n "), sv_dump(obj)));
595 GvAV(sv) = NULL;
596 SvREFCNT_dec_NN(obj);
597 }
598 if ( ((obj = MUTABLE_SV(GvHV(sv)) )) && SvOBJECT(obj)) {
599 DEBUG_D((PerlIO_printf(Perl_debug_log,
600 "Cleaning named glob HV object:\n "), sv_dump(obj)));
601 GvHV(sv) = NULL;
602 SvREFCNT_dec_NN(obj);
603 }
604 if ( ((obj = MUTABLE_SV(GvCV(sv)) )) && SvOBJECT(obj)) {
605 DEBUG_D((PerlIO_printf(Perl_debug_log,
606 "Cleaning named glob CV object:\n "), sv_dump(obj)));
607 GvCV_set(sv, NULL);
608 SvREFCNT_dec_NN(obj);
609 }
610 SvREFCNT_dec_NN(sv); /* undo the inc above */
611 }
612
613 /* clear any IO slots in a GV which hold objects (except stderr, defout);
614 * called by sv_clean_objs() for each live GV */
615
616 static void
617 do_clean_named_io_objs(pTHX_ SV *const sv)
618 {
619 SV *obj;
620 assert(SvTYPE(sv) == SVt_PVGV);
621 assert(isGV_with_GP(sv));
622 if (!GvGP(sv) || sv == (SV*)PL_stderrgv || sv == (SV*)PL_defoutgv)
623 return;
624
625 SvREFCNT_inc(sv);
626 if ( ((obj = MUTABLE_SV(GvIO(sv)) )) && SvOBJECT(obj)) {
627 DEBUG_D((PerlIO_printf(Perl_debug_log,
628 "Cleaning named glob IO object:\n "), sv_dump(obj)));
629 GvIOp(sv) = NULL;
630 SvREFCNT_dec_NN(obj);
631 }
632 SvREFCNT_dec_NN(sv); /* undo the inc above */
633 }
634
635 /* Void wrapper to pass to visit() */
636 static void
637 do_curse(pTHX_ SV * const sv) {
638 if ((PL_stderrgv && GvGP(PL_stderrgv) && (SV*)GvIO(PL_stderrgv) == sv)
639 || (PL_defoutgv && GvGP(PL_defoutgv) && (SV*)GvIO(PL_defoutgv) == sv))
640 return;
641 (void)curse(sv, 0);
642 }
643
644 /*
645 =for apidoc sv_clean_objs
646
647 Attempt to destroy all objects not yet freed.
648
649 =cut
650 */
651
652 void
653 Perl_sv_clean_objs(pTHX)
654 {
655 GV *olddef, *olderr;
656 PL_in_clean_objs = TRUE;
657 visit(do_clean_objs, SVf_ROK, SVf_ROK);
658 /* Some barnacles may yet remain, clinging to typeglobs.
659 * Run the non-IO destructors first: they may want to output
660 * error messages, close files etc */
661 visit(do_clean_named_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
662 visit(do_clean_named_io_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
663 /* And if there are some very tenacious barnacles clinging to arrays,
664 closures, or what have you.... */
665 visit(do_curse, SVs_OBJECT, SVs_OBJECT);
666 olddef = PL_defoutgv;
667 PL_defoutgv = NULL; /* disable skip of PL_defoutgv */
668 if (olddef && isGV_with_GP(olddef))
669 do_clean_named_io_objs(aTHX_ MUTABLE_SV(olddef));
670 olderr = PL_stderrgv;
671 PL_stderrgv = NULL; /* disable skip of PL_stderrgv */
672 if (olderr && isGV_with_GP(olderr))
673 do_clean_named_io_objs(aTHX_ MUTABLE_SV(olderr));
674 SvREFCNT_dec(olddef);
675 PL_in_clean_objs = FALSE;
676 }
677
678 /* called by sv_clean_all() for each live SV */
679
680 static void
681 do_clean_all(pTHX_ SV *const sv)
682 {
683 if (sv == (const SV *) PL_fdpid || sv == (const SV *)PL_strtab) {
684 /* don't clean pid table and strtab */
685 return;
686 }
687 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
688 SvFLAGS(sv) |= SVf_BREAK;
689 SvREFCNT_dec_NN(sv);
690 }
691
692 /*
693 =for apidoc sv_clean_all
694
695 Decrement the refcnt of each remaining SV, possibly triggering a
696 cleanup. This function may have to be called multiple times to free
697 SVs which are in complex self-referential hierarchies.
698
699 =cut
700 */
701
702 I32
703 Perl_sv_clean_all(pTHX)
704 {
705 I32 cleaned;
706 PL_in_clean_all = TRUE;
707 cleaned = visit(do_clean_all, 0,0);
708 return cleaned;
709 }
710
711 /*
712 ARENASETS: a meta-arena implementation which separates arena-info
713 into struct arena_set, which contains an array of struct
714 arena_descs, each holding info for a single arena. By separating
715 the meta-info from the arena, we recover the 1st slot, formerly
716 borrowed for list management. The arena_set is about the size of an
717 arena, avoiding the needless malloc overhead of a naive linked-list.
718
719 The cost is 1 arena-set malloc per ~320 arena-mallocs, + the unused
720 memory in the last arena-set (1/2 on average). In trade, we get
721 back the 1st slot in each arena (ie 1.7% of a CV-arena, less for
722 smaller types). The recovery of the wasted space allows use of
723 small arenas for large, rare body types, by changing array* fields
724 in body_details_by_type[] below.
725 */
726 struct arena_desc {
727 char *arena; /* the raw storage, allocated aligned */
728 size_t size; /* its size ~4k typ */
729 svtype utype; /* bodytype stored in arena */
730 };
731
732 struct arena_set;
733
734 /* Get the maximum number of elements in set[] such that struct arena_set
735 will fit within PERL_ARENA_SIZE, which is probably just under 4K, and
736 therefore likely to be 1 aligned memory page. */
737
738 #define ARENAS_PER_SET ((PERL_ARENA_SIZE - sizeof(struct arena_set*) \
739 - 2 * sizeof(int)) / sizeof (struct arena_desc))
740
741 struct arena_set {
742 struct arena_set* next;
743 unsigned int set_size; /* ie ARENAS_PER_SET */
744 unsigned int curr; /* index of next available arena-desc */
745 struct arena_desc set[ARENAS_PER_SET];
746 };
747
748 /*
749 =for apidoc sv_free_arenas
750
751 Deallocate the memory used by all arenas. Note that all the individual SV
752 heads and bodies within the arenas must already have been freed.
753
754 =cut
755
756 */
757 void
758 Perl_sv_free_arenas(pTHX)
759 {
760 SV* sva;
761 SV* svanext;
762 unsigned int i;
763
764 /* Free arenas here, but be careful about fake ones. (We assume
765 contiguity of the fake ones with the corresponding real ones.) */
766
767 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
768 svanext = MUTABLE_SV(SvANY(sva));
769 while (svanext && SvFAKE(svanext))
770 svanext = MUTABLE_SV(SvANY(svanext));
771
772 if (!SvFAKE(sva))
773 Safefree(sva);
774 }
775
776 {
777 struct arena_set *aroot = (struct arena_set*) PL_body_arenas;
778
779 while (aroot) {
780 struct arena_set *current = aroot;
781 i = aroot->curr;
782 while (i--) {
783 assert(aroot->set[i].arena);
784 Safefree(aroot->set[i].arena);
785 }
786 aroot = aroot->next;
787 Safefree(current);
788 }
789 }
790 PL_body_arenas = 0;
791
792 i = PERL_ARENA_ROOTS_SIZE;
793 while (i--)
794 PL_body_roots[i] = 0;
795
796 PL_sv_arenaroot = 0;
797 PL_sv_root = 0;
798 }
799
800 /*
801 Here are mid-level routines that manage the allocation of bodies out
802 of the various arenas. There are 5 kinds of arenas:
803
804 1. SV-head arenas, which are discussed and handled above
805 2. regular body arenas
806 3. arenas for reduced-size bodies
807 4. Hash-Entry arenas
808
809 Arena types 2 & 3 are chained by body-type off an array of
810 arena-root pointers, which is indexed by svtype. Some of the
811 larger/less used body types are malloced singly, since a large
812 unused block of them is wasteful. Also, several svtypes dont have
813 bodies; the data fits into the sv-head itself. The arena-root
814 pointer thus has a few unused root-pointers (which may be hijacked
815 later for arena types 4,5)
816
817 3 differs from 2 as an optimization; some body types have several
818 unused fields in the front of the structure (which are kept in-place
819 for consistency). These bodies can be allocated in smaller chunks,
820 because the leading fields arent accessed. Pointers to such bodies
821 are decremented to point at the unused 'ghost' memory, knowing that
822 the pointers are used with offsets to the real memory.
823
824
825 =head1 SV-Body Allocation
826
827 =cut
828
829 Allocation of SV-bodies is similar to SV-heads, differing as follows;
830 the allocation mechanism is used for many body types, so is somewhat
831 more complicated, it uses arena-sets, and has no need for still-live
832 SV detection.
833
834 At the outermost level, (new|del)_X*V macros return bodies of the
835 appropriate type. These macros call either (new|del)_body_type or
836 (new|del)_body_allocated macro pairs, depending on specifics of the
837 type. Most body types use the former pair, the latter pair is used to
838 allocate body types with "ghost fields".
839
840 "ghost fields" are fields that are unused in certain types, and
841 consequently don't need to actually exist. They are declared because
842 they're part of a "base type", which allows use of functions as
843 methods. The simplest examples are AVs and HVs, 2 aggregate types
844 which don't use the fields which support SCALAR semantics.
845
846 For these types, the arenas are carved up into appropriately sized
847 chunks, we thus avoid wasted memory for those unaccessed members.
848 When bodies are allocated, we adjust the pointer back in memory by the
849 size of the part not allocated, so it's as if we allocated the full
850 structure. (But things will all go boom if you write to the part that
851 is "not there", because you'll be overwriting the last members of the
852 preceding structure in memory.)
853
854 We calculate the correction using the STRUCT_OFFSET macro on the first
855 member present. If the allocated structure is smaller (no initial NV
856 actually allocated) then the net effect is to subtract the size of the NV
857 from the pointer, to return a new pointer as if an initial NV were actually
858 allocated. (We were using structures named *_allocated for this, but
859 this turned out to be a subtle bug, because a structure without an NV
860 could have a lower alignment constraint, but the compiler is allowed to
861 optimised accesses based on the alignment constraint of the actual pointer
862 to the full structure, for example, using a single 64 bit load instruction
863 because it "knows" that two adjacent 32 bit members will be 8-byte aligned.)
864
865 This is the same trick as was used for NV and IV bodies. Ironically it
866 doesn't need to be used for NV bodies any more, because NV is now at
867 the start of the structure. IV bodies, and also in some builds NV bodies,
868 don't need it either, because they are no longer allocated.
869
870 In turn, the new_body_* allocators call S_new_body(), which invokes
871 new_body_inline macro, which takes a lock, and takes a body off the
872 linked list at PL_body_roots[sv_type], calling Perl_more_bodies() if
873 necessary to refresh an empty list. Then the lock is released, and
874 the body is returned.
875
876 Perl_more_bodies allocates a new arena, and carves it up into an array of N
877 bodies, which it strings into a linked list. It looks up arena-size
878 and body-size from the body_details table described below, thus
879 supporting the multiple body-types.
880
881 If PURIFY is defined, or PERL_ARENA_SIZE=0, arenas are not used, and
882 the (new|del)_X*V macros are mapped directly to malloc/free.
883
884 For each sv-type, struct body_details bodies_by_type[] carries
885 parameters which control these aspects of SV handling:
886
887 Arena_size determines whether arenas are used for this body type, and if
888 so, how big they are. PURIFY or PERL_ARENA_SIZE=0 set this field to
889 zero, forcing individual mallocs and frees.
890
891 Body_size determines how big a body is, and therefore how many fit into
892 each arena. Offset carries the body-pointer adjustment needed for
893 "ghost fields", and is used in *_allocated macros.
894
895 But its main purpose is to parameterize info needed in
896 Perl_sv_upgrade(). The info here dramatically simplifies the function
897 vs the implementation in 5.8.8, making it table-driven. All fields
898 are used for this, except for arena_size.
899
900 For the sv-types that have no bodies, arenas are not used, so those
901 PL_body_roots[sv_type] are unused, and can be overloaded. In
902 something of a special case, SVt_NULL is borrowed for HE arenas;
903 PL_body_roots[HE_SVSLOT=SVt_NULL] is filled by S_more_he, but the
904 bodies_by_type[SVt_NULL] slot is not used, as the table is not
905 available in hv.c.
906
907 */
908
909 struct body_details {
910 U8 body_size; /* Size to allocate */
911 U8 copy; /* Size of structure to copy (may be shorter) */
912 U8 offset; /* Size of unalloced ghost fields to first alloced field*/
913 PERL_BITFIELD8 type : 4; /* We have space for a sanity check. */
914 PERL_BITFIELD8 cant_upgrade : 1;/* Cannot upgrade this type */
915 PERL_BITFIELD8 zero_nv : 1; /* zero the NV when upgrading from this */
916 PERL_BITFIELD8 arena : 1; /* Allocated from an arena */
917 U32 arena_size; /* Size of arena to allocate */
918 };
919
920 #define HADNV FALSE
921 #define NONV TRUE
922
923
924 #ifdef PURIFY
925 /* With -DPURFIY we allocate everything directly, and don't use arenas.
926 This seems a rather elegant way to simplify some of the code below. */
927 #define HASARENA FALSE
928 #else
929 #define HASARENA TRUE
930 #endif
931 #define NOARENA FALSE
932
933 /* Size the arenas to exactly fit a given number of bodies. A count
934 of 0 fits the max number bodies into a PERL_ARENA_SIZE.block,
935 simplifying the default. If count > 0, the arena is sized to fit
936 only that many bodies, allowing arenas to be used for large, rare
937 bodies (XPVFM, XPVIO) without undue waste. The arena size is
938 limited by PERL_ARENA_SIZE, so we can safely oversize the
939 declarations.
940 */
941 #define FIT_ARENA0(body_size) \
942 ((size_t)(PERL_ARENA_SIZE / body_size) * body_size)
943 #define FIT_ARENAn(count,body_size) \
944 ( count * body_size <= PERL_ARENA_SIZE) \
945 ? count * body_size \
946 : FIT_ARENA0 (body_size)
947 #define FIT_ARENA(count,body_size) \
948 (U32)(count \
949 ? FIT_ARENAn (count, body_size) \
950 : FIT_ARENA0 (body_size))
951
952 /* Calculate the length to copy. Specifically work out the length less any
953 final padding the compiler needed to add. See the comment in sv_upgrade
954 for why copying the padding proved to be a bug. */
955
956 #define copy_length(type, last_member) \
957 STRUCT_OFFSET(type, last_member) \
958 + sizeof (((type*)SvANY((const SV *)0))->last_member)
959
960 static const struct body_details bodies_by_type[] = {
961 /* HEs use this offset for their arena. */
962 { 0, 0, 0, SVt_NULL, FALSE, NONV, NOARENA, 0 },
963
964 /* IVs are in the head, so the allocation size is 0. */
965 { 0,
966 sizeof(IV), /* This is used to copy out the IV body. */
967 STRUCT_OFFSET(XPVIV, xiv_iv), SVt_IV, FALSE, NONV,
968 NOARENA /* IVS don't need an arena */, 0
969 },
970
971 #if NVSIZE <= IVSIZE
972 { 0, sizeof(NV),
973 STRUCT_OFFSET(XPVNV, xnv_u),
974 SVt_NV, FALSE, HADNV, NOARENA, 0 },
975 #else
976 { sizeof(NV), sizeof(NV),
977 STRUCT_OFFSET(XPVNV, xnv_u),
978 SVt_NV, FALSE, HADNV, HASARENA, FIT_ARENA(0, sizeof(NV)) },
979 #endif
980
981 { sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur),
982 copy_length(XPV, xpv_len) - STRUCT_OFFSET(XPV, xpv_cur),
983 + STRUCT_OFFSET(XPV, xpv_cur),
984 SVt_PV, FALSE, NONV, HASARENA,
985 FIT_ARENA(0, sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur)) },
986
987 { sizeof(XINVLIST) - STRUCT_OFFSET(XPV, xpv_cur),
988 copy_length(XINVLIST, is_offset) - STRUCT_OFFSET(XPV, xpv_cur),
989 + STRUCT_OFFSET(XPV, xpv_cur),
990 SVt_INVLIST, TRUE, NONV, HASARENA,
991 FIT_ARENA(0, sizeof(XINVLIST) - STRUCT_OFFSET(XPV, xpv_cur)) },
992
993 { sizeof(XPVIV) - STRUCT_OFFSET(XPV, xpv_cur),
994 copy_length(XPVIV, xiv_u) - STRUCT_OFFSET(XPV, xpv_cur),
995 + STRUCT_OFFSET(XPV, xpv_cur),
996 SVt_PVIV, FALSE, NONV, HASARENA,
997 FIT_ARENA(0, sizeof(XPVIV) - STRUCT_OFFSET(XPV, xpv_cur)) },
998
999 { sizeof(XPVNV) - STRUCT_OFFSET(XPV, xpv_cur),
1000 copy_length(XPVNV, xnv_u) - STRUCT_OFFSET(XPV, xpv_cur),
1001 + STRUCT_OFFSET(XPV, xpv_cur),
1002 SVt_PVNV, FALSE, HADNV, HASARENA,
1003 FIT_ARENA(0, sizeof(XPVNV) - STRUCT_OFFSET(XPV, xpv_cur)) },
1004
1005 { sizeof(XPVMG), copy_length(XPVMG, xnv_u), 0, SVt_PVMG, FALSE, HADNV,
1006 HASARENA, FIT_ARENA(0, sizeof(XPVMG)) },
1007
1008 { sizeof(regexp),
1009 sizeof(regexp),
1010 0,
1011 SVt_REGEXP, TRUE, NONV, HASARENA,
1012 FIT_ARENA(0, sizeof(regexp))
1013 },
1014
1015 { sizeof(XPVGV), sizeof(XPVGV), 0, SVt_PVGV, TRUE, HADNV,
1016 HASARENA, FIT_ARENA(0, sizeof(XPVGV)) },
1017
1018 { sizeof(XPVLV), sizeof(XPVLV), 0, SVt_PVLV, TRUE, HADNV,
1019 HASARENA, FIT_ARENA(0, sizeof(XPVLV)) },
1020
1021 { sizeof(XPVAV),
1022 copy_length(XPVAV, xav_alloc),
1023 0,
1024 SVt_PVAV, TRUE, NONV, HASARENA,
1025 FIT_ARENA(0, sizeof(XPVAV)) },
1026
1027 { sizeof(XPVHV),
1028 copy_length(XPVHV, xhv_max),
1029 0,
1030 SVt_PVHV, TRUE, NONV, HASARENA,
1031 FIT_ARENA(0, sizeof(XPVHV)) },
1032
1033 { sizeof(XPVCV),
1034 sizeof(XPVCV),
1035 0,
1036 SVt_PVCV, TRUE, NONV, HASARENA,
1037 FIT_ARENA(0, sizeof(XPVCV)) },
1038
1039 { sizeof(XPVFM),
1040 sizeof(XPVFM),
1041 0,
1042 SVt_PVFM, TRUE, NONV, NOARENA,
1043 FIT_ARENA(20, sizeof(XPVFM)) },
1044
1045 { sizeof(XPVIO),
1046 sizeof(XPVIO),
1047 0,
1048 SVt_PVIO, TRUE, NONV, HASARENA,
1049 FIT_ARENA(24, sizeof(XPVIO)) },
1050 };
1051
1052 #define new_body_allocated(sv_type) \
1053 (void *)((char *)S_new_body(aTHX_ sv_type) \
1054 - bodies_by_type[sv_type].offset)
1055
1056 /* return a thing to the free list */
1057
1058 #define del_body(thing, root) \
1059 STMT_START { \
1060 void ** const thing_copy = (void **)thing; \
1061 *thing_copy = *root; \
1062 *root = (void*)thing_copy; \
1063 } STMT_END
1064
1065 #ifdef PURIFY
1066 #if !(NVSIZE <= IVSIZE)
1067 # define new_XNV() safemalloc(sizeof(XPVNV))
1068 #endif
1069 #define new_XPVNV() safemalloc(sizeof(XPVNV))
1070 #define new_XPVMG() safemalloc(sizeof(XPVMG))
1071
1072 #define del_XPVGV(p) safefree(p)
1073
1074 #else /* !PURIFY */
1075
1076 #if !(NVSIZE <= IVSIZE)
1077 # define new_XNV() new_body_allocated(SVt_NV)
1078 #endif
1079 #define new_XPVNV() new_body_allocated(SVt_PVNV)
1080 #define new_XPVMG() new_body_allocated(SVt_PVMG)
1081
1082 #define del_XPVGV(p) del_body(p + bodies_by_type[SVt_PVGV].offset, \
1083 &PL_body_roots[SVt_PVGV])
1084
1085 #endif /* PURIFY */
1086
1087 /* no arena for you! */
1088
1089 #define new_NOARENA(details) \
1090 safemalloc((details)->body_size + (details)->offset)
1091 #define new_NOARENAZ(details) \
1092 safecalloc((details)->body_size + (details)->offset, 1)
1093
1094 void *
1095 Perl_more_bodies (pTHX_ const svtype sv_type, const size_t body_size,
1096 const size_t arena_size)
1097 {
1098 void ** const root = &PL_body_roots[sv_type];
1099 struct arena_desc *adesc;
1100 struct arena_set *aroot = (struct arena_set *) PL_body_arenas;
1101 unsigned int curr;
1102 char *start;
1103 const char *end;
1104 const size_t good_arena_size = Perl_malloc_good_size(arena_size);
1105 #if defined(DEBUGGING) && defined(PERL_GLOBAL_STRUCT)
1106 dVAR;
1107 #endif
1108 #if defined(DEBUGGING) && !defined(PERL_GLOBAL_STRUCT_PRIVATE)
1109 static bool done_sanity_check;
1110
1111 /* PERL_GLOBAL_STRUCT_PRIVATE cannot coexist with global
1112 * variables like done_sanity_check. */
1113 if (!done_sanity_check) {
1114 unsigned int i = SVt_LAST;
1115
1116 done_sanity_check = TRUE;
1117
1118 while (i--)
1119 assert (bodies_by_type[i].type == i);
1120 }
1121 #endif
1122
1123 assert(arena_size);
1124
1125 /* may need new arena-set to hold new arena */
1126 if (!aroot || aroot->curr >= aroot->set_size) {
1127 struct arena_set *newroot;
1128 Newxz(newroot, 1, struct arena_set);
1129 newroot->set_size = ARENAS_PER_SET;
1130 newroot->next = aroot;
1131 aroot = newroot;
1132 PL_body_arenas = (void *) newroot;
1133 DEBUG_m(PerlIO_printf(Perl_debug_log, "new arenaset %p\n", (void*)aroot));
1134 }
1135
1136 /* ok, now have arena-set with at least 1 empty/available arena-desc */
1137 curr = aroot->curr++;
1138 adesc = &(aroot->set[curr]);
1139 assert(!adesc->arena);
1140
1141 Newx(adesc->arena, good_arena_size, char);
1142 adesc->size = good_arena_size;
1143 adesc->utype = sv_type;
1144 DEBUG_m(PerlIO_printf(Perl_debug_log, "arena %d added: %p size %"UVuf"\n",
1145 curr, (void*)adesc->arena, (UV)good_arena_size));
1146
1147 start = (char *) adesc->arena;
1148
1149 /* Get the address of the byte after the end of the last body we can fit.
1150 Remember, this is integer division: */
1151 end = start + good_arena_size / body_size * body_size;
1152
1153 /* computed count doesn't reflect the 1st slot reservation */
1154 #if defined(MYMALLOC) || defined(HAS_MALLOC_GOOD_SIZE)
1155 DEBUG_m(PerlIO_printf(Perl_debug_log,
1156 "arena %p end %p arena-size %d (from %d) type %d "
1157 "size %d ct %d\n",
1158 (void*)start, (void*)end, (int)good_arena_size,
1159 (int)arena_size, sv_type, (int)body_size,
1160 (int)good_arena_size / (int)body_size));
1161 #else
1162 DEBUG_m(PerlIO_printf(Perl_debug_log,
1163 "arena %p end %p arena-size %d type %d size %d ct %d\n",
1164 (void*)start, (void*)end,
1165 (int)arena_size, sv_type, (int)body_size,
1166 (int)good_arena_size / (int)body_size));
1167 #endif
1168 *root = (void *)start;
1169
1170 while (1) {
1171 /* Where the next body would start: */
1172 char * const next = start + body_size;
1173
1174 if (next >= end) {
1175 /* This is the last body: */
1176 assert(next == end);
1177
1178 *(void **)start = 0;
1179 return *root;
1180 }
1181
1182 *(void**) start = (void *)next;
1183 start = next;
1184 }
1185 }
1186
1187 /* grab a new thing from the free list, allocating more if necessary.
1188 The inline version is used for speed in hot routines, and the
1189 function using it serves the rest (unless PURIFY).
1190 */
1191 #define new_body_inline(xpv, sv_type) \
1192 STMT_START { \
1193 void ** const r3wt = &PL_body_roots[sv_type]; \
1194 xpv = (PTR_TBL_ENT_t*) (*((void **)(r3wt)) \
1195 ? *((void **)(r3wt)) : Perl_more_bodies(aTHX_ sv_type, \
1196 bodies_by_type[sv_type].body_size,\
1197 bodies_by_type[sv_type].arena_size)); \
1198 *(r3wt) = *(void**)(xpv); \
1199 } STMT_END
1200
1201 #ifndef PURIFY
1202
1203 STATIC void *
1204 S_new_body(pTHX_ const svtype sv_type)
1205 {
1206 void *xpv;
1207 new_body_inline(xpv, sv_type);
1208 return xpv;
1209 }
1210
1211 #endif
1212
1213 static const struct body_details fake_rv =
1214 { 0, 0, 0, SVt_IV, FALSE, NONV, NOARENA, 0 };
1215
1216 /*
1217 =for apidoc sv_upgrade
1218
1219 Upgrade an SV to a more complex form. Generally adds a new body type to the
1220 SV, then copies across as much information as possible from the old body.
1221 It croaks if the SV is already in a more complex form than requested. You
1222 generally want to use the C<SvUPGRADE> macro wrapper, which checks the type
1223 before calling C<sv_upgrade>, and hence does not croak. See also
1224 C<svtype>.
1225
1226 =cut
1227 */
1228
1229 void
1230 Perl_sv_upgrade(pTHX_ SV *const sv, svtype new_type)
1231 {
1232 void* old_body;
1233 void* new_body;
1234 const svtype old_type = SvTYPE(sv);
1235 const struct body_details *new_type_details;
1236 const struct body_details *old_type_details
1237 = bodies_by_type + old_type;
1238 SV *referant = NULL;
1239
1240 PERL_ARGS_ASSERT_SV_UPGRADE;
1241
1242 if (old_type == new_type)
1243 return;
1244
1245 /* This clause was purposefully added ahead of the early return above to
1246 the shared string hackery for (sort {$a <=> $b} keys %hash), with the
1247 inference by Nick I-S that it would fix other troublesome cases. See
1248 changes 7162, 7163 (f130fd4589cf5fbb24149cd4db4137c8326f49c1 and parent)
1249
1250 Given that shared hash key scalars are no longer PVIV, but PV, there is
1251 no longer need to unshare so as to free up the IVX slot for its proper
1252 purpose. So it's safe to move the early return earlier. */
1253
1254 if (new_type > SVt_PVMG && SvIsCOW(sv)) {
1255 sv_force_normal_flags(sv, 0);
1256 }
1257
1258 old_body = SvANY(sv);
1259
1260 /* Copying structures onto other structures that have been neatly zeroed
1261 has a subtle gotcha. Consider XPVMG
1262
1263 +------+------+------+------+------+-------+-------+
1264 | NV | CUR | LEN | IV | MAGIC | STASH |
1265 +------+------+------+------+------+-------+-------+
1266 0 4 8 12 16 20 24 28
1267
1268 where NVs are aligned to 8 bytes, so that sizeof that structure is
1269 actually 32 bytes long, with 4 bytes of padding at the end:
1270
1271 +------+------+------+------+------+-------+-------+------+
1272 | NV | CUR | LEN | IV | MAGIC | STASH | ??? |
1273 +------+------+------+------+------+-------+-------+------+
1274 0 4 8 12 16 20 24 28 32
1275
1276 so what happens if you allocate memory for this structure:
1277
1278 +------+------+------+------+------+-------+-------+------+------+...
1279 | NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME |
1280 +------+------+------+------+------+-------+-------+------+------+...
1281 0 4 8 12 16 20 24 28 32 36
1282
1283 zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
1284 expect, because you copy the area marked ??? onto GP. Now, ??? may have
1285 started out as zero once, but it's quite possible that it isn't. So now,
1286 rather than a nicely zeroed GP, you have it pointing somewhere random.
1287 Bugs ensue.
1288
1289 (In fact, GP ends up pointing at a previous GP structure, because the
1290 principle cause of the padding in XPVMG getting garbage is a copy of
1291 sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob. Right now
1292 this happens to be moot because XPVGV has been re-ordered, with GP
1293 no longer after STASH)
1294
1295 So we are careful and work out the size of used parts of all the
1296 structures. */
1297
1298 switch (old_type) {
1299 case SVt_NULL:
1300 break;
1301 case SVt_IV:
1302 if (SvROK(sv)) {
1303 referant = SvRV(sv);
1304 old_type_details = &fake_rv;
1305 if (new_type == SVt_NV)
1306 new_type = SVt_PVNV;
1307 } else {
1308 if (new_type < SVt_PVIV) {
1309 new_type = (new_type == SVt_NV)
1310 ? SVt_PVNV : SVt_PVIV;
1311 }
1312 }
1313 break;
1314 case SVt_NV:
1315 if (new_type < SVt_PVNV) {
1316 new_type = SVt_PVNV;
1317 }
1318 break;
1319 case SVt_PV:
1320 assert(new_type > SVt_PV);
1321 STATIC_ASSERT_STMT(SVt_IV < SVt_PV);
1322 STATIC_ASSERT_STMT(SVt_NV < SVt_PV);
1323 break;
1324 case SVt_PVIV:
1325 break;
1326 case SVt_PVNV:
1327 break;
1328 case SVt_PVMG:
1329 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1330 there's no way that it can be safely upgraded, because perl.c
1331 expects to Safefree(SvANY(PL_mess_sv)) */
1332 assert(sv != PL_mess_sv);
1333 break;
1334 default:
1335 if (UNLIKELY(old_type_details->cant_upgrade))
1336 Perl_croak(aTHX_ "Can't upgrade %s (%" UVuf ") to %" UVuf,
1337 sv_reftype(sv, 0), (UV) old_type, (UV) new_type);
1338 }
1339
1340 if (UNLIKELY(old_type > new_type))
1341 Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
1342 (int)old_type, (int)new_type);
1343
1344 new_type_details = bodies_by_type + new_type;
1345
1346 SvFLAGS(sv) &= ~SVTYPEMASK;
1347 SvFLAGS(sv) |= new_type;
1348
1349 /* This can't happen, as SVt_NULL is <= all values of new_type, so one of
1350 the return statements above will have triggered. */
1351 assert (new_type != SVt_NULL);
1352 switch (new_type) {
1353 case SVt_IV:
1354 assert(old_type == SVt_NULL);
1355 SET_SVANY_FOR_BODYLESS_IV(sv);
1356 SvIV_set(sv, 0);
1357 return;
1358 case SVt_NV:
1359 assert(old_type == SVt_NULL);
1360 #if NVSIZE <= IVSIZE
1361 SET_SVANY_FOR_BODYLESS_NV(sv);
1362 #else
1363 SvANY(sv) = new_XNV();
1364 #endif
1365 SvNV_set(sv, 0);
1366 return;
1367 case SVt_PVHV:
1368 case SVt_PVAV:
1369 assert(new_type_details->body_size);
1370
1371 #ifndef PURIFY
1372 assert(new_type_details->arena);
1373 assert(new_type_details->arena_size);
1374 /* This points to the start of the allocated area. */
1375 new_body_inline(new_body, new_type);
1376 Zero(new_body, new_type_details->body_size, char);
1377 new_body = ((char *)new_body) - new_type_details->offset;
1378 #else
1379 /* We always allocated the full length item with PURIFY. To do this
1380 we fake things so that arena is false for all 16 types.. */
1381 new_body = new_NOARENAZ(new_type_details);
1382 #endif
1383 SvANY(sv) = new_body;
1384 if (new_type == SVt_PVAV) {
1385 AvMAX(sv) = -1;
1386 AvFILLp(sv) = -1;
1387 AvREAL_only(sv);
1388 if (old_type_details->body_size) {
1389 AvALLOC(sv) = 0;
1390 } else {
1391 /* It will have been zeroed when the new body was allocated.
1392 Lets not write to it, in case it confuses a write-back
1393 cache. */
1394 }
1395 } else {
1396 assert(!SvOK(sv));
1397 SvOK_off(sv);
1398 #ifndef NODEFAULT_SHAREKEYS
1399 HvSHAREKEYS_on(sv); /* key-sharing on by default */
1400 #endif
1401 /* start with PERL_HASH_DEFAULT_HvMAX+1 buckets: */
1402 HvMAX(sv) = PERL_HASH_DEFAULT_HvMAX;
1403 }
1404
1405 /* SVt_NULL isn't the only thing upgraded to AV or HV.
1406 The target created by newSVrv also is, and it can have magic.
1407 However, it never has SvPVX set.
1408 */
1409 if (old_type == SVt_IV) {
1410 assert(!SvROK(sv));
1411 } else if (old_type >= SVt_PV) {
1412 assert(SvPVX_const(sv) == 0);
1413 }
1414
1415 if (old_type >= SVt_PVMG) {
1416 SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_u.xmg_magic);
1417 SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
1418 } else {
1419 sv->sv_u.svu_array = NULL; /* or svu_hash */
1420 }
1421 break;
1422
1423 case SVt_PVIV:
1424 /* XXX Is this still needed? Was it ever needed? Surely as there is
1425 no route from NV to PVIV, NOK can never be true */
1426 assert(!SvNOKp(sv));
1427 assert(!SvNOK(sv));
1428 /* FALLTHROUGH */
1429 case SVt_PVIO:
1430 case SVt_PVFM:
1431 case SVt_PVGV:
1432 case SVt_PVCV:
1433 case SVt_PVLV:
1434 case SVt_INVLIST:
1435 case SVt_REGEXP:
1436 case SVt_PVMG:
1437 case SVt_PVNV:
1438 case SVt_PV:
1439
1440 assert(new_type_details->body_size);
1441 /* We always allocated the full length item with PURIFY. To do this
1442 we fake things so that arena is false for all 16 types.. */
1443 if(new_type_details->arena) {
1444 /* This points to the start of the allocated area. */
1445 new_body_inline(new_body, new_type);
1446 Zero(new_body, new_type_details->body_size, char);
1447 new_body = ((char *)new_body) - new_type_details->offset;
1448 } else {
1449 new_body = new_NOARENAZ(new_type_details);
1450 }
1451 SvANY(sv) = new_body;
1452
1453 if (old_type_details->copy) {
1454 /* There is now the potential for an upgrade from something without
1455 an offset (PVNV or PVMG) to something with one (PVCV, PVFM) */
1456 int offset = old_type_details->offset;
1457 int length = old_type_details->copy;
1458
1459 if (new_type_details->offset > old_type_details->offset) {
1460 const int difference
1461 = new_type_details->offset - old_type_details->offset;
1462 offset += difference;
1463 length -= difference;
1464 }
1465 assert (length >= 0);
1466
1467 Copy((char *)old_body + offset, (char *)new_body + offset, length,
1468 char);
1469 }
1470
1471 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1472 /* If NV 0.0 is stores as all bits 0 then Zero() already creates a
1473 * correct 0.0 for us. Otherwise, if the old body didn't have an
1474 * NV slot, but the new one does, then we need to initialise the
1475 * freshly created NV slot with whatever the correct bit pattern is
1476 * for 0.0 */
1477 if (old_type_details->zero_nv && !new_type_details->zero_nv
1478 && !isGV_with_GP(sv))
1479 SvNV_set(sv, 0);
1480 #endif
1481
1482 if (UNLIKELY(new_type == SVt_PVIO)) {
1483 IO * const io = MUTABLE_IO(sv);
1484 GV *iogv = gv_fetchpvs("IO::File::", GV_ADD, SVt_PVHV);
1485
1486 SvOBJECT_on(io);
1487 /* Clear the stashcache because a new IO could overrule a package
1488 name */
1489 DEBUG_o(Perl_deb(aTHX_ "sv_upgrade clearing PL_stashcache\n"));
1490 hv_clear(PL_stashcache);
1491
1492 SvSTASH_set(io, MUTABLE_HV(SvREFCNT_inc(GvHV(iogv))));
1493 IoPAGE_LEN(sv) = 60;
1494 }
1495 if (UNLIKELY(new_type == SVt_REGEXP))
1496 sv->sv_u.svu_rx = (regexp *)new_body;
1497 else if (old_type < SVt_PV) {
1498 /* referant will be NULL unless the old type was SVt_IV emulating
1499 SVt_RV */
1500 sv->sv_u.svu_rv = referant;
1501 }
1502 break;
1503 default:
1504 Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu",
1505 (unsigned long)new_type);
1506 }
1507
1508 /* if this is zero, this is a body-less SVt_NULL, SVt_IV/SVt_RV,
1509 and sometimes SVt_NV */
1510 if (old_type_details->body_size) {
1511 #ifdef PURIFY
1512 safefree(old_body);
1513 #else
1514 /* Note that there is an assumption that all bodies of types that
1515 can be upgraded came from arenas. Only the more complex non-
1516 upgradable types are allowed to be directly malloc()ed. */
1517 assert(old_type_details->arena);
1518 del_body((void*)((char*)old_body + old_type_details->offset),
1519 &PL_body_roots[old_type]);
1520 #endif
1521 }
1522 }
1523
1524 /*
1525 =for apidoc sv_backoff
1526
1527 Remove any string offset. You should normally use the C<SvOOK_off> macro
1528 wrapper instead.
1529
1530 =cut
1531 */
1532
1533 int
1534 Perl_sv_backoff(SV *const sv)
1535 {
1536 STRLEN delta;
1537 const char * const s = SvPVX_const(sv);
1538
1539 PERL_ARGS_ASSERT_SV_BACKOFF;
1540
1541 assert(SvOOK(sv));
1542 assert(SvTYPE(sv) != SVt_PVHV);
1543 assert(SvTYPE(sv) != SVt_PVAV);
1544
1545 SvOOK_offset(sv, delta);
1546
1547 SvLEN_set(sv, SvLEN(sv) + delta);
1548 SvPV_set(sv, SvPVX(sv) - delta);
1549 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1550 SvFLAGS(sv) &= ~SVf_OOK;
1551 return 0;
1552 }
1553
1554 /*
1555 =for apidoc sv_grow
1556
1557 Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1558 upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1559 Use the C<SvGROW> wrapper instead.
1560
1561 =cut
1562 */
1563
1564 static void S_sv_uncow(pTHX_ SV * const sv, const U32 flags);
1565
1566 char *
1567 Perl_sv_grow(pTHX_ SV *const sv, STRLEN newlen)
1568 {
1569 char *s;
1570
1571 PERL_ARGS_ASSERT_SV_GROW;
1572
1573 if (SvROK(sv))
1574 sv_unref(sv);
1575 if (SvTYPE(sv) < SVt_PV) {
1576 sv_upgrade(sv, SVt_PV);
1577 s = SvPVX_mutable(sv);
1578 }
1579 else if (SvOOK(sv)) { /* pv is offset? */
1580 sv_backoff(sv);
1581 s = SvPVX_mutable(sv);
1582 if (newlen > SvLEN(sv))
1583 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1584 }
1585 else
1586 {
1587 if (SvIsCOW(sv)) S_sv_uncow(aTHX_ sv, 0);
1588 s = SvPVX_mutable(sv);
1589 }
1590
1591 #ifdef PERL_NEW_COPY_ON_WRITE
1592 /* the new COW scheme uses SvPVX(sv)[SvLEN(sv)-1] (if spare)
1593 * to store the COW count. So in general, allocate one more byte than
1594 * asked for, to make it likely this byte is always spare: and thus
1595 * make more strings COW-able.
1596 * If the new size is a big power of two, don't bother: we assume the
1597 * caller wanted a nice 2^N sized block and will be annoyed at getting
1598 * 2^N+1.
1599 * Only increment if the allocation isn't MEM_SIZE_MAX,
1600 * otherwise it will wrap to 0.
1601 */
1602 if (newlen & 0xff && newlen != MEM_SIZE_MAX)
1603 newlen++;
1604 #endif
1605
1606 #if defined(PERL_USE_MALLOC_SIZE) && defined(Perl_safesysmalloc_size)
1607 #define PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1608 #endif
1609
1610 if (newlen > SvLEN(sv)) { /* need more room? */
1611 STRLEN minlen = SvCUR(sv);
1612 minlen += (minlen >> PERL_STRLEN_EXPAND_SHIFT) + 10;
1613 if (newlen < minlen)
1614 newlen = minlen;
1615 #ifndef PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1616
1617 /* Don't round up on the first allocation, as odds are pretty good that
1618 * the initial request is accurate as to what is really needed */
1619 if (SvLEN(sv)) {
1620 STRLEN rounded = PERL_STRLEN_ROUNDUP(newlen);
1621 if (rounded > newlen)
1622 newlen = rounded;
1623 }
1624 #endif
1625 if (SvLEN(sv) && s) {
1626 s = (char*)saferealloc(s, newlen);
1627 }
1628 else {
1629 s = (char*)safemalloc(newlen);
1630 if (SvPVX_const(sv) && SvCUR(sv)) {
1631 Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
1632 }
1633 }
1634 SvPV_set(sv, s);
1635 #ifdef PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1636 /* Do this here, do it once, do it right, and then we will never get
1637 called back into sv_grow() unless there really is some growing
1638 needed. */
1639 SvLEN_set(sv, Perl_safesysmalloc_size(s));
1640 #else
1641 SvLEN_set(sv, newlen);
1642 #endif
1643 }
1644 return s;
1645 }
1646
1647 /*
1648 =for apidoc sv_setiv
1649
1650 Copies an integer into the given SV, upgrading first if necessary.
1651 Does not handle 'set' magic. See also C<sv_setiv_mg>.
1652
1653 =cut
1654 */
1655
1656 void
1657 Perl_sv_setiv(pTHX_ SV *const sv, const IV i)
1658 {
1659 PERL_ARGS_ASSERT_SV_SETIV;
1660
1661 SV_CHECK_THINKFIRST_COW_DROP(sv);
1662 switch (SvTYPE(sv)) {
1663 case SVt_NULL:
1664 case SVt_NV:
1665 sv_upgrade(sv, SVt_IV);
1666 break;
1667 case SVt_PV:
1668 sv_upgrade(sv, SVt_PVIV);
1669 break;
1670
1671 case SVt_PVGV:
1672 if (!isGV_with_GP(sv))
1673 break;
1674 case SVt_PVAV:
1675 case SVt_PVHV:
1676 case SVt_PVCV:
1677 case SVt_PVFM:
1678 case SVt_PVIO:
1679 /* diag_listed_as: Can't coerce %s to %s in %s */
1680 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1681 OP_DESC(PL_op));
1682 default: NOOP;
1683 }
1684 (void)SvIOK_only(sv); /* validate number */
1685 SvIV_set(sv, i);
1686 SvTAINT(sv);
1687 }
1688
1689 /*
1690 =for apidoc sv_setiv_mg
1691
1692 Like C<sv_setiv>, but also handles 'set' magic.
1693
1694 =cut
1695 */
1696
1697 void
1698 Perl_sv_setiv_mg(pTHX_ SV *const sv, const IV i)
1699 {
1700 PERL_ARGS_ASSERT_SV_SETIV_MG;
1701
1702 sv_setiv(sv,i);
1703 SvSETMAGIC(sv);
1704 }
1705
1706 /*
1707 =for apidoc sv_setuv
1708
1709 Copies an unsigned integer into the given SV, upgrading first if necessary.
1710 Does not handle 'set' magic. See also C<sv_setuv_mg>.
1711
1712 =cut
1713 */
1714
1715 void
1716 Perl_sv_setuv(pTHX_ SV *const sv, const UV u)
1717 {
1718 PERL_ARGS_ASSERT_SV_SETUV;
1719
1720 /* With the if statement to ensure that integers are stored as IVs whenever
1721 possible:
1722 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
1723
1724 without
1725 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1726
1727 If you wish to remove the following if statement, so that this routine
1728 (and its callers) always return UVs, please benchmark to see what the
1729 effect is. Modern CPUs may be different. Or may not :-)
1730 */
1731 if (u <= (UV)IV_MAX) {
1732 sv_setiv(sv, (IV)u);
1733 return;
1734 }
1735 sv_setiv(sv, 0);
1736 SvIsUV_on(sv);
1737 SvUV_set(sv, u);
1738 }
1739
1740 /*
1741 =for apidoc sv_setuv_mg
1742
1743 Like C<sv_setuv>, but also handles 'set' magic.
1744
1745 =cut
1746 */
1747
1748 void
1749 Perl_sv_setuv_mg(pTHX_ SV *const sv, const UV u)
1750 {
1751 PERL_ARGS_ASSERT_SV_SETUV_MG;
1752
1753 sv_setuv(sv,u);
1754 SvSETMAGIC(sv);
1755 }
1756
1757 /*
1758 =for apidoc sv_setnv
1759
1760 Copies a double into the given SV, upgrading first if necessary.
1761 Does not handle 'set' magic. See also C<sv_setnv_mg>.
1762
1763 =cut
1764 */
1765
1766 void
1767 Perl_sv_setnv(pTHX_ SV *const sv, const NV num)
1768 {
1769 PERL_ARGS_ASSERT_SV_SETNV;
1770
1771 SV_CHECK_THINKFIRST_COW_DROP(sv);
1772 switch (SvTYPE(sv)) {
1773 case SVt_NULL:
1774 case SVt_IV:
1775 sv_upgrade(sv, SVt_NV);
1776 break;
1777 case SVt_PV:
1778 case SVt_PVIV:
1779 sv_upgrade(sv, SVt_PVNV);
1780 break;
1781
1782 case SVt_PVGV:
1783 if (!isGV_with_GP(sv))
1784 break;
1785 case SVt_PVAV:
1786 case SVt_PVHV:
1787 case SVt_PVCV:
1788 case SVt_PVFM:
1789 case SVt_PVIO:
1790 /* diag_listed_as: Can't coerce %s to %s in %s */
1791 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1792 OP_DESC(PL_op));
1793 default: NOOP;
1794 }
1795 SvNV_set(sv, num);
1796 (void)SvNOK_only(sv); /* validate number */
1797 SvTAINT(sv);
1798 }
1799
1800 /*
1801 =for apidoc sv_setnv_mg
1802
1803 Like C<sv_setnv>, but also handles 'set' magic.
1804
1805 =cut
1806 */
1807
1808 void
1809 Perl_sv_setnv_mg(pTHX_ SV *const sv, const NV num)
1810 {
1811 PERL_ARGS_ASSERT_SV_SETNV_MG;
1812
1813 sv_setnv(sv,num);
1814 SvSETMAGIC(sv);
1815 }
1816
1817 /* Return a cleaned-up, printable version of sv, for non-numeric, or
1818 * not incrementable warning display.
1819 * Originally part of S_not_a_number().
1820 * The return value may be != tmpbuf.
1821 */
1822
1823 STATIC const char *
1824 S_sv_display(pTHX_ SV *const sv, char *tmpbuf, STRLEN tmpbuf_size) {
1825 const char *pv;
1826
1827 PERL_ARGS_ASSERT_SV_DISPLAY;
1828
1829 if (DO_UTF8(sv)) {
1830 SV *dsv = newSVpvs_flags("", SVs_TEMP);
1831 pv = sv_uni_display(dsv, sv, 10, UNI_DISPLAY_ISPRINT);
1832 } else {
1833 char *d = tmpbuf;
1834 const char * const limit = tmpbuf + tmpbuf_size - 8;
1835 /* each *s can expand to 4 chars + "...\0",
1836 i.e. need room for 8 chars */
1837
1838 const char *s = SvPVX_const(sv);
1839 const char * const end = s + SvCUR(sv);
1840 for ( ; s < end && d < limit; s++ ) {
1841 int ch = *s & 0xFF;
1842 if (! isASCII(ch) && !isPRINT_LC(ch)) {
1843 *d++ = 'M';
1844 *d++ = '-';
1845
1846 /* Map to ASCII "equivalent" of Latin1 */
1847 ch = LATIN1_TO_NATIVE(NATIVE_TO_LATIN1(ch) & 127);
1848 }
1849 if (ch == '\n') {
1850 *d++ = '\\';
1851 *d++ = 'n';
1852 }
1853 else if (ch == '\r') {
1854 *d++ = '\\';
1855 *d++ = 'r';
1856 }
1857 else if (ch == '\f') {
1858 *d++ = '\\';
1859 *d++ = 'f';
1860 }
1861 else if (ch == '\\') {
1862 *d++ = '\\';
1863 *d++ = '\\';
1864 }
1865 else if (ch == '\0') {
1866 *d++ = '\\';
1867 *d++ = '0';
1868 }
1869 else if (isPRINT_LC(ch))
1870 *d++ = ch;
1871 else {
1872 *d++ = '^';
1873 *d++ = toCTRL(ch);
1874 }
1875 }
1876 if (s < end) {
1877 *d++ = '.';
1878 *d++ = '.';
1879 *d++ = '.';
1880 }
1881 *d = '\0';
1882 pv = tmpbuf;
1883 }
1884
1885 return pv;
1886 }
1887
1888 /* Print an "isn't numeric" warning, using a cleaned-up,
1889 * printable version of the offending string
1890 */
1891
1892 STATIC void
1893 S_not_a_number(pTHX_ SV *const sv)
1894 {
1895 char tmpbuf[64];
1896 const char *pv;
1897
1898 PERL_ARGS_ASSERT_NOT_A_NUMBER;
1899
1900 pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
1901
1902 if (PL_op)
1903 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1904 /* diag_listed_as: Argument "%s" isn't numeric%s */
1905 "Argument \"%s\" isn't numeric in %s", pv,
1906 OP_DESC(PL_op));
1907 else
1908 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1909 /* diag_listed_as: Argument "%s" isn't numeric%s */
1910 "Argument \"%s\" isn't numeric", pv);
1911 }
1912
1913 STATIC void
1914 S_not_incrementable(pTHX_ SV *const sv) {
1915 char tmpbuf[64];
1916 const char *pv;
1917
1918 PERL_ARGS_ASSERT_NOT_INCREMENTABLE;
1919
1920 pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
1921
1922 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1923 "Argument \"%s\" treated as 0 in increment (++)", pv);
1924 }
1925
1926 /*
1927 =for apidoc looks_like_number
1928
1929 Test if the content of an SV looks like a number (or is a number).
1930 C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1931 non-numeric warning), even if your atof() doesn't grok them. Get-magic is
1932 ignored.
1933
1934 =cut
1935 */
1936
1937 I32
1938 Perl_looks_like_number(pTHX_ SV *const sv)
1939 {
1940 const char *sbegin;
1941 STRLEN len;
1942 int numtype;
1943
1944 PERL_ARGS_ASSERT_LOOKS_LIKE_NUMBER;
1945
1946 if (SvPOK(sv) || SvPOKp(sv)) {
1947 sbegin = SvPV_nomg_const(sv, len);
1948 }
1949 else
1950 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
1951 numtype = grok_number(sbegin, len, NULL);
1952 return ((numtype & IS_NUMBER_TRAILING)) ? 0 : numtype;
1953 }
1954
1955 STATIC bool
1956 S_glob_2number(pTHX_ GV * const gv)
1957 {
1958 PERL_ARGS_ASSERT_GLOB_2NUMBER;
1959
1960 /* We know that all GVs stringify to something that is not-a-number,
1961 so no need to test that. */
1962 if (ckWARN(WARN_NUMERIC))
1963 {
1964 SV *const buffer = sv_newmortal();
1965 gv_efullname3(buffer, gv, "*");
1966 not_a_number(buffer);
1967 }
1968 /* We just want something true to return, so that S_sv_2iuv_common
1969 can tail call us and return true. */
1970 return TRUE;
1971 }
1972
1973 /* Actually, ISO C leaves conversion of UV to IV undefined, but
1974 until proven guilty, assume that things are not that bad... */
1975
1976 /*
1977 NV_PRESERVES_UV:
1978
1979 As 64 bit platforms often have an NV that doesn't preserve all bits of
1980 an IV (an assumption perl has been based on to date) it becomes necessary
1981 to remove the assumption that the NV always carries enough precision to
1982 recreate the IV whenever needed, and that the NV is the canonical form.
1983 Instead, IV/UV and NV need to be given equal rights. So as to not lose
1984 precision as a side effect of conversion (which would lead to insanity
1985 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1986 1) to distinguish between IV/UV/NV slots that have a valid conversion cached
1987 where precision was lost, and IV/UV/NV slots that have a valid conversion
1988 which has lost no precision
1989 2) to ensure that if a numeric conversion to one form is requested that
1990 would lose precision, the precise conversion (or differently
1991 imprecise conversion) is also performed and cached, to prevent
1992 requests for different numeric formats on the same SV causing
1993 lossy conversion chains. (lossless conversion chains are perfectly
1994 acceptable (still))
1995
1996
1997 flags are used:
1998 SvIOKp is true if the IV slot contains a valid value
1999 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
2000 SvNOKp is true if the NV slot contains a valid value
2001 SvNOK is true only if the NV value is accurate
2002
2003 so
2004 while converting from PV to NV, check to see if converting that NV to an
2005 IV(or UV) would lose accuracy over a direct conversion from PV to
2006 IV(or UV). If it would, cache both conversions, return NV, but mark
2007 SV as IOK NOKp (ie not NOK).
2008
2009 While converting from PV to IV, check to see if converting that IV to an
2010 NV would lose accuracy over a direct conversion from PV to NV. If it
2011 would, cache both conversions, flag similarly.
2012
2013 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
2014 correctly because if IV & NV were set NV *always* overruled.
2015 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
2016 changes - now IV and NV together means that the two are interchangeable:
2017 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
2018
2019 The benefit of this is that operations such as pp_add know that if
2020 SvIOK is true for both left and right operands, then integer addition
2021 can be used instead of floating point (for cases where the result won't
2022 overflow). Before, floating point was always used, which could lead to
2023 loss of precision compared with integer addition.
2024
2025 * making IV and NV equal status should make maths accurate on 64 bit
2026 platforms
2027 * may speed up maths somewhat if pp_add and friends start to use
2028 integers when possible instead of fp. (Hopefully the overhead in
2029 looking for SvIOK and checking for overflow will not outweigh the
2030 fp to integer speedup)
2031 * will slow down integer operations (callers of SvIV) on "inaccurate"
2032 values, as the change from SvIOK to SvIOKp will cause a call into
2033 sv_2iv each time rather than a macro access direct to the IV slot
2034 * should speed up number->string conversion on integers as IV is
2035 favoured when IV and NV are equally accurate
2036
2037 ####################################################################
2038 You had better be using SvIOK_notUV if you want an IV for arithmetic:
2039 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
2040 On the other hand, SvUOK is true iff UV.
2041 ####################################################################
2042
2043 Your mileage will vary depending your CPU's relative fp to integer
2044 performance ratio.
2045 */
2046
2047 #ifndef NV_PRESERVES_UV
2048 # define IS_NUMBER_UNDERFLOW_IV 1
2049 # define IS_NUMBER_UNDERFLOW_UV 2
2050 # define IS_NUMBER_IV_AND_UV 2
2051 # define IS_NUMBER_OVERFLOW_IV 4
2052 # define IS_NUMBER_OVERFLOW_UV 5
2053
2054 /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
2055
2056 /* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2057 STATIC int
2058 S_sv_2iuv_non_preserve(pTHX_ SV *const sv
2059 # ifdef DEBUGGING
2060 , I32 numtype
2061 # endif
2062 )
2063 {
2064 PERL_ARGS_ASSERT_SV_2IUV_NON_PRESERVE;
2065 PERL_UNUSED_CONTEXT;
2066
2067 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));
2068 if (SvNVX(sv) < (NV)IV_MIN) {
2069 (void)SvIOKp_on(sv);
2070 (void)SvNOK_on(sv);
2071 SvIV_set(sv, IV_MIN);
2072 return IS_NUMBER_UNDERFLOW_IV;
2073 }
2074 if (SvNVX(sv) > (NV)UV_MAX) {
2075 (void)SvIOKp_on(sv);
2076 (void)SvNOK_on(sv);
2077 SvIsUV_on(sv);
2078 SvUV_set(sv, UV_MAX);
2079 return IS_NUMBER_OVERFLOW_UV;
2080 }
2081 (void)SvIOKp_on(sv);
2082 (void)SvNOK_on(sv);
2083 /* Can't use strtol etc to convert this string. (See truth table in
2084 sv_2iv */
2085 if (SvNVX(sv) <= (UV)IV_MAX) {
2086 SvIV_set(sv, I_V(SvNVX(sv)));
2087 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2088 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2089 } else {
2090 /* Integer is imprecise. NOK, IOKp */
2091 }
2092 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2093 }
2094 SvIsUV_on(sv);
2095 SvUV_set(sv, U_V(SvNVX(sv)));
2096 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2097 if (SvUVX(sv) == UV_MAX) {
2098 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2099 possibly be preserved by NV. Hence, it must be overflow.
2100 NOK, IOKp */
2101 return IS_NUMBER_OVERFLOW_UV;
2102 }
2103 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2104 } else {
2105 /* Integer is imprecise. NOK, IOKp */
2106 }
2107 return IS_NUMBER_OVERFLOW_IV;
2108 }
2109 #endif /* !NV_PRESERVES_UV*/
2110
2111 /* If numtype is infnan, set the NV of the sv accordingly.
2112 * If numtype is anything else, try setting the NV using Atof(PV). */
2113 #ifdef USING_MSVC6
2114 # pragma warning(push)
2115 # pragma warning(disable:4756;disable:4056)
2116 #endif
2117 static void
2118 S_sv_setnv(pTHX_ SV* sv, int numtype)
2119 {
2120 bool pok = cBOOL(SvPOK(sv));
2121 bool nok = FALSE;
2122 if ((numtype & IS_NUMBER_INFINITY)) {
2123 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -NV_INF : NV_INF);
2124 nok = TRUE;
2125 }
2126 else if ((numtype & IS_NUMBER_NAN)) {
2127 SvNV_set(sv, NV_NAN);
2128 nok = TRUE;
2129 }
2130 else if (pok) {
2131 SvNV_set(sv, Atof(SvPVX_const(sv)));
2132 /* Purposefully no true nok here, since we don't want to blow
2133 * away the possible IOK/UV of an existing sv. */
2134 }
2135 if (nok) {
2136 SvNOK_only(sv); /* No IV or UV please, this is pure infnan. */
2137 if (pok)
2138 SvPOK_on(sv); /* PV is okay, though. */
2139 }
2140 }
2141 #ifdef USING_MSVC6
2142 # pragma warning(pop)
2143 #endif
2144
2145 STATIC bool
2146 S_sv_2iuv_common(pTHX_ SV *const sv)
2147 {
2148 PERL_ARGS_ASSERT_SV_2IUV_COMMON;
2149
2150 if (SvNOKp(sv)) {
2151 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2152 * without also getting a cached IV/UV from it at the same time
2153 * (ie PV->NV conversion should detect loss of accuracy and cache
2154 * IV or UV at same time to avoid this. */
2155 /* IV-over-UV optimisation - choose to cache IV if possible */
2156
2157 if (SvTYPE(sv) == SVt_NV)
2158 sv_upgrade(sv, SVt_PVNV);
2159
2160 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2161 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2162 certainly cast into the IV range at IV_MAX, whereas the correct
2163 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2164 cases go to UV */
2165 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2166 if (Perl_isnan(SvNVX(sv))) {
2167 SvUV_set(sv, 0);
2168 SvIsUV_on(sv);
2169 return FALSE;
2170 }
2171 #endif
2172 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2173 SvIV_set(sv, I_V(SvNVX(sv)));
2174 if (SvNVX(sv) == (NV) SvIVX(sv)
2175 #ifndef NV_PRESERVES_UV
2176 && SvIVX(sv) != IV_MIN /* avoid negating IV_MIN below */
2177 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2178 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2179 /* Don't flag it as "accurately an integer" if the number
2180 came from a (by definition imprecise) NV operation, and
2181 we're outside the range of NV integer precision */
2182 #endif
2183 ) {
2184 if (SvNOK(sv))
2185 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2186 else {
2187 /* scalar has trailing garbage, eg "42a" */
2188 }
2189 DEBUG_c(PerlIO_printf(Perl_debug_log,
2190 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
2191 PTR2UV(sv),
2192 SvNVX(sv),
2193 SvIVX(sv)));
2194
2195 } else {
2196 /* IV not precise. No need to convert from PV, as NV
2197 conversion would already have cached IV if it detected
2198 that PV->IV would be better than PV->NV->IV
2199 flags already correct - don't set public IOK. */
2200 DEBUG_c(PerlIO_printf(Perl_debug_log,
2201 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
2202 PTR2UV(sv),
2203 SvNVX(sv),
2204 SvIVX(sv)));
2205 }
2206 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2207 but the cast (NV)IV_MIN rounds to a the value less (more
2208 negative) than IV_MIN which happens to be equal to SvNVX ??
2209 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2210 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2211 (NV)UVX == NVX are both true, but the values differ. :-(
2212 Hopefully for 2s complement IV_MIN is something like
2213 0x8000000000000000 which will be exact. NWC */
2214 }
2215 else {
2216 SvUV_set(sv, U_V(SvNVX(sv)));
2217 if (
2218 (SvNVX(sv) == (NV) SvUVX(sv))
2219 #ifndef NV_PRESERVES_UV
2220 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2221 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2222 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2223 /* Don't flag it as "accurately an integer" if the number
2224 came from a (by definition imprecise) NV operation, and
2225 we're outside the range of NV integer precision */
2226 #endif
2227 && SvNOK(sv)
2228 )
2229 SvIOK_on(sv);
2230 SvIsUV_on(sv);
2231 DEBUG_c(PerlIO_printf(Perl_debug_log,
2232 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
2233 PTR2UV(sv),
2234 SvUVX(sv),
2235 SvUVX(sv)));
2236 }
2237 }
2238 else if (SvPOKp(sv)) {
2239 UV value;
2240 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2241 /* We want to avoid a possible problem when we cache an IV/ a UV which
2242 may be later translated to an NV, and the resulting NV is not
2243 the same as the direct translation of the initial string
2244 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2245 be careful to ensure that the value with the .456 is around if the
2246 NV value is requested in the future).
2247
2248 This means that if we cache such an IV/a UV, we need to cache the
2249 NV as well. Moreover, we trade speed for space, and do not
2250 cache the NV if we are sure it's not needed.
2251 */
2252
2253 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2254 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2255 == IS_NUMBER_IN_UV) {
2256 /* It's definitely an integer, only upgrade to PVIV */
2257 if (SvTYPE(sv) < SVt_PVIV)
2258 sv_upgrade(sv, SVt_PVIV);
2259 (void)SvIOK_on(sv);
2260 } else if (SvTYPE(sv) < SVt_PVNV)
2261 sv_upgrade(sv, SVt_PVNV);
2262
2263 if ((numtype & (IS_NUMBER_INFINITY | IS_NUMBER_NAN))) {
2264 if (ckWARN(WARN_NUMERIC) && ((numtype & IS_NUMBER_TRAILING)))
2265 not_a_number(sv);
2266 S_sv_setnv(aTHX_ sv, numtype);
2267 return FALSE;
2268 }
2269
2270 /* If NVs preserve UVs then we only use the UV value if we know that
2271 we aren't going to call atof() below. If NVs don't preserve UVs
2272 then the value returned may have more precision than atof() will
2273 return, even though value isn't perfectly accurate. */
2274 if ((numtype & (IS_NUMBER_IN_UV
2275 #ifdef NV_PRESERVES_UV
2276 | IS_NUMBER_NOT_INT
2277 #endif
2278 )) == IS_NUMBER_IN_UV) {
2279 /* This won't turn off the public IOK flag if it was set above */
2280 (void)SvIOKp_on(sv);
2281
2282 if (!(numtype & IS_NUMBER_NEG)) {
2283 /* positive */;
2284 if (value <= (UV)IV_MAX) {
2285 SvIV_set(sv, (IV)value);
2286 } else {
2287 /* it didn't overflow, and it was positive. */
2288 SvUV_set(sv, value);
2289 SvIsUV_on(sv);
2290 }
2291 } else {
2292 /* 2s complement assumption */
2293 if (value <= (UV)IV_MIN) {
2294 SvIV_set(sv, value == (UV)IV_MIN
2295 ? IV_MIN : -(IV)value);
2296 } else {
2297 /* Too negative for an IV. This is a double upgrade, but
2298 I'm assuming it will be rare. */
2299 if (SvTYPE(sv) < SVt_PVNV)
2300 sv_upgrade(sv, SVt_PVNV);
2301 SvNOK_on(sv);
2302 SvIOK_off(sv);
2303 SvIOKp_on(sv);
2304 SvNV_set(sv, -(NV)value);
2305 SvIV_set(sv, IV_MIN);
2306 }
2307 }
2308 }
2309 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2310 will be in the previous block to set the IV slot, and the next
2311 block to set the NV slot. So no else here. */
2312
2313 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2314 != IS_NUMBER_IN_UV) {
2315 /* It wasn't an (integer that doesn't overflow the UV). */
2316 S_sv_setnv(aTHX_ sv, numtype);
2317
2318 if (! numtype && ckWARN(WARN_NUMERIC))
2319 not_a_number(sv);
2320
2321 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" NVgf ")\n",
2322 PTR2UV(sv), SvNVX(sv)));
2323
2324 #ifdef NV_PRESERVES_UV
2325 (void)SvIOKp_on(sv);
2326 (void)SvNOK_on(sv);
2327 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2328 if (Perl_isnan(SvNVX(sv))) {
2329 SvUV_set(sv, 0);
2330 SvIsUV_on(sv);
2331 return FALSE;
2332 }
2333 #endif
2334 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2335 SvIV_set(sv, I_V(SvNVX(sv)));
2336 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2337 SvIOK_on(sv);
2338 } else {
2339 NOOP; /* Integer is imprecise. NOK, IOKp */
2340 }
2341 /* UV will not work better than IV */
2342 } else {
2343 if (SvNVX(sv) > (NV)UV_MAX) {
2344 SvIsUV_on(sv);
2345 /* Integer is inaccurate. NOK, IOKp, is UV */
2346 SvUV_set(sv, UV_MAX);
2347 } else {
2348 SvUV_set(sv, U_V(SvNVX(sv)));
2349 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2350 NV preservse UV so can do correct comparison. */
2351 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2352 SvIOK_on(sv);
2353 } else {
2354 NOOP; /* Integer is imprecise. NOK, IOKp, is UV */
2355 }
2356 }
2357 SvIsUV_on(sv);
2358 }
2359 #else /* NV_PRESERVES_UV */
2360 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2361 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2362 /* The IV/UV slot will have been set from value returned by
2363 grok_number above. The NV slot has just been set using
2364 Atof. */
2365 SvNOK_on(sv);
2366 assert (SvIOKp(sv));
2367 } else {
2368 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2369 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2370 /* Small enough to preserve all bits. */
2371 (void)SvIOKp_on(sv);
2372 SvNOK_on(sv);
2373 SvIV_set(sv, I_V(SvNVX(sv)));
2374 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2375 SvIOK_on(sv);
2376 /* Assumption: first non-preserved integer is < IV_MAX,
2377 this NV is in the preserved range, therefore: */
2378 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2379 < (UV)IV_MAX)) {
2380 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);
2381 }
2382 } else {
2383 /* IN_UV NOT_INT
2384 0 0 already failed to read UV.
2385 0 1 already failed to read UV.
2386 1 0 you won't get here in this case. IV/UV
2387 slot set, public IOK, Atof() unneeded.
2388 1 1 already read UV.
2389 so there's no point in sv_2iuv_non_preserve() attempting
2390 to use atol, strtol, strtoul etc. */
2391 # ifdef DEBUGGING
2392 sv_2iuv_non_preserve (sv, numtype);
2393 # else
2394 sv_2iuv_non_preserve (sv);
2395 # endif
2396 }
2397 }
2398 #endif /* NV_PRESERVES_UV */
2399 /* It might be more code efficient to go through the entire logic above
2400 and conditionally set with SvIOKp_on() rather than SvIOK(), but it
2401 gets complex and potentially buggy, so more programmer efficient
2402 to do it this way, by turning off the public flags: */
2403 if (!numtype)
2404 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
2405 }
2406 }
2407 else {
2408 if (isGV_with_GP(sv))
2409 return glob_2number(MUTABLE_GV(sv));
2410
2411 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2412 report_uninit(sv);
2413 if (SvTYPE(sv) < SVt_IV)
2414 /* Typically the caller expects that sv_any is not NULL now. */
2415 sv_upgrade(sv, SVt_IV);
2416 /* Return 0 from the caller. */
2417 return TRUE;
2418 }
2419 return FALSE;
2420 }
2421
2422 /*
2423 =for apidoc sv_2iv_flags
2424
2425 Return the integer value of an SV, doing any necessary string
2426 conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2427 Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2428
2429 =cut
2430 */
2431
2432 IV
2433 Perl_sv_2iv_flags(pTHX_ SV *const sv, const I32 flags)
2434 {
2435 PERL_ARGS_ASSERT_SV_2IV_FLAGS;
2436
2437 assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2438 && SvTYPE(sv) != SVt_PVFM);
2439
2440 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2441 mg_get(sv);
2442
2443 if (SvROK(sv)) {
2444 if (SvAMAGIC(sv)) {
2445 SV * tmpstr;
2446 if (flags & SV_SKIP_OVERLOAD)
2447 return 0;
2448 tmpstr = AMG_CALLunary(sv, numer_amg);
2449 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2450 return SvIV(tmpstr);
2451 }
2452 }
2453 return PTR2IV(SvRV(sv));
2454 }
2455
2456 if (SvVALID(sv) || isREGEXP(sv)) {
2457 /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2458 the same flag bit as SVf_IVisUV, so must not let them cache IVs.
2459 In practice they are extremely unlikely to actually get anywhere
2460 accessible by user Perl code - the only way that I'm aware of is when
2461 a constant subroutine which is used as the second argument to index.
2462
2463 Regexps have no SvIVX and SvNVX fields.
2464 */
2465 assert(isREGEXP(sv) || SvPOKp(sv));
2466 {
2467 UV value;
2468 const char * const ptr =
2469 isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
2470 const int numtype
2471 = grok_number(ptr, SvCUR(sv), &value);
2472
2473 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2474 == IS_NUMBER_IN_UV) {
2475 /* It's definitely an integer */
2476 if (numtype & IS_NUMBER_NEG) {
2477 if (value < (UV)IV_MIN)
2478 return -(IV)value;
2479 } else {
2480 if (value < (UV)IV_MAX)
2481 return (IV)value;
2482 }
2483 }
2484
2485 /* Quite wrong but no good choices. */
2486 if ((numtype & IS_NUMBER_INFINITY)) {
2487 return (numtype & IS_NUMBER_NEG) ? IV_MIN : IV_MAX;
2488 } else if ((numtype & IS_NUMBER_NAN)) {
2489 return 0; /* So wrong. */
2490 }
2491
2492 if (!numtype) {
2493 if (ckWARN(WARN_NUMERIC))
2494 not_a_number(sv);
2495 }
2496 return I_V(Atof(ptr));
2497 }
2498 }
2499
2500 if (SvTHINKFIRST(sv)) {
2501 #ifdef PERL_OLD_COPY_ON_WRITE
2502 if (SvIsCOW(sv)) {
2503 sv_force_normal_flags(sv, 0);
2504 }
2505 #endif
2506 if (SvREADONLY(sv) && !SvOK(sv)) {
2507 if (ckWARN(WARN_UNINITIALIZED))
2508 report_uninit(sv);
2509 return 0;
2510 }
2511 }
2512
2513 if (!SvIOKp(sv)) {
2514 if (S_sv_2iuv_common(aTHX_ sv))
2515 return 0;
2516 }
2517
2518 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2519 PTR2UV(sv),SvIVX(sv)));
2520 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
2521 }
2522
2523 /*
2524 =for apidoc sv_2uv_flags
2525
2526 Return the unsigned integer value of an SV, doing any necessary string
2527 conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2528 Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
2529
2530 =cut
2531 */
2532
2533 UV
2534 Perl_sv_2uv_flags(pTHX_ SV *const sv, const I32 flags)
2535 {
2536 PERL_ARGS_ASSERT_SV_2UV_FLAGS;
2537
2538 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2539 mg_get(sv);
2540
2541 if (SvROK(sv)) {
2542 if (SvAMAGIC(sv)) {
2543 SV *tmpstr;
2544 if (flags & SV_SKIP_OVERLOAD)
2545 return 0;
2546 tmpstr = AMG_CALLunary(sv, numer_amg);
2547 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2548 return SvUV(tmpstr);
2549 }
2550 }
2551 return PTR2UV(SvRV(sv));
2552 }
2553
2554 if (SvVALID(sv) || isREGEXP(sv)) {
2555 /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2556 the same flag bit as SVf_IVisUV, so must not let them cache IVs.
2557 Regexps have no SvIVX and SvNVX fields. */
2558 assert(isREGEXP(sv) || SvPOKp(sv));
2559 {
2560 UV value;
2561 const char * const ptr =
2562 isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
2563 const int numtype
2564 = grok_number(ptr, SvCUR(sv), &value);
2565
2566 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2567 == IS_NUMBER_IN_UV) {
2568 /* It's definitely an integer */
2569 if (!(numtype & IS_NUMBER_NEG))
2570 return value;
2571 }
2572
2573 /* Quite wrong but no good choices. */
2574 if ((numtype & IS_NUMBER_INFINITY)) {
2575 return UV_MAX; /* So wrong. */
2576 } else if ((numtype & IS_NUMBER_NAN)) {
2577 return 0; /* So wrong. */
2578 }
2579
2580 if (!numtype) {
2581 if (ckWARN(WARN_NUMERIC))
2582 not_a_number(sv);
2583 }
2584 return U_V(Atof(ptr));
2585 }
2586 }
2587
2588 if (SvTHINKFIRST(sv)) {
2589 #ifdef PERL_OLD_COPY_ON_WRITE
2590 if (SvIsCOW(sv)) {
2591 sv_force_normal_flags(sv, 0);
2592 }
2593 #endif
2594 if (SvREADONLY(sv) && !SvOK(sv)) {
2595 if (ckWARN(WARN_UNINITIALIZED))
2596 report_uninit(sv);
2597 return 0;
2598 }
2599 }
2600
2601 if (!SvIOKp(sv)) {
2602 if (S_sv_2iuv_common(aTHX_ sv))
2603 return 0;
2604 }
2605
2606 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2607 PTR2UV(sv),SvUVX(sv)));
2608 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
2609 }
2610
2611 /*
2612 =for apidoc sv_2nv_flags
2613
2614 Return the num value of an SV, doing any necessary string or integer
2615 conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2616 Normally used via the C<SvNV(sv)> and C<SvNVx(sv)> macros.
2617
2618 =cut
2619 */
2620
2621 NV
2622 Perl_sv_2nv_flags(pTHX_ SV *const sv, const I32 flags)
2623 {
2624 PERL_ARGS_ASSERT_SV_2NV_FLAGS;
2625
2626 assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2627 && SvTYPE(sv) != SVt_PVFM);
2628 if (SvGMAGICAL(sv) || SvVALID(sv) || isREGEXP(sv)) {
2629 /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2630 the same flag bit as SVf_IVisUV, so must not let them cache NVs.
2631 Regexps have no SvIVX and SvNVX fields. */
2632 const char *ptr;
2633 if (flags & SV_GMAGIC)
2634 mg_get(sv);
2635 if (SvNOKp(sv))
2636 return SvNVX(sv);
2637 if (SvPOKp(sv) && !SvIOKp(sv)) {
2638 ptr = SvPVX_const(sv);
2639 grokpv:
2640 if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) &&
2641 !grok_number(ptr, SvCUR(sv), NULL))
2642 not_a_number(sv);
2643 return Atof(ptr);
2644 }
2645 if (SvIOKp(sv)) {
2646 if (SvIsUV(sv))
2647 return (NV)SvUVX(sv);
2648 else
2649 return (NV)SvIVX(sv);
2650 }
2651 if (SvROK(sv)) {
2652 goto return_rok;
2653 }
2654 if (isREGEXP(sv)) {
2655 ptr = RX_WRAPPED((REGEXP *)sv);
2656 goto grokpv;
2657 }
2658 assert(SvTYPE(sv) >= SVt_PVMG);
2659 /* This falls through to the report_uninit near the end of the
2660 function. */
2661 } else if (SvTHINKFIRST(sv)) {
2662 if (SvROK(sv)) {
2663 return_rok:
2664 if (SvAMAGIC(sv)) {
2665 SV *tmpstr;
2666 if (flags & SV_SKIP_OVERLOAD)
2667 return 0;
2668 tmpstr = AMG_CALLunary(sv, numer_amg);
2669 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2670 return SvNV(tmpstr);
2671 }
2672 }
2673 return PTR2NV(SvRV(sv));
2674 }
2675 #ifdef PERL_OLD_COPY_ON_WRITE
2676 if (SvIsCOW(sv)) {
2677 sv_force_normal_flags(sv, 0);
2678 }
2679 #endif
2680 if (SvREADONLY(sv) && !SvOK(sv)) {
2681 if (ckWARN(WARN_UNINITIALIZED))
2682 report_uninit(sv);
2683 return 0.0;
2684 }
2685 }
2686 if (SvTYPE(sv) < SVt_NV) {
2687 /* The logic to use SVt_PVNV if necessary is in sv_upgrade. */
2688 sv_upgrade(sv, SVt_NV);
2689 DEBUG_c({
2690 STORE_NUMERIC_LOCAL_SET_STANDARD();
2691 PerlIO_printf(Perl_debug_log,
2692 "0x%"UVxf" num(%" NVgf ")\n",
2693 PTR2UV(sv), SvNVX(sv));
2694 RESTORE_NUMERIC_LOCAL();
2695 });
2696 }
2697 else if (SvTYPE(sv) < SVt_PVNV)
2698 sv_upgrade(sv, SVt_PVNV);
2699 if (SvNOKp(sv)) {
2700 return SvNVX(sv);
2701 }
2702 if (SvIOKp(sv)) {
2703 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
2704 #ifdef NV_PRESERVES_UV
2705 if (SvIOK(sv))
2706 SvNOK_on(sv);
2707 else
2708 SvNOKp_on(sv);
2709 #else
2710 /* Only set the public NV OK flag if this NV preserves the IV */
2711 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2712 if (SvIOK(sv) &&
2713 SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2714 : (SvIVX(sv) == I_V(SvNVX(sv))))
2715 SvNOK_on(sv);
2716 else
2717 SvNOKp_on(sv);
2718 #endif
2719 }
2720 else if (SvPOKp(sv)) {
2721 UV value;
2722 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2723 if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC))
2724 not_a_number(sv);
2725 #ifdef NV_PRESERVES_UV
2726 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2727 == IS_NUMBER_IN_UV) {
2728 /* It's definitely an integer */
2729 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
2730 } else {
2731 S_sv_setnv(aTHX_ sv, numtype);
2732 }
2733 if (numtype)
2734 SvNOK_on(sv);
2735 else
2736 SvNOKp_on(sv);
2737 #else
2738 SvNV_set(sv, Atof(SvPVX_const(sv)));
2739 /* Only set the public NV OK flag if this NV preserves the value in
2740 the PV at least as well as an IV/UV would.
2741 Not sure how to do this 100% reliably. */
2742 /* if that shift count is out of range then Configure's test is
2743 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2744 UV_BITS */
2745 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2746 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2747 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
2748 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2749 /* Can't use strtol etc to convert this string, so don't try.
2750 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2751 SvNOK_on(sv);
2752 } else {
2753 /* value has been set. It may not be precise. */
2754 if ((numtype & IS_NUMBER_NEG) && (value >= (UV)IV_MIN)) {
2755 /* 2s complement assumption for (UV)IV_MIN */
2756 SvNOK_on(sv); /* Integer is too negative. */
2757 } else {
2758 SvNOKp_on(sv);
2759 SvIOKp_on(sv);
2760
2761 if (numtype & IS_NUMBER_NEG) {
2762 /* -IV_MIN is undefined, but we should never reach
2763 * this point with both IS_NUMBER_NEG and value ==
2764 * (UV)IV_MIN */
2765 assert(value != (UV)IV_MIN);
2766 SvIV_set(sv, -(IV)value);
2767 } else if (value <= (UV)IV_MAX) {
2768 SvIV_set(sv, (IV)value);
2769 } else {
2770 SvUV_set(sv, value);
2771 SvIsUV_on(sv);
2772 }
2773
2774 if (numtype & IS_NUMBER_NOT_INT) {
2775 /* I believe that even if the original PV had decimals,
2776 they are lost beyond the limit of the FP precision.
2777 However, neither is canonical, so both only get p
2778 flags. NWC, 2000/11/25 */
2779 /* Both already have p flags, so do nothing */
2780 } else {
2781 const NV nv = SvNVX(sv);
2782 /* XXX should this spot have NAN_COMPARE_BROKEN, too? */
2783 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2784 if (SvIVX(sv) == I_V(nv)) {
2785 SvNOK_on(sv);
2786 } else {
2787 /* It had no "." so it must be integer. */
2788 }
2789 SvIOK_on(sv);
2790 } else {
2791 /* between IV_MAX and NV(UV_MAX).
2792 Could be slightly > UV_MAX */
2793
2794 if (numtype & IS_NUMBER_NOT_INT) {
2795 /* UV and NV both imprecise. */
2796 } else {
2797 const UV nv_as_uv = U_V(nv);
2798
2799 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2800 SvNOK_on(sv);
2801 }
2802 SvIOK_on(sv);
2803 }
2804 }
2805 }
2806 }
2807 }
2808 /* It might be more code efficient to go through the entire logic above
2809 and conditionally set with SvNOKp_on() rather than SvNOK(), but it
2810 gets complex and potentially buggy, so more programmer efficient
2811 to do it this way, by turning off the public flags: */
2812 if (!numtype)
2813 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
2814 #endif /* NV_PRESERVES_UV */
2815 }
2816 else {
2817 if (isGV_with_GP(sv)) {
2818 glob_2number(MUTABLE_GV(sv));
2819 return 0.0;
2820 }
2821
2822 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2823 report_uninit(sv);
2824 assert (SvTYPE(sv) >= SVt_NV);
2825 /* Typically the caller expects that sv_any is not NULL now. */
2826 /* XXX Ilya implies that this is a bug in callers that assume this
2827 and ideally should be fixed. */
2828 return 0.0;
2829 }
2830 DEBUG_c({
2831 STORE_NUMERIC_LOCAL_SET_STANDARD();
2832 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" NVgf ")\n",
2833 PTR2UV(sv), SvNVX(sv));
2834 RESTORE_NUMERIC_LOCAL();
2835 });
2836 return SvNVX(sv);
2837 }
2838
2839 /*
2840 =for apidoc sv_2num
2841
2842 Return an SV with the numeric value of the source SV, doing any necessary
2843 reference or overload conversion. The caller is expected to have handled
2844 get-magic already.
2845
2846 =cut
2847 */
2848
2849 SV *
2850 Perl_sv_2num(pTHX_ SV *const sv)
2851 {
2852 PERL_ARGS_ASSERT_SV_2NUM;
2853
2854 if (!SvROK(sv))
2855 return sv;
2856 if (SvAMAGIC(sv)) {
2857 SV * const tmpsv = AMG_CALLunary(sv, numer_amg);
2858 TAINT_IF(tmpsv && SvTAINTED(tmpsv));
2859 if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
2860 return sv_2num(tmpsv);
2861 }
2862 return sv_2mortal(newSVuv(PTR2UV(SvRV(sv))));
2863 }
2864
2865 /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2866 * UV as a string towards the end of buf, and return pointers to start and
2867 * end of it.
2868 *
2869 * We assume that buf is at least TYPE_CHARS(UV) long.
2870 */
2871
2872 static char *
2873 S_uiv_2buf(char *const buf, const IV iv, UV uv, const int is_uv, char **const peob)
2874 {
2875 char *ptr = buf + TYPE_CHARS(UV);
2876 char * const ebuf = ptr;
2877 int sign;
2878
2879 PERL_ARGS_ASSERT_UIV_2BUF;
2880
2881 if (is_uv)
2882 sign = 0;
2883 else if (iv >= 0) {
2884 uv = iv;
2885 sign = 0;
2886 } else {
2887 uv = (iv == IV_MIN) ? (UV)iv : (UV)(-iv);
2888 sign = 1;
2889 }
2890 do {
2891 *--ptr = '0' + (char)(uv % 10);
2892 } while (uv /= 10);
2893 if (sign)
2894 *--ptr = '-';
2895 *peob = ebuf;
2896 return ptr;
2897 }
2898
2899 /* Helper for sv_2pv_flags and sv_vcatpvfn_flags. If the NV is an
2900 * infinity or a not-a-number, writes the appropriate strings to the
2901 * buffer, including a zero byte. On success returns the written length,
2902 * excluding the zero byte, on failure (not an infinity, not a nan, or the
2903 * maxlen too small) returns zero.
2904 *
2905 * XXX for "Inf", "-Inf", and "NaN", we could have three read-only
2906 * shared string constants we point to, instead of generating a new
2907 * string for each instance. */
2908 STATIC size_t
2909 S_infnan_2pv(NV nv, char* buffer, size_t maxlen, char plus) {
2910 assert(maxlen >= 4);
2911 if (maxlen < 4) /* "Inf\0", "NaN\0" */
2912 return 0;
2913 else {
2914 char* s = buffer;
2915 if (Perl_isinf(nv)) {
2916 if (nv < 0) {
2917 if (maxlen < 5) /* "-Inf\0" */
2918 return 0;
2919 *s++ = '-';
2920 } else if (plus) {
2921 *s++ = '+';
2922 }
2923 *s++ = 'I';
2924 *s++ = 'n';
2925 *s++ = 'f';
2926 } else if (Perl_isnan(nv)) {
2927 *s++ = 'N';
2928 *s++ = 'a';
2929 *s++ = 'N';
2930 /* XXX optionally output the payload mantissa bits as
2931 * "(unsigned)" (to match the nan("...") C99 function,
2932 * or maybe as "(0xhhh...)" would make more sense...
2933 * provide a format string so that the user can decide?
2934 * NOTE: would affect the maxlen and assert() logic.*/
2935 }
2936
2937 else
2938 return 0;
2939 assert((s == buffer + 3) || (s == buffer + 4));
2940 *s++ = 0;
2941 return s - buffer - 1; /* -1: excluding the zero byte */
2942 }
2943 }
2944
2945 /*
2946 =for apidoc sv_2pv_flags
2947
2948 Returns a pointer to the string value of an SV, and sets *lp to its length.
2949 If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a
2950 string if necessary. Normally invoked via the C<SvPV_flags> macro.
2951 C<sv_2pv()> and C<sv_2pv_nomg> usually end up here too.
2952
2953 =cut
2954 */
2955
2956 char *
2957 Perl_sv_2pv_flags(pTHX_ SV *const sv, STRLEN *const lp, const I32 flags)
2958 {
2959 char *s;
2960
2961 PERL_ARGS_ASSERT_SV_2PV_FLAGS;
2962
2963 assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2964 && SvTYPE(sv) != SVt_PVFM);
2965 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2966 mg_get(sv);
2967 if (SvROK(sv)) {
2968 if (SvAMAGIC(sv)) {
2969 SV *tmpstr;
2970 if (flags & SV_SKIP_OVERLOAD)
2971 return NULL;
2972 tmpstr = AMG_CALLunary(sv, string_amg);
2973 TAINT_IF(tmpstr && SvTAINTED(tmpstr));
2974 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2975 /* Unwrap this: */
2976 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr);
2977 */
2978
2979 char *pv;
2980 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
2981 if (flags & SV_CONST_RETURN) {
2982 pv = (char *) SvPVX_const(tmpstr);
2983 } else {
2984 pv = (flags & SV_MUTABLE_RETURN)
2985 ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
2986 }
2987 if (lp)
2988 *lp = SvCUR(tmpstr);
2989 } else {
2990 pv = sv_2pv_flags(tmpstr, lp, flags);
2991 }
2992 if (SvUTF8(tmpstr))
2993 SvUTF8_on(sv);
2994 else
2995 SvUTF8_off(sv);
2996 return pv;
2997 }
2998 }
2999 {
3000 STRLEN len;
3001 char *retval;
3002 char *buffer;
3003 SV *const referent = SvRV(sv);
3004
3005 if (!referent) {
3006 len = 7;
3007 retval = buffer = savepvn("NULLREF", len);
3008 } else if (SvTYPE(referent) == SVt_REGEXP &&
3009 (!(PL_curcop->cop_hints & HINT_NO_AMAGIC) ||
3010 amagic_is_enabled(string_amg))) {
3011 REGEXP * const re = (REGEXP *)MUTABLE_PTR(referent);
3012
3013 assert(re);
3014
3015 /* If the regex is UTF-8 we want the containing scalar to
3016 have an UTF-8 flag too */
3017 if (RX_UTF8(re))
3018 SvUTF8_on(sv);
3019 else
3020 SvUTF8_off(sv);
3021
3022 if (lp)
3023 *lp = RX_WRAPLEN(re);
3024
3025 return RX_WRAPPED(re);
3026 } else {
3027 const char *const typestr = sv_reftype(referent, 0);
3028 const STRLEN typelen = strlen(typestr);
3029 UV addr = PTR2UV(referent);
3030 const char *stashname = NULL;
3031 STRLEN stashnamelen = 0; /* hush, gcc */
3032 const char *buffer_end;
3033
3034 if (SvOBJECT(referent)) {
3035 const HEK *const name = HvNAME_HEK(SvSTASH(referent));
3036
3037 if (name) {
3038 stashname = HEK_KEY(name);
3039 stashnamelen = HEK_LEN(name);
3040
3041 if (HEK_UTF8(name)) {
3042 SvUTF8_on(sv);
3043 } else {
3044 SvUTF8_off(sv);
3045 }
3046 } else {
3047 stashname = "__ANON__";
3048 stashnamelen = 8;
3049 }
3050 len = stashnamelen + 1 /* = */ + typelen + 3 /* (0x */
3051 + 2 * sizeof(UV) + 2 /* )\0 */;
3052 } else {
3053 len = typelen + 3 /* (0x */
3054 + 2 * sizeof(UV) + 2 /* )\0 */;
3055 }
3056
3057 Newx(buffer, len, char);
3058 buffer_end = retval = buffer + len;
3059
3060 /* Working backwards */
3061 *--retval = '\0';
3062 *--retval = ')';
3063 do {
3064 *--retval = PL_hexdigit[addr & 15];
3065 } while (addr >>= 4);
3066 *--retval = 'x';
3067 *--retval = '0';
3068 *--retval = '(';
3069
3070 retval -= typelen;
3071 memcpy(retval, typestr, typelen);
3072
3073 if (stashname) {
3074 *--retval = '=';
3075 retval -= stashnamelen;
3076 memcpy(retval, stashname, stashnamelen);
3077 }
3078 /* retval may not necessarily have reached the start of the
3079 buffer here. */
3080 assert (retval >= buffer);
3081
3082 len = buffer_end - retval - 1; /* -1 for that \0 */
3083 }
3084 if (lp)
3085 *lp = len;
3086 SAVEFREEPV(buffer);
3087 return retval;
3088 }
3089 }
3090
3091 if (SvPOKp(sv)) {
3092 if (lp)
3093 *lp = SvCUR(sv);
3094 if (flags & SV_MUTABLE_RETURN)
3095 return SvPVX_mutable(sv);
3096 if (flags & SV_CONST_RETURN)
3097 return (char *)SvPVX_const(sv);
3098 return SvPVX(sv);
3099 }
3100
3101 if (SvIOK(sv)) {
3102 /* I'm assuming that if both IV and NV are equally valid then
3103 converting the IV is going to be more efficient */
3104 const U32 isUIOK = SvIsUV(sv);
3105 char buf[TYPE_CHARS(UV)];
3106 char *ebuf, *ptr;
3107 STRLEN len;
3108
3109 if (SvTYPE(sv) < SVt_PVIV)
3110 sv_upgrade(sv, SVt_PVIV);
3111 ptr = uiv_2buf(buf, SvIVX(sv), SvUVX(sv), isUIOK, &ebuf);
3112 len = ebuf - ptr;
3113 /* inlined from sv_setpvn */
3114 s = SvGROW_mutable(sv, len + 1);
3115 Move(ptr, s, len, char);
3116 s += len;
3117 *s = '\0';
3118 SvPOK_on(sv);
3119 }
3120 else if (SvNOK(sv)) {
3121 if (SvTYPE(sv) < SVt_PVNV)
3122 sv_upgrade(sv, SVt_PVNV);
3123 if (SvNVX(sv) == 0.0
3124 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
3125 && !Perl_isnan(SvNVX(sv))
3126 #endif
3127 ) {
3128 s = SvGROW_mutable(sv, 2);
3129 *s++ = '0';
3130 *s = '\0';
3131 } else {
3132 STRLEN len;
3133 STRLEN size = 5; /* "-Inf\0" */
3134
3135 s = SvGROW_mutable(sv, size);
3136 len = S_infnan_2pv(SvNVX(sv), s, size, 0);
3137 if (len > 0) {
3138 s += len;
3139 SvPOK_on(sv);
3140 }
3141 else {
3142 /* some Xenix systems wipe out errno here */
3143 dSAVE_ERRNO;
3144
3145 size =
3146 1 + /* sign */
3147 1 + /* "." */
3148 NV_DIG +
3149 1 + /* "e" */
3150 1 + /* sign */
3151 5 + /* exponent digits */
3152 1 + /* \0 */
3153 2; /* paranoia */
3154
3155 s = SvGROW_mutable(sv, size);
3156 #ifndef USE_LOCALE_NUMERIC
3157 SNPRINTF_G(SvNVX(sv), s, SvLEN(sv), NV_DIG);
3158
3159 SvPOK_on(sv);
3160 #else
3161 {
3162 bool local_radix;
3163 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
3164 STORE_LC_NUMERIC_SET_TO_NEEDED();
3165
3166 local_radix =
3167 PL_numeric_local &&
3168 PL_numeric_radix_sv &&
3169 SvUTF8(PL_numeric_radix_sv);
3170 if (local_radix && SvLEN(PL_numeric_radix_sv) > 1) {
3171 size += SvLEN(PL_numeric_radix_sv) - 1;
3172 s = SvGROW_mutable(sv, size);
3173 }
3174
3175 SNPRINTF_G(SvNVX(sv), s, SvLEN(sv), NV_DIG);
3176
3177 /* If the radix character is UTF-8, and actually is in the
3178 * output, turn on the UTF-8 flag for the scalar */
3179 if (local_radix &&
3180 instr(s, SvPVX_const(PL_numeric_radix_sv))) {
3181 SvUTF8_on(sv);
3182 }
3183
3184 RESTORE_LC_NUMERIC();
3185 }
3186
3187 /* We don't call SvPOK_on(), because it may come to
3188 * pass that the locale changes so that the
3189 * stringification we just did is no longer correct. We
3190 * will have to re-stringify every time it is needed */
3191 #endif
3192 RESTORE_ERRNO;
3193 }
3194 while (*s) s++;
3195 }
3196 }
3197 else if (isGV_with_GP(sv)) {
3198 GV *const gv = MUTABLE_GV(sv);
3199 SV *const buffer = sv_newmortal();
3200
3201 gv_efullname3(buffer, gv, "*");
3202
3203 assert(SvPOK(buffer));
3204 if (SvUTF8(buffer))
3205 SvUTF8_on(sv);
3206 if (lp)
3207 *lp = SvCUR(buffer);
3208 return SvPVX(buffer);
3209 }
3210 else if (isREGEXP(sv)) {
3211 if (lp) *lp = RX_WRAPLEN((REGEXP *)sv);
3212 return RX_WRAPPED((REGEXP *)sv);
3213 }
3214 else {
3215 if (lp)
3216 *lp = 0;
3217 if (flags & SV_UNDEF_RETURNS_NULL)
3218 return NULL;
3219 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
3220 report_uninit(sv);
3221 /* Typically the caller expects that sv_any is not NULL now. */
3222 if (!SvREADONLY(sv) && SvTYPE(sv) < SVt_PV)
3223 sv_upgrade(sv, SVt_PV);
3224 return (char *)"";
3225 }
3226
3227 {
3228 const STRLEN len = s - SvPVX_const(sv);
3229 if (lp)
3230 *lp = len;
3231 SvCUR_set(sv, len);
3232 }
3233 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3234 PTR2UV(sv),SvPVX_const(sv)));
3235 if (flags & SV_CONST_RETURN)
3236 return (char *)SvPVX_const(sv);
3237 if (flags & SV_MUTABLE_RETURN)
3238 return SvPVX_mutable(sv);
3239 return SvPVX(sv);
3240 }
3241
3242 /*
3243 =for apidoc sv_copypv
3244
3245 Copies a stringified representation of the source SV into the
3246 destination SV. Automatically performs any necessary mg_get and
3247 coercion of numeric values into strings. Guaranteed to preserve
3248 UTF8 flag even from overloaded objects. Similar in nature to
3249 sv_2pv[_flags] but operates directly on an SV instead of just the
3250 string. Mostly uses sv_2pv_flags to do its work, except when that
3251 would lose the UTF-8'ness of the PV.
3252
3253 =for apidoc sv_copypv_nomg
3254
3255 Like sv_copypv, but doesn't invoke get magic first.
3256
3257 =for apidoc sv_copypv_flags
3258
3259 Implementation of sv_copypv and sv_copypv_nomg. Calls get magic iff flags
3260 include SV_GMAGIC.
3261
3262 =cut
3263 */
3264
3265 void
3266 Perl_sv_copypv_flags(pTHX_ SV *const dsv, SV *const ssv, const I32 flags)
3267 {
3268 STRLEN len;
3269 const char *s;
3270
3271 PERL_ARGS_ASSERT_SV_COPYPV_FLAGS;
3272
3273 s = SvPV_flags_const(ssv,len,(flags & SV_GMAGIC));
3274 sv_setpvn(dsv,s,len);
3275 if (SvUTF8(ssv))
3276 SvUTF8_on(dsv);
3277 else
3278 SvUTF8_off(dsv);
3279 }
3280
3281 /*
3282 =for apidoc sv_2pvbyte
3283
3284 Return a pointer to the byte-encoded representation of the SV, and set *lp
3285 to its length. May cause the SV to be downgraded from UTF-8 as a
3286 side-effect.
3287
3288 Usually accessed via the C<SvPVbyte> macro.
3289
3290 =cut
3291 */
3292
3293 char *
3294 Perl_sv_2pvbyte(pTHX_ SV *sv, STRLEN *const lp)
3295 {
3296 PERL_ARGS_ASSERT_SV_2PVBYTE;
3297
3298 SvGETMAGIC(sv);
3299 if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
3300 || isGV_with_GP(sv) || SvROK(sv)) {
3301 SV *sv2 = sv_newmortal();
3302 sv_copypv_nomg(sv2,sv);
3303 sv = sv2;
3304 }
3305 sv_utf8_downgrade(sv,0);
3306 return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
3307 }
3308
3309 /*
3310 =for apidoc sv_2pvutf8
3311
3312 Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3313 to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
3314
3315 Usually accessed via the C<SvPVutf8> macro.
3316
3317 =cut
3318 */
3319
3320 char *
3321 Perl_sv_2pvutf8(pTHX_ SV *sv, STRLEN *const lp)
3322 {
3323 PERL_ARGS_ASSERT_SV_2PVUTF8;
3324
3325 if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
3326 || isGV_with_GP(sv) || SvROK(sv))
3327 sv = sv_mortalcopy(sv);
3328 else
3329 SvGETMAGIC(sv);
3330 sv_utf8_upgrade_nomg(sv);
3331 return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
3332 }
3333
3334
3335 /*
3336 =for apidoc sv_2bool
3337
3338 This macro is only used by sv_true() or its macro equivalent, and only if
3339 the latter's argument is neither SvPOK, SvIOK nor SvNOK.
3340 It calls sv_2bool_flags with the SV_GMAGIC flag.
3341
3342 =for apidoc sv_2bool_flags
3343
3344 This function is only used by sv_true() and friends, and only if
3345 the latter's argument is neither SvPOK, SvIOK nor SvNOK. If the flags
3346 contain SV_GMAGIC, then it does an mg_get() first.
3347
3348
3349 =cut
3350 */
3351
3352 bool
3353 Perl_sv_2bool_flags(pTHX_ SV *sv, I32 flags)
3354 {
3355 PERL_ARGS_ASSERT_SV_2BOOL_FLAGS;
3356
3357 restart:
3358 if(flags & SV_GMAGIC) SvGETMAGIC(sv);
3359
3360 if (!SvOK(sv))
3361 return 0;
3362 if (SvROK(sv)) {
3363 if (SvAMAGIC(sv)) {
3364 SV * const tmpsv = AMG_CALLunary(sv, bool__amg);
3365 if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) {
3366 bool svb;
3367 sv = tmpsv;
3368 if(SvGMAGICAL(sv)) {
3369 flags = SV_GMAGIC;
3370 goto restart; /* call sv_2bool */
3371 }
3372 /* expanded SvTRUE_common(sv, (flags = 0, goto restart)) */
3373 else if(!SvOK(sv)) {
3374 svb = 0;
3375 }
3376 else if(SvPOK(sv)) {
3377 svb = SvPVXtrue(sv);
3378 }
3379 else if((SvFLAGS(sv) & (SVf_IOK|SVf_NOK))) {
3380 svb = (SvIOK(sv) && SvIVX(sv) != 0)
3381 || (SvNOK(sv) && SvNVX(sv) != 0.0);
3382 }
3383 else {
3384 flags = 0;
3385 goto restart; /* call sv_2bool_nomg */
3386 }
3387 return cBOOL(svb);
3388 }
3389 }
3390 return SvRV(sv) != 0;
3391 }
3392 if (isREGEXP(sv))
3393 return
3394 RX_WRAPLEN(sv) > 1 || (RX_WRAPLEN(sv) && *RX_WRAPPED(sv) != '0');
3395 return SvTRUE_common(sv, isGV_with_GP(sv) ? 1 : 0);
3396 }
3397
3398 /*
3399 =for apidoc sv_utf8_upgrade
3400
3401 Converts the PV of an SV to its UTF-8-encoded form.
3402 Forces the SV to string form if it is not already.
3403 Will C<mg_get> on C<sv> if appropriate.
3404 Always sets the SvUTF8 flag to avoid future validity checks even
3405 if the whole string is the same in UTF-8 as not.
3406 Returns the number of bytes in the converted string
3407
3408 This is not a general purpose byte encoding to Unicode interface:
3409 use the Encode extension for that.
3410
3411 =for apidoc sv_utf8_upgrade_nomg
3412
3413 Like sv_utf8_upgrade, but doesn't do magic on C<sv>.
3414
3415 =for apidoc sv_utf8_upgrade_flags
3416
3417 Converts the PV of an SV to its UTF-8-encoded form.
3418 Forces the SV to string form if it is not already.
3419 Always sets the SvUTF8 flag to avoid future validity checks even
3420 if all the bytes are invariant in UTF-8.
3421 If C<flags> has C<SV_GMAGIC> bit set,
3422 will C<mg_get> on C<sv> if appropriate, else not.
3423
3424 If C<flags> has SV_FORCE_UTF8_UPGRADE set, this function assumes that the PV
3425 will expand when converted to UTF-8, and skips the extra work of checking for
3426 that. Typically this flag is used by a routine that has already parsed the
3427 string and found such characters, and passes this information on so that the
3428 work doesn't have to be repeated.
3429
3430 Returns the number of bytes in the converted string.
3431
3432 This is not a general purpose byte encoding to Unicode interface:
3433 use the Encode extension for that.
3434
3435 =for apidoc sv_utf8_upgrade_flags_grow
3436
3437 Like sv_utf8_upgrade_flags, but has an additional parameter C<extra>, which is
3438 the number of unused bytes the string of 'sv' is guaranteed to have free after
3439 it upon return. This allows the caller to reserve extra space that it intends
3440 to fill, to avoid extra grows.
3441
3442 C<sv_utf8_upgrade>, C<sv_utf8_upgrade_nomg>, and C<sv_utf8_upgrade_flags>
3443 are implemented in terms of this function.
3444
3445 Returns the number of bytes in the converted string (not including the spares).
3446
3447 =cut
3448
3449 (One might think that the calling routine could pass in the position of the
3450 first variant character when it has set SV_FORCE_UTF8_UPGRADE, so it wouldn't
3451 have to be found again. But that is not the case, because typically when the
3452 caller is likely to use this flag, it won't be calling this routine unless it
3453 finds something that won't fit into a byte. Otherwise it tries to not upgrade
3454 and just use bytes. But some things that do fit into a byte are variants in
3455 utf8, and the caller may not have been keeping track of these.)
3456
3457 If the routine itself changes the string, it adds a trailing C<NUL>. Such a
3458 C<NUL> isn't guaranteed due to having other routines do the work in some input
3459 cases, or if the input is already flagged as being in utf8.
3460
3461 The speed of this could perhaps be improved for many cases if someone wanted to
3462 write a fast function that counts the number of variant characters in a string,
3463 especially if it could return the position of the first one.
3464
3465 */
3466
3467 STRLEN
3468 Perl_sv_utf8_upgrade_flags_grow(pTHX_ SV *const sv, const I32 flags, STRLEN extra)
3469 {
3470 PERL_ARGS_ASSERT_SV_UTF8_UPGRADE_FLAGS_GROW;
3471
3472 if (sv == &PL_sv_undef)
3473 return 0;
3474 if (!SvPOK_nog(sv)) {
3475 STRLEN len = 0;
3476 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3477 (void) sv_2pv_flags(sv,&len, flags);
3478 if (SvUTF8(sv)) {
3479 if (extra) SvGROW(sv, SvCUR(sv) + extra);
3480 return len;
3481 }
3482 } else {
3483 (void) SvPV_force_flags(sv,len,flags & SV_GMAGIC);
3484 }
3485 }
3486
3487 if (SvUTF8(sv)) {
3488 if (extra) SvGROW(sv, SvCUR(sv) + extra);
3489 return SvCUR(sv);
3490 }
3491
3492 if (SvIsCOW(sv)) {
3493 S_sv_uncow(aTHX_ sv, 0);
3494 }
3495
3496 if (IN_ENCODING && !(flags & SV_UTF8_NO_ENCODING)) {
3497 sv_recode_to_utf8(sv, _get_encoding());
3498 if (extra) SvGROW(sv, SvCUR(sv) + extra);
3499 return SvCUR(sv);
3500 }
3501
3502 if (SvCUR(sv) == 0) {
3503 if (extra) SvGROW(sv, extra);
3504 } else { /* Assume Latin-1/EBCDIC */
3505 /* This function could be much more efficient if we
3506 * had a FLAG in SVs to signal if there are any variant
3507 * chars in the PV. Given that there isn't such a flag
3508 * make the loop as fast as possible (although there are certainly ways
3509 * to speed this up, eg. through vectorization) */
3510 U8 * s = (U8 *) SvPVX_const(sv);
3511 U8 * e = (U8 *) SvEND(sv);
3512 U8 *t = s;
3513 STRLEN two_byte_count = 0;
3514
3515 if (flags & SV_FORCE_UTF8_UPGRADE) goto must_be_utf8;
3516
3517 /* See if really will need to convert to utf8. We mustn't rely on our
3518 * incoming SV being well formed and having a trailing '\0', as certain
3519 * code in pp_formline can send us partially built SVs. */
3520
3521 while (t < e) {
3522 const U8 ch = *t++;
3523 if (NATIVE_BYTE_IS_INVARIANT(ch)) continue;
3524
3525 t--; /* t already incremented; re-point to first variant */
3526 two_byte_count = 1;
3527 goto must_be_utf8;
3528 }
3529
3530 /* utf8 conversion not needed because all are invariants. Mark as
3531 * UTF-8 even if no variant - saves scanning loop */
3532 SvUTF8_on(sv);
3533 if (extra) SvGROW(sv, SvCUR(sv) + extra);
3534 return SvCUR(sv);
3535
3536 must_be_utf8:
3537
3538 /* Here, the string should be converted to utf8, either because of an
3539 * input flag (two_byte_count = 0), or because a character that
3540 * requires 2 bytes was found (two_byte_count = 1). t points either to
3541 * the beginning of the string (if we didn't examine anything), or to
3542 * the first variant. In either case, everything from s to t - 1 will
3543 * occupy only 1 byte each on output.
3544 *
3545 * There are two main ways to convert. One is to create a new string
3546 * and go through the input starting from the beginning, appending each
3547 * converted value onto the new string as we go along. It's probably
3548 * best to allocate enough space in the string for the worst possible
3549 * case rather than possibly running out of space and having to
3550 * reallocate and then copy what we've done so far. Since everything
3551 * from s to t - 1 is invariant, the destination can be initialized
3552 * with these using a fast memory copy
3553 *
3554 * The other way is to figure out exactly how big the string should be
3555 * by parsing the entire input. Then you don't have to make it big
3556 * enough to handle the worst possible case, and more importantly, if
3557 * the string you already have is large enough, you don't have to
3558 * allocate a new string, you can copy the last character in the input
3559 * string to the final position(s) that will be occupied by the
3560 * converted string and go backwards, stopping at t, since everything
3561 * before that is invariant.
3562 *
3563 * There are advantages and disadvantages to each method.
3564 *
3565 * In the first method, we can allocate a new string, do the memory
3566 * copy from the s to t - 1, and then proceed through the rest of the
3567 * string byte-by-byte.
3568 *
3569 * In the second method, we proceed through the rest of the input
3570 * string just calculating how big the converted string will be. Then
3571 * there are two cases:
3572 * 1) if the string has enough extra space to handle the converted
3573 * value. We go backwards through the string, converting until we
3574 * get to the position we are at now, and then stop. If this
3575 * position is far enough along in the string, this method is
3576 * faster than the other method. If the memory copy were the same
3577 * speed as the byte-by-byte loop, that position would be about
3578 * half-way, as at the half-way mark, parsing to the end and back
3579 * is one complete string's parse, the same amount as starting
3580 * over and going all the way through. Actually, it would be
3581 * somewhat less than half-way, as it's faster to just count bytes
3582 * than to also copy, and we don't have the overhead of allocating
3583 * a new string, changing the scalar to use it, and freeing the
3584 * existing one. But if the memory copy is fast, the break-even
3585 * point is somewhere after half way. The counting loop could be
3586 * sped up by vectorization, etc, to move the break-even point
3587 * further towards the beginning.
3588 * 2) if the string doesn't have enough space to handle the converted
3589 * value. A new string will have to be allocated, and one might
3590 * as well, given that, start from the beginning doing the first
3591 * method. We've spent extra time parsing the string and in
3592 * exchange all we've gotten is that we know precisely how big to
3593 * make the new one. Perl is more optimized for time than space,
3594 * so this case is a loser.
3595 * So what I've decided to do is not use the 2nd method unless it is
3596 * guaranteed that a new string won't have to be allocated, assuming
3597 * the worst case. I also decided not to put any more conditions on it
3598 * than this, for now. It seems likely that, since the worst case is
3599 * twice as big as the unknown portion of the string (plus 1), we won't
3600 * be guaranteed enough space, causing us to go to the first method,
3601 * unless the string is short, or the first variant character is near
3602 * the end of it. In either of these cases, it seems best to use the
3603 * 2nd method. The only circumstance I can think of where this would
3604 * be really slower is if the string had once had much more data in it
3605 * than it does now, but there is still a substantial amount in it */
3606
3607 {
3608 STRLEN invariant_head = t - s;
3609 STRLEN size = invariant_head + (e - t) * 2 + 1 + extra;
3610 if (SvLEN(sv) < size) {
3611
3612 /* Here, have decided to allocate a new string */
3613
3614 U8 *dst;
3615 U8 *d;
3616
3617 Newx(dst, size, U8);
3618
3619 /* If no known invariants at the beginning of the input string,
3620 * set so starts from there. Otherwise, can use memory copy to
3621 * get up to where we are now, and then start from here */
3622
3623 if (invariant_head == 0) {
3624 d = dst;
3625 } else {
3626 Copy(s, dst, invariant_head, char);
3627 d = dst + invariant_head;
3628 }
3629
3630 while (t < e) {
3631 append_utf8_from_native_byte(*t, &d);
3632 t++;
3633 }
3634 *d = '\0';
3635 SvPV_free(sv); /* No longer using pre-existing string */
3636 SvPV_set(sv, (char*)dst);
3637 SvCUR_set(sv, d - dst);
3638 SvLEN_set(sv, size);
3639 } else {
3640
3641 /* Here, have decided to get the exact size of the string.
3642 * Currently this happens only when we know that there is
3643 * guaranteed enough space to fit the converted string, so
3644 * don't have to worry about growing. If two_byte_count is 0,
3645 * then t points to the first byte of the string which hasn't
3646 * been examined yet. Otherwise two_byte_count is 1, and t
3647 * points to the first byte in the string that will expand to
3648 * two. Depending on this, start examining at t or 1 after t.
3649 * */
3650
3651 U8 *d = t + two_byte_count;
3652
3653
3654 /* Count up the remaining bytes that expand to two */
3655
3656 while (d < e) {
3657 const U8 chr = *d++;
3658 if (! NATIVE_BYTE_IS_INVARIANT(chr)) two_byte_count++;
3659 }
3660
3661 /* The string will expand by just the number of bytes that
3662 * occupy two positions. But we are one afterwards because of
3663 * the increment just above. This is the place to put the
3664 * trailing NUL, and to set the length before we decrement */
3665
3666 d += two_byte_count;
3667 SvCUR_set(sv, d - s);
3668 *d-- = '\0';
3669
3670
3671 /* Having decremented d, it points to the position to put the
3672 * very last byte of the expanded string. Go backwards through
3673 * the string, copying and expanding as we go, stopping when we
3674 * get to the part that is invariant the rest of the way down */
3675
3676 e--;
3677 while (e >= t) {
3678 if (NATIVE_BYTE_IS_INVARIANT(*e)) {
3679 *d-- = *e;
3680 } else {
3681 *d-- = UTF8_EIGHT_BIT_LO(*e);
3682 *d-- = UTF8_EIGHT_BIT_HI(*e);
3683 }
3684 e--;
3685 }
3686 }
3687
3688 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3689 /* Update pos. We do it at the end rather than during
3690 * the upgrade, to avoid slowing down the common case
3691 * (upgrade without pos).
3692 * pos can be stored as either bytes or characters. Since
3693 * this was previously a byte string we can just turn off
3694 * the bytes flag. */
3695 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3696 if (mg) {
3697 mg->mg_flags &= ~MGf_BYTES;
3698 }
3699 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3700 magic_setutf8(sv,mg); /* clear UTF8 cache */
3701 }
3702 }
3703 }
3704
3705 /* Mark as UTF-8 even if no variant - saves scanning loop */
3706 SvUTF8_on(sv);
3707 return SvCUR(sv);
3708 }
3709
3710 /*
3711 =for apidoc sv_utf8_downgrade
3712
3713 Attempts to convert the PV of an SV from characters to bytes.
3714 If the PV contains a character that cannot fit
3715 in a byte, this conversion will fail;
3716 in this case, either returns false or, if C<fail_ok> is not
3717 true, croaks.
3718
3719 This is not a general purpose Unicode to byte encoding interface:
3720 use the Encode extension for that.
3721
3722 =cut
3723 */
3724
3725 bool
3726 Perl_sv_utf8_downgrade(pTHX_ SV *const sv, const bool fail_ok)
3727 {
3728 PERL_ARGS_ASSERT_SV_UTF8_DOWNGRADE;
3729
3730 if (SvPOKp(sv) && SvUTF8(sv)) {
3731 if (SvCUR(sv)) {
3732 U8 *s;
3733 STRLEN len;
3734 int mg_flags = SV_GMAGIC;
3735
3736 if (SvIsCOW(sv)) {
3737 S_sv_uncow(aTHX_ sv, 0);
3738 }
3739 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3740 /* update pos */
3741 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3742 if (mg && mg->mg_len > 0 && mg->mg_flags & MGf_BYTES) {
3743 mg->mg_len = sv_pos_b2u_flags(sv, mg->mg_len,
3744 SV_GMAGIC|SV_CONST_RETURN);
3745 mg_flags = 0; /* sv_pos_b2u does get magic */
3746 }
3747 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3748 magic_setutf8(sv,mg); /* clear UTF8 cache */
3749
3750 }
3751 s = (U8 *) SvPV_flags(sv, len, mg_flags);
3752
3753 if (!utf8_to_bytes(s, &len)) {
3754 if (fail_ok)
3755 return FALSE;
3756 else {
3757 if (PL_op)
3758 Perl_croak(aTHX_ "Wide character in %s",
3759 OP_DESC(PL_op));
3760 else
3761 Perl_croak(aTHX_ "Wide character");
3762 }
3763 }
3764 SvCUR_set(sv, len);
3765 }
3766 }
3767 SvUTF8_off(sv);
3768 return TRUE;
3769 }
3770
3771 /*
3772 =for apidoc sv_utf8_encode
3773
3774 Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3775 flag off so that it looks like octets again.
3776
3777 =cut
3778 */
3779
3780 void
3781 Perl_sv_utf8_encode(pTHX_ SV *const sv)
3782 {
3783 PERL_ARGS_ASSERT_SV_UTF8_ENCODE;
3784
3785 if (SvREADONLY(sv)) {
3786 sv_force_normal_flags(sv, 0);
3787 }
3788 (void) sv_utf8_upgrade(sv);
3789 SvUTF8_off(sv);
3790 }
3791
3792 /*
3793 =for apidoc sv_utf8_decode
3794
3795 If the PV of the SV is an octet sequence in UTF-8
3796 and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3797 so that it looks like a character. If the PV contains only single-byte
3798 characters, the C<SvUTF8> flag stays off.
3799 Scans PV for validity and returns false if the PV is invalid UTF-8.
3800
3801 =cut
3802 */
3803
3804 bool
3805 Perl_sv_utf8_decode(pTHX_ SV *const sv)
3806 {
3807 PERL_ARGS_ASSERT_SV_UTF8_DECODE;
3808
3809 if (SvPOKp(sv)) {
3810 const U8 *start, *c;
3811 const U8 *e;
3812
3813 /* The octets may have got themselves encoded - get them back as
3814 * bytes
3815 */
3816 if (!sv_utf8_downgrade(sv, TRUE))
3817 return FALSE;
3818
3819 /* it is actually just a matter of turning the utf8 flag on, but
3820 * we want to make sure everything inside is valid utf8 first.
3821 */
3822 c = start = (const U8 *) SvPVX_const(sv);
3823 if (!is_utf8_string(c, SvCUR(sv)))
3824 return FALSE;
3825 e = (const U8 *) SvEND(sv);
3826 while (c < e) {
3827 const U8 ch = *c++;
3828 if (!UTF8_IS_INVARIANT(ch)) {
3829 SvUTF8_on(sv);
3830 break;
3831 }
3832 }
3833 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3834 /* XXX Is this dead code? XS_utf8_decode calls SvSETMAGIC
3835 after this, clearing pos. Does anything on CPAN
3836 need this? */
3837 /* adjust pos to the start of a UTF8 char sequence */
3838 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3839 if (mg) {
3840 I32 pos = mg->mg_len;
3841 if (pos > 0) {
3842 for (c = start + pos; c > start; c--) {
3843 if (UTF8_IS_START(*c))
3844 break;
3845 }
3846 mg->mg_len = c - start;
3847 }
3848 }
3849 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3850 magic_setutf8(sv,mg); /* clear UTF8 cache */
3851 }
3852 }
3853 return TRUE;
3854 }
3855
3856 /*
3857 =for apidoc sv_setsv
3858
3859 Copies the contents of the source SV C<ssv> into the destination SV
3860 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3861 function if the source SV needs to be reused. Does not handle 'set' magic on
3862 destination SV. Calls 'get' magic on source SV. Loosely speaking, it
3863 performs a copy-by-value, obliterating any previous content of the
3864 destination.
3865
3866 You probably want to use one of the assortment of wrappers, such as
3867 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3868 C<SvSetMagicSV_nosteal>.
3869
3870 =for apidoc sv_setsv_flags
3871
3872 Copies the contents of the source SV C<ssv> into the destination SV
3873 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3874 function if the source SV needs to be reused. Does not handle 'set' magic.
3875 Loosely speaking, it performs a copy-by-value, obliterating any previous
3876 content of the destination.
3877 If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
3878 C<ssv> if appropriate, else not. If the C<flags>
3879 parameter has the C<SV_NOSTEAL> bit set then the
3880 buffers of temps will not be stolen. <sv_setsv>
3881 and C<sv_setsv_nomg> are implemented in terms of this function.
3882
3883 You probably want to use one of the assortment of wrappers, such as
3884 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3885 C<SvSetMagicSV_nosteal>.
3886
3887 This is the primary function for copying scalars, and most other
3888 copy-ish functions and macros use this underneath.
3889
3890 =cut
3891 */
3892
3893 static void
3894 S_glob_assign_glob(pTHX_ SV *const dstr, SV *const sstr, const int dtype)
3895 {
3896 I32 mro_changes = 0; /* 1 = method, 2 = isa, 3 = recursive isa */
3897 HV *old_stash = NULL;
3898
3899 PERL_ARGS_ASSERT_GLOB_ASSIGN_GLOB;
3900
3901 if (dtype != SVt_PVGV && !isGV_with_GP(dstr)) {
3902 const char * const name = GvNAME(sstr);
3903 const STRLEN len = GvNAMELEN(sstr);
3904 {
3905 if (dtype >= SVt_PV) {
3906 SvPV_free(dstr);
3907 SvPV_set(dstr, 0);
3908 SvLEN_set(dstr, 0);
3909 SvCUR_set(dstr, 0);
3910 }
3911 SvUPGRADE(dstr, SVt_PVGV);
3912 (void)SvOK_off(dstr);
3913 isGV_with_GP_on(dstr);
3914 }
3915 GvSTASH(dstr) = GvSTASH(sstr);
3916 if (GvSTASH(dstr))
3917 Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dstr)), dstr);
3918 gv_name_set(MUTABLE_GV(dstr), name, len,
3919 GV_ADD | (GvNAMEUTF8(sstr) ? SVf_UTF8 : 0 ));
3920 SvFAKE_on(dstr); /* can coerce to non-glob */
3921 }
3922
3923 if(GvGP(MUTABLE_GV(sstr))) {
3924 /* If source has method cache entry, clear it */
3925 if(GvCVGEN(sstr)) {
3926 SvREFCNT_dec(GvCV(sstr));
3927 GvCV_set(sstr, NULL);
3928 GvCVGEN(sstr) = 0;
3929 }
3930 /* If source has a real method, then a method is
3931 going to change */
3932 else if(
3933 GvCV((const GV *)sstr) && GvSTASH(dstr) && HvENAME(GvSTASH(dstr))
3934 ) {
3935 mro_changes = 1;
3936 }
3937 }
3938
3939 /* If dest already had a real method, that's a change as well */
3940 if(
3941 !mro_changes && GvGP(MUTABLE_GV(dstr)) && GvCVu((const GV *)dstr)
3942 && GvSTASH(dstr) && HvENAME(GvSTASH(dstr))
3943 ) {
3944 mro_changes = 1;
3945 }
3946
3947 /* We don't need to check the name of the destination if it was not a
3948 glob to begin with. */
3949 if(dtype == SVt_PVGV) {
3950 const char * const name = GvNAME((const GV *)dstr);
3951 if(
3952 strEQ(name,"ISA")
3953 /* The stash may have been detached from the symbol table, so
3954 check its name. */
3955 && GvSTASH(dstr) && HvENAME(GvSTASH(dstr))
3956 )
3957 mro_changes = 2;
3958 else {
3959 const STRLEN len = GvNAMELEN(dstr);
3960 if ((len > 1 && name[len-2] == ':' && name[len-1] == ':')
3961 || (len == 1 && name[0] == ':')) {
3962 mro_changes = 3;
3963
3964 /* Set aside the old stash, so we can reset isa caches on
3965 its subclasses. */
3966 if((old_stash = GvHV(dstr)))
3967 /* Make sure we do not lose it early. */
3968 SvREFCNT_inc_simple_void_NN(
3969 sv_2mortal((SV *)old_stash)
3970 );
3971 }
3972 }
3973
3974 SvREFCNT_inc_simple_void_NN(sv_2mortal(dstr));
3975 }
3976
3977 /* freeing dstr's GP might free sstr (e.g. *x = $x),
3978 * so temporarily protect it */
3979 ENTER;
3980 SAVEFREESV(SvREFCNT_inc_simple_NN(sstr));
3981 gp_free(MUTABLE_GV(dstr));
3982 GvINTRO_off(dstr); /* one-shot flag */
3983 GvGP_set(dstr, gp_ref(GvGP(sstr)));
3984 LEAVE;
3985
3986 if (SvTAINTED(sstr))
3987 SvTAINT(dstr);
3988 if (GvIMPORTED(dstr) != GVf_IMPORTED
3989 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3990 {
3991 GvIMPORTED_on(dstr);
3992 }
3993 GvMULTI_on(dstr);
3994 if(mro_changes == 2) {
3995 if (GvAV((const GV *)sstr)) {
3996 MAGIC *mg;
3997 SV * const sref = (SV *)GvAV((const GV *)dstr);
3998 if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) {
3999 if (SvTYPE(mg->mg_obj) != SVt_PVAV) {
4000 AV * const ary = newAV();
4001 av_push(ary, mg->mg_obj); /* takes the refcount */
4002 mg->mg_obj = (SV *)ary;
4003 }
4004 av_push((AV *)mg->mg_obj, SvREFCNT_inc_simple_NN(dstr));
4005 }
4006 else sv_magic(sref, dstr, PERL_MAGIC_isa, NULL, 0);
4007 }
4008 mro_isa_changed_in(GvSTASH(dstr));
4009 }
4010 else if(mro_changes == 3) {
4011 HV * const stash = GvHV(dstr);
4012 if(old_stash ? (HV *)HvENAME_get(old_stash) : stash)
4013 mro_package_moved(
4014 stash, old_stash,
4015 (GV *)dstr, 0
4016 );
4017 }
4018 else if(mro_changes) mro_method_changed_in(GvSTASH(dstr));
4019 if (GvIO(dstr) && dtype == SVt_PVGV) {
4020 DEBUG_o(Perl_deb(aTHX_
4021 "glob_assign_glob clearing PL_stashcache\n"));
4022 /* It's a cache. It will rebuild itself quite happily.
4023 It's a lot of effort to work out exactly which key (or keys)
4024 might be invalidated by the creation of the this file handle.
4025 */
4026 hv_clear(PL_stashcache);
4027 }
4028 return;
4029 }
4030
4031 void
4032 Perl_gv_setref(pTHX_ SV *const dstr, SV *const sstr)
4033 {
4034 SV * const sref = SvRV(sstr);
4035 SV *dref;
4036 const int intro = GvINTRO(dstr);
4037 SV **location;
4038 U8 import_flag = 0;
4039 const U32 stype = SvTYPE(sref);
4040
4041 PERL_ARGS_ASSERT_GV_SETREF;
4042
4043 if (intro) {
4044 GvINTRO_off(dstr); /* one-shot flag */
4045 GvLINE(dstr) = CopLINE(PL_curcop);
4046 GvEGV(dstr) = MUTABLE_GV(dstr);
4047 }
4048 GvMULTI_on(dstr);
4049 switch (stype) {
4050 case SVt_PVCV:
4051 location = (SV **) &(GvGP(dstr)->gp_cv); /* XXX bypassing GvCV_set */
4052 import_flag = GVf_IMPORTED_CV;
4053 goto common;
4054 case SVt_PVHV:
4055 location = (SV **) &GvHV(dstr);
4056 import_flag = GVf_IMPORTED_HV;
4057 goto common;
4058 case SVt_PVAV:
4059 location = (SV **) &GvAV(dstr);
4060 import_flag = GVf_IMPORTED_AV;
4061 goto common;
4062 case SVt_PVIO:
4063 location = (SV **) &GvIOp(dstr);
4064 goto common;
4065 case SVt_PVFM:
4066 location = (SV **) &GvFORM(dstr);
4067 goto common;
4068 default:
4069 location = &GvSV(dstr);
4070 import_flag = GVf_IMPORTED_SV;
4071 common:
4072 if (intro) {
4073 if (stype == SVt_PVCV) {
4074 /*if (GvCVGEN(dstr) && (GvCV(dstr) != (const CV *)sref || GvCVGEN(dstr))) {*/
4075 if (GvCVGEN(dstr)) {
4076 SvREFCNT_dec(GvCV(dstr));
4077 GvCV_set(dstr, NULL);
4078 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
4079 }
4080 }
4081 /* SAVEt_GVSLOT takes more room on the savestack and has more
4082 overhead in leave_scope than SAVEt_GENERIC_SV. But for CVs
4083 leave_scope needs access to the GV so it can reset method
4084 caches. We must use SAVEt_GVSLOT whenever the type is
4085 SVt_PVCV, even if the stash is anonymous, as the stash may
4086 gain a name somehow before leave_scope. */
4087 if (stype == SVt_PVCV) {
4088 /* There is no save_pushptrptrptr. Creating it for this
4089 one call site would be overkill. So inline the ss add
4090 routines here. */
4091 dSS_ADD;
4092 SS_ADD_PTR(dstr);
4093 SS_ADD_PTR(location);
4094 SS_ADD_PTR(SvREFCNT_inc(*location));
4095 SS_ADD_UV(SAVEt_GVSLOT);
4096 SS_ADD_END(4);
4097 }
4098 else SAVEGENERICSV(*location);
4099 }
4100 dref = *location;
4101 if (stype == SVt_PVCV && (*location != sref || GvCVGEN(dstr))) {
4102 CV* const cv = MUTABLE_CV(*location);
4103 if (cv) {
4104 if (!GvCVGEN((const GV *)dstr) &&
4105 (CvROOT(cv) || CvXSUB(cv)) &&
4106 /* redundant check that avoids creating the extra SV
4107 most of the time: */
4108 (CvCONST(cv) || ckWARN(WARN_REDEFINE)))
4109 {
4110 SV * const new_const_sv =
4111 CvCONST((const CV *)sref)
4112 ? cv_const_sv((const CV *)sref)
4113 : NULL;
4114 report_redefined_cv(
4115 sv_2mortal(Perl_newSVpvf(aTHX_
4116 "%"HEKf"::%"HEKf,
4117 HEKfARG(
4118 HvNAME_HEK(GvSTASH((const GV *)dstr))
4119 ),
4120 HEKfARG(GvENAME_HEK(MUTABLE_GV(dstr)))
4121 )),
4122 cv,
4123 CvCONST((const CV *)sref) ? &new_const_sv : NULL
4124 );
4125 }
4126 if (!intro)
4127 cv_ckproto_len_flags(cv, (const GV *)dstr,
4128 SvPOK(sref) ? CvPROTO(sref) : NULL,
4129 SvPOK(sref) ? CvPROTOLEN(sref) : 0,
4130 SvPOK(sref) ? SvUTF8(sref) : 0);
4131 }
4132 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
4133 GvASSUMECV_on(dstr);
4134 if(GvSTASH(dstr)) { /* sub foo { 1 } sub bar { 2 } *bar = \&foo */
4135 if (intro && GvREFCNT(dstr) > 1) {
4136 /* temporary remove extra savestack's ref */
4137 --GvREFCNT(dstr);
4138 gv_method_changed(dstr);
4139 ++GvREFCNT(dstr);
4140 }
4141 else gv_method_changed(dstr);
4142 }
4143 }
4144 *location = SvREFCNT_inc_simple_NN(sref);
4145 if (import_flag && !(GvFLAGS(dstr) & import_flag)
4146 && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) {
4147 GvFLAGS(dstr) |= import_flag;
4148 }
4149 if (import_flag == GVf_IMPORTED_SV) {
4150 if (intro) {
4151 save_aliased_sv((GV *)dstr);
4152 }
4153 /* Turn off the flag if sref is not referenced elsewhere,
4154 even by weak refs. (SvRMAGICAL is a pessimistic check for
4155 back refs.) */
4156 if (SvREFCNT(sref) <= 2 && !SvRMAGICAL(sref))
4157 GvALIASED_SV_off(dstr);
4158 else
4159 GvALIASED_SV_on(dstr);
4160 }
4161 if (stype == SVt_PVHV) {
4162 const char * const name = GvNAME((GV*)dstr);
4163 const STRLEN len = GvNAMELEN(dstr);
4164 if (
4165 (
4166 (len > 1 && name[len-2] == ':' && name[len-1] == ':')
4167 || (len == 1 && name[0] == ':')
4168 )
4169 && (!dref || HvENAME_get(dref))
4170 ) {
4171 mro_package_moved(
4172 (HV *)sref, (HV *)dref,
4173 (GV *)dstr, 0
4174 );
4175 }
4176 }
4177 else if (
4178 stype == SVt_PVAV && sref != dref
4179 && strEQ(GvNAME((GV*)dstr), "ISA")
4180 /* The stash may have been detached from the symbol table, so
4181 check its name before doing anything. */
4182 && GvSTASH(dstr) && HvENAME(GvSTASH(dstr))
4183 ) {
4184 MAGIC *mg;
4185 MAGIC * const omg = dref && SvSMAGICAL(dref)
4186 ? mg_find(dref, PERL_MAGIC_isa)
4187 : NULL;
4188 if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) {
4189 if (SvTYPE(mg->mg_obj) != SVt_PVAV) {
4190 AV * const ary = newAV();
4191 av_push(ary, mg->mg_obj); /* takes the refcount */
4192 mg->mg_obj = (SV *)ary;
4193 }
4194 if (omg) {
4195 if (SvTYPE(omg->mg_obj) == SVt_PVAV) {
4196 SV **svp = AvARRAY((AV *)omg->mg_obj);
4197 I32 items = AvFILLp((AV *)omg->mg_obj) + 1;
4198 while (items--)
4199 av_push(
4200 (AV *)mg->mg_obj,
4201 SvREFCNT_inc_simple_NN(*svp++)
4202 );
4203 }
4204 else
4205 av_push(
4206 (AV *)mg->mg_obj,
4207 SvREFCNT_inc_simple_NN(omg->mg_obj)
4208 );
4209 }
4210 else
4211 av_push((AV *)mg->mg_obj,SvREFCNT_inc_simple_NN(dstr));
4212 }
4213 else
4214 {
4215 sv_magic(
4216 sref, omg ? omg->mg_obj : dstr, PERL_MAGIC_isa, NULL, 0
4217 );
4218 mg = mg_find(sref, PERL_MAGIC_isa);
4219 }
4220 /* Since the *ISA assignment could have affected more than
4221 one stash, don't call mro_isa_changed_in directly, but let
4222 magic_clearisa do it for us, as it already has the logic for
4223 dealing with globs vs arrays of globs. */
4224 assert(mg);
4225 Perl_magic_clearisa(aTHX_ NULL, mg);
4226 }
4227 else if (stype == SVt_PVIO) {
4228 DEBUG_o(Perl_deb(aTHX_ "gv_setref clearing PL_stashcache\n"));
4229 /* It's a cache. It will rebuild itself quite happily.
4230 It's a lot of effort to work out exactly which key (or keys)
4231 might be invalidated by the creation of the this file handle.
4232 */
4233 hv_clear(PL_stashcache);
4234 }
4235 break;
4236 }
4237 if (!intro) SvREFCNT_dec(dref);
4238 if (SvTAINTED(sstr))
4239 SvTAINT(dstr);
4240 return;
4241 }
4242
4243
4244
4245
4246 #ifdef PERL_DEBUG_READONLY_COW
4247 # include <sys/mman.h>
4248
4249 # ifndef PERL_MEMORY_DEBUG_HEADER_SIZE
4250 # define PERL_MEMORY_DEBUG_HEADER_SIZE 0
4251 # endif
4252
4253 void
4254 Perl_sv_buf_to_ro(pTHX_ SV *sv)
4255 {
4256 struct perl_memory_debug_header * const header =
4257 (struct perl_memory_debug_header *)(SvPVX(sv)-PERL_MEMORY_DEBUG_HEADER_SIZE);
4258 const MEM_SIZE len = header->size;
4259 PERL_ARGS_ASSERT_SV_BUF_TO_RO;
4260 # ifdef PERL_TRACK_MEMPOOL
4261 if (!header->readonly) header->readonly = 1;
4262 # endif
4263 if (mprotect(header, len, PROT_READ))
4264 Perl_warn(aTHX_ "mprotect RW for COW string %p %lu failed with %d",
4265 header, len, errno);
4266 }
4267
4268 static void
4269 S_sv_buf_to_rw(pTHX_ SV *sv)
4270 {
4271 struct perl_memory_debug_header * const header =
4272 (struct perl_memory_debug_header *)(SvPVX(sv)-PERL_MEMORY_DEBUG_HEADER_SIZE);
4273 const MEM_SIZE len = header->size;
4274 PERL_ARGS_ASSERT_SV_BUF_TO_RW;
4275 if (mprotect(header, len, PROT_READ|PROT_WRITE))
4276 Perl_warn(aTHX_ "mprotect for COW string %p %lu failed with %d",
4277 header, len, errno);
4278 # ifdef PERL_TRACK_MEMPOOL
4279 header->readonly = 0;
4280 # endif
4281 }
4282
4283 #else
4284 # define sv_buf_to_ro(sv) NOOP
4285 # define sv_buf_to_rw(sv) NOOP
4286 #endif
4287
4288 void
4289 Perl_sv_setsv_flags(pTHX_ SV *dstr, SV* sstr, const I32 flags)
4290 {
4291 U32 sflags;
4292 int dtype;
4293 svtype stype;
4294
4295 PERL_ARGS_ASSERT_SV_SETSV_FLAGS;
4296
4297 if (UNLIKELY( sstr == dstr ))
4298 return;
4299
4300 if (SvIS_FREED(dstr)) {
4301 Perl_croak(aTHX_ "panic: attempt to copy value %" SVf
4302 " to a freed scalar %p", SVfARG(sstr), (void *)dstr);
4303 }
4304 SV_CHECK_THINKFIRST_COW_DROP(dstr);
4305 if (UNLIKELY( !sstr ))
4306 sstr = &PL_sv_undef;
4307 if (SvIS_FREED(sstr)) {
4308 Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p to %p",
4309 (void*)sstr, (void*)dstr);
4310 }
4311 stype = SvTYPE(sstr);
4312 dtype = SvTYPE(dstr);
4313
4314 /* There's a lot of redundancy below but we're going for speed here */
4315
4316 switch (stype) {
4317 case SVt_NULL:
4318 undef_sstr:
4319 if (LIKELY( dtype != SVt_PVGV && dtype != SVt_PVLV )) {
4320 (void)SvOK_off(dstr);
4321 return;
4322 }
4323 break;
4324 case SVt_IV:
4325 if (SvIOK(sstr)) {
4326 switch (dtype) {
4327 case SVt_NULL:
4328 /* For performance, we inline promoting to type SVt_IV. */
4329 /* We're starting from SVt_NULL, so provided that define is
4330 * actual 0, we don't have to unset any SV type flags
4331 * to promote to SVt_IV. */
4332 STATIC_ASSERT_STMT(SVt_NULL == 0);
4333 SET_SVANY_FOR_BODYLESS_IV(dstr);
4334 SvFLAGS(dstr) |= SVt_IV;
4335 break;
4336 case SVt_NV:
4337 case SVt_PV:
4338 sv_upgrade(dstr, SVt_PVIV);
4339 break;
4340 case SVt_PVGV:
4341 case SVt_PVLV:
4342 goto end_of_first_switch;
4343 }
4344 (void)SvIOK_only(dstr);
4345 SvIV_set(dstr, SvIVX(sstr));
4346 if (SvIsUV(sstr))
4347 SvIsUV_on(dstr);
4348 /* SvTAINTED can only be true if the SV has taint magic, which in
4349 turn means that the SV type is PVMG (or greater). This is the
4350 case statement for SVt_IV, so this cannot be true (whatever gcov
4351 may say). */
4352 assert(!SvTAINTED(sstr));
4353 return;
4354 }
4355 if (!SvROK(sstr))
4356 goto undef_sstr;
4357 if (dtype < SVt_PV && dtype != SVt_IV)
4358 sv_upgrade(dstr, SVt_IV);
4359 break;
4360
4361 case SVt_NV:
4362 if (LIKELY( SvNOK(sstr) )) {
4363 switch (dtype) {
4364 case SVt_NULL:
4365 case SVt_IV:
4366 sv_upgrade(dstr, SVt_NV);
4367 break;
4368 case SVt_PV:
4369 case SVt_PVIV:
4370 sv_upgrade(dstr, SVt_PVNV);
4371 break;
4372 case SVt_PVGV:
4373 case SVt_PVLV:
4374 goto end_of_first_switch;
4375 }
4376 SvNV_set(dstr, SvNVX(sstr));
4377 (void)SvNOK_only(dstr);
4378 /* SvTAINTED can only be true if the SV has taint magic, which in
4379 turn means that the SV type is PVMG (or greater). This is the
4380 case statement for SVt_NV, so this cannot be true (whatever gcov
4381 may say). */
4382 assert(!SvTAINTED(sstr));
4383 return;
4384 }
4385 goto undef_sstr;
4386
4387 case SVt_PV:
4388 if (dtype < SVt_PV)
4389 sv_upgrade(dstr, SVt_PV);
4390 break;
4391 case SVt_PVIV:
4392 if (dtype < SVt_PVIV)
4393 sv_upgrade(dstr, SVt_PVIV);
4394 break;
4395 case SVt_PVNV:
4396 if (dtype < SVt_PVNV)
4397 sv_upgrade(dstr, SVt_PVNV);
4398 break;
4399 default:
4400 {
4401 const char * const type = sv_reftype(sstr,0);
4402 if (PL_op)
4403 /* diag_listed_as: Bizarre copy of %s */
4404 Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_DESC(PL_op));
4405 else
4406 Perl_croak(aTHX_ "Bizarre copy of %s", type);
4407 }
4408 NOT_REACHED; /* NOTREACHED */
4409
4410 case SVt_REGEXP:
4411 upgregexp:
4412 if (dtype < SVt_REGEXP)
4413 {
4414 if (dtype >= SVt_PV) {
4415 SvPV_free(dstr);
4416 SvPV_set(dstr, 0);
4417 SvLEN_set(dstr, 0);
4418 SvCUR_set(dstr, 0);
4419 }
4420 sv_upgrade(dstr, SVt_REGEXP);
4421 }
4422 break;
4423
4424 case SVt_INVLIST:
4425 case SVt_PVLV:
4426 case SVt_PVGV:
4427 case SVt_PVMG:
4428 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
4429 mg_get(sstr);
4430 if (SvTYPE(sstr) != stype)
4431 stype = SvTYPE(sstr);
4432 }
4433 if (isGV_with_GP(sstr) && dtype <= SVt_PVLV) {
4434 glob_assign_glob(dstr, sstr, dtype);
4435 return;
4436 }
4437 if (stype == SVt_PVLV)
4438 {
4439 if (isREGEXP(sstr)) goto upgregexp;
4440 SvUPGRADE(dstr, SVt_PVNV);
4441 }
4442 else
4443 SvUPGRADE(dstr, (svtype)stype);
4444 }
4445 end_of_first_switch:
4446
4447 /* dstr may have been upgraded. */
4448 dtype = SvTYPE(dstr);
4449 sflags = SvFLAGS(sstr);
4450
4451 if (UNLIKELY( dtype == SVt_PVCV )) {
4452 /* Assigning to a subroutine sets the prototype. */
4453 if (SvOK(sstr)) {
4454 STRLEN len;
4455 const char *const ptr = SvPV_const(sstr, len);
4456
4457 SvGROW(dstr, len + 1);
4458 Copy(ptr, SvPVX(dstr), len + 1, char);
4459 SvCUR_set(dstr, len);
4460 SvPOK_only(dstr);
4461 SvFLAGS(dstr) |= sflags & SVf_UTF8;
4462 CvAUTOLOAD_off(dstr);
4463 } else {
4464 SvOK_off(dstr);
4465 }
4466 }
4467 else if (UNLIKELY(dtype == SVt_PVAV || dtype == SVt_PVHV
4468 || dtype == SVt_PVFM))
4469 {
4470 const char * const type = sv_reftype(dstr,0);
4471 if (PL_op)
4472 /* diag_listed_as: Cannot copy to %s */
4473 Perl_croak(aTHX_ "Cannot copy to %s in %s", type, OP_DESC(PL_op));
4474 else
4475 Perl_croak(aTHX_ "Cannot copy to %s", type);
4476 } else if (sflags & SVf_ROK) {
4477 if (isGV_with_GP(dstr)
4478 && SvTYPE(SvRV(sstr)) == SVt_PVGV && isGV_with_GP(SvRV(sstr))) {
4479 sstr = SvRV(sstr);
4480 if (sstr == dstr) {
4481 if (GvIMPORTED(dstr) != GVf_IMPORTED
4482 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4483 {
4484 GvIMPORTED_on(dstr);
4485 }
4486 GvMULTI_on(dstr);
4487 return;
4488 }
4489 glob_assign_glob(dstr, sstr, dtype);
4490 return;
4491 }
4492
4493 if (dtype >= SVt_PV) {
4494 if (isGV_with_GP(dstr)) {
4495 gv_setref(dstr, sstr);
4496 return;
4497 }
4498 if (SvPVX_const(dstr)) {
4499 SvPV_free(dstr);
4500 SvLEN_set(dstr, 0);
4501 SvCUR_set(dstr, 0);
4502 }
4503 }
4504 (void)SvOK_off(dstr);
4505 SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
4506 SvFLAGS(dstr) |= sflags & SVf_ROK;
4507 assert(!(sflags & SVp_NOK));
4508 assert(!(sflags & SVp_IOK));
4509 assert(!(sflags & SVf_NOK));
4510 assert(!(sflags & SVf_IOK));
4511 }
4512 else if (isGV_with_GP(dstr)) {
4513 if (!(sflags & SVf_OK)) {
4514 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
4515 "Undefined value assigned to typeglob");
4516 }
4517 else {
4518 GV *gv = gv_fetchsv_nomg(sstr, GV_ADD, SVt_PVGV);
4519 if (dstr != (const SV *)gv) {
4520 const char * const name = GvNAME((const GV *)dstr);
4521 const STRLEN len = GvNAMELEN(dstr);
4522 HV *old_stash = NULL;
4523 bool reset_isa = FALSE;
4524 if ((len > 1 && name[len-2] == ':' && name[len-1] == ':')
4525 || (len == 1 && name[0] == ':')) {
4526 /* Set aside the old stash, so we can reset isa caches
4527 on its subclasses. */
4528 if((old_stash = GvHV(dstr))) {
4529 /* Make sure we do not lose it early. */
4530 SvREFCNT_inc_simple_void_NN(
4531 sv_2mortal((SV *)old_stash)
4532 );
4533 }
4534 reset_isa = TRUE;
4535 }
4536
4537 if (GvGP(dstr)) {
4538 SvREFCNT_inc_simple_void_NN(sv_2mortal(dstr));
4539 gp_free(MUTABLE_GV(dstr));
4540 }
4541 GvGP_set(dstr, gp_ref(GvGP(gv)));
4542
4543 if (reset_isa) {
4544 HV * const stash = GvHV(dstr);
4545 if(
4546 old_stash ? (HV *)HvENAME_get(old_stash) : stash
4547 )
4548 mro_package_moved(
4549 stash, old_stash,
4550 (GV *)dstr, 0
4551 );
4552 }
4553 }
4554 }
4555 }
4556 else if ((dtype == SVt_REGEXP || dtype == SVt_PVLV)
4557 && (stype == SVt_REGEXP || isREGEXP(sstr))) {
4558 reg_temp_copy((REGEXP*)dstr, (REGEXP*)sstr);
4559 }
4560 else if (sflags & SVp_POK) {
4561 const STRLEN cur = SvCUR(sstr);
4562 const STRLEN len = SvLEN(sstr);
4563
4564 /*
4565 * We have three basic ways to copy the string:
4566 *
4567 * 1. Swipe
4568 * 2. Copy-on-write
4569 * 3. Actual copy
4570 *
4571 * Which we choose is based on various factors. The following
4572 * things are listed in order of speed, fastest to slowest:
4573 * - Swipe
4574 * - Copying a short string
4575 * - Copy-on-write bookkeeping
4576 * - malloc
4577 * - Copying a long string
4578 *
4579 * We swipe the string (steal the string buffer) if the SV on the
4580 * rhs is about to be freed anyway (TEMP and refcnt==1). This is a
4581 * big win on long strings. It should be a win on short strings if
4582 * SvPVX_const(dstr) has to be allocated. If not, it should not
4583 * slow things down, as SvPVX_const(sstr) would have been freed
4584 * soon anyway.
4585 *
4586 * We also steal the buffer from a PADTMP (operator target) if it
4587 * is ‘long enough’. For short strings, a swipe does not help
4588 * here, as it causes more malloc calls the next time the target
4589 * is used. Benchmarks show that even if SvPVX_const(dstr) has to
4590 * be allocated it is still not worth swiping PADTMPs for short
4591 * strings, as the savings here are small.
4592 *
4593 * If swiping is not an option, then we see whether it is
4594 * worth using copy-on-write. If the lhs already has a buf-
4595 * fer big enough and the string is short, we skip it and fall back
4596 * to method 3, since memcpy is faster for short strings than the
4597 * later bookkeeping overhead that copy-on-write entails.
4598
4599 * If the rhs is not a copy-on-write string yet, then we also
4600 * consider whether the buffer is too large relative to the string
4601 * it holds. Some operations such as readline allocate a large
4602 * buffer in the expectation of reusing it. But turning such into
4603 * a COW buffer is counter-productive because it increases memory
4604 * usage by making readline allocate a new large buffer the sec-
4605 * ond time round. So, if the buffer is too large, again, we use
4606 * method 3 (copy).
4607 *
4608 * Finally, if there is no buffer on the left, or the buffer is too
4609 * small, then we use copy-on-write and make both SVs share the
4610 * string buffer.
4611 *
4612 */
4613
4614 /* Whichever path we take through the next code, we want this true,
4615 and doing it now facilitates the COW check. */
4616 (void)SvPOK_only(dstr);
4617
4618 if (
4619 ( /* Either ... */
4620 /* slated for free anyway (and not COW)? */
4621 (sflags & (SVs_TEMP|SVf_IsCOW)) == SVs_TEMP
4622 /* or a swipable TARG */
4623 || ((sflags &
4624 (SVs_PADTMP|SVf_READONLY|SVf_PROTECT|SVf_IsCOW))
4625 == SVs_PADTMP
4626 /* whose buffer is worth stealing */
4627 && CHECK_COWBUF_THRESHOLD(cur,len)
4628 )
4629 ) &&
4630 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4631 (!(flags & SV_NOSTEAL)) &&
4632 /* and we're allowed to steal temps */
4633 SvREFCNT(sstr) == 1 && /* and no other references to it? */
4634 len) /* and really is a string */
4635 { /* Passes the swipe test. */
4636 if (SvPVX_const(dstr)) /* we know that dtype >= SVt_PV */
4637 SvPV_free(dstr);
4638 SvPV_set(dstr, SvPVX_mutable(sstr));
4639 SvLEN_set(dstr, SvLEN(sstr));
4640 SvCUR_set(dstr, SvCUR(sstr));
4641
4642 SvTEMP_off(dstr);
4643 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
4644 SvPV_set(sstr, NULL);
4645 SvLEN_set(sstr, 0);
4646 SvCUR_set(sstr, 0);
4647 SvTEMP_off(sstr);
4648 }
4649 else if (flags & SV_COW_SHARED_HASH_KEYS
4650 &&
4651 #ifdef PERL_OLD_COPY_ON_WRITE
4652 ( sflags & SVf_IsCOW
4653 || ( (sflags & CAN_COW_MASK) == CAN_COW_FLAGS
4654 && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS
4655 && SvTYPE(sstr) >= SVt_PVIV && len
4656 )
4657 )
4658 #elif defined(PERL_NEW_COPY_ON_WRITE)
4659 (sflags & SVf_IsCOW
4660 ? (!len ||
4661 ( (CHECK_COWBUF_THRESHOLD(cur,len) || SvLEN(dstr) < cur+1)
4662 /* If this is a regular (non-hek) COW, only so
4663 many COW "copies" are possible. */
4664 && CowREFCNT(sstr) != SV_COW_REFCNT_MAX ))
4665 : ( (sflags & CAN_COW_MASK) == CAN_COW_FLAGS
4666 && !(SvFLAGS(dstr) & SVf_BREAK)
4667 && CHECK_COW_THRESHOLD(cur,len) && cur+1 < len
4668 && (CHECK_COWBUF_THRESHOLD(cur,len) || SvLEN(dstr) < cur+1)
4669 ))
4670 #else
4671 sflags & SVf_IsCOW
4672 && !(SvFLAGS(dstr) & SVf_BREAK)
4673 #endif
4674 ) {
4675 /* Either it's a shared hash key, or it's suitable for
4676 copy-on-write. */
4677 if (DEBUG_C_TEST) {
4678 PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
4679 sv_dump(sstr);
4680 sv_dump(dstr);
4681 }
4682 #ifdef PERL_ANY_COW
4683 if (!(sflags & SVf_IsCOW)) {
4684 SvIsCOW_on(sstr);
4685 # ifdef PERL_OLD_COPY_ON_WRITE
4686 /* Make the source SV into a loop of 1.
4687 (about to become 2) */
4688 SV_COW_NEXT_SV_SET(sstr, sstr);
4689 # else
4690 CowREFCNT(sstr) = 0;
4691 # endif
4692 }
4693 #endif
4694 if (SvPVX_const(dstr)) { /* we know that dtype >= SVt_PV */
4695 SvPV_free(dstr);
4696 }
4697
4698 #ifdef PERL_ANY_COW
4699 if (len) {
4700 # ifdef PERL_OLD_COPY_ON_WRITE
4701 assert (SvTYPE(dstr) >= SVt_PVIV);
4702 /* SvIsCOW_normal */
4703 /* splice us in between source and next-after-source. */
4704 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4705 SV_COW_NEXT_SV_SET(sstr, dstr);
4706 # else
4707 if (sflags & SVf_IsCOW) {
4708 sv_buf_to_rw(sstr);
4709 }
4710 CowREFCNT(sstr)++;
4711 # endif
4712 SvPV_set(dstr, SvPVX_mutable(sstr));
4713 sv_buf_to_ro(sstr);
4714 } else
4715 #endif
4716 {
4717 /* SvIsCOW_shared_hash */
4718 DEBUG_C(PerlIO_printf(Perl_debug_log,
4719 "Copy on write: Sharing hash\n"));
4720
4721 assert (SvTYPE(dstr) >= SVt_PV);
4722 SvPV_set(dstr,
4723 HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)))));
4724 }
4725 SvLEN_set(dstr, len);
4726 SvCUR_set(dstr, cur);
4727 SvIsCOW_on(dstr);
4728 } else {
4729 /* Failed the swipe test, and we cannot do copy-on-write either.
4730 Have to copy the string. */
4731 SvGROW(dstr, cur + 1); /* inlined from sv_setpvn */
4732 Move(SvPVX_const(sstr),SvPVX(dstr),cur,char);
4733 SvCUR_set(dstr, cur);
4734 *SvEND(dstr) = '\0';
4735 }
4736 if (sflags & SVp_NOK) {
4737 SvNV_set(dstr, SvNVX(sstr));
4738 }
4739 if (sflags & SVp_IOK) {
4740 SvIV_set(dstr, SvIVX(sstr));
4741 /* Must do this otherwise some other overloaded use of 0x80000000
4742 gets confused. I guess SVpbm_VALID */
4743 if (sflags & SVf_IVisUV)
4744 SvIsUV_on(dstr);
4745 }
4746 SvFLAGS(dstr) |= sflags & (SVf_IOK|SVp_IOK|SVf_NOK|SVp_NOK|SVf_UTF8);
4747 {
4748 const MAGIC * const smg = SvVSTRING_mg(sstr);
4749 if (smg) {
4750 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4751 smg->mg_ptr, smg->mg_len);
4752 SvRMAGICAL_on(dstr);
4753 }
4754 }
4755 }
4756 else if (sflags & (SVp_IOK|SVp_NOK)) {
4757 (void)SvOK_off(dstr);
4758 SvFLAGS(dstr) |= sflags & (SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_NOK|SVp_NOK);
4759 if (sflags & SVp_IOK) {
4760 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
4761 SvIV_set(dstr, SvIVX(sstr));
4762 }
4763 if (sflags & SVp_NOK) {
4764 SvNV_set(dstr, SvNVX(sstr));
4765 }
4766 }
4767 else {
4768 if (isGV_with_GP(sstr)) {
4769 gv_efullname3(dstr, MUTABLE_GV(sstr), "*");
4770 }
4771 else
4772 (void)SvOK_off(dstr);
4773 }
4774 if (SvTAINTED(sstr))
4775 SvTAINT(dstr);
4776 }
4777
4778 /*
4779 =for apidoc sv_setsv_mg
4780
4781 Like C<sv_setsv>, but also handles 'set' magic.
4782
4783 =cut
4784 */
4785
4786 void
4787 Perl_sv_setsv_mg(pTHX_ SV *const dstr, SV *const sstr)
4788 {
4789 PERL_ARGS_ASSERT_SV_SETSV_MG;
4790
4791 sv_setsv(dstr,sstr);
4792 SvSETMAGIC(dstr);
4793 }
4794
4795 #ifdef PERL_ANY_COW
4796 # ifdef PERL_OLD_COPY_ON_WRITE
4797 # define SVt_COW SVt_PVIV
4798 # else
4799 # define SVt_COW SVt_PV
4800 # endif
4801 SV *
4802 Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr)
4803 {
4804 STRLEN cur = SvCUR(sstr);
4805 STRLEN len = SvLEN(sstr);
4806 char *new_pv;
4807 #if defined(PERL_DEBUG_READONLY_COW) && defined(PERL_NEW_COPY_ON_WRITE)
4808 const bool already = cBOOL(SvIsCOW(sstr));
4809 #endif
4810
4811 PERL_ARGS_ASSERT_SV_SETSV_COW;
4812
4813 if (DEBUG_C_TEST) {
4814 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4815 (void*)sstr, (void*)dstr);
4816 sv_dump(sstr);
4817 if (dstr)
4818 sv_dump(dstr);
4819 }
4820
4821 if (dstr) {
4822 if (SvTHINKFIRST(dstr))
4823 sv_force_normal_flags(dstr, SV_COW_DROP_PV);
4824 else if (SvPVX_const(dstr))
4825 Safefree(SvPVX_mutable(dstr));
4826 }
4827 else
4828 new_SV(dstr);
4829 SvUPGRADE(dstr, SVt_COW);
4830
4831 assert (SvPOK(sstr));
4832 assert (SvPOKp(sstr));
4833 # ifdef PERL_OLD_COPY_ON_WRITE
4834 assert (!SvIOK(sstr));
4835 assert (!SvIOKp(sstr));
4836 assert (!SvNOK(sstr));
4837 assert (!SvNOKp(sstr));
4838 # endif
4839
4840 if (SvIsCOW(sstr)) {
4841
4842 if (SvLEN(sstr) == 0) {
4843 /* source is a COW shared hash key. */
4844 DEBUG_C(PerlIO_printf(Perl_debug_log,
4845 "Fast copy on write: Sharing hash\n"));
4846 new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr))));
4847 goto common_exit;
4848 }
4849 # ifdef PERL_OLD_COPY_ON_WRITE
4850 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4851 # else
4852 assert(SvCUR(sstr)+1 < SvLEN(sstr));
4853 assert(CowREFCNT(sstr) < SV_COW_REFCNT_MAX);
4854 # endif
4855 } else {
4856 assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS);
4857 SvUPGRADE(sstr, SVt_COW);
4858 SvIsCOW_on(sstr);
4859 DEBUG_C(PerlIO_printf(Perl_debug_log,
4860 "Fast copy on write: Converting sstr to COW\n"));
4861 # ifdef PERL_OLD_COPY_ON_WRITE
4862 SV_COW_NEXT_SV_SET(dstr, sstr);
4863 # else
4864 CowREFCNT(sstr) = 0;
4865 # endif
4866 }
4867 # ifdef PERL_OLD_COPY_ON_WRITE
4868 SV_COW_NEXT_SV_SET(sstr, dstr);
4869 # else
4870 # ifdef PERL_DEBUG_READONLY_COW
4871 if (already) sv_buf_to_rw(sstr);
4872 # endif
4873 CowREFCNT(sstr)++;
4874 # endif
4875 new_pv = SvPVX_mutable(sstr);
4876 sv_buf_to_ro(sstr);
4877
4878 common_exit:
4879 SvPV_set(dstr, new_pv);
4880 SvFLAGS(dstr) = (SVt_COW|SVf_POK|SVp_POK|SVf_IsCOW);
4881 if (SvUTF8(sstr))
4882 SvUTF8_on(dstr);
4883 SvLEN_set(dstr, len);
4884 SvCUR_set(dstr, cur);
4885 if (DEBUG_C_TEST) {
4886 sv_dump(dstr);
4887 }
4888 return dstr;
4889 }
4890 #endif
4891
4892 /*
4893 =for apidoc sv_setpvn
4894
4895 Copies a string (possibly containing embedded C<NUL> characters) into an SV.
4896 The C<len> parameter indicates the number of
4897 bytes to be copied. If the C<ptr> argument is NULL the SV will become
4898 undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>.
4899
4900 =cut
4901 */
4902
4903 void
4904 Perl_sv_setpvn(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
4905 {
4906 char *dptr;
4907
4908 PERL_ARGS_ASSERT_SV_SETPVN;
4909
4910 SV_CHECK_THINKFIRST_COW_DROP(sv);
4911 if (!ptr) {
4912 (void)SvOK_off(sv);
4913 return;
4914 }
4915 else {
4916 /* len is STRLEN which is unsigned, need to copy to signed */
4917 const IV iv = len;
4918 if (iv < 0)
4919 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen %"
4920 IVdf, iv);
4921 }
4922 SvUPGRADE(sv, SVt_PV);
4923
4924 dptr = SvGROW(sv, len + 1);
4925 Move(ptr,dptr,len,char);
4926 dptr[len] = '\0';
4927 SvCUR_set(sv, len);
4928 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4929 SvTAINT(sv);
4930 if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
4931 }
4932
4933 /*
4934 =for apidoc sv_setpvn_mg
4935
4936 Like C<sv_setpvn>, but also handles 'set' magic.
4937
4938 =cut
4939 */
4940
4941 void
4942 Perl_sv_setpvn_mg(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
4943 {
4944 PERL_ARGS_ASSERT_SV_SETPVN_MG;
4945
4946 sv_setpvn(sv,ptr,len);
4947 SvSETMAGIC(sv);
4948 }
4949
4950 /*
4951 =for apidoc sv_setpv
4952
4953 Copies a string into an SV. The string must be terminated with a C<NUL>
4954 character.
4955 Does not handle 'set' magic. See C<sv_setpv_mg>.
4956
4957 =cut
4958 */
4959
4960 void
4961 Perl_sv_setpv(pTHX_ SV *const sv, const char *const ptr)
4962 {
4963 STRLEN len;
4964
4965 PERL_ARGS_ASSERT_SV_SETPV;
4966
4967 SV_CHECK_THINKFIRST_COW_DROP(sv);
4968 if (!ptr) {
4969 (void)SvOK_off(sv);
4970 return;
4971 }
4972 len = strlen(ptr);
4973 SvUPGRADE(sv, SVt_PV);
4974
4975 SvGROW(sv, len + 1);
4976 Move(ptr,SvPVX(sv),len+1,char);
4977 SvCUR_set(sv, len);
4978 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4979 SvTAINT(sv);
4980 if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
4981 }
4982
4983 /*
4984 =for apidoc sv_setpv_mg
4985
4986 Like C<sv_setpv>, but also handles 'set' magic.
4987
4988 =cut
4989 */
4990
4991 void
4992 Perl_sv_setpv_mg(pTHX_ SV *const sv, const char *const ptr)
4993 {
4994 PERL_ARGS_ASSERT_SV_SETPV_MG;
4995
4996 sv_setpv(sv,ptr);
4997 SvSETMAGIC(sv);
4998 }
4999
5000 void
5001 Perl_sv_sethek(pTHX_ SV *const sv, const HEK *const hek)
5002 {
5003 PERL_ARGS_ASSERT_SV_SETHEK;
5004
5005 if (!hek) {
5006 return;
5007 }
5008
5009 if (HEK_LEN(hek) == HEf_SVKEY) {
5010 sv_setsv(sv, *(SV**)HEK_KEY(hek));
5011 return;
5012 } else {
5013 const int flags = HEK_FLAGS(hek);
5014 if (flags & HVhek_WASUTF8) {
5015 STRLEN utf8_len = HEK_LEN(hek);
5016 char *as_utf8 = (char *)bytes_to_utf8((U8*)HEK_KEY(hek), &utf8_len);
5017 sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL);
5018 SvUTF8_on(sv);
5019 return;
5020 } else if (flags & HVhek_UNSHARED) {
5021 sv_setpvn(sv, HEK_KEY(hek), HEK_LEN(hek));
5022 if (HEK_UTF8(hek))
5023 SvUTF8_on(sv);
5024 else SvUTF8_off(sv);
5025 return;
5026 }
5027 {
5028 SV_CHECK_THINKFIRST_COW_DROP(sv);
5029 SvUPGRADE(sv, SVt_PV);
5030 SvPV_free(sv);
5031 SvPV_set(sv,(char *)HEK_KEY(share_hek_hek(hek)));
5032 SvCUR_set(sv, HEK_LEN(hek));
5033 SvLEN_set(sv, 0);
5034 SvIsCOW_on(sv);
5035 SvPOK_on(sv);
5036 if (HEK_UTF8(hek))
5037 SvUTF8_on(sv);
5038 else SvUTF8_off(sv);
5039 return;
5040 }
5041 }
5042 }
5043
5044
5045 /*
5046 =for apidoc sv_usepvn_flags
5047
5048 Tells an SV to use C<ptr> to find its string value. Normally the
5049 string is stored inside the SV, but sv_usepvn allows the SV to use an
5050 outside string. The C<ptr> should point to memory that was allocated
5051 by L<Newx|perlclib/Memory Management and String Handling>. It must be
5052 the start of a Newx-ed block of memory, and not a pointer to the
5053 middle of it (beware of L<OOK|perlguts/Offsets> and copy-on-write),
5054 and not be from a non-Newx memory allocator like C<malloc>. The
5055 string length, C<len>, must be supplied. By default this function
5056 will C<Renew> (i.e. realloc, move) the memory pointed to by C<ptr>,
5057 so that pointer should not be freed or used by the programmer after
5058 giving it to sv_usepvn, and neither should any pointers from "behind"
5059 that pointer (e.g. ptr + 1) be used.
5060
5061 If C<flags> & SV_SMAGIC is true, will call SvSETMAGIC. If C<flags> &
5062 SV_HAS_TRAILING_NUL is true, then C<ptr[len]> must be C<NUL>, and the realloc
5063 will be skipped (i.e. the buffer is actually at least 1 byte longer than
5064 C<len>, and already meets the requirements for storing in C<SvPVX>).
5065
5066 =cut
5067 */
5068
5069 void
5070 Perl_sv_usepvn_flags(pTHX_ SV *const sv, char *ptr, const STRLEN len, const U32 flags)
5071 {
5072 STRLEN allocate;
5073
5074 PERL_ARGS_ASSERT_SV_USEPVN_FLAGS;
5075
5076 SV_CHECK_THINKFIRST_COW_DROP(sv);
5077 SvUPGRADE(sv, SVt_PV);
5078 if (!ptr) {
5079 (void)SvOK_off(sv);
5080 if (flags & SV_SMAGIC)
5081 SvSETMAGIC(sv);
5082 return;
5083 }
5084 if (SvPVX_const(sv))
5085 SvPV_free(sv);
5086
5087 #ifdef DEBUGGING
5088 if (flags & SV_HAS_TRAILING_NUL)
5089 assert(ptr[len] == '\0');
5090 #endif
5091
5092 allocate = (flags & SV_HAS_TRAILING_NUL)
5093 ? len + 1 :
5094 #ifdef Perl_safesysmalloc_size
5095 len + 1;
5096 #else
5097 PERL_STRLEN_ROUNDUP(len + 1);
5098 #endif
5099 if (flags & SV_HAS_TRAILING_NUL) {
5100 /* It's long enough - do nothing.
5101 Specifically Perl_newCONSTSUB is relying on this. */
5102 } else {
5103 #ifdef DEBUGGING
5104 /* Force a move to shake out bugs in callers. */
5105 char *new_ptr = (char*)safemalloc(allocate);
5106 Copy(ptr, new_ptr, len, char);
5107 PoisonFree(ptr,len,char);
5108 Safefree(ptr);
5109 ptr = new_ptr;
5110 #else
5111 ptr = (char*) saferealloc (ptr, allocate);
5112 #endif
5113 }
5114 #ifdef Perl_safesysmalloc_size
5115 SvLEN_set(sv, Perl_safesysmalloc_size(ptr));
5116 #else
5117 SvLEN_set(sv, allocate);
5118 #endif
5119 SvCUR_set(sv, len);
5120 SvPV_set(sv, ptr);
5121 if (!(flags & SV_HAS_TRAILING_NUL)) {
5122 ptr[len] = '\0';
5123 }
5124 (void)SvPOK_only_UTF8(sv); /* validate pointer */
5125 SvTAINT(sv);
5126 if (flags & SV_SMAGIC)
5127 SvSETMAGIC(sv);
5128 }
5129
5130 #ifdef PERL_OLD_COPY_ON_WRITE
5131 /* Need to do this *after* making the SV normal, as we need the buffer
5132 pointer to remain valid until after we've copied it. If we let go too early,
5133 another thread could invalidate it by unsharing last of the same hash key
5134 (which it can do by means other than releasing copy-on-write Svs)
5135 or by changing the other copy-on-write SVs in the loop. */
5136 STATIC void
5137 S_sv_release_COW(pTHX_ SV *sv, const char *pvx, SV *after)
5138 {
5139 PERL_ARGS_ASSERT_SV_RELEASE_COW;
5140
5141 { /* this SV was SvIsCOW_normal(sv) */
5142 /* we need to find the SV pointing to us. */
5143 SV *current = SV_COW_NEXT_SV(after);
5144
5145 if (current == sv) {
5146 /* The SV we point to points back to us (there were only two of us
5147 in the loop.)
5148 Hence other SV is no longer copy on write either. */
5149 SvIsCOW_off(after);
5150 sv_buf_to_rw(after);
5151 } else {
5152 /* We need to follow the pointers around the loop. */
5153 SV *next;
5154 while ((next = SV_COW_NEXT_SV(current)) != sv) {
5155 assert (next);
5156 current = next;
5157 /* don't loop forever if the structure is bust, and we have
5158 a pointer into a closed loop. */
5159 assert (current != after);
5160 assert (SvPVX_const(current) == pvx);
5161 }
5162 /* Make the SV before us point to the SV after us. */
5163 SV_COW_NEXT_SV_SET(current, after);
5164 }
5165 }
5166 }
5167 #endif
5168 /*
5169 =for apidoc sv_force_normal_flags
5170
5171 Undo various types of fakery on an SV, where fakery means
5172 "more than" a string: if the PV is a shared string, make
5173 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
5174 an xpvmg; if we're a copy-on-write scalar, this is the on-write time when
5175 we do the copy, and is also used locally; if this is a
5176 vstring, drop the vstring magic. If C<SV_COW_DROP_PV> is set
5177 then a copy-on-write scalar drops its PV buffer (if any) and becomes
5178 SvPOK_off rather than making a copy. (Used where this
5179 scalar is about to be set to some other value.) In addition,
5180 the C<flags> parameter gets passed to C<sv_unref_flags()>
5181 when unreffing. C<sv_force_normal> calls this function
5182 with flags set to 0.
5183
5184 This function is expected to be used to signal to perl that this SV is
5185 about to be written to, and any extra book-keeping needs to be taken care
5186 of. Hence, it croaks on read-only values.
5187
5188 =cut
5189 */
5190
5191 static void
5192 S_sv_uncow(pTHX_ SV * const sv, const U32 flags)
5193 {
5194 assert(SvIsCOW(sv));
5195 {
5196 #ifdef PERL_ANY_COW
5197 const char * const pvx = SvPVX_const(sv);
5198 const STRLEN len = SvLEN(sv);
5199 const STRLEN cur = SvCUR(sv);
5200 # ifdef PERL_OLD_COPY_ON_WRITE
5201 /* next COW sv in the loop. If len is 0 then this is a shared-hash
5202 key scalar, so we mustn't attempt to call SV_COW_NEXT_SV(), as
5203 we'll fail an assertion. */
5204 SV * const next = len ? SV_COW_NEXT_SV(sv) : 0;
5205 # endif
5206
5207 if (DEBUG_C_TEST) {
5208 PerlIO_printf(Perl_debug_log,
5209 "Copy on write: Force normal %ld\n",
5210 (long) flags);
5211 sv_dump(sv);
5212 }
5213 SvIsCOW_off(sv);
5214 # ifdef PERL_NEW_COPY_ON_WRITE
5215 if (len) {
5216 /* Must do this first, since the CowREFCNT uses SvPVX and
5217 we need to write to CowREFCNT, or de-RO the whole buffer if we are
5218 the only owner left of the buffer. */
5219 sv_buf_to_rw(sv); /* NOOP if RO-ing not supported */
5220 {
5221 U8 cowrefcnt = CowREFCNT(sv);
5222 if(cowrefcnt != 0) {
5223 cowrefcnt--;
5224 CowREFCNT(sv) = cowrefcnt;
5225 sv_buf_to_ro(sv);
5226 goto copy_over;
5227 }
5228 }
5229 /* Else we are the only owner of the buffer. */
5230 }
5231 else
5232 # endif
5233 {
5234 /* This SV doesn't own the buffer, so need to Newx() a new one: */
5235 copy_over:
5236 SvPV_set(sv, NULL);
5237 SvCUR_set(sv, 0);
5238 SvLEN_set(sv, 0);
5239 if (flags & SV_COW_DROP_PV) {
5240 /* OK, so we don't need to copy our buffer. */
5241 SvPOK_off(sv);
5242 } else {
5243 SvGROW(sv, cur + 1);
5244 Move(pvx,SvPVX(sv),cur,char);
5245 SvCUR_set(sv, cur);
5246 *SvEND(sv) = '\0';
5247 }
5248 if (len) {
5249 # ifdef PERL_OLD_COPY_ON_WRITE
5250 sv_release_COW(sv, pvx, next);
5251 # endif
5252 } else {
5253 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
5254 }
5255 if (DEBUG_C_TEST) {
5256 sv_dump(sv);
5257 }
5258 }
5259 #else
5260 const char * const pvx = SvPVX_const(sv);
5261 const STRLEN len = SvCUR(sv);
5262 SvIsCOW_off(sv);
5263 SvPV_set(sv, NULL);
5264 SvLEN_set(sv, 0);
5265 if (flags & SV_COW_DROP_PV) {
5266 /* OK, so we don't need to copy our buffer. */
5267 SvPOK_off(sv);
5268 } else {
5269 SvGROW(sv, len + 1);
5270 Move(pvx,SvPVX(sv),len,char);
5271 *SvEND(sv) = '\0';
5272 }
5273 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
5274 #endif
5275 }
5276 }
5277
5278 void
5279 Perl_sv_force_normal_flags(pTHX_ SV *const sv, const U32 flags)
5280 {
5281 PERL_ARGS_ASSERT_SV_FORCE_NORMAL_FLAGS;
5282
5283 if (SvREADONLY(sv))
5284 Perl_croak_no_modify();
5285 else if (SvIsCOW(sv) && LIKELY(SvTYPE(sv) != SVt_PVHV))
5286 S_sv_uncow(aTHX_ sv, flags);
5287 if (SvROK(sv))
5288 sv_unref_flags(sv, flags);
5289 else if (SvFAKE(sv) && isGV_with_GP(sv))
5290 sv_unglob(sv, flags);
5291 else if (SvFAKE(sv) && isREGEXP(sv)) {
5292 /* Need to downgrade the REGEXP to a simple(r) scalar. This is analogous
5293 to sv_unglob. We only need it here, so inline it. */
5294 const bool islv = SvTYPE(sv) == SVt_PVLV;
5295 const svtype new_type =
5296 islv ? SVt_NULL : SvMAGIC(sv) || SvSTASH(sv) ? SVt_PVMG : SVt_PV;
5297 SV *const temp = newSV_type(new_type);
5298 regexp *const temp_p = ReANY((REGEXP *)sv);
5299
5300 if (new_type == SVt_PVMG) {
5301 SvMAGIC_set(temp, SvMAGIC(sv));
5302 SvMAGIC_set(sv, NULL);
5303 SvSTASH_set(temp, SvSTASH(sv));
5304 SvSTASH_set(sv, NULL);
5305 }
5306 if (!islv) SvCUR_set(temp, SvCUR(sv));
5307 /* Remember that SvPVX is in the head, not the body. But
5308 RX_WRAPPED is in the body. */
5309 assert(ReANY((REGEXP *)sv)->mother_re);
5310 /* Their buffer is already owned by someone else. */
5311 if (flags & SV_COW_DROP_PV) {
5312 /* SvLEN is already 0. For SVt_REGEXP, we have a brand new
5313 zeroed body. For SVt_PVLV, it should have been set to 0
5314 before turning into a regexp. */
5315 assert(!SvLEN(islv ? sv : temp));
5316 sv->sv_u.svu_pv = 0;
5317 }
5318 else {
5319 sv->sv_u.svu_pv = savepvn(RX_WRAPPED((REGEXP *)sv), SvCUR(sv));
5320 SvLEN_set(islv ? sv : temp, SvCUR(sv)+1);
5321 SvPOK_on(sv);
5322 }
5323
5324 /* Now swap the rest of the bodies. */
5325
5326 SvFAKE_off(sv);
5327 if (!islv) {
5328 SvFLAGS(sv) &= ~SVTYPEMASK;
5329 SvFLAGS(sv) |= new_type;
5330 SvANY(sv) = SvANY(temp);
5331 }
5332
5333 SvFLAGS(temp) &= ~(SVTYPEMASK);
5334 SvFLAGS(temp) |= SVt_REGEXP|SVf_FAKE;
5335 SvANY(temp) = temp_p;
5336 temp->sv_u.svu_rx = (regexp *)temp_p;
5337
5338 SvREFCNT_dec_NN(temp);
5339 }
5340 else if (SvVOK(sv)) sv_unmagic(sv, PERL_MAGIC_vstring);
5341 }
5342
5343 /*
5344 =for apidoc sv_chop
5345
5346 Efficient removal of characters from the beginning of the string buffer.
5347 SvPOK(sv), or at least SvPOKp(sv), must be true and the C<ptr> must be a
5348 pointer to somewhere inside the string buffer. The C<ptr> becomes the first
5349 character of the adjusted string. Uses the "OOK hack". On return, only
5350 SvPOK(sv) and SvPOKp(sv) among the OK flags will be true.
5351
5352 Beware: after this function returns, C<ptr> and SvPVX_const(sv) may no longer
5353 refer to the same chunk of data.
5354
5355 The unfortunate similarity of this function's name to that of Perl's C<chop>
5356 operator is strictly coincidental. This function works from the left;
5357 C<chop> works from the right.
5358
5359 =cut
5360 */
5361
5362 void
5363 Perl_sv_chop(pTHX_ SV *const sv, const char *const ptr)
5364 {
5365 STRLEN delta;
5366 STRLEN old_delta;
5367 U8 *p;
5368 #ifdef DEBUGGING
5369 const U8 *evacp;
5370 STRLEN evacn;
5371 #endif
5372 STRLEN max_delta;
5373
5374 PERL_ARGS_ASSERT_SV_CHOP;
5375
5376 if (!ptr || !SvPOKp(sv))
5377 return;
5378 delta = ptr - SvPVX_const(sv);
5379 if (!delta) {
5380 /* Nothing to do. */
5381 return;
5382 }
5383 max_delta = SvLEN(sv) ? SvLEN(sv) : SvCUR(sv);
5384 if (delta > max_delta)
5385 Perl_croak(aTHX_ "panic: sv_chop ptr=%p, start=%p, end=%p",
5386 ptr, SvPVX_const(sv), SvPVX_const(sv) + max_delta);
5387 /* SvPVX(sv) may move in SV_CHECK_THINKFIRST(sv), so don't use ptr any more */
5388 SV_CHECK_THINKFIRST(sv);
5389 SvPOK_only_UTF8(sv);
5390
5391 if (!SvOOK(sv)) {
5392 if (!SvLEN(sv)) { /* make copy of shared string */
5393 const char *pvx = SvPVX_const(sv);
5394 const STRLEN len = SvCUR(sv);
5395 SvGROW(sv, len + 1);
5396 Move(pvx,SvPVX(sv),len,char);
5397 *SvEND(sv) = '\0';
5398 }
5399 SvOOK_on(sv);
5400 old_delta = 0;
5401 } else {
5402 SvOOK_offset(sv, old_delta);
5403 }
5404 SvLEN_set(sv, SvLEN(sv) - delta);
5405 SvCUR_set(sv, SvCUR(sv) - delta);
5406 SvPV_set(sv, SvPVX(sv) + delta);
5407
5408 p = (U8 *)SvPVX_const(sv);
5409
5410 #ifdef DEBUGGING
5411 /* how many bytes were evacuated? we will fill them with sentinel
5412 bytes, except for the part holding the new offset of course. */
5413 evacn = delta;
5414 if (old_delta)
5415 evacn += (old_delta < 0x100 ? 1 : 1 + sizeof(STRLEN));
5416 assert(evacn);
5417 assert(evacn <= delta + old_delta);
5418 evacp = p - evacn;
5419 #endif
5420
5421 /* This sets 'delta' to the accumulated value of all deltas so far */
5422 delta += old_delta;
5423 assert(delta);
5424
5425 /* If 'delta' fits in a byte, store it just prior to the new beginning of
5426 * the string; otherwise store a 0 byte there and store 'delta' just prior
5427 * to that, using as many bytes as a STRLEN occupies. Thus it overwrites a
5428 * portion of the chopped part of the string */
5429 if (delta < 0x100) {
5430 *--p = (U8) delta;
5431 } else {
5432 *--p = 0;
5433 p -= sizeof(STRLEN);
5434 Copy((U8*)&delta, p, sizeof(STRLEN), U8);
5435 }
5436
5437 #ifdef DEBUGGING
5438 /* Fill the preceding buffer with sentinals to verify that no-one is
5439 using it. */
5440 while (p > evacp) {
5441 --p;
5442 *p = (U8)PTR2UV(p);
5443 }
5444 #endif
5445 }
5446
5447 /*
5448 =for apidoc sv_catpvn
5449
5450 Concatenates the string onto the end of the string which is in the SV. The
5451 C<len> indicates number of bytes to copy. If the SV has the UTF-8
5452 status set, then the bytes appended should be valid UTF-8.
5453 Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>.
5454
5455 =for apidoc sv_catpvn_flags
5456
5457 Concatenates the string onto the end of the string which is in the SV. The
5458 C<len> indicates number of bytes to copy.
5459
5460 By default, the string appended is assumed to be valid UTF-8 if the SV has
5461 the UTF-8 status set, and a string of bytes otherwise. One can force the
5462 appended string to be interpreted as UTF-8 by supplying the C<SV_CATUTF8>
5463 flag, and as bytes by supplying the C<SV_CATBYTES> flag; the SV or the
5464 string appended will be upgraded to UTF-8 if necessary.
5465
5466 If C<flags> has the C<SV_SMAGIC> bit set, will
5467 C<mg_set> on C<dsv> afterwards if appropriate.
5468 C<sv_catpvn> and C<sv_catpvn_nomg> are implemented
5469 in terms of this function.
5470
5471 =cut
5472 */
5473
5474 void
5475 Perl_sv_catpvn_flags(pTHX_ SV *const dsv, const char *sstr, const STRLEN slen, const I32 flags)
5476 {
5477 STRLEN dlen;
5478 const char * const dstr = SvPV_force_flags(dsv, dlen, flags);
5479
5480 PERL_ARGS_ASSERT_SV_CATPVN_FLAGS;
5481 assert((flags & (SV_CATBYTES|SV_CATUTF8)) != (SV_CATBYTES|SV_CATUTF8));
5482
5483 if (!(flags & SV_CATBYTES) || !SvUTF8(dsv)) {
5484 if (flags & SV_CATUTF8 && !SvUTF8(dsv)) {
5485 sv_utf8_upgrade_flags_grow(dsv, 0, slen + 1);
5486 dlen = SvCUR(dsv);
5487 }
5488 else SvGROW(dsv, dlen + slen + 1);
5489 if (sstr == dstr)
5490 sstr = SvPVX_const(dsv);
5491 Move(sstr, SvPVX(dsv) + dlen, slen, char);
5492 SvCUR_set(dsv, SvCUR(dsv) + slen);
5493 }
5494 else {
5495 /* We inline bytes_to_utf8, to avoid an extra malloc. */
5496 const char * const send = sstr + slen;
5497 U8 *d;
5498
5499 /* Something this code does not account for, which I think is
5500 impossible; it would require the same pv to be treated as
5501 bytes *and* utf8, which would indicate a bug elsewhere. */
5502 assert(sstr != dstr);
5503
5504 SvGROW(dsv, dlen + slen * 2 + 1);
5505 d = (U8 *)SvPVX(dsv) + dlen;
5506
5507 while (sstr < send) {
5508 append_utf8_from_native_byte(*sstr, &d);
5509 sstr++;
5510 }
5511 SvCUR_set(dsv, d-(const U8 *)SvPVX(dsv));
5512 }
5513 *SvEND(dsv) = '\0';
5514 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
5515 SvTAINT(dsv);
5516 if (flags & SV_SMAGIC)
5517 SvSETMAGIC(dsv);
5518 }
5519
5520 /*
5521 =for apidoc sv_catsv
5522
5523 Concatenates the string from SV C<ssv> onto the end of the string in SV
5524 C<dsv>. If C<ssv> is null, does nothing; otherwise modifies only C<dsv>.
5525 Handles 'get' magic on both SVs, but no 'set' magic. See C<sv_catsv_mg> and
5526 C<sv_catsv_nomg>.
5527
5528 =for apidoc sv_catsv_flags
5529
5530 Concatenates the string from SV C<ssv> onto the end of the string in SV
5531 C<dsv>. If C<ssv> is null, does nothing; otherwise modifies only C<dsv>.
5532 If C<flags> include C<SV_GMAGIC> bit set, will call C<mg_get> on both SVs if
5533 appropriate. If C<flags> include C<SV_SMAGIC>, C<mg_set> will be called on
5534 the modified SV afterward, if appropriate. C<sv_catsv>, C<sv_catsv_nomg>,
5535 and C<sv_catsv_mg> are implemented in terms of this function.
5536
5537 =cut */
5538
5539 void
5540 Perl_sv_catsv_flags(pTHX_ SV *const dsv, SV *const ssv, const I32 flags)
5541 {
5542 PERL_ARGS_ASSERT_SV_CATSV_FLAGS;
5543
5544 if (ssv) {
5545 STRLEN slen;
5546 const char *spv = SvPV_flags_const(ssv, slen, flags);
5547 if (flags & SV_GMAGIC)
5548 SvGETMAGIC(dsv);
5549 sv_catpvn_flags(dsv, spv, slen,
5550 DO_UTF8(ssv) ? SV_CATUTF8 : SV_CATBYTES);
5551 if (flags & SV_SMAGIC)
5552 SvSETMAGIC(dsv);
5553 }
5554 }
5555
5556 /*
5557 =for apidoc sv_catpv
5558
5559 Concatenates the C<NUL>-terminated string onto the end of the string which is
5560 in the SV.
5561 If the SV has the UTF-8 status set, then the bytes appended should be
5562 valid UTF-8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
5563
5564 =cut */
5565
5566 void
5567 Perl_sv_catpv(pTHX_ SV *const sv, const char *ptr)
5568 {
5569 STRLEN len;
5570 STRLEN tlen;
5571 char *junk;
5572
5573 PERL_ARGS_ASSERT_SV_CATPV;
5574
5575 if (!ptr)
5576 return;
5577 junk = SvPV_force(sv, tlen);
5578 len = strlen(ptr);
5579 SvGROW(sv, tlen + len + 1);
5580 if (ptr == junk)
5581 ptr = SvPVX_const(sv);
5582 Move(ptr,SvPVX(sv)+tlen,len+1,char);
5583 SvCUR_set(sv, SvCUR(sv) + len);
5584 (void)SvPOK_only_UTF8(sv); /* validate pointer */
5585 SvTAINT(sv);
5586 }
5587
5588 /*
5589 =for apidoc sv_catpv_flags
5590
5591 Concatenates the C<NUL>-terminated string onto the end of the string which is
5592 in the SV.
5593 If the SV has the UTF-8 status set, then the bytes appended should
5594 be valid UTF-8. If C<flags> has the C<SV_SMAGIC> bit set, will C<mg_set>
5595 on the modified SV if appropriate.
5596
5597 =cut
5598 */
5599
5600 void
5601 Perl_sv_catpv_flags(pTHX_ SV *dstr, const char *sstr, const I32 flags)
5602 {
5603 PERL_ARGS_ASSERT_SV_CATPV_FLAGS;
5604 sv_catpvn_flags(dstr, sstr, strlen(sstr), flags);
5605 }
5606
5607 /*
5608 =for apidoc sv_catpv_mg
5609
5610 Like C<sv_catpv>, but also handles 'set' magic.
5611
5612 =cut
5613 */
5614
5615 void
5616 Perl_sv_catpv_mg(pTHX_ SV *const sv, const char *const ptr)
5617 {
5618 PERL_ARGS_ASSERT_SV_CATPV_MG;
5619
5620 sv_catpv(sv,ptr);
5621 SvSETMAGIC(sv);
5622 }
5623
5624 /*
5625 =for apidoc newSV
5626
5627 Creates a new SV. A non-zero C<len> parameter indicates the number of
5628 bytes of preallocated string space the SV should have. An extra byte for a
5629 trailing C<NUL> is also reserved. (SvPOK is not set for the SV even if string
5630 space is allocated.) The reference count for the new SV is set to 1.
5631
5632 In 5.9.3, newSV() replaces the older NEWSV() API, and drops the first
5633 parameter, I<x>, a debug aid which allowed callers to identify themselves.
5634 This aid has been superseded by a new build option, PERL_MEM_LOG (see
5635 L<perlhacktips/PERL_MEM_LOG>). The older API is still there for use in XS
5636 modules supporting older perls.
5637
5638 =cut
5639 */
5640
5641 SV *
5642 Perl_newSV(pTHX_ const STRLEN len)
5643 {
5644 SV *sv;
5645
5646 new_SV(sv);
5647 if (len) {
5648 sv_grow(sv, len + 1);
5649 }
5650 return sv;
5651 }
5652 /*
5653 =for apidoc sv_magicext
5654
5655 Adds magic to an SV, upgrading it if necessary. Applies the
5656 supplied vtable and returns a pointer to the magic added.
5657
5658 Note that C<sv_magicext> will allow things that C<sv_magic> will not.
5659 In particular, you can add magic to SvREADONLY SVs, and add more than
5660 one instance of the same 'how'.
5661
5662 If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
5663 stored, if C<namlen> is zero then C<name> is stored as-is and - as another
5664 special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
5665 to contain an C<SV*> and is stored as-is with its REFCNT incremented.
5666
5667 (This is now used as a subroutine by C<sv_magic>.)
5668
5669 =cut
5670 */
5671 MAGIC *
5672 Perl_sv_magicext(pTHX_ SV *const sv, SV *const obj, const int how,
5673 const MGVTBL *const vtable, const char *const name, const I32 namlen)
5674 {
5675 MAGIC* mg;
5676
5677 PERL_ARGS_ASSERT_SV_MAGICEXT;
5678
5679 SvUPGRADE(sv, SVt_PVMG);
5680 Newxz(mg, 1, MAGIC);
5681 mg->mg_moremagic = SvMAGIC(sv);
5682 SvMAGIC_set(sv, mg);
5683
5684 /* Sometimes a magic contains a reference loop, where the sv and
5685 object refer to each other. To prevent a reference loop that
5686 would prevent such objects being freed, we look for such loops
5687 and if we find one we avoid incrementing the object refcount.
5688
5689 Note we cannot do this to avoid self-tie loops as intervening RV must
5690 have its REFCNT incremented to keep it in existence.
5691
5692 */
5693 if (!obj || obj == sv ||
5694 how == PERL_MAGIC_arylen ||
5695 how == PERL_MAGIC_symtab ||
5696 (SvTYPE(obj) == SVt_PVGV &&
5697 (GvSV(obj) == sv || GvHV(obj) == (const HV *)sv
5698 || GvAV(obj) == (const AV *)sv || GvCV(obj) == (const CV *)sv
5699 || GvIOp(obj) == (const IO *)sv || GvFORM(obj) == (const CV *)sv)))
5700 {
5701 mg->mg_obj = obj;
5702 }
5703 else {
5704 mg->mg_obj = SvREFCNT_inc_simple(obj);
5705 mg->mg_flags |= MGf_REFCOUNTED;
5706 }
5707
5708 /* Normal self-ties simply pass a null object, and instead of
5709 using mg_obj directly, use the SvTIED_obj macro to produce a
5710 new RV as needed. For glob "self-ties", we are tieing the PVIO
5711 with an RV obj pointing to the glob containing the PVIO. In
5712 this case, to avoid a reference loop, we need to weaken the
5713 reference.
5714 */
5715
5716 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
5717 obj && SvROK(obj) && GvIO(SvRV(obj)) == (const IO *)sv)
5718 {
5719 sv_rvweaken(obj);
5720 }
5721
5722 mg->mg_type = how;
5723 mg->mg_len = namlen;
5724 if (name) {
5725 if (namlen > 0)
5726 mg->mg_ptr = savepvn(name, namlen);
5727 else if (namlen == HEf_SVKEY) {
5728 /* Yes, this is casting away const. This is only for the case of
5729 HEf_SVKEY. I think we need to document this aberation of the
5730 constness of the API, rather than making name non-const, as
5731 that change propagating outwards a long way. */
5732 mg->mg_ptr = (char*)SvREFCNT_inc_simple_NN((SV *)name);
5733 } else
5734 mg->mg_ptr = (char *) name;
5735 }
5736 mg->mg_virtual = (MGVTBL *) vtable;
5737
5738 mg_magical(sv);
5739 return mg;
5740 }
5741
5742 MAGIC *
5743 Perl_sv_magicext_mglob(pTHX_ SV *sv)
5744 {
5745 PERL_ARGS_ASSERT_SV_MAGICEXT_MGLOB;
5746 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
5747 /* This sv is only a delegate. //g magic must be attached to
5748 its target. */
5749 vivify_defelem(sv);
5750 sv = LvTARG(sv);
5751 }
5752 #ifdef PERL_OLD_COPY_ON_WRITE
5753 if (SvIsCOW(sv))
5754 sv_force_normal_flags(sv, 0);
5755 #endif
5756 return sv_magicext(sv, NULL, PERL_MAGIC_regex_global,
5757 &PL_vtbl_mglob, 0, 0);
5758 }
5759
5760 /*
5761 =for apidoc sv_magic
5762
5763 Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if
5764 necessary, then adds a new magic item of type C<how> to the head of the
5765 magic list.
5766
5767 See C<sv_magicext> (which C<sv_magic> now calls) for a description of the
5768 handling of the C<name> and C<namlen> arguments.
5769
5770 You need to use C<sv_magicext> to add magic to SvREADONLY SVs and also
5771 to add more than one instance of the same 'how'.
5772
5773 =cut
5774 */
5775
5776 void
5777 Perl_sv_magic(pTHX_ SV *const sv, SV *const obj, const int how,
5778 const char *const name, const I32 namlen)
5779 {
5780 const MGVTBL *vtable;
5781 MAGIC* mg;
5782 unsigned int flags;
5783 unsigned int vtable_index;
5784
5785 PERL_ARGS_ASSERT_SV_MAGIC;
5786
5787 if (how < 0 || (unsigned)how >= C_ARRAY_LENGTH(PL_magic_data)
5788 || ((flags = PL_magic_data[how]),
5789 (vtable_index = flags & PERL_MAGIC_VTABLE_MASK)
5790 > magic_vtable_max))
5791 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
5792
5793 /* PERL_MAGIC_ext is reserved for use by extensions not perl internals.
5794 Useful for attaching extension internal data to perl vars.
5795 Note that multiple extensions may clash if magical scalars
5796 etc holding private data from one are passed to another. */
5797
5798 vtable = (vtable_index == magic_vtable_max)
5799 ? NULL : PL_magic_vtables + vtable_index;
5800
5801 #ifdef PERL_OLD_COPY_ON_WRITE
5802 if (SvIsCOW(sv))
5803 sv_force_normal_flags(sv, 0);
5804 #endif
5805 if (SvREADONLY(sv)) {
5806 if (
5807 !PERL_MAGIC_TYPE_READONLY_ACCEPTABLE(how)
5808 )
5809 {
5810 Perl_croak_no_modify();
5811 }
5812 }
5813 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
5814 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
5815 /* sv_magic() refuses to add a magic of the same 'how' as an
5816 existing one
5817 */
5818 if (how == PERL_MAGIC_taint)
5819 mg->mg_len |= 1;
5820 return;
5821 }
5822 }
5823
5824 /* Force pos to be stored as characters, not bytes. */
5825 if (SvMAGICAL(sv) && DO_UTF8(sv)
5826 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
5827 && mg->mg_len != -1
5828 && mg->mg_flags & MGf_BYTES) {
5829 mg->mg_len = (SSize_t)sv_pos_b2u_flags(sv, (STRLEN)mg->mg_len,
5830 SV_CONST_RETURN);
5831 mg->mg_flags &= ~MGf_BYTES;
5832 }
5833
5834 /* Rest of work is done else where */
5835 mg = sv_magicext(sv,obj,how,vtable,name,namlen);
5836
5837 switch (how) {
5838 case PERL_MAGIC_taint:
5839 mg->mg_len = 1;
5840 break;
5841 case PERL_MAGIC_ext:
5842 case PERL_MAGIC_dbfile:
5843 SvRMAGICAL_on(sv);
5844 break;
5845 }
5846 }
5847
5848 static int
5849 S_sv_unmagicext_flags(pTHX_ SV *const sv, const int type, MGVTBL *vtbl, const U32 flags)
5850 {
5851 MAGIC* mg;
5852 MAGIC** mgp;
5853
5854 assert(flags <= 1);
5855
5856 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
5857 return 0;
5858 mgp = &(((XPVMG*) SvANY(sv))->xmg_u.xmg_magic);
5859 for (mg = *mgp; mg; mg = *mgp) {
5860 const MGVTBL* const virt = mg->mg_virtual;
5861 if (mg->mg_type == type && (!flags || virt == vtbl)) {
5862 *mgp = mg->mg_moremagic;
5863 if (virt && virt->svt_free)
5864 virt->svt_free(aTHX_ sv, mg);
5865 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
5866 if (mg->mg_len > 0)
5867 Safefree(mg->mg_ptr);
5868 else if (mg->mg_len == HEf_SVKEY)
5869 SvREFCNT_dec(MUTABLE_SV(mg->mg_ptr));
5870 else if (mg->mg_type == PERL_MAGIC_utf8)
5871 Safefree(mg->mg_ptr);
5872 }
5873 if (mg->mg_flags & MGf_REFCOUNTED)
5874 SvREFCNT_dec(mg->mg_obj);
5875 Safefree(mg);
5876 }
5877 else
5878 mgp = &mg->mg_moremagic;
5879 }
5880 if (SvMAGIC(sv)) {
5881 if (SvMAGICAL(sv)) /* if we're under save_magic, wait for restore_magic; */
5882 mg_magical(sv); /* else fix the flags now */
5883 }
5884 else {
5885 SvMAGICAL_off(sv);
5886 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
5887 }
5888 return 0;
5889 }
5890
5891 /*
5892 =for apidoc sv_unmagic
5893
5894 Removes all magic of type C<type> from an SV.
5895
5896 =cut
5897 */
5898
5899 int
5900 Perl_sv_unmagic(pTHX_ SV *const sv, const int type)
5901 {
5902 PERL_ARGS_ASSERT_SV_UNMAGIC;
5903 return S_sv_unmagicext_flags(aTHX_ sv, type, NULL, 0);
5904 }
5905
5906 /*
5907 =for apidoc sv_unmagicext
5908
5909 Removes all magic of type C<type> with the specified C<vtbl> from an SV.
5910
5911 =cut
5912 */
5913
5914 int
5915 Perl_sv_unmagicext(pTHX_ SV *const sv, const int type, MGVTBL *vtbl)
5916 {
5917 PERL_ARGS_ASSERT_SV_UNMAGICEXT;
5918 return S_sv_unmagicext_flags(aTHX_ sv, type, vtbl, 1);
5919 }
5920
5921 /*
5922 =for apidoc sv_rvweaken
5923
5924 Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
5925 referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
5926 push a back-reference to this RV onto the array of backreferences
5927 associated with that magic. If the RV is magical, set magic will be
5928 called after the RV is cleared.
5929
5930 =cut
5931 */
5932
5933 SV *
5934 Perl_sv_rvweaken(pTHX_ SV *const sv)
5935 {
5936 SV *tsv;
5937
5938 PERL_ARGS_ASSERT_SV_RVWEAKEN;
5939
5940 if (!SvOK(sv)) /* let undefs pass */
5941 return sv;
5942 if (!SvROK(sv))
5943 Perl_croak(aTHX_ "Can't weaken a nonreference");
5944 else if (SvWEAKREF(sv)) {
5945 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
5946 return sv;
5947 }
5948 else if (SvREADONLY(sv)) croak_no_modify();
5949 tsv = SvRV(sv);
5950 Perl_sv_add_backref(aTHX_ tsv, sv);
5951 SvWEAKREF_on(sv);
5952 SvREFCNT_dec_NN(tsv);
5953 return sv;
5954 }
5955
5956 /*
5957 =for apidoc sv_get_backrefs
5958
5959 If the sv is the target of a weak reference then it returns the back
5960 references structure associated with the sv; otherwise return NULL.
5961
5962 When returning a non-null result the type of the return is relevant. If it
5963 is an AV then the elements of the AV are the weak reference RVs which
5964 point at this item. If it is any other type then the item itself is the
5965 weak reference.
5966
5967 See also Perl_sv_add_backref(), Perl_sv_del_backref(),
5968 Perl_sv_kill_backrefs()
5969
5970 =cut
5971 */
5972
5973 SV *
5974 Perl_sv_get_backrefs(SV *const sv)
5975 {
5976 SV *backrefs= NULL;
5977
5978 PERL_ARGS_ASSERT_SV_GET_BACKREFS;
5979
5980 /* find slot to store array or singleton backref */
5981
5982 if (SvTYPE(sv) == SVt_PVHV) {
5983 if (SvOOK(sv)) {
5984 struct xpvhv_aux * const iter = HvAUX((HV *)sv);
5985 backrefs = (SV *)iter->xhv_backreferences;
5986 }
5987 } else if (SvMAGICAL(sv)) {
5988 MAGIC *mg = mg_find(sv, PERL_MAGIC_backref);
5989 if (mg)
5990 backrefs = mg->mg_obj;
5991 }
5992 return backrefs;
5993 }
5994
5995 /* Give tsv backref magic if it hasn't already got it, then push a
5996 * back-reference to sv onto the array associated with the backref magic.
5997 *
5998 * As an optimisation, if there's only one backref and it's not an AV,
5999 * store it directly in the HvAUX or mg_obj slot, avoiding the need to
6000 * allocate an AV. (Whether the slot holds an AV tells us whether this is
6001 * active.)
6002 */
6003
6004 /* A discussion about the backreferences array and its refcount:
6005 *
6006 * The AV holding the backreferences is pointed to either as the mg_obj of
6007 * PERL_MAGIC_backref, or in the specific case of a HV, from the
6008 * xhv_backreferences field. The array is created with a refcount
6009 * of 2. This means that if during global destruction the array gets
6010 * picked on before its parent to have its refcount decremented by the
6011 * random zapper, it won't actually be freed, meaning it's still there for
6012 * when its parent gets freed.
6013 *
6014 * When the parent SV is freed, the extra ref is killed by
6015 * Perl_sv_kill_backrefs. The other ref is killed, in the case of magic,
6016 * by mg_free() / MGf_REFCOUNTED, or for a hash, by Perl_hv_kill_backrefs.
6017 *
6018 * When a single backref SV is stored directly, it is not reference
6019 * counted.
6020 */
6021
6022 void
6023 Perl_sv_add_backref(pTHX_ SV *const tsv, SV *const sv)
6024 {
6025 SV **svp;
6026 AV *av = NULL;
6027 MAGIC *mg = NULL;
6028
6029 PERL_ARGS_ASSERT_SV_ADD_BACKREF;
6030
6031 /* find slot to store array or singleton backref */
6032
6033 if (SvTYPE(tsv) == SVt_PVHV) {
6034 svp = (SV**)Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv));
6035 } else {
6036 if (SvMAGICAL(tsv))
6037 mg = mg_find(tsv, PERL_MAGIC_backref);
6038 if (!mg)
6039 mg = sv_magicext(tsv, NULL, PERL_MAGIC_backref, &PL_vtbl_backref, NULL, 0);
6040 svp = &(mg->mg_obj);
6041 }
6042
6043 /* create or retrieve the array */
6044
6045 if ( (!*svp && SvTYPE(sv) == SVt_PVAV)
6046 || (*svp && SvTYPE(*svp) != SVt_PVAV)
6047 ) {
6048 /* create array */
6049 if (mg)
6050 mg->mg_flags |= MGf_REFCOUNTED;
6051 av = newAV();
6052 AvREAL_off(av);
6053 SvREFCNT_inc_simple_void_NN(av);
6054 /* av now has a refcnt of 2; see discussion above */
6055 av_extend(av, *svp ? 2 : 1);
6056 if (*svp) {
6057 /* move single existing backref to the array */
6058 AvARRAY(av)[++AvFILLp(av)] = *svp; /* av_push() */
6059 }
6060 *svp = (SV*)av;
6061 }
6062 else {
6063 av = MUTABLE_AV(*svp);
6064 if (!av) {
6065 /* optimisation: store single backref directly in HvAUX or mg_obj */
6066 *svp = sv;
6067 return;
6068 }
6069 assert(SvTYPE(av) == SVt_PVAV);
6070 if (AvFILLp(av) >= AvMAX(av)) {
6071 av_extend(av, AvFILLp(av)+1);
6072 }
6073 }
6074 /* push new backref */
6075 AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
6076 }
6077
6078 /* delete a back-reference to ourselves from the backref magic associated
6079 * with the SV we point to.
6080 */
6081
6082 void
6083 Perl_sv_del_backref(pTHX_ SV *const tsv, SV *const sv)
6084 {
6085 SV **svp = NULL;
6086
6087 PERL_ARGS_ASSERT_SV_DEL_BACKREF;
6088
6089 if (SvTYPE(tsv) == SVt_PVHV) {
6090 if (SvOOK(tsv))
6091 svp = (SV**)Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv));
6092 }
6093 else if (SvIS_FREED(tsv) && PL_phase == PERL_PHASE_DESTRUCT) {
6094 /* It's possible for the the last (strong) reference to tsv to have
6095 become freed *before* the last thing holding a weak reference.
6096 If both survive longer than the backreferences array, then when
6097 the referent's reference count drops to 0 and it is freed, it's
6098 not able to chase the backreferences, so they aren't NULLed.
6099
6100 For example, a CV holds a weak reference to its stash. If both the
6101 CV and the stash survive longer than the backreferences array,
6102 and the CV gets picked for the SvBREAK() treatment first,
6103 *and* it turns out that the stash is only being kept alive because
6104 of an our variable in the pad of the CV, then midway during CV
6105 destruction the stash gets freed, but CvSTASH() isn't set to NULL.
6106 It ends up pointing to the freed HV. Hence it's chased in here, and
6107 if this block wasn't here, it would hit the !svp panic just below.
6108
6109 I don't believe that "better" destruction ordering is going to help
6110 here - during global destruction there's always going to be the
6111 chance that something goes out of order. We've tried to make it
6112 foolproof before, and it only resulted in evolutionary pressure on
6113 fools. Which made us look foolish for our hubris. :-(
6114 */
6115 return;
6116 }
6117 else {
6118 MAGIC *const mg
6119 = SvMAGICAL(tsv) ? mg_find(tsv, PERL_MAGIC_backref) : NULL;
6120 svp = mg ? &(mg->mg_obj) : NULL;
6121 }
6122
6123 if (!svp)
6124 Perl_croak(aTHX_ "panic: del_backref, svp=0");
6125 if (!*svp) {
6126 /* It's possible that sv is being freed recursively part way through the
6127 freeing of tsv. If this happens, the backreferences array of tsv has
6128 already been freed, and so svp will be NULL. If this is the case,
6129 we should not panic. Instead, nothing needs doing, so return. */
6130 if (PL_phase == PERL_PHASE_DESTRUCT && SvREFCNT(tsv) == 0)
6131 return;
6132 Perl_croak(aTHX_ "panic: del_backref, *svp=%p phase=%s refcnt=%" UVuf,
6133 (void*)*svp, PL_phase_names[PL_phase], (UV)SvREFCNT(tsv));
6134 }
6135
6136 if (SvTYPE(*svp) == SVt_PVAV) {
6137 #ifdef DEBUGGING
6138 int count = 1;
6139 #endif
6140 AV * const av = (AV*)*svp;
6141 SSize_t fill;
6142 assert(!SvIS_FREED(av));
6143 fill = AvFILLp(av);
6144 assert(fill > -1);
6145 svp = AvARRAY(av);
6146 /* for an SV with N weak references to it, if all those
6147 * weak refs are deleted, then sv_del_backref will be called
6148 * N times and O(N^2) compares will be done within the backref
6149 * array. To ameliorate this potential slowness, we:
6150 * 1) make sure this code is as tight as possible;
6151 * 2) when looking for SV, look for it at both the head and tail of the
6152 * array first before searching the rest, since some create/destroy
6153 * patterns will cause the backrefs to be freed in order.
6154 */
6155 if (*svp == sv) {
6156 AvARRAY(av)++;
6157 AvMAX(av)--;
6158 }
6159 else {
6160 SV **p = &svp[fill];
6161 SV *const topsv = *p;
6162 if (topsv != sv) {
6163 #ifdef DEBUGGING
6164 count = 0;
6165 #endif
6166 while (--p > svp) {
6167 if (*p == sv) {
6168 /* We weren't the last entry.
6169 An unordered list has this property that you
6170 can take the last element off the end to fill
6171 the hole, and it's still an unordered list :-)
6172 */
6173 *p = topsv;
6174 #ifdef DEBUGGING
6175 count++;
6176 #else
6177 break; /* should only be one */
6178 #endif
6179 }
6180 }
6181 }
6182 }
6183 assert(count ==1);
6184 AvFILLp(av) = fill-1;
6185 }
6186 else if (SvIS_FREED(*svp) && PL_phase == PERL_PHASE_DESTRUCT) {
6187 /* freed AV; skip */
6188 }
6189 else {
6190 /* optimisation: only a single backref, stored directly */
6191 if (*svp != sv)
6192 Perl_croak(aTHX_ "panic: del_backref, *svp=%p, sv=%p",
6193 (void*)*svp, (void*)sv);
6194 *svp = NULL;
6195 }
6196
6197 }
6198
6199 void
6200 Perl_sv_kill_backrefs(pTHX_ SV *const sv, AV *const av)
6201 {
6202 SV **svp;
6203 SV **last;
6204 bool is_array;
6205
6206 PERL_ARGS_ASSERT_SV_KILL_BACKREFS;
6207
6208 if (!av)
6209 return;
6210
6211 /* after multiple passes through Perl_sv_clean_all() for a thingy
6212 * that has badly leaked, the backref array may have gotten freed,
6213 * since we only protect it against 1 round of cleanup */
6214 if (SvIS_FREED(av)) {
6215 if (PL_in_clean_all) /* All is fair */
6216 return;
6217 Perl_croak(aTHX_
6218 "panic: magic_killbackrefs (freed backref AV/SV)");
6219 }
6220
6221
6222 is_array = (SvTYPE(av) == SVt_PVAV);
6223 if (is_array) {
6224 assert(!SvIS_FREED(av));
6225 svp = AvARRAY(av);
6226 if (svp)
6227 last = svp + AvFILLp(av);
6228 }
6229 else {
6230 /* optimisation: only a single backref, stored directly */
6231 svp = (SV**)&av;
6232 last = svp;
6233 }
6234
6235 if (svp) {
6236 while (svp <= last) {
6237 if (*svp) {
6238 SV *const referrer = *svp;
6239 if (SvWEAKREF(referrer)) {
6240 /* XXX Should we check that it hasn't changed? */
6241 assert(SvROK(referrer));
6242 SvRV_set(referrer, 0);
6243 SvOK_off(referrer);
6244 SvWEAKREF_off(referrer);
6245 SvSETMAGIC(referrer);
6246 } else if (SvTYPE(referrer) == SVt_PVGV ||
6247 SvTYPE(referrer) == SVt_PVLV) {
6248 assert(SvTYPE(sv) == SVt_PVHV); /* stash backref */
6249 /* You lookin' at me? */
6250 assert(GvSTASH(referrer));
6251 assert(GvSTASH(referrer) == (const HV *)sv);
6252 GvSTASH(referrer) = 0;
6253 } else if (SvTYPE(referrer) == SVt_PVCV ||
6254 SvTYPE(referrer) == SVt_PVFM) {
6255 if (SvTYPE(sv) == SVt_PVHV) { /* stash backref */
6256 /* You lookin' at me? */
6257 assert(CvSTASH(referrer));
6258 assert(CvSTASH(referrer) == (const HV *)sv);
6259 SvANY(MUTABLE_CV(referrer))->xcv_stash = 0;
6260 }
6261 else {
6262 assert(SvTYPE(sv) == SVt_PVGV);
6263 /* You lookin' at me? */
6264 assert(CvGV(referrer));
6265 assert(CvGV(referrer) == (const GV *)sv);
6266 anonymise_cv_maybe(MUTABLE_GV(sv),
6267 MUTABLE_CV(referrer));
6268 }
6269
6270 } else {
6271 Perl_croak(aTHX_
6272 "panic: magic_killbackrefs (flags=%"UVxf")",
6273 (UV)SvFLAGS(referrer));
6274 }
6275
6276 if (is_array)
6277 *svp = NULL;
6278 }
6279 svp++;
6280 }
6281 }
6282 if (is_array) {
6283 AvFILLp(av) = -1;
6284 SvREFCNT_dec_NN(av); /* remove extra count added by sv_add_backref() */
6285 }
6286 return;
6287 }
6288
6289 /*
6290 =for apidoc sv_insert
6291
6292 Inserts a string at the specified offset/length within the SV. Similar to
6293 the Perl substr() function. Handles get magic.
6294
6295 =for apidoc sv_insert_flags
6296
6297 Same as C<sv_insert>, but the extra C<flags> are passed to the
6298 C<SvPV_force_flags> that applies to C<bigstr>.
6299
6300 =cut
6301 */
6302
6303 void
6304 Perl_sv_insert_flags(pTHX_ SV *const bigstr, const STRLEN offset, const STRLEN len, const char *const little, const STRLEN littlelen, const U32 flags)
6305 {
6306 char *big;
6307 char *mid;
6308 char *midend;
6309 char *bigend;
6310 SSize_t i; /* better be sizeof(STRLEN) or bad things happen */
6311 STRLEN curlen;
6312
6313 PERL_ARGS_ASSERT_SV_INSERT_FLAGS;
6314
6315 SvPV_force_flags(bigstr, curlen, flags);
6316 (void)SvPOK_only_UTF8(bigstr);
6317 if (offset + len > curlen) {
6318 SvGROW(bigstr, offset+len+1);
6319 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
6320 SvCUR_set(bigstr, offset+len);
6321 }
6322
6323 SvTAINT(bigstr);
6324 i = littlelen - len;
6325 if (i > 0) { /* string might grow */
6326 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
6327 mid = big + offset + len;
6328 midend = bigend = big + SvCUR(bigstr);
6329 bigend += i;
6330 *bigend = '\0';
6331 while (midend > mid) /* shove everything down */
6332 *--bigend = *--midend;
6333 Move(little,big+offset,littlelen,char);
6334 SvCUR_set(bigstr, SvCUR(bigstr) + i);
6335 SvSETMAGIC(bigstr);
6336 return;
6337 }
6338 else if (i == 0) {
6339 Move(little,SvPVX(bigstr)+offset,len,char);
6340 SvSETMAGIC(bigstr);
6341 return;
6342 }
6343
6344 big = SvPVX(bigstr);
6345 mid = big + offset;
6346 midend = mid + len;
6347 bigend = big + SvCUR(bigstr);
6348
6349 if (midend > bigend)
6350 Perl_croak(aTHX_ "panic: sv_insert, midend=%p, bigend=%p",
6351 midend, bigend);
6352
6353 if (mid - big > bigend - midend) { /* faster to shorten from end */
6354 if (littlelen) {
6355 Move(little, mid, littlelen,char);
6356 mid += littlelen;
6357 }
6358 i = bigend - midend;
6359 if (i > 0) {
6360 Move(midend, mid, i,char);
6361 mid += i;
6362 }
6363 *mid = '\0';
6364 SvCUR_set(bigstr, mid - big);
6365 }
6366 else if ((i = mid - big)) { /* faster from front */
6367 midend -= littlelen;
6368 mid = midend;
6369 Move(big, midend - i, i, char);
6370 sv_chop(bigstr,midend-i);
6371 if (littlelen)
6372 Move(little, mid, littlelen,char);
6373 }
6374 else if (littlelen) {
6375 midend -= littlelen;
6376 sv_chop(bigstr,midend);
6377 Move(little,midend,littlelen,char);
6378 }
6379 else {
6380 sv_chop(bigstr,midend);
6381 }
6382 SvSETMAGIC(bigstr);
6383 }
6384
6385 /*
6386 =for apidoc sv_replace
6387
6388 Make the first argument a copy of the second, then delete the original.
6389 The target SV physically takes over ownership of the body of the source SV
6390 and inherits its flags; however, the target keeps any magic it owns,
6391 and any magic in the source is discarded.
6392 Note that this is a rather specialist SV copying operation; most of the
6393 time you'll want to use C<sv_setsv> or one of its many macro front-ends.
6394
6395 =cut
6396 */
6397
6398 void
6399 Perl_sv_replace(pTHX_ SV *const sv, SV *const nsv)
6400 {
6401 const U32 refcnt = SvREFCNT(sv);
6402
6403 PERL_ARGS_ASSERT_SV_REPLACE;
6404
6405 SV_CHECK_THINKFIRST_COW_DROP(sv);
6406 if (SvREFCNT(nsv) != 1) {
6407 Perl_croak(aTHX_ "panic: reference miscount on nsv in sv_replace()"
6408 " (%" UVuf " != 1)", (UV) SvREFCNT(nsv));
6409 }
6410 if (SvMAGICAL(sv)) {
6411 if (SvMAGICAL(nsv))
6412 mg_free(nsv);
6413 else
6414 sv_upgrade(nsv, SVt_PVMG);
6415 SvMAGIC_set(nsv, SvMAGIC(sv));
6416 SvFLAGS(nsv) |= SvMAGICAL(sv);
6417 SvMAGICAL_off(sv);
6418 SvMAGIC_set(sv, NULL);
6419 }
6420 SvREFCNT(sv) = 0;
6421 sv_clear(sv);
6422 assert(!SvREFCNT(sv));
6423 #ifdef DEBUG_LEAKING_SCALARS
6424 sv->sv_flags = nsv->sv_flags;
6425 sv->sv_any = nsv->sv_any;
6426 sv->sv_refcnt = nsv->sv_refcnt;
6427 sv->sv_u = nsv->sv_u;
6428 #else
6429 StructCopy(nsv,sv,SV);
6430 #endif
6431 if(SvTYPE(sv) == SVt_IV) {
6432 SET_SVANY_FOR_BODYLESS_IV(sv);
6433 }
6434
6435
6436 #ifdef PERL_OLD_COPY_ON_WRITE
6437 if (SvIsCOW_normal(nsv)) {
6438 /* We need to follow the pointers around the loop to make the
6439 previous SV point to sv, rather than nsv. */
6440 SV *next;
6441 SV *current = nsv;
6442 while ((next = SV_COW_NEXT_SV(current)) != nsv) {
6443 assert(next);
6444 current = next;
6445 assert(SvPVX_const(current) == SvPVX_const(nsv));
6446 }
6447 /* Make the SV before us point to the SV after us. */
6448 if (DEBUG_C_TEST) {
6449 PerlIO_printf(Perl_debug_log, "previous is\n");
6450 sv_dump(current);
6451 PerlIO_printf(Perl_debug_log,
6452 "move it from 0x%"UVxf" to 0x%"UVxf"\n",
6453 (UV) SV_COW_NEXT_SV(current), (UV) sv);
6454 }
6455 SV_COW_NEXT_SV_SET(current, sv);
6456 }
6457 #endif
6458 SvREFCNT(sv) = refcnt;
6459 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
6460 SvREFCNT(nsv) = 0;
6461 del_SV(nsv);
6462 }
6463
6464 /* We're about to free a GV which has a CV that refers back to us.
6465 * If that CV will outlive us, make it anonymous (i.e. fix up its CvGV
6466 * field) */
6467
6468 STATIC void
6469 S_anonymise_cv_maybe(pTHX_ GV *gv, CV* cv)
6470 {
6471 SV *gvname;
6472 GV *anongv;
6473
6474 PERL_ARGS_ASSERT_ANONYMISE_CV_MAYBE;
6475
6476 /* be assertive! */
6477 assert(SvREFCNT(gv) == 0);
6478 assert(isGV(gv) && isGV_with_GP(gv));
6479 assert(GvGP(gv));
6480 assert(!CvANON(cv));
6481 assert(CvGV(cv) == gv);
6482 assert(!CvNAMED(cv));
6483
6484 /* will the CV shortly be freed by gp_free() ? */
6485 if (GvCV(gv) == cv && GvGP(gv)->gp_refcnt < 2 && SvREFCNT(cv) < 2) {
6486 SvANY(cv)->xcv_gv_u.xcv_gv = NULL;
6487 return;
6488 }
6489
6490 /* if not, anonymise: */
6491 gvname = (GvSTASH(gv) && HvNAME(GvSTASH(gv)) && HvENAME(GvSTASH(gv)))
6492 ? newSVhek(HvENAME_HEK(GvSTASH(gv)))
6493 : newSVpvn_flags( "__ANON__", 8, 0 );
6494 sv_catpvs(gvname, "::__ANON__");
6495 anongv = gv_fetchsv(gvname, GV_ADDMULTI, SVt_PVCV);
6496 SvREFCNT_dec_NN(gvname);
6497
6498 CvANON_on(cv);
6499 CvCVGV_RC_on(cv);
6500 SvANY(cv)->xcv_gv_u.xcv_gv = MUTABLE_GV(SvREFCNT_inc(anongv));
6501 }
6502
6503
6504 /*
6505 =for apidoc sv_clear
6506
6507 Clear an SV: call any destructors, free up any memory used by the body,
6508 and free the body itself. The SV's head is I<not> freed, although
6509 its type is set to all 1's so that it won't inadvertently be assumed
6510 to be live during global destruction etc.
6511 This function should only be called when REFCNT is zero. Most of the time
6512 you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
6513 instead.
6514
6515 =cut
6516 */
6517
6518 void
6519 Perl_sv_clear(pTHX_ SV *const orig_sv)
6520 {
6521 dVAR;
6522 HV *stash;
6523 U32 type;
6524 const struct body_details *sv_type_details;
6525 SV* iter_sv = NULL;
6526 SV* next_sv = NULL;
6527 SV *sv = orig_sv;
6528 STRLEN hash_index = 0; /* initialise to make Coverity et al happy.
6529 Not strictly necessary */
6530
6531 PERL_ARGS_ASSERT_SV_CLEAR;
6532
6533 /* within this loop, sv is the SV currently being freed, and
6534 * iter_sv is the most recent AV or whatever that's being iterated
6535 * over to provide more SVs */
6536
6537 while (sv) {
6538
6539 type = SvTYPE(sv);
6540
6541 assert(SvREFCNT(sv) == 0);
6542 assert(SvTYPE(sv) != (svtype)SVTYPEMASK);
6543
6544 if (type <= SVt_IV) {
6545 /* See the comment in sv.h about the collusion between this
6546 * early return and the overloading of the NULL slots in the
6547 * size table. */
6548 if (SvROK(sv))
6549 goto free_rv;
6550 SvFLAGS(sv) &= SVf_BREAK;
6551 SvFLAGS(sv) |= SVTYPEMASK;
6552 goto free_head;
6553 }
6554
6555 /* objs are always >= MG, but pad names use the SVs_OBJECT flag
6556 for another purpose */
6557 assert(!SvOBJECT(sv) || type >= SVt_PVMG);
6558
6559 if (type >= SVt_PVMG) {
6560 if (SvOBJECT(sv)) {
6561 if (!curse(sv, 1)) goto get_next_sv;
6562 type = SvTYPE(sv); /* destructor may have changed it */
6563 }
6564 /* Free back-references before magic, in case the magic calls
6565 * Perl code that has weak references to sv. */
6566 if (type == SVt_PVHV) {
6567 Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
6568 if (SvMAGIC(sv))
6569 mg_free(sv);
6570 }
6571 else if (SvMAGIC(sv)) {
6572 /* Free back-references before other types of magic. */
6573 sv_unmagic(sv, PERL_MAGIC_backref);
6574 mg_free(sv);
6575 }
6576 SvMAGICAL_off(sv);
6577 }
6578 switch (type) {
6579 /* case SVt_INVLIST: */
6580 case SVt_PVIO:
6581 if (IoIFP(sv) &&
6582 IoIFP(sv) != PerlIO_stdin() &&
6583 IoIFP(sv) != PerlIO_stdout() &&
6584 IoIFP(sv) != PerlIO_stderr() &&
6585 !(IoFLAGS(sv) & IOf_FAKE_DIRP))
6586 {
6587 io_close(MUTABLE_IO(sv), NULL, FALSE,
6588 (IoTYPE(sv) == IoTYPE_WRONLY ||
6589 IoTYPE(sv) == IoTYPE_RDWR ||
6590 IoTYPE(sv) == IoTYPE_APPEND));
6591 }
6592 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
6593 PerlDir_close(IoDIRP(sv));
6594 IoDIRP(sv) = (DIR*)NULL;
6595 Safefree(IoTOP_NAME(sv));
6596 Safefree(IoFMT_NAME(sv));
6597 Safefree(IoBOTTOM_NAME(sv));
6598 if ((const GV *)sv == PL_statgv)
6599 PL_statgv = NULL;
6600 goto freescalar;
6601 case SVt_REGEXP:
6602 /* FIXME for plugins */
6603 freeregexp:
6604 pregfree2((REGEXP*) sv);
6605 goto freescalar;
6606 case SVt_PVCV:
6607 case SVt_PVFM:
6608 cv_undef(MUTABLE_CV(sv));
6609 /* If we're in a stash, we don't own a reference to it.
6610 * However it does have a back reference to us, which needs to
6611 * be cleared. */
6612 if ((stash = CvSTASH(sv)))
6613 sv_del_backref(MUTABLE_SV(stash), sv);
6614 goto freescalar;
6615 case SVt_PVHV:
6616 if (PL_last_swash_hv == (const HV *)sv) {
6617 PL_last_swash_hv = NULL;
6618 }
6619 if (HvTOTALKEYS((HV*)sv) > 0) {
6620 const HEK *hek;
6621 /* this statement should match the one at the beginning of
6622 * hv_undef_flags() */
6623 if ( PL_phase != PERL_PHASE_DESTRUCT
6624 && (hek = HvNAME_HEK((HV*)sv)))
6625 {
6626 if (PL_stashcache) {
6627 DEBUG_o(Perl_deb(aTHX_
6628 "sv_clear clearing PL_stashcache for '%"HEKf
6629 "'\n",
6630 HEKfARG(hek)));
6631 (void)hv_deletehek(PL_stashcache,
6632 hek, G_DISCARD);
6633 }
6634 hv_name_set((HV*)sv, NULL, 0, 0);
6635 }
6636
6637 /* save old iter_sv in unused SvSTASH field */
6638 assert(!SvOBJECT(sv));
6639 SvSTASH(sv) = (HV*)iter_sv;
6640 iter_sv = sv;
6641
6642 /* save old hash_index in unused SvMAGIC field */
6643 assert(!SvMAGICAL(sv));
6644 assert(!SvMAGIC(sv));
6645 ((XPVMG*) SvANY(sv))->xmg_u.xmg_hash_index = hash_index;
6646 hash_index = 0;
6647
6648 next_sv = Perl_hfree_next_entry(aTHX_ (HV*)sv, &hash_index);
6649 goto get_next_sv; /* process this new sv */
6650 }
6651 /* free empty hash */
6652 Perl_hv_undef_flags(aTHX_ MUTABLE_HV(sv), HV_NAME_SETALL);
6653 assert(!HvARRAY((HV*)sv));
6654 break;
6655 case SVt_PVAV:
6656 {
6657 AV* av = MUTABLE_AV(sv);
6658 if (PL_comppad == av) {
6659 PL_comppad = NULL;
6660 PL_curpad = NULL;
6661 }
6662 if (AvREAL(av) && AvFILLp(av) > -1) {
6663 next_sv = AvARRAY(av)[AvFILLp(av)--];
6664 /* save old iter_sv in top-most slot of AV,
6665 * and pray that it doesn't get wiped in the meantime */
6666 AvARRAY(av)[AvMAX(av)] = iter_sv;
6667 iter_sv = sv;
6668 goto get_next_sv; /* process this new sv */
6669 }
6670 Safefree(AvALLOC(av));
6671 }
6672
6673 break;
6674 case SVt_PVLV:
6675 if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
6676 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
6677 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
6678 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
6679 }
6680 else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */
6681 SvREFCNT_dec(LvTARG(sv));
6682 if (isREGEXP(sv)) goto freeregexp;
6683 /* FALLTHROUGH */
6684 case SVt_PVGV:
6685 if (isGV_with_GP(sv)) {
6686 if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
6687 && HvENAME_get(stash))
6688 mro_method_changed_in(stash);
6689 gp_free(MUTABLE_GV(sv));
6690 if (GvNAME_HEK(sv))
6691 unshare_hek(GvNAME_HEK(sv));
6692 /* If we're in a stash, we don't own a reference to it.
6693 * However it does have a back reference to us, which
6694 * needs to be cleared. */
6695 if (!SvVALID(sv) && (stash = GvSTASH(sv)))
6696 sv_del_backref(MUTABLE_SV(stash), sv);
6697 }
6698 /* FIXME. There are probably more unreferenced pointers to SVs
6699 * in the interpreter struct that we should check and tidy in
6700 * a similar fashion to this: */
6701 /* See also S_sv_unglob, which does the same thing. */
6702 if ((const GV *)sv == PL_last_in_gv)
6703 PL_last_in_gv = NULL;
6704 else if ((const GV *)sv == PL_statgv)
6705 PL_statgv = NULL;
6706 else if ((const GV *)sv == PL_stderrgv)
6707 PL_stderrgv = NULL;
6708 /* FALLTHROUGH */
6709 case SVt_PVMG:
6710 case SVt_PVNV:
6711 case SVt_PVIV:
6712 case SVt_INVLIST:
6713 case SVt_PV:
6714 freescalar:
6715 /* Don't bother with SvOOK_off(sv); as we're only going to
6716 * free it. */
6717 if (SvOOK(sv)) {
6718 STRLEN offset;
6719 SvOOK_offset(sv, offset);
6720 SvPV_set(sv, SvPVX_mutable(sv) - offset);
6721 /* Don't even bother with turning off the OOK flag. */
6722 }
6723 if (SvROK(sv)) {
6724 free_rv:
6725 {
6726 SV * const target = SvRV(sv);
6727 if (SvWEAKREF(sv))
6728 sv_del_backref(target, sv);
6729 else
6730 next_sv = target;
6731 }
6732 }
6733 #ifdef PERL_ANY_COW
6734 else if (SvPVX_const(sv)
6735 && !(SvTYPE(sv) == SVt_PVIO
6736 && !(IoFLAGS(sv) & IOf_FAKE_DIRP)))
6737 {
6738 if (SvIsCOW(sv)) {
6739 if (DEBUG_C_TEST) {
6740 PerlIO_printf(Perl_debug_log, "Copy on write: clear\n");
6741 sv_dump(sv);
6742 }
6743 if (SvLEN(sv)) {
6744 # ifdef PERL_OLD_COPY_ON_WRITE
6745 sv_release_COW(sv, SvPVX_const(sv), SV_COW_NEXT_SV(sv));
6746 # else
6747 if (CowREFCNT(sv)) {
6748 sv_buf_to_rw(sv);
6749 CowREFCNT(sv)--;
6750 sv_buf_to_ro(sv);
6751 SvLEN_set(sv, 0);
6752 }
6753 # endif
6754 } else {
6755 unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
6756 }
6757
6758 }
6759 # ifdef PERL_OLD_COPY_ON_WRITE
6760 else
6761 # endif
6762 if (SvLEN(sv)) {
6763 Safefree(SvPVX_mutable(sv));
6764 }
6765 }
6766 #else
6767 else if (SvPVX_const(sv) && SvLEN(sv)
6768 && !(SvTYPE(sv) == SVt_PVIO
6769 && !(IoFLAGS(sv) & IOf_FAKE_DIRP)))
6770 Safefree(SvPVX_mutable(sv));
6771 else if (SvPVX_const(sv) && SvIsCOW(sv)) {
6772 unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
6773 }
6774 #endif
6775 break;
6776 case SVt_NV:
6777 break;
6778 }
6779
6780 free_body:
6781
6782 SvFLAGS(sv) &= SVf_BREAK;
6783 SvFLAGS(sv) |= SVTYPEMASK;
6784
6785 sv_type_details = bodies_by_type + type;
6786 if (sv_type_details->arena) {
6787 del_body(((char *)SvANY(sv) + sv_type_details->offset),
6788 &PL_body_roots[type]);
6789 }
6790 else if (sv_type_details->body_size) {
6791 safefree(SvANY(sv));
6792 }
6793
6794 free_head:
6795 /* caller is responsible for freeing the head of the original sv */
6796 if (sv != orig_sv && !SvREFCNT(sv))
6797 del_SV(sv);
6798
6799 /* grab and free next sv, if any */
6800 get_next_sv:
6801 while (1) {
6802 sv = NULL;
6803 if (next_sv) {
6804 sv = next_sv;
6805 next_sv = NULL;
6806 }
6807 else if (!iter_sv) {
6808 break;
6809 } else if (SvTYPE(iter_sv) == SVt_PVAV) {
6810 AV *const av = (AV*)iter_sv;
6811 if (AvFILLp(av) > -1) {
6812 sv = AvARRAY(av)[AvFILLp(av)--];
6813 }
6814 else { /* no more elements of current AV to free */
6815 sv = iter_sv;
6816 type = SvTYPE(sv);
6817 /* restore previous value, squirrelled away */
6818 iter_sv = AvARRAY(av)[AvMAX(av)];
6819 Safefree(AvALLOC(av));
6820 goto free_body;
6821 }
6822 } else if (SvTYPE(iter_sv) == SVt_PVHV) {
6823 sv = Perl_hfree_next_entry(aTHX_ (HV*)iter_sv, &hash_index);
6824 if (!sv && !HvTOTALKEYS((HV *)iter_sv)) {
6825 /* no more elements of current HV to free */
6826 sv = iter_sv;
6827 type = SvTYPE(sv);
6828 /* Restore previous values of iter_sv and hash_index,
6829 * squirrelled away */
6830 assert(!SvOBJECT(sv));
6831 iter_sv = (SV*)SvSTASH(sv);
6832 assert(!SvMAGICAL(sv));
6833 hash_index = ((XPVMG*) SvANY(sv))->xmg_u.xmg_hash_index;
6834 #ifdef DEBUGGING
6835 /* perl -DA does not like rubbish in SvMAGIC. */
6836 SvMAGIC_set(sv, 0);
6837 #endif
6838
6839 /* free any remaining detritus from the hash struct */
6840 Perl_hv_undef_flags(aTHX_ MUTABLE_HV(sv), HV_NAME_SETALL);
6841 assert(!HvARRAY((HV*)sv));
6842 goto free_body;
6843 }
6844 }
6845
6846 /* unrolled SvREFCNT_dec and sv_free2 follows: */
6847
6848 if (!sv)
6849 continue;
6850 if (!SvREFCNT(sv)) {
6851 sv_free(sv);
6852 continue;
6853 }
6854 if (--(SvREFCNT(sv)))
6855 continue;
6856 #ifdef DEBUGGING
6857 if (SvTEMP(sv)) {
6858 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING),
6859 "Attempt to free temp prematurely: SV 0x%"UVxf
6860 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
6861 continue;
6862 }
6863 #endif
6864 if (SvIMMORTAL(sv)) {
6865 /* make sure SvREFCNT(sv)==0 happens very seldom */
6866 SvREFCNT(sv) = SvREFCNT_IMMORTAL;
6867 continue;
6868 }
6869 break;
6870 } /* while 1 */
6871
6872 } /* while sv */
6873 }
6874
6875 /* This routine curses the sv itself, not the object referenced by sv. So
6876 sv does not have to be ROK. */
6877
6878 static bool
6879 S_curse(pTHX_ SV * const sv, const bool check_refcnt) {
6880 PERL_ARGS_ASSERT_CURSE;
6881 assert(SvOBJECT(sv));
6882
6883 if (PL_defstash && /* Still have a symbol table? */
6884 SvDESTROYABLE(sv))
6885 {
6886 dSP;
6887 HV* stash;
6888 do {
6889 stash = SvSTASH(sv);
6890 assert(SvTYPE(stash) == SVt_PVHV);
6891 if (HvNAME(stash)) {
6892 CV* destructor = NULL;
6893 assert (SvOOK(stash));
6894 if (!SvOBJECT(stash)) destructor = (CV *)SvSTASH(stash);
6895 if (!destructor || HvMROMETA(stash)->destroy_gen
6896 != PL_sub_generation)
6897 {
6898 GV * const gv =
6899 gv_fetchmeth_autoload(stash, "DESTROY", 7, 0);
6900 if (gv) destructor = GvCV(gv);
6901 if (!SvOBJECT(stash))
6902 {
6903 SvSTASH(stash) =
6904 destructor ? (HV *)destructor : ((HV *)0)+1;
6905 HvAUX(stash)->xhv_mro_meta->destroy_gen =
6906 PL_sub_generation;
6907 }
6908 }
6909 assert(!destructor || destructor == ((CV *)0)+1
6910 || SvTYPE(destructor) == SVt_PVCV);
6911 if (destructor && destructor != ((CV *)0)+1
6912 /* A constant subroutine can have no side effects, so
6913 don't bother calling it. */
6914 && !CvCONST(destructor)
6915 /* Don't bother calling an empty destructor or one that
6916 returns immediately. */
6917 && (CvISXSUB(destructor)
6918 || (CvSTART(destructor)
6919 && (CvSTART(destructor)->op_next->op_type
6920 != OP_LEAVESUB)
6921 && (CvSTART(destructor)->op_next->op_type
6922 != OP_PUSHMARK
6923 || CvSTART(destructor)->op_next->op_next->op_type
6924 != OP_RETURN
6925 )
6926 ))
6927 )
6928 {
6929 SV* const tmpref = newRV(sv);
6930 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
6931 ENTER;
6932 PUSHSTACKi(PERLSI_DESTROY);
6933 EXTEND(SP, 2);
6934 PUSHMARK(SP);
6935 PUSHs(tmpref);
6936 PUTBACK;
6937 call_sv(MUTABLE_SV(destructor),
6938 G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
6939 POPSTACK;
6940 SPAGAIN;
6941 LEAVE;
6942 if(SvREFCNT(tmpref) < 2) {
6943 /* tmpref is not kept alive! */
6944 SvREFCNT(sv)--;
6945 SvRV_set(tmpref, NULL);
6946 SvROK_off(tmpref);
6947 }
6948 SvREFCNT_dec_NN(tmpref);
6949 }
6950 }
6951 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
6952
6953
6954 if (check_refcnt && SvREFCNT(sv)) {
6955 if (PL_in_clean_objs)
6956 Perl_croak(aTHX_
6957 "DESTROY created new reference to dead object '%"HEKf"'",
6958 HEKfARG(HvNAME_HEK(stash)));
6959 /* DESTROY gave object new lease on life */
6960 return FALSE;
6961 }
6962 }
6963
6964 if (SvOBJECT(sv)) {
6965 HV * const stash = SvSTASH(sv);
6966 /* Curse before freeing the stash, as freeing the stash could cause
6967 a recursive call into S_curse. */
6968 SvOBJECT_off(sv); /* Curse the object. */
6969 SvSTASH_set(sv,0); /* SvREFCNT_dec may try to read this */
6970 SvREFCNT_dec(stash); /* possibly of changed persuasion */
6971 }
6972 return TRUE;
6973 }
6974
6975 /*
6976 =for apidoc sv_newref
6977
6978 Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
6979 instead.
6980
6981 =cut
6982 */
6983
6984 SV *
6985 Perl_sv_newref(pTHX_ SV *const sv)
6986 {
6987 PERL_UNUSED_CONTEXT;
6988 if (sv)
6989 (SvREFCNT(sv))++;
6990 return sv;
6991 }
6992
6993 /*
6994 =for apidoc sv_free
6995
6996 Decrement an SV's reference count, and if it drops to zero, call
6997 C<sv_clear> to invoke destructors and free up any memory used by
6998 the body; finally, deallocate the SV's head itself.
6999 Normally called via a wrapper macro C<SvREFCNT_dec>.
7000
7001 =cut
7002 */
7003
7004 void
7005 Perl_sv_free(pTHX_ SV *const sv)
7006 {
7007 SvREFCNT_dec(sv);
7008 }
7009
7010
7011 /* Private helper function for SvREFCNT_dec().
7012 * Called with rc set to original SvREFCNT(sv), where rc == 0 or 1 */
7013
7014 void
7015 Perl_sv_free2(pTHX_ SV *const sv, const U32 rc)
7016 {
7017 dVAR;
7018
7019 PERL_ARGS_ASSERT_SV_FREE2;
7020
7021 if (LIKELY( rc == 1 )) {
7022 /* normal case */
7023 SvREFCNT(sv) = 0;
7024
7025 #ifdef DEBUGGING
7026 if (SvTEMP(sv)) {
7027 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING),
7028 "Attempt to free temp prematurely: SV 0x%"UVxf
7029 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
7030 return;
7031 }
7032 #endif
7033 if (SvIMMORTAL(sv)) {
7034 /* make sure SvREFCNT(sv)==0 happens very seldom */
7035 SvREFCNT(sv) = SvREFCNT_IMMORTAL;
7036 return;
7037 }
7038 sv_clear(sv);
7039 if (! SvREFCNT(sv)) /* may have have been resurrected */
7040 del_SV(sv);
7041 return;
7042 }
7043
7044 /* handle exceptional cases */
7045
7046 assert(rc == 0);
7047
7048 if (SvFLAGS(sv) & SVf_BREAK)
7049 /* this SV's refcnt has been artificially decremented to
7050 * trigger cleanup */
7051 return;
7052 if (PL_in_clean_all) /* All is fair */
7053 return;
7054 if (SvIMMORTAL(sv)) {
7055 /* make sure SvREFCNT(sv)==0 happens very seldom */
7056 SvREFCNT(sv) = SvREFCNT_IMMORTAL;
7057 return;
7058 }
7059 if (ckWARN_d(WARN_INTERNAL)) {
7060 #ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
7061 Perl_dump_sv_child(aTHX_ sv);
7062 #else
7063 #ifdef DEBUG_LEAKING_SCALARS
7064 sv_dump(sv);
7065 #endif
7066 #ifdef DEBUG_LEAKING_SCALARS_ABORT
7067 if (PL_warnhook == PERL_WARNHOOK_FATAL
7068 || ckDEAD(packWARN(WARN_INTERNAL))) {
7069 /* Don't let Perl_warner cause us to escape our fate: */
7070 abort();
7071 }
7072 #endif
7073 /* This may not return: */
7074 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
7075 "Attempt to free unreferenced scalar: SV 0x%"UVxf
7076 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
7077 #endif
7078 }
7079 #ifdef DEBUG_LEAKING_SCALARS_ABORT
7080 abort();
7081 #endif
7082
7083 }
7084
7085
7086 /*
7087 =for apidoc sv_len
7088
7089 Returns the length of the string in the SV. Handles magic and type
7090 coercion and sets the UTF8 flag appropriately. See also C<SvCUR>, which
7091 gives raw access to the xpv_cur slot.
7092
7093 =cut
7094 */
7095
7096 STRLEN
7097 Perl_sv_len(pTHX_ SV *const sv)
7098 {
7099 STRLEN len;
7100
7101 if (!sv)
7102 return 0;
7103
7104 (void)SvPV_const(sv, len);
7105 return len;
7106 }
7107
7108 /*
7109 =for apidoc sv_len_utf8
7110
7111 Returns the number of characters in the string in an SV, counting wide
7112 UTF-8 bytes as a single character. Handles magic and type coercion.
7113
7114 =cut
7115 */
7116
7117 /*
7118 * The length is cached in PERL_MAGIC_utf8, in the mg_len field. Also the
7119 * mg_ptr is used, by sv_pos_u2b() and sv_pos_b2u() - see the comments below.
7120 * (Note that the mg_len is not the length of the mg_ptr field.
7121 * This allows the cache to store the character length of the string without
7122 * needing to malloc() extra storage to attach to the mg_ptr.)
7123 *
7124 */
7125
7126 STRLEN
7127 Perl_sv_len_utf8(pTHX_ SV *const sv)
7128 {
7129 if (!sv)
7130 return 0;
7131
7132 SvGETMAGIC(sv);
7133 return sv_len_utf8_nomg(sv);
7134 }
7135
7136 STRLEN
7137 Perl_sv_len_utf8_nomg(pTHX_ SV * const sv)
7138 {
7139 STRLEN len;
7140 const U8 *s = (U8*)SvPV_nomg_const(sv, len);
7141
7142 PERL_ARGS_ASSERT_SV_LEN_UTF8_NOMG;
7143
7144 if (PL_utf8cache && SvUTF8(sv)) {
7145 STRLEN ulen;
7146 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
7147
7148 if (mg && (mg->mg_len != -1 || mg->mg_ptr)) {
7149 if (mg->mg_len != -1)
7150 ulen = mg->mg_len;
7151 else {
7152 /* We can use the offset cache for a headstart.
7153 The longer value is stored in the first pair. */
7154 STRLEN *cache = (STRLEN *) mg->mg_ptr;
7155
7156 ulen = cache[0] + Perl_utf8_length(aTHX_ s + cache[1],
7157 s + len);
7158 }
7159
7160 if (PL_utf8cache < 0) {
7161 const STRLEN real = Perl_utf8_length(aTHX_ s, s + len);
7162 assert_uft8_cache_coherent("sv_len_utf8", ulen, real, sv);
7163 }
7164 }
7165 else {
7166 ulen = Perl_utf8_length(aTHX_ s, s + len);
7167 utf8_mg_len_cache_update(sv, &mg, ulen);
7168 }
7169 return ulen;
7170 }
7171 return SvUTF8(sv) ? Perl_utf8_length(aTHX_ s, s + len) : len;
7172 }
7173
7174 /* Walk forwards to find the byte corresponding to the passed in UTF-8
7175 offset. */
7176 static STRLEN
7177 S_sv_pos_u2b_forwards(const U8 *const start, const U8 *const send,
7178 STRLEN *const uoffset_p, bool *const at_end)
7179 {
7180 const U8 *s = start;
7181 STRLEN uoffset = *uoffset_p;
7182
7183 PERL_ARGS_ASSERT_SV_POS_U2B_FORWARDS;
7184
7185 while (s < send && uoffset) {
7186 --uoffset;
7187 s += UTF8SKIP(s);
7188 }
7189 if (s == send) {
7190 *at_end = TRUE;
7191 }
7192 else if (s > send) {
7193 *at_end = TRUE;
7194 /* This is the existing behaviour. Possibly it should be a croak, as
7195 it's actually a bounds error */
7196 s = send;
7197 }
7198 *uoffset_p -= uoffset;
7199 return s - start;
7200 }
7201
7202 /* Given the length of the string in both bytes and UTF-8 characters, decide
7203 whether to walk forwards or backwards to find the byte corresponding to
7204 the passed in UTF-8 offset. */
7205 static STRLEN
7206 S_sv_pos_u2b_midway(const U8 *const start, const U8 *send,
7207 STRLEN uoffset, const STRLEN uend)
7208 {
7209 STRLEN backw = uend - uoffset;
7210
7211 PERL_ARGS_ASSERT_SV_POS_U2B_MIDWAY;
7212
7213 if (uoffset < 2 * backw) {
7214 /* The assumption is that going forwards is twice the speed of going
7215 forward (that's where the 2 * backw comes from).
7216 (The real figure of course depends on the UTF-8 data.) */
7217 const U8 *s = start;
7218
7219 while (s < send && uoffset--)
7220 s += UTF8SKIP(s);
7221 assert (s <= send);
7222 if (s > send)
7223 s = send;
7224 return s - start;
7225 }
7226
7227 while (backw--) {
7228 send--;
7229 while (UTF8_IS_CONTINUATION(*send))
7230 send--;
7231 }
7232 return send - start;
7233 }
7234
7235 /* For the string representation of the given scalar, find the byte
7236 corresponding to the passed in UTF-8 offset. uoffset0 and boffset0
7237 give another position in the string, *before* the sought offset, which
7238 (which is always true, as 0, 0 is a valid pair of positions), which should
7239 help reduce the amount of linear searching.
7240 If *mgp is non-NULL, it should point to the UTF-8 cache magic, which
7241 will be used to reduce the amount of linear searching. The cache will be
7242 created if necessary, and the found value offered to it for update. */
7243 static STRLEN
7244 S_sv_pos_u2b_cached(pTHX_ SV *const sv, MAGIC **const mgp, const U8 *const start,
7245 const U8 *const send, STRLEN uoffset,
7246 STRLEN uoffset0, STRLEN boffset0)
7247 {
7248 STRLEN boffset = 0; /* Actually always set, but let's keep gcc happy. */
7249 bool found = FALSE;
7250 bool at_end = FALSE;
7251
7252 PERL_ARGS_ASSERT_SV_POS_U2B_CACHED;
7253
7254 assert (uoffset >= uoffset0);
7255
7256 if (!uoffset)
7257 return 0;
7258
7259 if (!SvREADONLY(sv) && !SvGMAGICAL(sv) && SvPOK(sv)
7260 && PL_utf8cache
7261 && (*mgp || (SvTYPE(sv) >= SVt_PVMG &&
7262 (*mgp = mg_find(sv, PERL_MAGIC_utf8))))) {
7263 if ((*mgp)->mg_ptr) {
7264 STRLEN *cache = (STRLEN *) (*mgp)->mg_ptr;
7265 if (cache[0] == uoffset) {
7266 /* An exact match. */
7267 return cache[1];
7268 }
7269 if (cache[2] == uoffset) {
7270 /* An exact match. */
7271 return cache[3];
7272 }
7273
7274 if (cache[0] < uoffset) {
7275 /* The cache already knows part of the way. */
7276 if (cache[0] > uoffset0) {
7277 /* The cache knows more than the passed in pair */
7278 uoffset0 = cache[0];
7279 boffset0 = cache[1];
7280 }
7281 if ((*mgp)->mg_len != -1) {
7282 /* And we know the end too. */
7283 boffset = boffset0
7284 + sv_pos_u2b_midway(start + boffset0, send,
7285 uoffset - uoffset0,
7286 (*mgp)->mg_len - uoffset0);
7287 } else {
7288 uoffset -= uoffset0;
7289 boffset = boffset0
7290 + sv_pos_u2b_forwards(start + boffset0,
7291 send, &uoffset, &at_end);
7292 uoffset += uoffset0;
7293 }
7294 }
7295 else if (cache[2] < uoffset) {
7296 /* We're between the two cache entries. */
7297 if (cache[2] > uoffset0) {
7298 /* and the cache knows more than the passed in pair */
7299 uoffset0 = cache[2];
7300 boffset0 = cache[3];
7301 }
7302
7303 boffset = boffset0
7304 + sv_pos_u2b_midway(start + boffset0,
7305 start + cache[1],
7306 uoffset - uoffset0,
7307 cache[0] - uoffset0);
7308 } else {
7309 boffset = boffset0
7310 + sv_pos_u2b_midway(start + boffset0,
7311 start + cache[3],
7312 uoffset - uoffset0,
7313 cache[2] - uoffset0);
7314 }
7315 found = TRUE;
7316 }
7317 else if ((*mgp)->mg_len != -1) {
7318 /* If we can take advantage of a passed in offset, do so. */
7319 /* In fact, offset0 is either 0, or less than offset, so don't
7320 need to worry about the other possibility. */
7321 boffset = boffset0
7322 + sv_pos_u2b_midway(start + boffset0, send,
7323 uoffset - uoffset0,
7324 (*mgp)->mg_len - uoffset0);
7325 found = TRUE;
7326 }
7327 }
7328
7329 if (!found || PL_utf8cache < 0) {
7330 STRLEN real_boffset;
7331 uoffset -= uoffset0;
7332 real_boffset = boffset0 + sv_pos_u2b_forwards(start + boffset0,
7333 send, &uoffset, &at_end);
7334 uoffset += uoffset0;
7335
7336 if (found && PL_utf8cache < 0)
7337 assert_uft8_cache_coherent("sv_pos_u2b_cache", boffset,
7338 real_boffset, sv);
7339 boffset = real_boffset;
7340 }
7341
7342 if (PL_utf8cache && !SvGMAGICAL(sv) && SvPOK(sv)) {
7343 if (at_end)
7344 utf8_mg_len_cache_update(sv, mgp, uoffset);
7345 else
7346 utf8_mg_pos_cache_update(sv, mgp, boffset, uoffset, send - start);
7347 }
7348 return boffset;
7349 }
7350
7351
7352 /*
7353 =for apidoc sv_pos_u2b_flags
7354
7355 Converts the offset from a count of UTF-8 chars from
7356 the start of the string, to a count of the equivalent number of bytes; if
7357 lenp is non-zero, it does the same to lenp, but this time starting from
7358 the offset, rather than from the start
7359 of the string. Handles type coercion.
7360 I<flags> is passed to C<SvPV_flags>, and usually should be
7361 C<SV_GMAGIC|SV_CONST_RETURN> to handle magic.
7362
7363 =cut
7364 */
7365
7366 /*
7367 * sv_pos_u2b_flags() uses, like sv_pos_b2u(), the mg_ptr of the potential
7368 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7369 * byte offsets. See also the comments of S_utf8_mg_pos_cache_update().
7370 *
7371 */
7372
7373 STRLEN
7374 Perl_sv_pos_u2b_flags(pTHX_ SV *const sv, STRLEN uoffset, STRLEN *const lenp,
7375 U32 flags)
7376 {
7377 const U8 *start;
7378 STRLEN len;
7379 STRLEN boffset;
7380
7381 PERL_ARGS_ASSERT_SV_POS_U2B_FLAGS;
7382
7383 start = (U8*)SvPV_flags(sv, len, flags);
7384 if (len) {
7385 const U8 * const send = start + len;
7386 MAGIC *mg = NULL;
7387 boffset = sv_pos_u2b_cached(sv, &mg, start, send, uoffset, 0, 0);
7388
7389 if (lenp
7390 && *lenp /* don't bother doing work for 0, as its bytes equivalent
7391 is 0, and *lenp is already set to that. */) {
7392 /* Convert the relative offset to absolute. */
7393 const STRLEN uoffset2 = uoffset + *lenp;
7394 const STRLEN boffset2
7395 = sv_pos_u2b_cached(sv, &mg, start, send, uoffset2,
7396 uoffset, boffset) - boffset;
7397
7398 *lenp = boffset2;
7399 }
7400 } else {
7401 if (lenp)
7402 *lenp = 0;
7403 boffset = 0;
7404 }
7405
7406 return boffset;
7407 }
7408
7409 /*
7410 =for apidoc sv_pos_u2b
7411
7412 Converts the value pointed to by offsetp from a count of UTF-8 chars from
7413 the start of the string, to a count of the equivalent number of bytes; if
7414 lenp is non-zero, it does the same to lenp, but this time starting from
7415 the offset, rather than from the start of the string. Handles magic and
7416 type coercion.
7417
7418 Use C<sv_pos_u2b_flags> in preference, which correctly handles strings longer
7419 than 2Gb.
7420
7421 =cut
7422 */
7423
7424 /*
7425 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
7426 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7427 * byte offsets. See also the comments of S_utf8_mg_pos_cache_update().
7428 *
7429 */
7430
7431 /* This function is subject to size and sign problems */
7432
7433 void
7434 Perl_sv_pos_u2b(pTHX_ SV *const sv, I32 *const offsetp, I32 *const lenp)
7435 {
7436 PERL_ARGS_ASSERT_SV_POS_U2B;
7437
7438 if (lenp) {
7439 STRLEN ulen = (STRLEN)*lenp;
7440 *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, &ulen,
7441 SV_GMAGIC|SV_CONST_RETURN);
7442 *lenp = (I32)ulen;
7443 } else {
7444 *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, NULL,
7445 SV_GMAGIC|SV_CONST_RETURN);
7446 }
7447 }
7448
7449 static void
7450 S_utf8_mg_len_cache_update(pTHX_ SV *const sv, MAGIC **const mgp,
7451 const STRLEN ulen)
7452 {
7453 PERL_ARGS_ASSERT_UTF8_MG_LEN_CACHE_UPDATE;
7454 if (SvREADONLY(sv) || SvGMAGICAL(sv) || !SvPOK(sv))
7455 return;
7456
7457 if (!*mgp && (SvTYPE(sv) < SVt_PVMG ||
7458 !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
7459 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, &PL_vtbl_utf8, 0, 0);
7460 }
7461 assert(*mgp);
7462
7463 (*mgp)->mg_len = ulen;
7464 }
7465
7466 /* Create and update the UTF8 magic offset cache, with the proffered utf8/
7467 byte length pairing. The (byte) length of the total SV is passed in too,
7468 as blen, because for some (more esoteric) SVs, the call to SvPV_const()
7469 may not have updated SvCUR, so we can't rely on reading it directly.
7470
7471 The proffered utf8/byte length pairing isn't used if the cache already has
7472 two pairs, and swapping either for the proffered pair would increase the
7473 RMS of the intervals between known byte offsets.
7474
7475 The cache itself consists of 4 STRLEN values
7476 0: larger UTF-8 offset
7477 1: corresponding byte offset
7478 2: smaller UTF-8 offset
7479 3: corresponding byte offset
7480
7481 Unused cache pairs have the value 0, 0.
7482 Keeping the cache "backwards" means that the invariant of
7483 cache[0] >= cache[2] is maintained even with empty slots, which means that
7484 the code that uses it doesn't need to worry if only 1 entry has actually
7485 been set to non-zero. It also makes the "position beyond the end of the
7486 cache" logic much simpler, as the first slot is always the one to start
7487 from.
7488 */
7489 static void
7490 S_utf8_mg_pos_cache_update(pTHX_ SV *const sv, MAGIC **const mgp, const STRLEN byte,
7491 const STRLEN utf8, const STRLEN blen)
7492 {
7493 STRLEN *cache;
7494
7495 PERL_ARGS_ASSERT_UTF8_MG_POS_CACHE_UPDATE;
7496
7497 if (SvREADONLY(sv))
7498 return;
7499
7500 if (!*mgp && (SvTYPE(sv) < SVt_PVMG ||
7501 !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
7502 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0,
7503 0);
7504 (*mgp)->mg_len = -1;
7505 }
7506 assert(*mgp);
7507
7508 if (!(cache = (STRLEN *)(*mgp)->mg_ptr)) {
7509 Newxz(cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
7510 (*mgp)->mg_ptr = (char *) cache;
7511 }
7512 assert(cache);
7513
7514 if (PL_utf8cache < 0 && SvPOKp(sv)) {
7515 /* SvPOKp() because, if sv is a reference, then SvPVX() is actually
7516 a pointer. Note that we no longer cache utf8 offsets on refer-
7517 ences, but this check is still a good idea, for robustness. */
7518 const U8 *start = (const U8 *) SvPVX_const(sv);
7519 const STRLEN realutf8 = utf8_length(start, start + byte);
7520
7521 assert_uft8_cache_coherent("utf8_mg_pos_cache_update", utf8, realutf8,
7522 sv);
7523 }
7524
7525 /* Cache is held with the later position first, to simplify the code
7526 that deals with unbounded ends. */
7527
7528 ASSERT_UTF8_CACHE(cache);
7529 if (cache[1] == 0) {
7530 /* Cache is totally empty */
7531 cache[0] = utf8;
7532 cache[1] = byte;
7533 } else if (cache[3] == 0) {
7534 if (byte > cache[1]) {
7535 /* New one is larger, so goes first. */
7536 cache[2] = cache[0];
7537 cache[3] = cache[1];
7538 cache[0] = utf8;
7539 cache[1] = byte;
7540 } else {
7541 cache[2] = utf8;
7542 cache[3] = byte;
7543 }
7544 } else {
7545 /* float casts necessary? XXX */
7546 #define THREEWAY_SQUARE(a,b,c,d) \
7547 ((float)((d) - (c))) * ((float)((d) - (c))) \
7548 + ((float)((c) - (b))) * ((float)((c) - (b))) \
7549 + ((float)((b) - (a))) * ((float)((b) - (a)))
7550
7551 /* Cache has 2 slots in use, and we know three potential pairs.
7552 Keep the two that give the lowest RMS distance. Do the
7553 calculation in bytes simply because we always know the byte
7554 length. squareroot has the same ordering as the positive value,
7555 so don't bother with the actual square root. */
7556 if (byte > cache[1]) {
7557 /* New position is after the existing pair of pairs. */
7558 const float keep_earlier
7559 = THREEWAY_SQUARE(0, cache[3], byte, blen);
7560 const float keep_later
7561 = THREEWAY_SQUARE(0, cache[1], byte, blen);
7562
7563 if (keep_later < keep_earlier) {
7564 cache[2] = cache[0];
7565 cache[3] = cache[1];
7566 }
7567 cache[0] = utf8;
7568 cache[1] = byte;
7569 }
7570 else {
7571 const float keep_later = THREEWAY_SQUARE(0, byte, cache[1], blen);
7572 float b, c, keep_earlier;
7573 if (byte > cache[3]) {
7574 /* New position is between the existing pair of pairs. */
7575 b = (float)cache[3];
7576 c = (float)byte;
7577 } else {
7578 /* New position is before the existing pair of pairs. */
7579 b = (float)byte;
7580 c = (float)cache[3];
7581 }
7582 keep_earlier = THREEWAY_SQUARE(0, b, c, blen);
7583 if (byte > cache[3]) {
7584 if (keep_later < keep_earlier) {
7585 cache[2] = utf8;
7586 cache[3] = byte;
7587 }
7588 else {
7589 cache[0] = utf8;
7590 cache[1] = byte;
7591 }
7592 }
7593 else {
7594 if (! (keep_later < keep_earlier)) {
7595 cache[0] = cache[2];
7596 cache[1] = cache[3];
7597 }
7598 cache[2] = utf8;
7599 cache[3] = byte;
7600 }
7601 }
7602 }
7603 ASSERT_UTF8_CACHE(cache);
7604 }
7605
7606 /* We already know all of the way, now we may be able to walk back. The same
7607 assumption is made as in S_sv_pos_u2b_midway(), namely that walking
7608 backward is half the speed of walking forward. */
7609 static STRLEN
7610 S_sv_pos_b2u_midway(pTHX_ const U8 *const s, const U8 *const target,
7611 const U8 *end, STRLEN endu)
7612 {
7613 const STRLEN forw = target - s;
7614 STRLEN backw = end - target;
7615
7616 PERL_ARGS_ASSERT_SV_POS_B2U_MIDWAY;
7617
7618 if (forw < 2 * backw) {
7619 return utf8_length(s, target);
7620 }
7621
7622 while (end > target) {
7623 end--;
7624 while (UTF8_IS_CONTINUATION(*end)) {
7625 end--;
7626 }
7627 endu--;
7628 }
7629 return endu;
7630 }
7631
7632 /*
7633 =for apidoc sv_pos_b2u_flags
7634
7635 Converts the offset from a count of bytes from the start of the string, to
7636 a count of the equivalent number of UTF-8 chars. Handles type coercion.
7637 I<flags> is passed to C<SvPV_flags>, and usually should be
7638 C<SV_GMAGIC|SV_CONST_RETURN> to handle magic.
7639
7640 =cut
7641 */
7642
7643 /*
7644 * sv_pos_b2u_flags() uses, like sv_pos_u2b_flags(), the mg_ptr of the
7645 * potential PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8
7646 * and byte offsets.
7647 *
7648 */
7649 STRLEN
7650 Perl_sv_pos_b2u_flags(pTHX_ SV *const sv, STRLEN const offset, U32 flags)
7651 {
7652 const U8* s;
7653 STRLEN len = 0; /* Actually always set, but let's keep gcc happy. */
7654 STRLEN blen;
7655 MAGIC* mg = NULL;
7656 const U8* send;
7657 bool found = FALSE;
7658
7659 PERL_ARGS_ASSERT_SV_POS_B2U_FLAGS;
7660
7661 s = (const U8*)SvPV_flags(sv, blen, flags);
7662
7663 if (blen < offset)
7664 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset, blen=%"UVuf
7665 ", byte=%"UVuf, (UV)blen, (UV)offset);
7666
7667 send = s + offset;
7668
7669 if (!SvREADONLY(sv)
7670 && PL_utf8cache
7671 && SvTYPE(sv) >= SVt_PVMG
7672 && (mg = mg_find(sv, PERL_MAGIC_utf8)))
7673 {
7674 if (mg->mg_ptr) {
7675 STRLEN * const cache = (STRLEN *) mg->mg_ptr;
7676 if (cache[1] == offset) {
7677 /* An exact match. */
7678 return cache[0];
7679 }
7680 if (cache[3] == offset) {
7681 /* An exact match. */
7682 return cache[2];
7683 }
7684
7685 if (cache[1] < offset) {
7686 /* We already know part of the way. */
7687 if (mg->mg_len != -1) {
7688 /* Actually, we know the end too. */
7689 len = cache[0]
7690 + S_sv_pos_b2u_midway(aTHX_ s + cache[1], send,
7691 s + blen, mg->mg_len - cache[0]);
7692 } else {
7693 len = cache[0] + utf8_length(s + cache[1], send);
7694 }
7695 }
7696 else if (cache[3] < offset) {
7697 /* We're between the two cached pairs, so we do the calculation
7698 offset by the byte/utf-8 positions for the earlier pair,
7699 then add the utf-8 characters from the string start to
7700 there. */
7701 len = S_sv_pos_b2u_midway(aTHX_ s + cache[3], send,
7702 s + cache[1], cache[0] - cache[2])
7703 + cache[2];
7704
7705 }
7706 else { /* cache[3] > offset */
7707 len = S_sv_pos_b2u_midway(aTHX_ s, send, s + cache[3],
7708 cache[2]);
7709
7710 }
7711 ASSERT_UTF8_CACHE(cache);
7712 found = TRUE;
7713 } else if (mg->mg_len != -1) {
7714 len = S_sv_pos_b2u_midway(aTHX_ s, send, s + blen, mg->mg_len);
7715 found = TRUE;
7716 }
7717 }
7718 if (!found || PL_utf8cache < 0) {
7719 const STRLEN real_len = utf8_length(s, send);
7720
7721 if (found && PL_utf8cache < 0)
7722 assert_uft8_cache_coherent("sv_pos_b2u", len, real_len, sv);
7723 len = real_len;
7724 }
7725
7726 if (PL_utf8cache) {
7727 if (blen == offset)
7728 utf8_mg_len_cache_update(sv, &mg, len);
7729 else
7730 utf8_mg_pos_cache_update(sv, &mg, offset, len, blen);
7731 }
7732
7733 return len;
7734 }
7735
7736 /*
7737 =for apidoc sv_pos_b2u
7738
7739 Converts the value pointed to by offsetp from a count of bytes from the
7740 start of the string, to a count of the equivalent number of UTF-8 chars.
7741 Handles magic and type coercion.
7742
7743 Use C<sv_pos_b2u_flags> in preference, which correctly handles strings
7744 longer than 2Gb.
7745
7746 =cut
7747 */
7748
7749 /*
7750 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
7751 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7752 * byte offsets.
7753 *
7754 */
7755 void
7756 Perl_sv_pos_b2u(pTHX_ SV *const sv, I32 *const offsetp)
7757 {
7758 PERL_ARGS_ASSERT_SV_POS_B2U;
7759
7760 if (!sv)
7761 return;
7762
7763 *offsetp = (I32)sv_pos_b2u_flags(sv, (STRLEN)*offsetp,
7764 SV_GMAGIC|SV_CONST_RETURN);
7765 }
7766
7767 static void
7768 S_assert_uft8_cache_coherent(pTHX_ const char *const func, STRLEN from_cache,
7769 STRLEN real, SV *const sv)
7770 {
7771 PERL_ARGS_ASSERT_ASSERT_UFT8_CACHE_COHERENT;
7772
7773 /* As this is debugging only code, save space by keeping this test here,
7774 rather than inlining it in all the callers. */
7775 if (from_cache == real)
7776 return;
7777
7778 /* Need to turn the assertions off otherwise we may recurse infinitely
7779 while printing error messages. */
7780 SAVEI8(PL_utf8cache);
7781 PL_utf8cache = 0;
7782 Perl_croak(aTHX_ "panic: %s cache %"UVuf" real %"UVuf" for %"SVf,
7783 func, (UV) from_cache, (UV) real, SVfARG(sv));
7784 }
7785
7786 /*
7787 =for apidoc sv_eq
7788
7789 Returns a boolean indicating whether the strings in the two SVs are
7790 identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will
7791 coerce its args to strings if necessary.
7792
7793 =for apidoc sv_eq_flags
7794
7795 Returns a boolean indicating whether the strings in the two SVs are
7796 identical. Is UTF-8 and 'use bytes' aware and coerces its args to strings
7797 if necessary. If the flags include SV_GMAGIC, it handles get-magic, too.
7798
7799 =cut
7800 */
7801
7802 I32
7803 Perl_sv_eq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
7804 {
7805 const char *pv1;
7806 STRLEN cur1;
7807 const char *pv2;
7808 STRLEN cur2;
7809 I32 eq = 0;
7810 SV* svrecode = NULL;
7811
7812 if (!sv1) {
7813 pv1 = "";
7814 cur1 = 0;
7815 }
7816 else {
7817 /* if pv1 and pv2 are the same, second SvPV_const call may
7818 * invalidate pv1 (if we are handling magic), so we may need to
7819 * make a copy */
7820 if (sv1 == sv2 && flags & SV_GMAGIC
7821 && (SvTHINKFIRST(sv1) || SvGMAGICAL(sv1))) {
7822 pv1 = SvPV_const(sv1, cur1);
7823 sv1 = newSVpvn_flags(pv1, cur1, SVs_TEMP | SvUTF8(sv2));
7824 }
7825 pv1 = SvPV_flags_const(sv1, cur1, flags);
7826 }
7827
7828 if (!sv2){
7829 pv2 = "";
7830 cur2 = 0;
7831 }
7832 else
7833 pv2 = SvPV_flags_const(sv2, cur2, flags);
7834
7835 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
7836 /* Differing utf8ness.
7837 * Do not UTF8size the comparands as a side-effect. */
7838 if (IN_ENCODING) {
7839 if (SvUTF8(sv1)) {
7840 svrecode = newSVpvn(pv2, cur2);
7841 sv_recode_to_utf8(svrecode, _get_encoding());
7842 pv2 = SvPV_const(svrecode, cur2);
7843 }
7844 else {
7845 svrecode = newSVpvn(pv1, cur1);
7846 sv_recode_to_utf8(svrecode, _get_encoding());
7847 pv1 = SvPV_const(svrecode, cur1);
7848 }
7849 /* Now both are in UTF-8. */
7850 if (cur1 != cur2) {
7851 SvREFCNT_dec_NN(svrecode);
7852 return FALSE;
7853 }
7854 }
7855 else {
7856 if (SvUTF8(sv1)) {
7857 /* sv1 is the UTF-8 one */
7858 return bytes_cmp_utf8((const U8*)pv2, cur2,
7859 (const U8*)pv1, cur1) == 0;
7860 }
7861 else {
7862 /* sv2 is the UTF-8 one */
7863 return bytes_cmp_utf8((const U8*)pv1, cur1,
7864 (const U8*)pv2, cur2) == 0;
7865 }
7866 }
7867 }
7868
7869 if (cur1 == cur2)
7870 eq = (pv1 == pv2) || memEQ(pv1, pv2, cur1);
7871
7872 SvREFCNT_dec(svrecode);
7873
7874 return eq;
7875 }
7876
7877 /*
7878 =for apidoc sv_cmp
7879
7880 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
7881 string in C<sv1> is less than, equal to, or greater than the string in
7882 C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will
7883 coerce its args to strings if necessary. See also C<sv_cmp_locale>.
7884
7885 =for apidoc sv_cmp_flags
7886
7887 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
7888 string in C<sv1> is less than, equal to, or greater than the string in
7889 C<sv2>. Is UTF-8 and 'use bytes' aware and will coerce its args to strings
7890 if necessary. If the flags include SV_GMAGIC, it handles get magic. See
7891 also C<sv_cmp_locale_flags>.
7892
7893 =cut
7894 */
7895
7896 I32
7897 Perl_sv_cmp(pTHX_ SV *const sv1, SV *const sv2)
7898 {
7899 return sv_cmp_flags(sv1, sv2, SV_GMAGIC);
7900 }
7901
7902 I32
7903 Perl_sv_cmp_flags(pTHX_ SV *const sv1, SV *const sv2,
7904 const U32 flags)
7905 {
7906 STRLEN cur1, cur2;
7907 const char *pv1, *pv2;
7908 I32 cmp;
7909 SV *svrecode = NULL;
7910
7911 if (!sv1) {
7912 pv1 = "";
7913 cur1 = 0;
7914 }
7915 else
7916 pv1 = SvPV_flags_const(sv1, cur1, flags);
7917
7918 if (!sv2) {
7919 pv2 = "";
7920 cur2 = 0;
7921 }
7922 else
7923 pv2 = SvPV_flags_const(sv2, cur2, flags);
7924
7925 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
7926 /* Differing utf8ness.
7927 * Do not UTF8size the comparands as a side-effect. */
7928 if (SvUTF8(sv1)) {
7929 if (IN_ENCODING) {
7930 svrecode = newSVpvn(pv2, cur2);
7931 sv_recode_to_utf8(svrecode, _get_encoding());
7932 pv2 = SvPV_const(svrecode, cur2);
7933 }
7934 else {
7935 const int retval = -bytes_cmp_utf8((const U8*)pv2, cur2,
7936 (const U8*)pv1, cur1);
7937 return retval ? retval < 0 ? -1 : +1 : 0;
7938 }
7939 }
7940 else {
7941 if (IN_ENCODING) {
7942 svrecode = newSVpvn(pv1, cur1);
7943 sv_recode_to_utf8(svrecode, _get_encoding());
7944 pv1 = SvPV_const(svrecode, cur1);
7945 }
7946 else {
7947 const int retval = bytes_cmp_utf8((const U8*)pv1, cur1,
7948 (const U8*)pv2, cur2);
7949 return retval ? retval < 0 ? -1 : +1 : 0;
7950 }
7951 }
7952 }
7953
7954 if (!cur1) {
7955 cmp = cur2 ? -1 : 0;
7956 } else if (!cur2) {
7957 cmp = 1;
7958 } else {
7959 const I32 retval = memcmp((const void*)pv1, (const void*)pv2, cur1 < cur2 ? cur1 : cur2);
7960
7961 if (retval) {
7962 cmp = retval < 0 ? -1 : 1;
7963 } else if (cur1 == cur2) {
7964 cmp = 0;
7965 } else {
7966 cmp = cur1 < cur2 ? -1 : 1;
7967 }
7968 }
7969
7970 SvREFCNT_dec(svrecode);
7971
7972 return cmp;
7973 }
7974
7975 /*
7976 =for apidoc sv_cmp_locale
7977
7978 Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
7979 'use bytes' aware, handles get magic, and will coerce its args to strings
7980 if necessary. See also C<sv_cmp>.
7981
7982 =for apidoc sv_cmp_locale_flags
7983
7984 Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
7985 'use bytes' aware and will coerce its args to strings if necessary. If the
7986 flags contain SV_GMAGIC, it handles get magic. See also C<sv_cmp_flags>.
7987
7988 =cut
7989 */
7990
7991 I32
7992 Perl_sv_cmp_locale(pTHX_ SV *const sv1, SV *const sv2)
7993 {
7994 return sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC);
7995 }
7996
7997 I32
7998 Perl_sv_cmp_locale_flags(pTHX_ SV *const sv1, SV *const sv2,
7999 const U32 flags)
8000 {
8001 #ifdef USE_LOCALE_COLLATE
8002
8003 char *pv1, *pv2;
8004 STRLEN len1, len2;
8005 I32 retval;
8006
8007 if (PL_collation_standard)
8008 goto raw_compare;
8009
8010 len1 = 0;
8011 pv1 = sv1 ? sv_collxfrm_flags(sv1, &len1, flags) : (char *) NULL;
8012 len2 = 0;
8013 pv2 = sv2 ? sv_collxfrm_flags(sv2, &len2, flags) : (char *) NULL;
8014
8015 if (!pv1 || !len1) {
8016 if (pv2 && len2)
8017 return -1;
8018 else
8019 goto raw_compare;
8020 }
8021 else {
8022 if (!pv2 || !len2)
8023 return 1;
8024 }
8025
8026 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
8027
8028 if (retval)
8029 return retval < 0 ? -1 : 1;
8030
8031 /*
8032 * When the result of collation is equality, that doesn't mean
8033 * that there are no differences -- some locales exclude some
8034 * characters from consideration. So to avoid false equalities,
8035 * we use the raw string as a tiebreaker.
8036 */
8037
8038 raw_compare:
8039 /* FALLTHROUGH */
8040
8041 #else
8042 PERL_UNUSED_ARG(flags);
8043 #endif /* USE_LOCALE_COLLATE */
8044
8045 return sv_cmp(sv1, sv2);
8046 }
8047
8048
8049 #ifdef USE_LOCALE_COLLATE
8050
8051 /*
8052 =for apidoc sv_collxfrm
8053
8054 This calls C<sv_collxfrm_flags> with the SV_GMAGIC flag. See
8055 C<sv_collxfrm_flags>.
8056
8057 =for apidoc sv_collxfrm_flags
8058
8059 Add Collate Transform magic to an SV if it doesn't already have it. If the
8060 flags contain SV_GMAGIC, it handles get-magic.
8061
8062 Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the
8063 scalar data of the variable, but transformed to such a format that a normal
8064 memory comparison can be used to compare the data according to the locale
8065 settings.
8066
8067 =cut
8068 */
8069
8070 char *
8071 Perl_sv_collxfrm_flags(pTHX_ SV *const sv, STRLEN *const nxp, const I32 flags)
8072 {
8073 MAGIC *mg;
8074
8075 PERL_ARGS_ASSERT_SV_COLLXFRM_FLAGS;
8076
8077 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
8078 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
8079 const char *s;
8080 char *xf;
8081 STRLEN len, xlen;
8082
8083 if (mg)
8084 Safefree(mg->mg_ptr);
8085 s = SvPV_flags_const(sv, len, flags);
8086 if ((xf = mem_collxfrm(s, len, &xlen))) {
8087 if (! mg) {
8088 #ifdef PERL_OLD_COPY_ON_WRITE
8089 if (SvIsCOW(sv))
8090 sv_force_normal_flags(sv, 0);
8091 #endif
8092 mg = sv_magicext(sv, 0, PERL_MAGIC_collxfrm, &PL_vtbl_collxfrm,
8093 0, 0);
8094 assert(mg);
8095 }
8096 mg->mg_ptr = xf;
8097 mg->mg_len = xlen;
8098 }
8099 else {
8100 if (mg) {
8101 mg->mg_ptr = NULL;
8102 mg->mg_len = -1;
8103 }
8104 }
8105 }
8106 if (mg && mg->mg_ptr) {
8107 *nxp = mg->mg_len;
8108 return mg->mg_ptr + sizeof(PL_collation_ix);
8109 }
8110 else {
8111 *nxp = 0;
8112 return NULL;
8113 }
8114 }
8115
8116 #endif /* USE_LOCALE_COLLATE */
8117
8118 static char *
8119 S_sv_gets_append_to_utf8(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8120 {
8121 SV * const tsv = newSV(0);
8122 ENTER;
8123 SAVEFREESV(tsv);
8124 sv_gets(tsv, fp, 0);
8125 sv_utf8_upgrade_nomg(tsv);
8126 SvCUR_set(sv,append);
8127 sv_catsv(sv,tsv);
8128 LEAVE;
8129 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8130 }
8131
8132 static char *
8133 S_sv_gets_read_record(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8134 {
8135 SSize_t bytesread;
8136 const STRLEN recsize = SvUV(SvRV(PL_rs)); /* RsRECORD() guarantees > 0. */
8137 /* Grab the size of the record we're getting */
8138 char *buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
8139
8140 /* Go yank in */
8141 #ifdef __VMS
8142 int fd;
8143 Stat_t st;
8144
8145 /* With a true, record-oriented file on VMS, we need to use read directly
8146 * to ensure that we respect RMS record boundaries. The user is responsible
8147 * for providing a PL_rs value that corresponds to the FAB$W_MRS (maximum
8148 * record size) field. N.B. This is likely to produce invalid results on
8149 * varying-width character data when a record ends mid-character.
8150 */
8151 fd = PerlIO_fileno(fp);
8152 if (fd != -1
8153 && PerlLIO_fstat(fd, &st) == 0
8154 && (st.st_fab_rfm == FAB$C_VAR
8155 || st.st_fab_rfm == FAB$C_VFC
8156 || st.st_fab_rfm == FAB$C_FIX)) {
8157
8158 bytesread = PerlLIO_read(fd, buffer, recsize);
8159 }
8160 else /* in-memory file from PerlIO::Scalar
8161 * or not a record-oriented file
8162 */
8163 #endif
8164 {
8165 bytesread = PerlIO_read(fp, buffer, recsize);
8166
8167 /* At this point, the logic in sv_get() means that sv will
8168 be treated as utf-8 if the handle is utf8.
8169 */
8170 if (PerlIO_isutf8(fp) && bytesread > 0) {
8171 char *bend = buffer + bytesread;
8172 char *bufp = buffer;
8173 size_t charcount = 0;
8174 bool charstart = TRUE;
8175 STRLEN skip = 0;
8176
8177 while (charcount < recsize) {
8178 /* count accumulated characters */
8179 while (bufp < bend) {
8180 if (charstart) {
8181 skip = UTF8SKIP(bufp);
8182 }
8183 if (bufp + skip > bend) {
8184 /* partial at the end */
8185 charstart = FALSE;
8186 break;
8187 }
8188 else {
8189 ++charcount;
8190 bufp += skip;
8191 charstart = TRUE;
8192 }
8193 }
8194
8195 if (charcount < recsize) {
8196 STRLEN readsize;
8197 STRLEN bufp_offset = bufp - buffer;
8198 SSize_t morebytesread;
8199
8200 /* originally I read enough to fill any incomplete
8201 character and the first byte of the next
8202 character if needed, but if there's many
8203 multi-byte encoded characters we're going to be
8204 making a read call for every character beyond
8205 the original read size.
8206
8207 So instead, read the rest of the character if
8208 any, and enough bytes to match at least the
8209 start bytes for each character we're going to
8210 read.
8211 */
8212 if (charstart)
8213 readsize = recsize - charcount;
8214 else
8215 readsize = skip - (bend - bufp) + recsize - charcount - 1;
8216 buffer = SvGROW(sv, append + bytesread + readsize + 1) + append;
8217 bend = buffer + bytesread;
8218 morebytesread = PerlIO_read(fp, bend, readsize);
8219 if (morebytesread <= 0) {
8220 /* we're done, if we still have incomplete
8221 characters the check code in sv_gets() will
8222 warn about them.
8223
8224 I'd originally considered doing
8225 PerlIO_ungetc() on all but the lead
8226 character of the incomplete character, but
8227 read() doesn't do that, so I don't.
8228 */
8229 break;
8230 }
8231
8232 /* prepare to scan some more */
8233 bytesread += morebytesread;
8234 bend = buffer + bytesread;
8235 bufp = buffer + bufp_offset;
8236 }
8237 }
8238 }
8239 }
8240
8241 if (bytesread < 0)
8242 bytesread = 0;
8243 SvCUR_set(sv, bytesread + append);
8244 buffer[bytesread] = '\0';
8245 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8246 }
8247
8248 /*
8249 =for apidoc sv_gets
8250
8251 Get a line from the filehandle and store it into the SV, optionally
8252 appending to the currently-stored string. If C<append> is not 0, the
8253 line is appended to the SV instead of overwriting it. C<append> should
8254 be set to the byte offset that the appended string should start at
8255 in the SV (typically, C<SvCUR(sv)> is a suitable choice).
8256
8257 =cut
8258 */
8259
8260 char *
8261 Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8262 {
8263 const char *rsptr;
8264 STRLEN rslen;
8265 STDCHAR rslast;
8266 STDCHAR *bp;
8267 SSize_t cnt;
8268 int i = 0;
8269 int rspara = 0;
8270
8271 PERL_ARGS_ASSERT_SV_GETS;
8272
8273 if (SvTHINKFIRST(sv))
8274 sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
8275 /* XXX. If you make this PVIV, then copy on write can copy scalars read
8276 from <>.
8277 However, perlbench says it's slower, because the existing swipe code
8278 is faster than copy on write.
8279 Swings and roundabouts. */
8280 SvUPGRADE(sv, SVt_PV);
8281
8282 if (append) {
8283 /* line is going to be appended to the existing buffer in the sv */
8284 if (PerlIO_isutf8(fp)) {
8285 if (!SvUTF8(sv)) {
8286 sv_utf8_upgrade_nomg(sv);
8287 sv_pos_u2b(sv,&append,0);
8288 }
8289 } else if (SvUTF8(sv)) {
8290 return S_sv_gets_append_to_utf8(aTHX_ sv, fp, append);
8291 }
8292 }
8293
8294 SvPOK_only(sv);
8295 if (!append) {
8296 /* not appending - "clear" the string by setting SvCUR to 0,
8297 * the pv is still avaiable. */
8298 SvCUR_set(sv,0);
8299 }
8300 if (PerlIO_isutf8(fp))
8301 SvUTF8_on(sv);
8302
8303 if (IN_PERL_COMPILETIME) {
8304 /* we always read code in line mode */
8305 rsptr = "\n";
8306 rslen = 1;
8307 }
8308 else if (RsSNARF(PL_rs)) {
8309 /* If it is a regular disk file use size from stat() as estimate
8310 of amount we are going to read -- may result in mallocing
8311 more memory than we really need if the layers below reduce
8312 the size we read (e.g. CRLF or a gzip layer).
8313 */
8314 Stat_t st;
8315 if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode)) {
8316 const Off_t offset = PerlIO_tell(fp);
8317 if (offset != (Off_t) -1 && st.st_size + append > offset) {
8318 #ifdef PERL_NEW_COPY_ON_WRITE
8319 /* Add an extra byte for the sake of copy-on-write's
8320 * buffer reference count. */
8321 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 2));
8322 #else
8323 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
8324 #endif
8325 }
8326 }
8327 rsptr = NULL;
8328 rslen = 0;
8329 }
8330 else if (RsRECORD(PL_rs)) {
8331 return S_sv_gets_read_record(aTHX_ sv, fp, append);
8332 }
8333 else if (RsPARA(PL_rs)) {
8334 rsptr = "\n\n";
8335 rslen = 2;
8336 rspara = 1;
8337 }
8338 else {
8339 /* Get $/ i.e. PL_rs into same encoding as stream wants */
8340 if (PerlIO_isutf8(fp)) {
8341 rsptr = SvPVutf8(PL_rs, rslen);
8342 }
8343 else {
8344 if (SvUTF8(PL_rs)) {
8345 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
8346 Perl_croak(aTHX_ "Wide character in $/");
8347 }
8348 }
8349 /* extract the raw pointer to the record separator */
8350 rsptr = SvPV_const(PL_rs, rslen);
8351 }
8352 }
8353
8354 /* rslast is the last character in the record separator
8355 * note we don't use rslast except when rslen is true, so the
8356 * null assign is a placeholder. */
8357 rslast = rslen ? rsptr[rslen - 1] : '\0';
8358
8359 if (rspara) { /* have to do this both before and after */
8360 do { /* to make sure file boundaries work right */
8361 if (PerlIO_eof(fp))
8362 return 0;
8363 i = PerlIO_getc(fp);
8364 if (i != '\n') {
8365 if (i == -1)
8366 return 0;
8367 PerlIO_ungetc(fp,i);
8368 break;
8369 }
8370 } while (i != EOF);
8371 }
8372
8373 /* See if we know enough about I/O mechanism to cheat it ! */
8374
8375 /* This used to be #ifdef test - it is made run-time test for ease
8376 of abstracting out stdio interface. One call should be cheap
8377 enough here - and may even be a macro allowing compile
8378 time optimization.
8379 */
8380
8381 if (PerlIO_fast_gets(fp)) {
8382 /*
8383 * We can do buffer based IO operations on this filehandle.
8384 *
8385 * This means we can bypass a lot of subcalls and process
8386 * the buffer directly, it also means we know the upper bound
8387 * on the amount of data we might read of the current buffer
8388 * into our sv. Knowing this allows us to preallocate the pv
8389 * to be able to hold that maximum, which allows us to simplify
8390 * a lot of logic. */
8391
8392 /*
8393 * We're going to steal some values from the stdio struct
8394 * and put EVERYTHING in the innermost loop into registers.
8395 */
8396 STDCHAR *ptr; /* pointer into fp's read-ahead buffer */
8397 STRLEN bpx; /* length of the data in the target sv
8398 used to fix pointers after a SvGROW */
8399 I32 shortbuffered; /* If the pv buffer is shorter than the amount
8400 of data left in the read-ahead buffer.
8401 If 0 then the pv buffer can hold the full
8402 amount left, otherwise this is the amount it
8403 can hold. */
8404
8405 #if defined(__VMS) && defined(PERLIO_IS_STDIO)
8406 /* An ungetc()d char is handled separately from the regular
8407 * buffer, so we getc() it back out and stuff it in the buffer.
8408 */
8409 i = PerlIO_getc(fp);
8410 if (i == EOF) return 0;
8411 *(--((*fp)->_ptr)) = (unsigned char) i;
8412 (*fp)->_cnt++;
8413 #endif
8414
8415 /* Here is some breathtakingly efficient cheating */
8416
8417 /* When you read the following logic resist the urge to think
8418 * of record separators that are 1 byte long. They are an
8419 * uninteresting special (simple) case.
8420 *
8421 * Instead think of record separators which are at least 2 bytes
8422 * long, and keep in mind that we need to deal with such
8423 * separators when they cross a read-ahead buffer boundary.
8424 *
8425 * Also consider that we need to gracefully deal with separators
8426 * that may be longer than a single read ahead buffer.
8427 *
8428 * Lastly do not forget we want to copy the delimiter as well. We
8429 * are copying all data in the file _up_to_and_including_ the separator
8430 * itself.
8431 *
8432 * Now that you have all that in mind here is what is happening below:
8433 *
8434 * 1. When we first enter the loop we do some memory book keeping to see
8435 * how much free space there is in the target SV. (This sub assumes that
8436 * it is operating on the same SV most of the time via $_ and that it is
8437 * going to be able to reuse the same pv buffer each call.) If there is
8438 * "enough" room then we set "shortbuffered" to how much space there is
8439 * and start reading forward.
8440 *
8441 * 2. When we scan forward we copy from the read-ahead buffer to the target
8442 * SV's pv buffer. While we go we watch for the end of the read-ahead buffer,
8443 * and the end of the of pv, as well as for the "rslast", which is the last
8444 * char of the separator.
8445 *
8446 * 3. When scanning forward if we see rslast then we jump backwards in *pv*
8447 * (which has a "complete" record up to the point we saw rslast) and check
8448 * it to see if it matches the separator. If it does we are done. If it doesn't
8449 * we continue on with the scan/copy.
8450 *
8451 * 4. If we run out of read-ahead buffer (cnt goes to 0) then we have to get
8452 * the IO system to read the next buffer. We do this by doing a getc(), which
8453 * returns a single char read (or EOF), and prefills the buffer, and also
8454 * allows us to find out how full the buffer is. We use this information to
8455 * SvGROW() the sv to the size remaining in the buffer, after which we copy
8456 * the returned single char into the target sv, and then go back into scan
8457 * forward mode.
8458 *
8459 * 5. If we run out of write-buffer then we SvGROW() it by the size of the
8460 * remaining space in the read-buffer.
8461 *
8462 * Note that this code despite its twisty-turny nature is pretty darn slick.
8463 * It manages single byte separators, multi-byte cross boundary separators,
8464 * and cross-read-buffer separators cleanly and efficiently at the cost
8465 * of potentially greatly overallocating the target SV.
8466 *
8467 * Yves
8468 */
8469
8470
8471 /* get the number of bytes remaining in the read-ahead buffer
8472 * on first call on a given fp this will return 0.*/
8473 cnt = PerlIO_get_cnt(fp);
8474
8475 /* make sure we have the room */
8476 if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
8477 /* Not room for all of it
8478 if we are looking for a separator and room for some
8479 */
8480 if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
8481 /* just process what we have room for */
8482 shortbuffered = cnt - SvLEN(sv) + append + 1;
8483 cnt -= shortbuffered;
8484 }
8485 else {
8486 /* ensure that the target sv has enough room to hold
8487 * the rest of the read-ahead buffer */
8488 shortbuffered = 0;
8489 /* remember that cnt can be negative */
8490 SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
8491 }
8492 }
8493 else {
8494 /* we have enough room to hold the full buffer, lets scream */
8495 shortbuffered = 0;
8496 }
8497
8498 /* extract the pointer to sv's string buffer, offset by append as necessary */
8499 bp = (STDCHAR*)SvPVX_const(sv) + append; /* move these two too to registers */
8500 /* extract the point to the read-ahead buffer */
8501 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
8502
8503 /* some trace debug output */
8504 DEBUG_P(PerlIO_printf(Perl_debug_log,
8505 "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
8506 DEBUG_P(PerlIO_printf(Perl_debug_log,
8507 "Screamer: entering: PerlIO * thinks ptr=%"UVuf", cnt=%"IVdf", base=%"
8508 UVuf"\n",
8509 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8510 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
8511
8512 for (;;) {
8513 screamer:
8514 /* if there is stuff left in the read-ahead buffer */
8515 if (cnt > 0) {
8516 /* if there is a separator */
8517 if (rslen) {
8518 /* loop until we hit the end of the read-ahead buffer */
8519 while (cnt > 0) { /* this | eat */
8520 /* scan forward copying and searching for rslast as we go */
8521 cnt--;
8522 if ((*bp++ = *ptr++) == rslast) /* really | dust */
8523 goto thats_all_folks; /* screams | sed :-) */
8524 }
8525 }
8526 else {
8527 /* no separator, slurp the full buffer */
8528 Copy(ptr, bp, cnt, char); /* this | eat */
8529 bp += cnt; /* screams | dust */
8530 ptr += cnt; /* louder | sed :-) */
8531 cnt = 0;
8532 assert (!shortbuffered);
8533 goto cannot_be_shortbuffered;
8534 }
8535 }
8536
8537 if (shortbuffered) { /* oh well, must extend */
8538 /* we didnt have enough room to fit the line into the target buffer
8539 * so we must extend the target buffer and keep going */
8540 cnt = shortbuffered;
8541 shortbuffered = 0;
8542 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
8543 SvCUR_set(sv, bpx);
8544 /* extned the target sv's buffer so it can hold the full read-ahead buffer */
8545 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
8546 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
8547 continue;
8548 }
8549
8550 cannot_be_shortbuffered:
8551 /* we need to refill the read-ahead buffer if possible */
8552
8553 DEBUG_P(PerlIO_printf(Perl_debug_log,
8554 "Screamer: going to getc, ptr=%"UVuf", cnt=%"IVdf"\n",
8555 PTR2UV(ptr),(IV)cnt));
8556 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
8557
8558 DEBUG_Pv(PerlIO_printf(Perl_debug_log,
8559 "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%"IVdf", base=%"UVuf"\n",
8560 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8561 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
8562
8563 /*
8564 call PerlIO_getc() to let it prefill the lookahead buffer
8565
8566 This used to call 'filbuf' in stdio form, but as that behaves like
8567 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
8568 another abstraction.
8569
8570 Note we have to deal with the char in 'i' if we are not at EOF
8571 */
8572 i = PerlIO_getc(fp); /* get more characters */
8573
8574 DEBUG_Pv(PerlIO_printf(Perl_debug_log,
8575 "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%"IVdf", base=%"UVuf"\n",
8576 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8577 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
8578
8579 /* find out how much is left in the read-ahead buffer, and rextract its pointer */
8580 cnt = PerlIO_get_cnt(fp);
8581 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
8582 DEBUG_P(PerlIO_printf(Perl_debug_log,
8583 "Screamer: after getc, ptr=%"UVuf", cnt=%"IVdf"\n",
8584 PTR2UV(ptr),(IV)cnt));
8585
8586 if (i == EOF) /* all done for ever? */
8587 goto thats_really_all_folks;
8588
8589 /* make sure we have enough space in the target sv */
8590 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
8591 SvCUR_set(sv, bpx);
8592 SvGROW(sv, bpx + cnt + 2);
8593 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
8594
8595 /* copy of the char we got from getc() */
8596 *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */
8597
8598 /* make sure we deal with the i being the last character of a separator */
8599 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
8600 goto thats_all_folks;
8601 }
8602
8603 thats_all_folks:
8604 /* check if we have actually found the separator - only really applies
8605 * when rslen > 1 */
8606 if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX_const(sv)) < rslen) ||
8607 memNE((char*)bp - rslen, rsptr, rslen))
8608 goto screamer; /* go back to the fray */
8609 thats_really_all_folks:
8610 if (shortbuffered)
8611 cnt += shortbuffered;
8612 DEBUG_P(PerlIO_printf(Perl_debug_log,
8613 "Screamer: quitting, ptr=%"UVuf", cnt=%"IVdf"\n",PTR2UV(ptr),(IV)cnt));
8614 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */
8615 DEBUG_P(PerlIO_printf(Perl_debug_log,
8616 "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%"IVdf", base=%"UVuf
8617 "\n",
8618 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8619 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
8620 *bp = '\0';
8621 SvCUR_set(sv, bp - (STDCHAR*)SvPVX_const(sv)); /* set length */
8622 DEBUG_P(PerlIO_printf(Perl_debug_log,
8623 "Screamer: done, len=%ld, string=|%.*s|\n",
8624 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX_const(sv)));
8625 }
8626 else
8627 {
8628 /*The big, slow, and stupid way. */
8629 #ifdef USE_HEAP_INSTEAD_OF_STACK /* Even slower way. */
8630 STDCHAR *buf = NULL;
8631 Newx(buf, 8192, STDCHAR);
8632 assert(buf);
8633 #else
8634 STDCHAR buf[8192];
8635 #endif
8636
8637 screamer2:
8638 if (rslen) {
8639 const STDCHAR * const bpe = buf + sizeof(buf);
8640 bp = buf;
8641 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
8642 ; /* keep reading */
8643 cnt = bp - buf;
8644 }
8645 else {
8646 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
8647 /* Accommodate broken VAXC compiler, which applies U8 cast to
8648 * both args of ?: operator, causing EOF to change into 255
8649 */
8650 if (cnt > 0)
8651 i = (U8)buf[cnt - 1];
8652 else
8653 i = EOF;
8654 }
8655
8656 if (cnt < 0)
8657 cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
8658 if (append)
8659 sv_catpvn_nomg(sv, (char *) buf, cnt);
8660 else
8661 sv_setpvn(sv, (char *) buf, cnt); /* "nomg" is implied */
8662
8663 if (i != EOF && /* joy */
8664 (!rslen ||
8665 SvCUR(sv) < rslen ||
8666 memNE(SvPVX_const(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
8667 {
8668 append = -1;
8669 /*
8670 * If we're reading from a TTY and we get a short read,
8671 * indicating that the user hit his EOF character, we need
8672 * to notice it now, because if we try to read from the TTY
8673 * again, the EOF condition will disappear.
8674 *
8675 * The comparison of cnt to sizeof(buf) is an optimization
8676 * that prevents unnecessary calls to feof().
8677 *
8678 * - jik 9/25/96
8679 */
8680 if (!(cnt < (I32)sizeof(buf) && PerlIO_eof(fp)))
8681 goto screamer2;
8682 }
8683
8684 #ifdef USE_HEAP_INSTEAD_OF_STACK
8685 Safefree(buf);
8686 #endif
8687 }
8688
8689 if (rspara) { /* have to do this both before and after */
8690 while (i != EOF) { /* to make sure file boundaries work right */
8691 i = PerlIO_getc(fp);
8692 if (i != '\n') {
8693 PerlIO_ungetc(fp,i);
8694 break;
8695 }
8696 }
8697 }
8698
8699 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8700 }
8701
8702 /*
8703 =for apidoc sv_inc
8704
8705 Auto-increment of the value in the SV, doing string to numeric conversion
8706 if necessary. Handles 'get' magic and operator overloading.
8707
8708 =cut
8709 */
8710
8711 void
8712 Perl_sv_inc(pTHX_ SV *const sv)
8713 {
8714 if (!sv)
8715 return;
8716 SvGETMAGIC(sv);
8717 sv_inc_nomg(sv);
8718 }
8719
8720 /*
8721 =for apidoc sv_inc_nomg
8722
8723 Auto-increment of the value in the SV, doing string to numeric conversion
8724 if necessary. Handles operator overloading. Skips handling 'get' magic.
8725
8726 =cut
8727 */
8728
8729 void
8730 Perl_sv_inc_nomg(pTHX_ SV *const sv)
8731 {
8732 char *d;
8733 int flags;
8734
8735 if (!sv)
8736 return;
8737 if (SvTHINKFIRST(sv)) {
8738 if (SvREADONLY(sv)) {
8739 Perl_croak_no_modify();
8740 }
8741 if (SvROK(sv)) {
8742 IV i;
8743 if (SvAMAGIC(sv) && AMG_CALLunary(sv, inc_amg))
8744 return;
8745 i = PTR2IV(SvRV(sv));
8746 sv_unref(sv);
8747 sv_setiv(sv, i);
8748 }
8749 else sv_force_normal_flags(sv, 0);
8750 }
8751 flags = SvFLAGS(sv);
8752 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
8753 /* It's (privately or publicly) a float, but not tested as an
8754 integer, so test it to see. */
8755 (void) SvIV(sv);
8756 flags = SvFLAGS(sv);
8757 }
8758 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
8759 /* It's publicly an integer, or privately an integer-not-float */
8760 #ifdef PERL_PRESERVE_IVUV
8761 oops_its_int:
8762 #endif
8763 if (SvIsUV(sv)) {
8764 if (SvUVX(sv) == UV_MAX)
8765 sv_setnv(sv, UV_MAX_P1);
8766 else
8767 (void)SvIOK_only_UV(sv);
8768 SvUV_set(sv, SvUVX(sv) + 1);
8769 } else {
8770 if (SvIVX(sv) == IV_MAX)
8771 sv_setuv(sv, (UV)IV_MAX + 1);
8772 else {
8773 (void)SvIOK_only(sv);
8774 SvIV_set(sv, SvIVX(sv) + 1);
8775 }
8776 }
8777 return;
8778 }
8779 if (flags & SVp_NOK) {
8780 const NV was = SvNVX(sv);
8781 if (LIKELY(!Perl_isinfnan(was)) &&
8782 NV_OVERFLOWS_INTEGERS_AT &&
8783 was >= NV_OVERFLOWS_INTEGERS_AT) {
8784 /* diag_listed_as: Lost precision when %s %f by 1 */
8785 Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION),
8786 "Lost precision when incrementing %" NVff " by 1",
8787 was);
8788 }
8789 (void)SvNOK_only(sv);
8790 SvNV_set(sv, was + 1.0);
8791 return;
8792 }
8793
8794 if (!(flags & SVp_POK) || !*SvPVX_const(sv)) {
8795 if ((flags & SVTYPEMASK) < SVt_PVIV)
8796 sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV ? SVt_PVIV : SVt_IV));
8797 (void)SvIOK_only(sv);
8798 SvIV_set(sv, 1);
8799 return;
8800 }
8801 d = SvPVX(sv);
8802 while (isALPHA(*d)) d++;
8803 while (isDIGIT(*d)) d++;
8804 if (d < SvEND(sv)) {
8805 const int numtype = grok_number_flags(SvPVX_const(sv), SvCUR(sv), NULL, PERL_SCAN_TRAILING);
8806 #ifdef PERL_PRESERVE_IVUV
8807 /* Got to punt this as an integer if needs be, but we don't issue
8808 warnings. Probably ought to make the sv_iv_please() that does
8809 the conversion if possible, and silently. */
8810 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
8811 /* Need to try really hard to see if it's an integer.
8812 9.22337203685478e+18 is an integer.
8813 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
8814 so $a="9.22337203685478e+18"; $a+0; $a++
8815 needs to be the same as $a="9.22337203685478e+18"; $a++
8816 or we go insane. */
8817
8818 (void) sv_2iv(sv);
8819 if (SvIOK(sv))
8820 goto oops_its_int;
8821
8822 /* sv_2iv *should* have made this an NV */
8823 if (flags & SVp_NOK) {
8824 (void)SvNOK_only(sv);
8825 SvNV_set(sv, SvNVX(sv) + 1.0);
8826 return;
8827 }
8828 /* I don't think we can get here. Maybe I should assert this
8829 And if we do get here I suspect that sv_setnv will croak. NWC
8830 Fall through. */
8831 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
8832 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
8833 }
8834 #endif /* PERL_PRESERVE_IVUV */
8835 if (!numtype && ckWARN(WARN_NUMERIC))
8836 not_incrementable(sv);
8837 sv_setnv(sv,Atof(SvPVX_const(sv)) + 1.0);
8838 return;
8839 }
8840 d--;
8841 while (d >= SvPVX_const(sv)) {
8842 if (isDIGIT(*d)) {
8843 if (++*d <= '9')
8844 return;
8845 *(d--) = '0';
8846 }
8847 else {
8848 #ifdef EBCDIC
8849 /* MKS: The original code here died if letters weren't consecutive.
8850 * at least it didn't have to worry about non-C locales. The
8851 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
8852 * arranged in order (although not consecutively) and that only
8853 * [A-Za-z] are accepted by isALPHA in the C locale.
8854 */
8855 if (isALPHA_FOLD_NE(*d, 'z')) {
8856 do { ++*d; } while (!isALPHA(*d));
8857 return;
8858 }
8859 *(d--) -= 'z' - 'a';
8860 #else
8861 ++*d;
8862 if (isALPHA(*d))
8863 return;
8864 *(d--) -= 'z' - 'a' + 1;
8865 #endif
8866 }
8867 }
8868 /* oh,oh, the number grew */
8869 SvGROW(sv, SvCUR(sv) + 2);
8870 SvCUR_set(sv, SvCUR(sv) + 1);
8871 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX_const(sv); d--)
8872 *d = d[-1];
8873 if (isDIGIT(d[1]))
8874 *d = '1';
8875 else
8876 *d = d[1];
8877 }
8878
8879 /*
8880 =for apidoc sv_dec
8881
8882 Auto-decrement of the value in the SV, doing string to numeric conversion
8883 if necessary. Handles 'get' magic and operator overloading.
8884
8885 =cut
8886 */
8887
8888 void
8889 Perl_sv_dec(pTHX_ SV *const sv)
8890 {
8891 if (!sv)
8892 return;
8893 SvGETMAGIC(sv);
8894 sv_dec_nomg(sv);
8895 }
8896
8897 /*
8898 =for apidoc sv_dec_nomg
8899
8900 Auto-decrement of the value in the SV, doing string to numeric conversion
8901 if necessary. Handles operator overloading. Skips handling 'get' magic.
8902
8903 =cut
8904 */
8905
8906 void
8907 Perl_sv_dec_nomg(pTHX_ SV *const sv)
8908 {
8909 int flags;
8910
8911 if (!sv)
8912 return;
8913 if (SvTHINKFIRST(sv)) {
8914 if (SvREADONLY(sv)) {
8915 Perl_croak_no_modify();
8916 }
8917 if (SvROK(sv)) {
8918 IV i;
8919 if (SvAMAGIC(sv) && AMG_CALLunary(sv, dec_amg))
8920 return;
8921 i = PTR2IV(SvRV(sv));
8922 sv_unref(sv);
8923 sv_setiv(sv, i);
8924 }
8925 else sv_force_normal_flags(sv, 0);
8926 }
8927 /* Unlike sv_inc we don't have to worry about string-never-numbers
8928 and keeping them magic. But we mustn't warn on punting */
8929 flags = SvFLAGS(sv);
8930 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
8931 /* It's publicly an integer, or privately an integer-not-float */
8932 #ifdef PERL_PRESERVE_IVUV
8933 oops_its_int:
8934 #endif
8935 if (SvIsUV(sv)) {
8936 if (SvUVX(sv) == 0) {
8937 (void)SvIOK_only(sv);
8938 SvIV_set(sv, -1);
8939 }
8940 else {
8941 (void)SvIOK_only_UV(sv);
8942 SvUV_set(sv, SvUVX(sv) - 1);
8943 }
8944 } else {
8945 if (SvIVX(sv) == IV_MIN) {
8946 sv_setnv(sv, (NV)IV_MIN);
8947 goto oops_its_num;
8948 }
8949 else {
8950 (void)SvIOK_only(sv);
8951 SvIV_set(sv, SvIVX(sv) - 1);
8952 }
8953 }
8954 return;
8955 }
8956 if (flags & SVp_NOK) {
8957 oops_its_num:
8958 {
8959 const NV was = SvNVX(sv);
8960 if (LIKELY(!Perl_isinfnan(was)) &&
8961 NV_OVERFLOWS_INTEGERS_AT &&
8962 was <= -NV_OVERFLOWS_INTEGERS_AT) {
8963 /* diag_listed_as: Lost precision when %s %f by 1 */
8964 Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION),
8965 "Lost precision when decrementing %" NVff " by 1",
8966 was);
8967 }
8968 (void)SvNOK_only(sv);
8969 SvNV_set(sv, was - 1.0);
8970 return;
8971 }
8972 }
8973 if (!(flags & SVp_POK)) {
8974 if ((flags & SVTYPEMASK) < SVt_PVIV)
8975 sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV) ? SVt_PVIV : SVt_IV);
8976 SvIV_set(sv, -1);
8977 (void)SvIOK_only(sv);
8978 return;
8979 }
8980 #ifdef PERL_PRESERVE_IVUV
8981 {
8982 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
8983 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
8984 /* Need to try really hard to see if it's an integer.
8985 9.22337203685478e+18 is an integer.
8986 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
8987 so $a="9.22337203685478e+18"; $a+0; $a--
8988 needs to be the same as $a="9.22337203685478e+18"; $a--
8989 or we go insane. */
8990
8991 (void) sv_2iv(sv);
8992 if (SvIOK(sv))
8993 goto oops_its_int;
8994
8995 /* sv_2iv *should* have made this an NV */
8996 if (flags & SVp_NOK) {
8997 (void)SvNOK_only(sv);
8998 SvNV_set(sv, SvNVX(sv) - 1.0);
8999 return;
9000 }
9001 /* I don't think we can get here. Maybe I should assert this
9002 And if we do get here I suspect that sv_setnv will croak. NWC
9003 Fall through. */
9004 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
9005 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
9006 }
9007 }
9008 #endif /* PERL_PRESERVE_IVUV */
9009 sv_setnv(sv,Atof(SvPVX_const(sv)) - 1.0); /* punt */
9010 }
9011
9012 /* this define is used to eliminate a chunk of duplicated but shared logic
9013 * it has the suffix __SV_C to signal that it isnt API, and isnt meant to be
9014 * used anywhere but here - yves
9015 */
9016 #define PUSH_EXTEND_MORTAL__SV_C(AnSv) \
9017 STMT_START { \
9018 SSize_t ix = ++PL_tmps_ix; \
9019 if (UNLIKELY(ix >= PL_tmps_max)) \
9020 ix = tmps_grow_p(ix); \
9021 PL_tmps_stack[ix] = (AnSv); \
9022 } STMT_END
9023
9024 /*
9025 =for apidoc sv_mortalcopy
9026
9027 Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
9028 The new SV is marked as mortal. It will be destroyed "soon", either by an
9029 explicit call to FREETMPS, or by an implicit call at places such as
9030 statement boundaries. See also C<sv_newmortal> and C<sv_2mortal>.
9031
9032 =cut
9033 */
9034
9035 /* Make a string that will exist for the duration of the expression
9036 * evaluation. Actually, it may have to last longer than that, but
9037 * hopefully we won't free it until it has been assigned to a
9038 * permanent location. */
9039
9040 SV *
9041 Perl_sv_mortalcopy_flags(pTHX_ SV *const oldstr, U32 flags)
9042 {
9043 SV *sv;
9044
9045 if (flags & SV_GMAGIC)
9046 SvGETMAGIC(oldstr); /* before new_SV, in case it dies */
9047 new_SV(sv);
9048 sv_setsv_flags(sv,oldstr,flags & ~SV_GMAGIC);
9049 PUSH_EXTEND_MORTAL__SV_C(sv);
9050 SvTEMP_on(sv);
9051 return sv;
9052 }
9053
9054 /*
9055 =for apidoc sv_newmortal
9056
9057 Creates a new null SV which is mortal. The reference count of the SV is
9058 set to 1. It will be destroyed "soon", either by an explicit call to
9059 FREETMPS, or by an implicit call at places such as statement boundaries.
9060 See also C<sv_mortalcopy> and C<sv_2mortal>.
9061
9062 =cut
9063 */
9064
9065 SV *
9066 Perl_sv_newmortal(pTHX)
9067 {
9068 SV *sv;
9069
9070 new_SV(sv);
9071 SvFLAGS(sv) = SVs_TEMP;
9072 PUSH_EXTEND_MORTAL__SV_C(sv);
9073 return sv;
9074 }
9075
9076
9077 /*
9078 =for apidoc newSVpvn_flags
9079
9080 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
9081 characters) into it. The reference count for the
9082 SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
9083 string. You are responsible for ensuring that the source string is at least
9084 C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
9085 Currently the only flag bits accepted are C<SVf_UTF8> and C<SVs_TEMP>.
9086 If C<SVs_TEMP> is set, then C<sv_2mortal()> is called on the result before
9087 returning. If C<SVf_UTF8> is set, C<s>
9088 is considered to be in UTF-8 and the
9089 C<SVf_UTF8> flag will be set on the new SV.
9090 C<newSVpvn_utf8()> is a convenience wrapper for this function, defined as
9091
9092 #define newSVpvn_utf8(s, len, u) \
9093 newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
9094
9095 =cut
9096 */
9097
9098 SV *
9099 Perl_newSVpvn_flags(pTHX_ const char *const s, const STRLEN len, const U32 flags)
9100 {
9101 SV *sv;
9102
9103 /* All the flags we don't support must be zero.
9104 And we're new code so I'm going to assert this from the start. */
9105 assert(!(flags & ~(SVf_UTF8|SVs_TEMP)));
9106 new_SV(sv);
9107 sv_setpvn(sv,s,len);
9108
9109 /* This code used to do a sv_2mortal(), however we now unroll the call to
9110 * sv_2mortal() and do what it does ourselves here. Since we have asserted
9111 * that flags can only have the SVf_UTF8 and/or SVs_TEMP flags set above we
9112 * can use it to enable the sv flags directly (bypassing SvTEMP_on), which
9113 * in turn means we dont need to mask out the SVf_UTF8 flag below, which
9114 * means that we eliminate quite a few steps than it looks - Yves
9115 * (explaining patch by gfx) */
9116
9117 SvFLAGS(sv) |= flags;
9118
9119 if(flags & SVs_TEMP){
9120 PUSH_EXTEND_MORTAL__SV_C(sv);
9121 }
9122
9123 return sv;
9124 }
9125
9126 /*
9127 =for apidoc sv_2mortal
9128
9129 Marks an existing SV as mortal. The SV will be destroyed "soon", either
9130 by an explicit call to FREETMPS, or by an implicit call at places such as
9131 statement boundaries. SvTEMP() is turned on which means that the SV's
9132 string buffer can be "stolen" if this SV is copied. See also C<sv_newmortal>
9133 and C<sv_mortalcopy>.
9134
9135 =cut
9136 */
9137
9138 SV *
9139 Perl_sv_2mortal(pTHX_ SV *const sv)
9140 {
9141 dVAR;
9142 if (!sv)
9143 return sv;
9144 if (SvIMMORTAL(sv))
9145 return sv;
9146 PUSH_EXTEND_MORTAL__SV_C(sv);
9147 SvTEMP_on(sv);
9148 return sv;
9149 }
9150
9151 /*
9152 =for apidoc newSVpv
9153
9154 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
9155 characters) into it. The reference count for the
9156 SV is set to 1. If C<len> is zero, Perl will compute the length using
9157 strlen(), (which means if you use this option, that C<s> can't have embedded
9158 C<NUL> characters and has to have a terminating C<NUL> byte).
9159
9160 For efficiency, consider using C<newSVpvn> instead.
9161
9162 =cut
9163 */
9164
9165 SV *
9166 Perl_newSVpv(pTHX_ const char *const s, const STRLEN len)
9167 {
9168 SV *sv;
9169
9170 new_SV(sv);
9171 sv_setpvn(sv, s, len || s == NULL ? len : strlen(s));
9172 return sv;
9173 }
9174
9175 /*
9176 =for apidoc newSVpvn
9177
9178 Creates a new SV and copies a string into it, which may contain C<NUL> characters
9179 (C<\0>) and other binary data. The reference count for the SV is set to 1.
9180 Note that if C<len> is zero, Perl will create a zero length (Perl) string. You
9181 are responsible for ensuring that the source buffer is at least
9182 C<len> bytes long. If the C<buffer> argument is NULL the new SV will be
9183 undefined.
9184
9185 =cut
9186 */
9187
9188 SV *
9189 Perl_newSVpvn(pTHX_ const char *const buffer, const STRLEN len)
9190 {
9191 SV *sv;
9192 new_SV(sv);
9193 sv_setpvn(sv,buffer,len);
9194 return sv;
9195 }
9196
9197 /*
9198 =for apidoc newSVhek
9199
9200 Creates a new SV from the hash key structure. It will generate scalars that
9201 point to the shared string table where possible. Returns a new (undefined)
9202 SV if the hek is NULL.
9203
9204 =cut
9205 */
9206
9207 SV *
9208 Perl_newSVhek(pTHX_ const HEK *const hek)
9209 {
9210 if (!hek) {
9211 SV *sv;
9212
9213 new_SV(sv);
9214 return sv;
9215 }
9216
9217 if (HEK_LEN(hek) == HEf_SVKEY) {
9218 return newSVsv(*(SV**)HEK_KEY(hek));
9219 } else {
9220 const int flags = HEK_FLAGS(hek);
9221 if (flags & HVhek_WASUTF8) {
9222 /* Trouble :-)
9223 Andreas would like keys he put in as utf8 to come back as utf8
9224 */
9225 STRLEN utf8_len = HEK_LEN(hek);
9226 SV * const sv = newSV_type(SVt_PV);
9227 char *as_utf8 = (char *)bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
9228 /* bytes_to_utf8() allocates a new string, which we can repurpose: */
9229 sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL);
9230 SvUTF8_on (sv);
9231 return sv;
9232 } else if (flags & HVhek_UNSHARED) {
9233 /* A hash that isn't using shared hash keys has to have
9234 the flag in every key so that we know not to try to call
9235 share_hek_hek on it. */
9236
9237 SV * const sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
9238 if (HEK_UTF8(hek))
9239 SvUTF8_on (sv);
9240 return sv;
9241 }
9242 /* This will be overwhelminly the most common case. */
9243 {
9244 /* Inline most of newSVpvn_share(), because share_hek_hek() is far
9245 more efficient than sharepvn(). */
9246 SV *sv;
9247
9248 new_SV(sv);
9249 sv_upgrade(sv, SVt_PV);
9250 SvPV_set(sv, (char *)HEK_KEY(share_hek_hek(hek)));
9251 SvCUR_set(sv, HEK_LEN(hek));
9252 SvLEN_set(sv, 0);
9253 SvIsCOW_on(sv);
9254 SvPOK_on(sv);
9255 if (HEK_UTF8(hek))
9256 SvUTF8_on(sv);
9257 return sv;
9258 }
9259 }
9260 }
9261
9262 /*
9263 =for apidoc newSVpvn_share
9264
9265 Creates a new SV with its SvPVX_const pointing to a shared string in the string
9266 table. If the string does not already exist in the table, it is
9267 created first. Turns on the SvIsCOW flag (or READONLY
9268 and FAKE in 5.16 and earlier). If the C<hash> parameter
9269 is non-zero, that value is used; otherwise the hash is computed.
9270 The string's hash can later be retrieved from the SV
9271 with the C<SvSHARED_HASH()> macro. The idea here is
9272 that as the string table is used for shared hash keys these strings will have
9273 SvPVX_const == HeKEY and hash lookup will avoid string compare.
9274
9275 =cut
9276 */
9277
9278 SV *
9279 Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
9280 {
9281 dVAR;
9282 SV *sv;
9283 bool is_utf8 = FALSE;
9284 const char *const orig_src = src;
9285
9286 if (len < 0) {
9287 STRLEN tmplen = -len;
9288 is_utf8 = TRUE;
9289 /* See the note in hv.c:hv_fetch() --jhi */
9290 src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
9291 len = tmplen;
9292 }
9293 if (!hash)
9294 PERL_HASH(hash, src, len);
9295 new_SV(sv);
9296 /* The logic for this is inlined in S_mro_get_linear_isa_dfs(), so if it
9297 changes here, update it there too. */
9298 sv_upgrade(sv, SVt_PV);
9299 SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
9300 SvCUR_set(sv, len);
9301 SvLEN_set(sv, 0);
9302 SvIsCOW_on(sv);
9303 SvPOK_on(sv);
9304 if (is_utf8)
9305 SvUTF8_on(sv);
9306 if (src != orig_src)
9307 Safefree(src);
9308 return sv;
9309 }
9310
9311 /*
9312 =for apidoc newSVpv_share
9313
9314 Like C<newSVpvn_share>, but takes a C<NUL>-terminated string instead of a
9315 string/length pair.
9316
9317 =cut
9318 */
9319
9320 SV *
9321 Perl_newSVpv_share(pTHX_ const char *src, U32 hash)
9322 {
9323 return newSVpvn_share(src, strlen(src), hash);
9324 }
9325
9326 #if defined(PERL_IMPLICIT_CONTEXT)
9327
9328 /* pTHX_ magic can't cope with varargs, so this is a no-context
9329 * version of the main function, (which may itself be aliased to us).
9330 * Don't access this version directly.
9331 */
9332
9333 SV *
9334 Perl_newSVpvf_nocontext(const char *const pat, ...)
9335 {
9336 dTHX;
9337 SV *sv;
9338 va_list args;
9339
9340 PERL_ARGS_ASSERT_NEWSVPVF_NOCONTEXT;
9341
9342 va_start(args, pat);
9343 sv = vnewSVpvf(pat, &args);
9344 va_end(args);
9345 return sv;
9346 }
9347 #endif
9348
9349 /*
9350 =for apidoc newSVpvf
9351
9352 Creates a new SV and initializes it with the string formatted like
9353 C<sprintf>.
9354
9355 =cut
9356 */
9357
9358 SV *
9359 Perl_newSVpvf(pTHX_ const char *const pat, ...)
9360 {
9361 SV *sv;
9362 va_list args;
9363
9364 PERL_ARGS_ASSERT_NEWSVPVF;
9365
9366 va_start(args, pat);
9367 sv = vnewSVpvf(pat, &args);
9368 va_end(args);
9369 return sv;
9370 }
9371
9372 /* backend for newSVpvf() and newSVpvf_nocontext() */
9373
9374 SV *
9375 Perl_vnewSVpvf(pTHX_ const char *const pat, va_list *const args)
9376 {
9377 SV *sv;
9378
9379 PERL_ARGS_ASSERT_VNEWSVPVF;
9380
9381 new_SV(sv);
9382 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
9383 return sv;
9384 }
9385
9386 /*
9387 =for apidoc newSVnv
9388
9389 Creates a new SV and copies a floating point value into it.
9390 The reference count for the SV is set to 1.
9391
9392 =cut
9393 */
9394
9395 SV *
9396 Perl_newSVnv(pTHX_ const NV n)
9397 {
9398 SV *sv;
9399
9400 new_SV(sv);
9401 sv_setnv(sv,n);
9402 return sv;
9403 }
9404
9405 /*
9406 =for apidoc newSViv
9407
9408 Creates a new SV and copies an integer into it. The reference count for the
9409 SV is set to 1.
9410
9411 =cut
9412 */
9413
9414 SV *
9415 Perl_newSViv(pTHX_ const IV i)
9416 {
9417 SV *sv;
9418
9419 new_SV(sv);
9420
9421 /* Inlining ONLY the small relevant subset of sv_setiv here
9422 * for performance. Makes a significant difference. */
9423
9424 /* We're starting from SVt_FIRST, so provided that's
9425 * actual 0, we don't have to unset any SV type flags
9426 * to promote to SVt_IV. */
9427 STATIC_ASSERT_STMT(SVt_FIRST == 0);
9428
9429 SET_SVANY_FOR_BODYLESS_IV(sv);
9430 SvFLAGS(sv) |= SVt_IV;
9431 (void)SvIOK_on(sv);
9432
9433 SvIV_set(sv, i);
9434 SvTAINT(sv);
9435
9436 return sv;
9437 }
9438
9439 /*
9440 =for apidoc newSVuv
9441
9442 Creates a new SV and copies an unsigned integer into it.
9443 The reference count for the SV is set to 1.
9444
9445 =cut
9446 */
9447
9448 SV *
9449 Perl_newSVuv(pTHX_ const UV u)
9450 {
9451 SV *sv;
9452
9453 /* Inlining ONLY the small relevant subset of sv_setuv here
9454 * for performance. Makes a significant difference. */
9455
9456 /* Using ivs is more efficient than using uvs - see sv_setuv */
9457 if (u <= (UV)IV_MAX) {
9458 return newSViv((IV)u);
9459 }
9460
9461 new_SV(sv);
9462
9463 /* We're starting from SVt_FIRST, so provided that's
9464 * actual 0, we don't have to unset any SV type flags
9465 * to promote to SVt_IV. */
9466 STATIC_ASSERT_STMT(SVt_FIRST == 0);
9467
9468 SET_SVANY_FOR_BODYLESS_IV(sv);
9469 SvFLAGS(sv) |= SVt_IV;
9470 (void)SvIOK_on(sv);
9471 (void)SvIsUV_on(sv);
9472
9473 SvUV_set(sv, u);
9474 SvTAINT(sv);
9475
9476 return sv;
9477 }
9478
9479 /*
9480 =for apidoc newSV_type
9481
9482 Creates a new SV, of the type specified. The reference count for the new SV
9483 is set to 1.
9484
9485 =cut
9486 */
9487
9488 SV *
9489 Perl_newSV_type(pTHX_ const svtype type)
9490 {
9491 SV *sv;
9492
9493 new_SV(sv);
9494 ASSUME(SvTYPE(sv) == SVt_FIRST);
9495 if(type != SVt_FIRST)
9496 sv_upgrade(sv, type);
9497 return sv;
9498 }
9499
9500 /*
9501 =for apidoc newRV_noinc
9502
9503 Creates an RV wrapper for an SV. The reference count for the original
9504 SV is B<not> incremented.
9505
9506 =cut
9507 */
9508
9509 SV *
9510 Perl_newRV_noinc(pTHX_ SV *const tmpRef)
9511 {
9512 SV *sv;
9513
9514 PERL_ARGS_ASSERT_NEWRV_NOINC;
9515
9516 new_SV(sv);
9517
9518 /* We're starting from SVt_FIRST, so provided that's
9519 * actual 0, we don't have to unset any SV type flags
9520 * to promote to SVt_IV. */
9521 STATIC_ASSERT_STMT(SVt_FIRST == 0);
9522
9523 SET_SVANY_FOR_BODYLESS_IV(sv);
9524 SvFLAGS(sv) |= SVt_IV;
9525 SvROK_on(sv);
9526 SvIV_set(sv, 0);
9527
9528 SvTEMP_off(tmpRef);
9529 SvRV_set(sv, tmpRef);
9530
9531 return sv;
9532 }
9533
9534 /* newRV_inc is the official function name to use now.
9535 * newRV_inc is in fact #defined to newRV in sv.h
9536 */
9537
9538 SV *
9539 Perl_newRV(pTHX_ SV *const sv)
9540 {
9541 PERL_ARGS_ASSERT_NEWRV;
9542
9543 return newRV_noinc(SvREFCNT_inc_simple_NN(sv));
9544 }
9545
9546 /*
9547 =for apidoc newSVsv
9548
9549 Creates a new SV which is an exact duplicate of the original SV.
9550 (Uses C<sv_setsv>.)
9551
9552 =cut
9553 */
9554
9555 SV *
9556 Perl_newSVsv(pTHX_ SV *const old)
9557 {
9558 SV *sv;
9559
9560 if (!old)
9561 return NULL;
9562 if (SvTYPE(old) == (svtype)SVTYPEMASK) {
9563 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
9564 return NULL;
9565 }
9566 /* Do this here, otherwise we leak the new SV if this croaks. */
9567 SvGETMAGIC(old);
9568 new_SV(sv);
9569 /* SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games
9570 with SvTEMP_off and SvTEMP_on round a call to sv_setsv. */
9571 sv_setsv_flags(sv, old, SV_NOSTEAL);
9572 return sv;
9573 }
9574
9575 /*
9576 =for apidoc sv_reset
9577
9578 Underlying implementation for the C<reset> Perl function.
9579 Note that the perl-level function is vaguely deprecated.
9580
9581 =cut
9582 */
9583
9584 void
9585 Perl_sv_reset(pTHX_ const char *s, HV *const stash)
9586 {
9587 PERL_ARGS_ASSERT_SV_RESET;
9588
9589 sv_resetpvn(*s ? s : NULL, strlen(s), stash);
9590 }
9591
9592 void
9593 Perl_sv_resetpvn(pTHX_ const char *s, STRLEN len, HV * const stash)
9594 {
9595 char todo[PERL_UCHAR_MAX+1];
9596 const char *send;
9597
9598 if (!stash || SvTYPE(stash) != SVt_PVHV)
9599 return;
9600
9601 if (!s) { /* reset ?? searches */
9602 MAGIC * const mg = mg_find((const SV *)stash, PERL_MAGIC_symtab);
9603 if (mg) {
9604 const U32 count = mg->mg_len / sizeof(PMOP**);
9605 PMOP **pmp = (PMOP**) mg->mg_ptr;
9606 PMOP *const *const end = pmp + count;
9607
9608 while (pmp < end) {
9609 #ifdef USE_ITHREADS
9610 SvREADONLY_off(PL_regex_pad[(*pmp)->op_pmoffset]);
9611 #else
9612 (*pmp)->op_pmflags &= ~PMf_USED;
9613 #endif
9614 ++pmp;
9615 }
9616 }
9617 return;
9618 }
9619
9620 /* reset variables */
9621
9622 if (!HvARRAY(stash))
9623 return;
9624
9625 Zero(todo, 256, char);
9626 send = s + len;
9627 while (s < send) {
9628 I32 max;
9629 I32 i = (unsigned char)*s;
9630 if (s[1] == '-') {
9631 s += 2;
9632 }
9633 max = (unsigned char)*s++;
9634 for ( ; i <= max; i++) {
9635 todo[i] = 1;
9636 }
9637 for (i = 0; i <= (I32) HvMAX(stash); i++) {
9638 HE *entry;
9639 for (entry = HvARRAY(stash)[i];
9640 entry;
9641 entry = HeNEXT(entry))
9642 {
9643 GV *gv;
9644 SV *sv;
9645
9646 if (!todo[(U8)*HeKEY(entry)])
9647 continue;
9648 gv = MUTABLE_GV(HeVAL(entry));
9649 sv = GvSV(gv);
9650 if (sv && !SvREADONLY(sv)) {
9651 SV_CHECK_THINKFIRST_COW_DROP(sv);
9652 if (!isGV(sv)) SvOK_off(sv);
9653 }
9654 if (GvAV(gv)) {
9655 av_clear(GvAV(gv));
9656 }
9657 if (GvHV(gv) && !HvNAME_get(GvHV(gv))) {
9658 hv_clear(GvHV(gv));
9659 }
9660 }
9661 }
9662 }
9663 }
9664
9665 /*
9666 =for apidoc sv_2io
9667
9668 Using various gambits, try to get an IO from an SV: the IO slot if its a
9669 GV; or the recursive result if we're an RV; or the IO slot of the symbol
9670 named after the PV if we're a string.
9671
9672 'Get' magic is ignored on the sv passed in, but will be called on
9673 C<SvRV(sv)> if sv is an RV.
9674
9675 =cut
9676 */
9677
9678 IO*
9679 Perl_sv_2io(pTHX_ SV *const sv)
9680 {
9681 IO* io;
9682 GV* gv;
9683
9684 PERL_ARGS_ASSERT_SV_2IO;
9685
9686 switch (SvTYPE(sv)) {
9687 case SVt_PVIO:
9688 io = MUTABLE_IO(sv);
9689 break;
9690 case SVt_PVGV:
9691 case SVt_PVLV:
9692 if (isGV_with_GP(sv)) {
9693 gv = MUTABLE_GV(sv);
9694 io = GvIO(gv);
9695 if (!io)
9696 Perl_croak(aTHX_ "Bad filehandle: %"HEKf,
9697 HEKfARG(GvNAME_HEK(gv)));
9698 break;
9699 }
9700 /* FALLTHROUGH */
9701 default:
9702 if (!SvOK(sv))
9703 Perl_croak(aTHX_ PL_no_usym, "filehandle");
9704 if (SvROK(sv)) {
9705 SvGETMAGIC(SvRV(sv));
9706 return sv_2io(SvRV(sv));
9707 }
9708 gv = gv_fetchsv_nomg(sv, 0, SVt_PVIO);
9709 if (gv)
9710 io = GvIO(gv);
9711 else
9712 io = 0;
9713 if (!io) {
9714 SV *newsv = sv;
9715 if (SvGMAGICAL(sv)) {
9716 newsv = sv_newmortal();
9717 sv_setsv_nomg(newsv, sv);
9718 }
9719 Perl_croak(aTHX_ "Bad filehandle: %"SVf, SVfARG(newsv));
9720 }
9721 break;
9722 }
9723 return io;
9724 }
9725
9726 /*
9727 =for apidoc sv_2cv
9728
9729 Using various gambits, try to get a CV from an SV; in addition, try if
9730 possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
9731 The flags in C<lref> are passed to gv_fetchsv.
9732
9733 =cut
9734 */
9735
9736 CV *
9737 Perl_sv_2cv(pTHX_ SV *sv, HV **const st, GV **const gvp, const I32 lref)
9738 {
9739 GV *gv = NULL;
9740 CV *cv = NULL;
9741
9742 PERL_ARGS_ASSERT_SV_2CV;
9743
9744 if (!sv) {
9745 *st = NULL;
9746 *gvp = NULL;
9747 return NULL;
9748 }
9749 switch (SvTYPE(sv)) {
9750 case SVt_PVCV:
9751 *st = CvSTASH(sv);
9752 *gvp = NULL;
9753 return MUTABLE_CV(sv);
9754 case SVt_PVHV:
9755 case SVt_PVAV:
9756 *st = NULL;
9757 *gvp = NULL;
9758 return NULL;
9759 default:
9760 SvGETMAGIC(sv);
9761 if (SvROK(sv)) {
9762 if (SvAMAGIC(sv))
9763 sv = amagic_deref_call(sv, to_cv_amg);
9764
9765 sv = SvRV(sv);
9766 if (SvTYPE(sv) == SVt_PVCV) {
9767 cv = MUTABLE_CV(sv);
9768 *gvp = NULL;
9769 *st = CvSTASH(cv);
9770 return cv;
9771 }
9772 else if(SvGETMAGIC(sv), isGV_with_GP(sv))
9773 gv = MUTABLE_GV(sv);
9774 else
9775 Perl_croak(aTHX_ "Not a subroutine reference");
9776 }
9777 else if (isGV_with_GP(sv)) {
9778 gv = MUTABLE_GV(sv);
9779 }
9780 else {
9781 gv = gv_fetchsv_nomg(sv, lref, SVt_PVCV);
9782 }
9783 *gvp = gv;
9784 if (!gv) {
9785 *st = NULL;
9786 return NULL;
9787 }
9788 /* Some flags to gv_fetchsv mean don't really create the GV */
9789 if (!isGV_with_GP(gv)) {
9790 *st = NULL;
9791 return NULL;
9792 }
9793 *st = GvESTASH(gv);
9794 if (lref & ~GV_ADDMG && !GvCVu(gv)) {
9795 /* XXX this is probably not what they think they're getting.
9796 * It has the same effect as "sub name;", i.e. just a forward
9797 * declaration! */
9798 newSTUB(gv,0);
9799 }
9800 return GvCVu(gv);
9801 }
9802 }
9803
9804 /*
9805 =for apidoc sv_true
9806
9807 Returns true if the SV has a true value by Perl's rules.
9808 Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
9809 instead use an in-line version.
9810
9811 =cut
9812 */
9813
9814 I32
9815 Perl_sv_true(pTHX_ SV *const sv)
9816 {
9817 if (!sv)
9818 return 0;
9819 if (SvPOK(sv)) {
9820 const XPV* const tXpv = (XPV*)SvANY(sv);
9821 if (tXpv &&
9822 (tXpv->xpv_cur > 1 ||
9823 (tXpv->xpv_cur && *sv->sv_u.svu_pv != '0')))
9824 return 1;
9825 else
9826 return 0;
9827 }
9828 else {
9829 if (SvIOK(sv))
9830 return SvIVX(sv) != 0;
9831 else {
9832 if (SvNOK(sv))
9833 return SvNVX(sv) != 0.0;
9834 else
9835 return sv_2bool(sv);
9836 }
9837 }
9838 }
9839
9840 /*
9841 =for apidoc sv_pvn_force
9842
9843 Get a sensible string out of the SV somehow.
9844 A private implementation of the C<SvPV_force> macro for compilers which
9845 can't cope with complex macro expressions. Always use the macro instead.
9846
9847 =for apidoc sv_pvn_force_flags
9848
9849 Get a sensible string out of the SV somehow.
9850 If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if
9851 appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
9852 implemented in terms of this function.
9853 You normally want to use the various wrapper macros instead: see
9854 C<SvPV_force> and C<SvPV_force_nomg>
9855
9856 =cut
9857 */
9858
9859 char *
9860 Perl_sv_pvn_force_flags(pTHX_ SV *const sv, STRLEN *const lp, const I32 flags)
9861 {
9862 PERL_ARGS_ASSERT_SV_PVN_FORCE_FLAGS;
9863
9864 if (flags & SV_GMAGIC) SvGETMAGIC(sv);
9865 if (SvTHINKFIRST(sv) && (!SvROK(sv) || SvREADONLY(sv)))
9866 sv_force_normal_flags(sv, 0);
9867
9868 if (SvPOK(sv)) {
9869 if (lp)
9870 *lp = SvCUR(sv);
9871 }
9872 else {
9873 char *s;
9874 STRLEN len;
9875
9876 if (SvTYPE(sv) > SVt_PVLV
9877 || isGV_with_GP(sv))
9878 /* diag_listed_as: Can't coerce %s to %s in %s */
9879 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
9880 OP_DESC(PL_op));
9881 s = sv_2pv_flags(sv, &len, flags &~ SV_GMAGIC);
9882 if (!s) {
9883 s = (char *)"";
9884 }
9885 if (lp)
9886 *lp = len;
9887
9888 if (SvTYPE(sv) < SVt_PV ||
9889 s != SvPVX_const(sv)) { /* Almost, but not quite, sv_setpvn() */
9890 if (SvROK(sv))
9891 sv_unref(sv);
9892 SvUPGRADE(sv, SVt_PV); /* Never FALSE */
9893 SvGROW(sv, len + 1);
9894 Move(s,SvPVX(sv),len,char);
9895 SvCUR_set(sv, len);
9896 SvPVX(sv)[len] = '\0';
9897 }
9898 if (!SvPOK(sv)) {
9899 SvPOK_on(sv); /* validate pointer */
9900 SvTAINT(sv);
9901 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
9902 PTR2UV(sv),SvPVX_const(sv)));
9903 }
9904 }
9905 (void)SvPOK_only_UTF8(sv);
9906 return SvPVX_mutable(sv);
9907 }
9908
9909 /*
9910 =for apidoc sv_pvbyten_force
9911
9912 The backend for the C<SvPVbytex_force> macro. Always use the macro
9913 instead.
9914
9915 =cut
9916 */
9917
9918 char *
9919 Perl_sv_pvbyten_force(pTHX_ SV *const sv, STRLEN *const lp)
9920 {
9921 PERL_ARGS_ASSERT_SV_PVBYTEN_FORCE;
9922
9923 sv_pvn_force(sv,lp);
9924 sv_utf8_downgrade(sv,0);
9925 *lp = SvCUR(sv);
9926 return SvPVX(sv);
9927 }
9928
9929 /*
9930 =for apidoc sv_pvutf8n_force
9931
9932 The backend for the C<SvPVutf8x_force> macro. Always use the macro
9933 instead.
9934
9935 =cut
9936 */
9937
9938 char *
9939 Perl_sv_pvutf8n_force(pTHX_ SV *const sv, STRLEN *const lp)
9940 {
9941 PERL_ARGS_ASSERT_SV_PVUTF8N_FORCE;
9942
9943 sv_pvn_force(sv,0);
9944 sv_utf8_upgrade_nomg(sv);
9945 *lp = SvCUR(sv);
9946 return SvPVX(sv);
9947 }
9948
9949 /*
9950 =for apidoc sv_reftype
9951
9952 Returns a string describing what the SV is a reference to.
9953
9954 =cut
9955 */
9956
9957 const char *
9958 Perl_sv_reftype(pTHX_ const SV *const sv, const int ob)
9959 {
9960 PERL_ARGS_ASSERT_SV_REFTYPE;
9961 if (ob && SvOBJECT(sv)) {
9962 return SvPV_nolen_const(sv_ref(NULL, sv, ob));
9963 }
9964 else {
9965 /* WARNING - There is code, for instance in mg.c, that assumes that
9966 * the only reason that sv_reftype(sv,0) would return a string starting
9967 * with 'L' or 'S' is that it is a LVALUE or a SCALAR.
9968 * Yes this a dodgy way to do type checking, but it saves practically reimplementing
9969 * this routine inside other subs, and it saves time.
9970 * Do not change this assumption without searching for "dodgy type check" in
9971 * the code.
9972 * - Yves */
9973 switch (SvTYPE(sv)) {
9974 case SVt_NULL:
9975 case SVt_IV:
9976 case SVt_NV:
9977 case SVt_PV:
9978 case SVt_PVIV:
9979 case SVt_PVNV:
9980 case SVt_PVMG:
9981 if (SvVOK(sv))
9982 return "VSTRING";
9983 if (SvROK(sv))
9984 return "REF";
9985 else
9986 return "SCALAR";
9987
9988 case SVt_PVLV: return (char *) (SvROK(sv) ? "REF"
9989 /* tied lvalues should appear to be
9990 * scalars for backwards compatibility */
9991 : (isALPHA_FOLD_EQ(LvTYPE(sv), 't'))
9992 ? "SCALAR" : "LVALUE");
9993 case SVt_PVAV: return "ARRAY";
9994 case SVt_PVHV: return "HASH";
9995 case SVt_PVCV: return "CODE";
9996 case SVt_PVGV: return (char *) (isGV_with_GP(sv)
9997 ? "GLOB" : "SCALAR");
9998 case SVt_PVFM: return "FORMAT";
9999 case SVt_PVIO: return "IO";
10000 case SVt_INVLIST: return "INVLIST";
10001 case SVt_REGEXP: return "REGEXP";
10002 default: return "UNKNOWN";
10003 }
10004 }
10005 }
10006
10007 /*
10008 =for apidoc sv_ref
10009
10010 Returns a SV describing what the SV passed in is a reference to.
10011
10012 =cut
10013 */
10014
10015 SV *
10016 Perl_sv_ref(pTHX_ SV *dst, const SV *const sv, const int ob)
10017 {
10018 PERL_ARGS_ASSERT_SV_REF;
10019
10020 if (!dst)
10021 dst = sv_newmortal();
10022
10023 if (ob && SvOBJECT(sv)) {
10024 HvNAME_get(SvSTASH(sv))
10025 ? sv_sethek(dst, HvNAME_HEK(SvSTASH(sv)))
10026 : sv_setpvn(dst, "__ANON__", 8);
10027 }
10028 else {
10029 const char * reftype = sv_reftype(sv, 0);
10030 sv_setpv(dst, reftype);
10031 }
10032 return dst;
10033 }
10034
10035 /*
10036 =for apidoc sv_isobject
10037
10038 Returns a boolean indicating whether the SV is an RV pointing to a blessed
10039 object. If the SV is not an RV, or if the object is not blessed, then this
10040 will return false.
10041
10042 =cut
10043 */
10044
10045 int
10046 Perl_sv_isobject(pTHX_ SV *sv)
10047 {
10048 if (!sv)
10049 return 0;
10050 SvGETMAGIC(sv);
10051 if (!SvROK(sv))
10052 return 0;
10053 sv = SvRV(sv);
10054 if (!SvOBJECT(sv))
10055 return 0;
10056 return 1;
10057 }
10058
10059 /*
10060 =for apidoc sv_isa
10061
10062 Returns a boolean indicating whether the SV is blessed into the specified
10063 class. This does not check for subtypes; use C<sv_derived_from> to verify
10064 an inheritance relationship.
10065
10066 =cut
10067 */
10068
10069 int
10070 Perl_sv_isa(pTHX_ SV *sv, const char *const name)
10071 {
10072 const char *hvname;
10073
10074 PERL_ARGS_ASSERT_SV_ISA;
10075
10076 if (!sv)
10077 return 0;
10078 SvGETMAGIC(sv);
10079 if (!SvROK(sv))
10080 return 0;
10081 sv = SvRV(sv);
10082 if (!SvOBJECT(sv))
10083 return 0;
10084 hvname = HvNAME_get(SvSTASH(sv));
10085 if (!hvname)
10086 return 0;
10087
10088 return strEQ(hvname, name);
10089 }
10090
10091 /*
10092 =for apidoc newSVrv
10093
10094 Creates a new SV for the existing RV, C<rv>, to point to. If C<rv> is not an
10095 RV then it will be upgraded to one. If C<classname> is non-null then the new
10096 SV will be blessed in the specified package. The new SV is returned and its
10097 reference count is 1. The reference count 1 is owned by C<rv>.
10098
10099 =cut
10100 */
10101
10102 SV*
10103 Perl_newSVrv(pTHX_ SV *const rv, const char *const classname)
10104 {
10105 SV *sv;
10106
10107 PERL_ARGS_ASSERT_NEWSVRV;
10108
10109 new_SV(sv);
10110
10111 SV_CHECK_THINKFIRST_COW_DROP(rv);
10112
10113 if (UNLIKELY( SvTYPE(rv) >= SVt_PVMG )) {
10114 const U32 refcnt = SvREFCNT(rv);
10115 SvREFCNT(rv) = 0;
10116 sv_clear(rv);
10117 SvFLAGS(rv) = 0;
10118 SvREFCNT(rv) = refcnt;
10119
10120 sv_upgrade(rv, SVt_IV);
10121 } else if (SvROK(rv)) {
10122 SvREFCNT_dec(SvRV(rv));
10123 } else {
10124 prepare_SV_for_RV(rv);
10125 }
10126
10127 SvOK_off(rv);
10128 SvRV_set(rv, sv);
10129 SvROK_on(rv);
10130
10131 if (classname) {
10132 HV* const stash = gv_stashpv(classname, GV_ADD);
10133 (void)sv_bless(rv, stash);
10134 }
10135 return sv;
10136 }
10137
10138 SV *
10139 Perl_newSVavdefelem(pTHX_ AV *av, SSize_t ix, bool extendible)
10140 {
10141 SV * const lv = newSV_type(SVt_PVLV);
10142 PERL_ARGS_ASSERT_NEWSVAVDEFELEM;
10143 LvTYPE(lv) = 'y';
10144 sv_magic(lv, NULL, PERL_MAGIC_defelem, NULL, 0);
10145 LvTARG(lv) = SvREFCNT_inc_simple_NN(av);
10146 LvSTARGOFF(lv) = ix;
10147 LvTARGLEN(lv) = extendible ? 1 : (STRLEN)UV_MAX;
10148 return lv;
10149 }
10150
10151 /*
10152 =for apidoc sv_setref_pv
10153
10154 Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
10155 argument will be upgraded to an RV. That RV will be modified to point to
10156 the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed
10157 into the SV. The C<classname> argument indicates the package for the
10158 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10159 will have a reference count of 1, and the RV will be returned.
10160
10161 Do not use with other Perl types such as HV, AV, SV, CV, because those
10162 objects will become corrupted by the pointer copy process.
10163
10164 Note that C<sv_setref_pvn> copies the string while this copies the pointer.
10165
10166 =cut
10167 */
10168
10169 SV*
10170 Perl_sv_setref_pv(pTHX_ SV *const rv, const char *const classname, void *const pv)
10171 {
10172 PERL_ARGS_ASSERT_SV_SETREF_PV;
10173
10174 if (!pv) {
10175 sv_setsv(rv, &PL_sv_undef);
10176 SvSETMAGIC(rv);
10177 }
10178 else
10179 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
10180 return rv;
10181 }
10182
10183 /*
10184 =for apidoc sv_setref_iv
10185
10186 Copies an integer into a new SV, optionally blessing the SV. The C<rv>
10187 argument will be upgraded to an RV. That RV will be modified to point to
10188 the new SV. The C<classname> argument indicates the package for the
10189 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10190 will have a reference count of 1, and the RV will be returned.
10191
10192 =cut
10193 */
10194
10195 SV*
10196 Perl_sv_setref_iv(pTHX_ SV *const rv, const char *const classname, const IV iv)
10197 {
10198 PERL_ARGS_ASSERT_SV_SETREF_IV;
10199
10200 sv_setiv(newSVrv(rv,classname), iv);
10201 return rv;
10202 }
10203
10204 /*
10205 =for apidoc sv_setref_uv
10206
10207 Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
10208 argument will be upgraded to an RV. That RV will be modified to point to
10209 the new SV. The C<classname> argument indicates the package for the
10210 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10211 will have a reference count of 1, and the RV will be returned.
10212
10213 =cut
10214 */
10215
10216 SV*
10217 Perl_sv_setref_uv(pTHX_ SV *const rv, const char *const classname, const UV uv)
10218 {
10219 PERL_ARGS_ASSERT_SV_SETREF_UV;
10220
10221 sv_setuv(newSVrv(rv,classname), uv);
10222 return rv;
10223 }
10224
10225 /*
10226 =for apidoc sv_setref_nv
10227
10228 Copies a double into a new SV, optionally blessing the SV. The C<rv>
10229 argument will be upgraded to an RV. That RV will be modified to point to
10230 the new SV. The C<classname> argument indicates the package for the
10231 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10232 will have a reference count of 1, and the RV will be returned.
10233
10234 =cut
10235 */
10236
10237 SV*
10238 Perl_sv_setref_nv(pTHX_ SV *const rv, const char *const classname, const NV nv)
10239 {
10240 PERL_ARGS_ASSERT_SV_SETREF_NV;
10241
10242 sv_setnv(newSVrv(rv,classname), nv);
10243 return rv;
10244 }
10245
10246 /*
10247 =for apidoc sv_setref_pvn
10248
10249 Copies a string into a new SV, optionally blessing the SV. The length of the
10250 string must be specified with C<n>. The C<rv> argument will be upgraded to
10251 an RV. That RV will be modified to point to the new SV. The C<classname>
10252 argument indicates the package for the blessing. Set C<classname> to
10253 C<NULL> to avoid the blessing. The new SV will have a reference count
10254 of 1, and the RV will be returned.
10255
10256 Note that C<sv_setref_pv> copies the pointer while this copies the string.
10257
10258 =cut
10259 */
10260
10261 SV*
10262 Perl_sv_setref_pvn(pTHX_ SV *const rv, const char *const classname,
10263 const char *const pv, const STRLEN n)
10264 {
10265 PERL_ARGS_ASSERT_SV_SETREF_PVN;
10266
10267 sv_setpvn(newSVrv(rv,classname), pv, n);
10268 return rv;
10269 }
10270
10271 /*
10272 =for apidoc sv_bless
10273
10274 Blesses an SV into a specified package. The SV must be an RV. The package
10275 must be designated by its stash (see C<gv_stashpv()>). The reference count
10276 of the SV is unaffected.
10277
10278 =cut
10279 */
10280
10281 SV*
10282 Perl_sv_bless(pTHX_ SV *const sv, HV *const stash)
10283 {
10284 SV *tmpRef;
10285 HV *oldstash = NULL;
10286
10287 PERL_ARGS_ASSERT_SV_BLESS;
10288
10289 SvGETMAGIC(sv);
10290 if (!SvROK(sv))
10291 Perl_croak(aTHX_ "Can't bless non-reference value");
10292 tmpRef = SvRV(sv);
10293 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY|SVf_PROTECT)) {
10294 if (SvREADONLY(tmpRef))
10295 Perl_croak_no_modify();
10296 if (SvOBJECT(tmpRef)) {
10297 oldstash = SvSTASH(tmpRef);
10298 }
10299 }
10300 SvOBJECT_on(tmpRef);
10301 SvUPGRADE(tmpRef, SVt_PVMG);
10302 SvSTASH_set(tmpRef, MUTABLE_HV(SvREFCNT_inc_simple(stash)));
10303 SvREFCNT_dec(oldstash);
10304
10305 if(SvSMAGICAL(tmpRef))
10306 if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
10307 mg_set(tmpRef);
10308
10309
10310
10311 return sv;
10312 }
10313
10314 /* Downgrades a PVGV to a PVMG. If it's actually a PVLV, we leave the type
10315 * as it is after unglobbing it.
10316 */
10317
10318 PERL_STATIC_INLINE void
10319 S_sv_unglob(pTHX_ SV *const sv, U32 flags)
10320 {
10321 void *xpvmg;
10322 HV *stash;
10323 SV * const temp = flags & SV_COW_DROP_PV ? NULL : sv_newmortal();
10324
10325 PERL_ARGS_ASSERT_SV_UNGLOB;
10326
10327 assert(SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVLV);
10328 SvFAKE_off(sv);
10329 if (!(flags & SV_COW_DROP_PV))
10330 gv_efullname3(temp, MUTABLE_GV(sv), "*");
10331
10332 SvREFCNT_inc_simple_void_NN(sv_2mortal(sv));
10333 if (GvGP(sv)) {
10334 if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
10335 && HvNAME_get(stash))
10336 mro_method_changed_in(stash);
10337 gp_free(MUTABLE_GV(sv));
10338 }
10339 if (GvSTASH(sv)) {
10340 sv_del_backref(MUTABLE_SV(GvSTASH(sv)), sv);
10341 GvSTASH(sv) = NULL;
10342 }
10343 GvMULTI_off(sv);
10344 if (GvNAME_HEK(sv)) {
10345 unshare_hek(GvNAME_HEK(sv));
10346 }
10347 isGV_with_GP_off(sv);
10348
10349 if(SvTYPE(sv) == SVt_PVGV) {
10350 /* need to keep SvANY(sv) in the right arena */
10351 xpvmg = new_XPVMG();
10352 StructCopy(SvANY(sv), xpvmg, XPVMG);
10353 del_XPVGV(SvANY(sv));
10354 SvANY(sv) = xpvmg;
10355
10356 SvFLAGS(sv) &= ~SVTYPEMASK;
10357 SvFLAGS(sv) |= SVt_PVMG;
10358 }
10359
10360 /* Intentionally not calling any local SET magic, as this isn't so much a
10361 set operation as merely an internal storage change. */
10362 if (flags & SV_COW_DROP_PV) SvOK_off(sv);
10363 else sv_setsv_flags(sv, temp, 0);
10364
10365 if ((const GV *)sv == PL_last_in_gv)
10366 PL_last_in_gv = NULL;
10367 else if ((const GV *)sv == PL_statgv)
10368 PL_statgv = NULL;
10369 }
10370
10371 /*
10372 =for apidoc sv_unref_flags
10373
10374 Unsets the RV status of the SV, and decrements the reference count of
10375 whatever was being referenced by the RV. This can almost be thought of
10376 as a reversal of C<newSVrv>. The C<cflags> argument can contain
10377 C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
10378 (otherwise the decrementing is conditional on the reference count being
10379 different from one or the reference being a readonly SV).
10380 See C<SvROK_off>.
10381
10382 =cut
10383 */
10384
10385 void
10386 Perl_sv_unref_flags(pTHX_ SV *const ref, const U32 flags)
10387 {
10388 SV* const target = SvRV(ref);
10389
10390 PERL_ARGS_ASSERT_SV_UNREF_FLAGS;
10391
10392 if (SvWEAKREF(ref)) {
10393 sv_del_backref(target, ref);
10394 SvWEAKREF_off(ref);
10395 SvRV_set(ref, NULL);
10396 return;
10397 }
10398 SvRV_set(ref, NULL);
10399 SvROK_off(ref);
10400 /* You can't have a || SvREADONLY(target) here, as $a = $$a, where $a was
10401 assigned to as BEGIN {$a = \"Foo"} will fail. */
10402 if (SvREFCNT(target) != 1 || (flags & SV_IMMEDIATE_UNREF))
10403 SvREFCNT_dec_NN(target);
10404 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
10405 sv_2mortal(target); /* Schedule for freeing later */
10406 }
10407
10408 /*
10409 =for apidoc sv_untaint
10410
10411 Untaint an SV. Use C<SvTAINTED_off> instead.
10412
10413 =cut
10414 */
10415
10416 void
10417 Perl_sv_untaint(pTHX_ SV *const sv)
10418 {
10419 PERL_ARGS_ASSERT_SV_UNTAINT;
10420 PERL_UNUSED_CONTEXT;
10421
10422 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
10423 MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
10424 if (mg)
10425 mg->mg_len &= ~1;
10426 }
10427 }
10428
10429 /*
10430 =for apidoc sv_tainted
10431
10432 Test an SV for taintedness. Use C<SvTAINTED> instead.
10433
10434 =cut
10435 */
10436
10437 bool
10438 Perl_sv_tainted(pTHX_ SV *const sv)
10439 {
10440 PERL_ARGS_ASSERT_SV_TAINTED;
10441 PERL_UNUSED_CONTEXT;
10442
10443 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
10444 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
10445 if (mg && (mg->mg_len & 1) )
10446 return TRUE;
10447 }
10448 return FALSE;
10449 }
10450
10451 /*
10452 =for apidoc sv_setpviv
10453
10454 Copies an integer into the given SV, also updating its string value.
10455 Does not handle 'set' magic. See C<sv_setpviv_mg>.
10456
10457 =cut
10458 */
10459
10460 void
10461 Perl_sv_setpviv(pTHX_ SV *const sv, const IV iv)
10462 {
10463 char buf[TYPE_CHARS(UV)];
10464 char *ebuf;
10465 char * const ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
10466
10467 PERL_ARGS_ASSERT_SV_SETPVIV;
10468
10469 sv_setpvn(sv, ptr, ebuf - ptr);
10470 }
10471
10472 /*
10473 =for apidoc sv_setpviv_mg
10474
10475 Like C<sv_setpviv>, but also handles 'set' magic.
10476
10477 =cut
10478 */
10479
10480 void
10481 Perl_sv_setpviv_mg(pTHX_ SV *const sv, const IV iv)
10482 {
10483 PERL_ARGS_ASSERT_SV_SETPVIV_MG;
10484
10485 sv_setpviv(sv, iv);
10486 SvSETMAGIC(sv);
10487 }
10488
10489 #if defined(PERL_IMPLICIT_CONTEXT)
10490
10491 /* pTHX_ magic can't cope with varargs, so this is a no-context
10492 * version of the main function, (which may itself be aliased to us).
10493 * Don't access this version directly.
10494 */
10495
10496 void
10497 Perl_sv_setpvf_nocontext(SV *const sv, const char *const pat, ...)
10498 {
10499 dTHX;
10500 va_list args;
10501
10502 PERL_ARGS_ASSERT_SV_SETPVF_NOCONTEXT;
10503
10504 va_start(args, pat);
10505 sv_vsetpvf(sv, pat, &args);
10506 va_end(args);
10507 }
10508
10509 /* pTHX_ magic can't cope with varargs, so this is a no-context
10510 * version of the main function, (which may itself be aliased to us).
10511 * Don't access this version directly.
10512 */
10513
10514 void
10515 Perl_sv_setpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
10516 {
10517 dTHX;
10518 va_list args;
10519
10520 PERL_ARGS_ASSERT_SV_SETPVF_MG_NOCONTEXT;
10521
10522 va_start(args, pat);
10523 sv_vsetpvf_mg(sv, pat, &args);
10524 va_end(args);
10525 }
10526 #endif
10527
10528 /*
10529 =for apidoc sv_setpvf
10530
10531 Works like C<sv_catpvf> but copies the text into the SV instead of
10532 appending it. Does not handle 'set' magic. See C<sv_setpvf_mg>.
10533
10534 =cut
10535 */
10536
10537 void
10538 Perl_sv_setpvf(pTHX_ SV *const sv, const char *const pat, ...)
10539 {
10540 va_list args;
10541
10542 PERL_ARGS_ASSERT_SV_SETPVF;
10543
10544 va_start(args, pat);
10545 sv_vsetpvf(sv, pat, &args);
10546 va_end(args);
10547 }
10548
10549 /*
10550 =for apidoc sv_vsetpvf
10551
10552 Works like C<sv_vcatpvf> but copies the text into the SV instead of
10553 appending it. Does not handle 'set' magic. See C<sv_vsetpvf_mg>.
10554
10555 Usually used via its frontend C<sv_setpvf>.
10556
10557 =cut
10558 */
10559
10560 void
10561 Perl_sv_vsetpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
10562 {
10563 PERL_ARGS_ASSERT_SV_VSETPVF;
10564
10565 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
10566 }
10567
10568 /*
10569 =for apidoc sv_setpvf_mg
10570
10571 Like C<sv_setpvf>, but also handles 'set' magic.
10572
10573 =cut
10574 */
10575
10576 void
10577 Perl_sv_setpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
10578 {
10579 va_list args;
10580
10581 PERL_ARGS_ASSERT_SV_SETPVF_MG;
10582
10583 va_start(args, pat);
10584 sv_vsetpvf_mg(sv, pat, &args);
10585 va_end(args);
10586 }
10587
10588 /*
10589 =for apidoc sv_vsetpvf_mg
10590
10591 Like C<sv_vsetpvf>, but also handles 'set' magic.
10592
10593 Usually used via its frontend C<sv_setpvf_mg>.
10594
10595 =cut
10596 */
10597
10598 void
10599 Perl_sv_vsetpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
10600 {
10601 PERL_ARGS_ASSERT_SV_VSETPVF_MG;
10602
10603 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
10604 SvSETMAGIC(sv);
10605 }
10606
10607 #if defined(PERL_IMPLICIT_CONTEXT)
10608
10609 /* pTHX_ magic can't cope with varargs, so this is a no-context
10610 * version of the main function, (which may itself be aliased to us).
10611 * Don't access this version directly.
10612 */
10613
10614 void
10615 Perl_sv_catpvf_nocontext(SV *const sv, const char *const pat, ...)
10616 {
10617 dTHX;
10618 va_list args;
10619
10620 PERL_ARGS_ASSERT_SV_CATPVF_NOCONTEXT;
10621
10622 va_start(args, pat);
10623 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
10624 va_end(args);
10625 }
10626
10627 /* pTHX_ magic can't cope with varargs, so this is a no-context
10628 * version of the main function, (which may itself be aliased to us).
10629 * Don't access this version directly.
10630 */
10631
10632 void
10633 Perl_sv_catpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
10634 {
10635 dTHX;
10636 va_list args;
10637
10638 PERL_ARGS_ASSERT_SV_CATPVF_MG_NOCONTEXT;
10639
10640 va_start(args, pat);
10641 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
10642 SvSETMAGIC(sv);
10643 va_end(args);
10644 }
10645 #endif
10646
10647 /*
10648 =for apidoc sv_catpvf
10649
10650 Processes its arguments like C<sprintf> and appends the formatted
10651 output to an SV. If the appended data contains "wide" characters
10652 (including, but not limited to, SVs with a UTF-8 PV formatted with %s,
10653 and characters >255 formatted with %c), the original SV might get
10654 upgraded to UTF-8. Handles 'get' magic, but not 'set' magic. See
10655 C<sv_catpvf_mg>. If the original SV was UTF-8, the pattern should be
10656 valid UTF-8; if the original SV was bytes, the pattern should be too.
10657
10658 =cut */
10659
10660 void
10661 Perl_sv_catpvf(pTHX_ SV *const sv, const char *const pat, ...)
10662 {
10663 va_list args;
10664
10665 PERL_ARGS_ASSERT_SV_CATPVF;
10666
10667 va_start(args, pat);
10668 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
10669 va_end(args);
10670 }
10671
10672 /*
10673 =for apidoc sv_vcatpvf
10674
10675 Processes its arguments like C<vsprintf> and appends the formatted output
10676 to an SV. Does not handle 'set' magic. See C<sv_vcatpvf_mg>.
10677
10678 Usually used via its frontend C<sv_catpvf>.
10679
10680 =cut
10681 */
10682
10683 void
10684 Perl_sv_vcatpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
10685 {
10686 PERL_ARGS_ASSERT_SV_VCATPVF;
10687
10688 sv_vcatpvfn_flags(sv, pat, strlen(pat), args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
10689 }
10690
10691 /*
10692 =for apidoc sv_catpvf_mg
10693
10694 Like C<sv_catpvf>, but also handles 'set' magic.
10695
10696 =cut
10697 */
10698
10699 void
10700 Perl_sv_catpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
10701 {
10702 va_list args;
10703
10704 PERL_ARGS_ASSERT_SV_CATPVF_MG;
10705
10706 va_start(args, pat);
10707 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
10708 SvSETMAGIC(sv);
10709 va_end(args);
10710 }
10711
10712 /*
10713 =for apidoc sv_vcatpvf_mg
10714
10715 Like C<sv_vcatpvf>, but also handles 'set' magic.
10716
10717 Usually used via its frontend C<sv_catpvf_mg>.
10718
10719 =cut
10720 */
10721
10722 void
10723 Perl_sv_vcatpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
10724 {
10725 PERL_ARGS_ASSERT_SV_VCATPVF_MG;
10726
10727 sv_vcatpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
10728 SvSETMAGIC(sv);
10729 }
10730
10731 /*
10732 =for apidoc sv_vsetpvfn
10733
10734 Works like C<sv_vcatpvfn> but copies the text into the SV instead of
10735 appending it.
10736
10737 Usually used via one of its frontends C<sv_vsetpvf> and C<sv_vsetpvf_mg>.
10738
10739 =cut
10740 */
10741
10742 void
10743 Perl_sv_vsetpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
10744 va_list *const args, SV **const svargs, const I32 svmax, bool *const maybe_tainted)
10745 {
10746 PERL_ARGS_ASSERT_SV_VSETPVFN;
10747
10748 sv_setpvs(sv, "");
10749 sv_vcatpvfn_flags(sv, pat, patlen, args, svargs, svmax, maybe_tainted, 0);
10750 }
10751
10752
10753 /*
10754 * Warn of missing argument to sprintf, and then return a defined value
10755 * to avoid inappropriate "use of uninit" warnings [perl #71000].
10756 */
10757 STATIC SV*
10758 S_vcatpvfn_missing_argument(pTHX) {
10759 if (ckWARN(WARN_MISSING)) {
10760 Perl_warner(aTHX_ packWARN(WARN_MISSING), "Missing argument in %s",
10761 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
10762 }
10763 return &PL_sv_no;
10764 }
10765
10766
10767 STATIC I32
10768 S_expect_number(pTHX_ char **const pattern)
10769 {
10770 I32 var = 0;
10771
10772 PERL_ARGS_ASSERT_EXPECT_NUMBER;
10773
10774 switch (**pattern) {
10775 case '1': case '2': case '3':
10776 case '4': case '5': case '6':
10777 case '7': case '8': case '9':
10778 var = *(*pattern)++ - '0';
10779 while (isDIGIT(**pattern)) {
10780 const I32 tmp = var * 10 + (*(*pattern)++ - '0');
10781 if (tmp < var)
10782 Perl_croak(aTHX_ "Integer overflow in format string for %s", (PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn"));
10783 var = tmp;
10784 }
10785 }
10786 return var;
10787 }
10788
10789 STATIC char *
10790 S_F0convert(NV nv, char *const endbuf, STRLEN *const len)
10791 {
10792 const int neg = nv < 0;
10793 UV uv;
10794
10795 PERL_ARGS_ASSERT_F0CONVERT;
10796
10797 if (UNLIKELY(Perl_isinfnan(nv))) {
10798 STRLEN n = S_infnan_2pv(nv, endbuf - *len, *len, 0);
10799 *len = n;
10800 return endbuf - n;
10801 }
10802 if (neg)
10803 nv = -nv;
10804 if (nv < UV_MAX) {
10805 char *p = endbuf;
10806 nv += 0.5;
10807 uv = (UV)nv;
10808 if (uv & 1 && uv == nv)
10809 uv--; /* Round to even */
10810 do {
10811 const unsigned dig = uv % 10;
10812 *--p = '0' + dig;
10813 } while (uv /= 10);
10814 if (neg)
10815 *--p = '-';
10816 *len = endbuf - p;
10817 return p;
10818 }
10819 return NULL;
10820 }
10821
10822
10823 /*
10824 =for apidoc sv_vcatpvfn
10825
10826 =for apidoc sv_vcatpvfn_flags
10827
10828 Processes its arguments like C<vsprintf> and appends the formatted output
10829 to an SV. Uses an array of SVs if the C style variable argument list is
10830 missing (NULL). When running with taint checks enabled, indicates via
10831 C<maybe_tainted> if results are untrustworthy (often due to the use of
10832 locales).
10833
10834 If called as C<sv_vcatpvfn> or flags include C<SV_GMAGIC>, calls get magic.
10835
10836 Usually used via one of its frontends C<sv_vcatpvf> and C<sv_vcatpvf_mg>.
10837
10838 =cut
10839 */
10840
10841 #define VECTORIZE_ARGS vecsv = va_arg(*args, SV*);\
10842 vecstr = (U8*)SvPV_const(vecsv,veclen);\
10843 vec_utf8 = DO_UTF8(vecsv);
10844
10845 /* XXX maybe_tainted is never assigned to, so the doc above is lying. */
10846
10847 void
10848 Perl_sv_vcatpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
10849 va_list *const args, SV **const svargs, const I32 svmax, bool *const maybe_tainted)
10850 {
10851 PERL_ARGS_ASSERT_SV_VCATPVFN;
10852
10853 sv_vcatpvfn_flags(sv, pat, patlen, args, svargs, svmax, maybe_tainted, SV_GMAGIC|SV_SMAGIC);
10854 }
10855
10856 #ifdef LONGDOUBLE_DOUBLEDOUBLE
10857 /* The first double can be as large as 2**1023, or '1' x '0' x 1023.
10858 * The second double can be as small as 2**-1074, or '0' x 1073 . '1'.
10859 * The sum of them can be '1' . '0' x 2096 . '1', with implied radix point
10860 * after the first 1023 zero bits.
10861 *
10862 * XXX The 2098 is quite large (262.25 bytes) and therefore some sort
10863 * of dynamically growing buffer might be better, start at just 16 bytes
10864 * (for example) and grow only when necessary. Or maybe just by looking
10865 * at the exponents of the two doubles? */
10866 # define DOUBLEDOUBLE_MAXBITS 2098
10867 #endif
10868
10869 /* vhex will contain the values (0..15) of the hex digits ("nybbles"
10870 * of 4 bits); 1 for the implicit 1, and the mantissa bits, four bits
10871 * per xdigit. For the double-double case, this can be rather many.
10872 * The non-double-double-long-double overshoots since all bits of NV
10873 * are not mantissa bits, there are also exponent bits. */
10874 #ifdef LONGDOUBLE_DOUBLEDOUBLE
10875 # define VHEX_SIZE (3+DOUBLEDOUBLE_MAXBITS/4)
10876 #else
10877 # define VHEX_SIZE (1+(NVSIZE * 8)/4)
10878 #endif
10879
10880 /* If we do not have a known long double format, (including not using
10881 * long doubles, or long doubles being equal to doubles) then we will
10882 * fall back to the ldexp/frexp route, with which we can retrieve at
10883 * most as many bits as our widest unsigned integer type is. We try
10884 * to get a 64-bit unsigned integer even if we are not using a 64-bit UV.
10885 *
10886 * (If you want to test the case of UVSIZE == 4, NVSIZE == 8,
10887 * set the MANTISSATYPE to int and the MANTISSASIZE to 4.)
10888 */
10889 #if defined(HAS_QUAD) && defined(Uquad_t)
10890 # define MANTISSATYPE Uquad_t
10891 # define MANTISSASIZE 8
10892 #else
10893 # define MANTISSATYPE UV
10894 # define MANTISSASIZE UVSIZE
10895 #endif
10896
10897 #if defined(DOUBLE_LITTLE_ENDIAN) || defined(LONGDOUBLE_LITTLE_ENDIAN)
10898 # define HEXTRACT_LITTLE_ENDIAN
10899 #elif defined(DOUBLE_BIG_ENDIAN) || defined(LONGDOUBLE_BIG_ENDIAN)
10900 # define HEXTRACT_BIG_ENDIAN
10901 #else
10902 # define HEXTRACT_MIX_ENDIAN
10903 #endif
10904
10905 /* S_hextract() is a helper for Perl_sv_vcatpvfn_flags, for extracting
10906 * the hexadecimal values (for %a/%A). The nv is the NV where the value
10907 * are being extracted from (either directly from the long double in-memory
10908 * presentation, or from the uquad computed via frexp+ldexp). frexp also
10909 * is used to update the exponent. vhex is the pointer to the beginning
10910 * of the output buffer (of VHEX_SIZE).
10911 *
10912 * The tricky part is that S_hextract() needs to be called twice:
10913 * the first time with vend as NULL, and the second time with vend as
10914 * the pointer returned by the first call. What happens is that on
10915 * the first round the output size is computed, and the intended
10916 * extraction sanity checked. On the second round the actual output
10917 * (the extraction of the hexadecimal values) takes place.
10918 * Sanity failures cause fatal failures during both rounds. */
10919 STATIC U8*
10920 S_hextract(pTHX_ const NV nv, int* exponent, U8* vhex, U8* vend)
10921 {
10922 U8* v = vhex;
10923 int ix;
10924 int ixmin = 0, ixmax = 0;
10925
10926 /* XXX Inf/NaN/denormal handling in the HEXTRACT_IMPLICIT_BIT,
10927 * and elsewhere. */
10928
10929 /* These macros are just to reduce typos, they have multiple
10930 * repetitions below, but usually only one (or sometimes two)
10931 * of them is really being used. */
10932 /* HEXTRACT_OUTPUT() extracts the high nybble first. */
10933 #define HEXTRACT_OUTPUT_HI(ix) (*v++ = nvp[ix] >> 4)
10934 #define HEXTRACT_OUTPUT_LO(ix) (*v++ = nvp[ix] & 0xF)
10935 #define HEXTRACT_OUTPUT(ix) \
10936 STMT_START { \
10937 HEXTRACT_OUTPUT_HI(ix); HEXTRACT_OUTPUT_LO(ix); \
10938 } STMT_END
10939 #define HEXTRACT_COUNT(ix, c) \
10940 STMT_START { \
10941 v += c; if (ix < ixmin) ixmin = ix; else if (ix > ixmax) ixmax = ix; \
10942 } STMT_END
10943 #define HEXTRACT_BYTE(ix) \
10944 STMT_START { \
10945 if (vend) HEXTRACT_OUTPUT(ix); else HEXTRACT_COUNT(ix, 2); \
10946 } STMT_END
10947 #define HEXTRACT_LO_NYBBLE(ix) \
10948 STMT_START { \
10949 if (vend) HEXTRACT_OUTPUT_LO(ix); else HEXTRACT_COUNT(ix, 1); \
10950 } STMT_END
10951 /* HEXTRACT_TOP_NYBBLE is just convenience disguise,
10952 * to make it look less odd when the top bits of a NV
10953 * are extracted using HEXTRACT_LO_NYBBLE: the highest
10954 * order bits can be in the "low nybble" of a byte. */
10955 #define HEXTRACT_TOP_NYBBLE(ix) HEXTRACT_LO_NYBBLE(ix)
10956 #define HEXTRACT_BYTES_LE(a, b) \
10957 for (ix = a; ix >= b; ix--) { HEXTRACT_BYTE(ix); }
10958 #define HEXTRACT_BYTES_BE(a, b) \
10959 for (ix = a; ix <= b; ix++) { HEXTRACT_BYTE(ix); }
10960 #define HEXTRACT_IMPLICIT_BIT(nv) \
10961 STMT_START { \
10962 if (vend) *v++ = ((nv) == 0.0) ? 0 : 1; else v++; \
10963 } STMT_END
10964
10965 /* Most formats do. Those which don't should undef this. */
10966 #define HEXTRACT_HAS_IMPLICIT_BIT
10967 /* Many formats do. Those which don't should undef this. */
10968 #define HEXTRACT_HAS_TOP_NYBBLE
10969
10970 /* HEXTRACTSIZE is the maximum number of xdigits. */
10971 #if defined(USE_LONG_DOUBLE) && defined(LONGDOUBLE_DOUBLEDOUBLE)
10972 # define HEXTRACTSIZE (2+DOUBLEDOUBLE_MAXBITS/4)
10973 #else
10974 # define HEXTRACTSIZE 2 * NVSIZE
10975 #endif
10976
10977 const U8* vmaxend = vhex + HEXTRACTSIZE;
10978 PERL_UNUSED_VAR(ix); /* might happen */
10979 (void)Perl_frexp(PERL_ABS(nv), exponent);
10980 if (vend && (vend <= vhex || vend > vmaxend)) {
10981 /* diag_listed_as: Hexadecimal float: internal error (%s) */
10982 Perl_croak(aTHX_ "Hexadecimal float: internal error (entry)");
10983 }
10984 {
10985 /* First check if using long doubles. */
10986 #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE)
10987 # if LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN
10988 /* Used in e.g. VMS and HP-UX IA-64, e.g. -0.1L:
10989 * 9a 99 99 99 99 99 99 99 99 99 99 99 99 99 fb 3f */
10990 /* The bytes 13..0 are the mantissa/fraction,
10991 * the 15,14 are the sign+exponent. */
10992 const U8* nvp = (const U8*)(&nv);
10993 HEXTRACT_IMPLICIT_BIT(nv);
10994 # undef HEXTRACT_HAS_TOP_NYBBLE
10995 HEXTRACT_BYTES_LE(13, 0);
10996 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN
10997 /* Used in e.g. Solaris Sparc and HP-UX PA-RISC, e.g. -0.1L:
10998 * bf fb 99 99 99 99 99 99 99 99 99 99 99 99 99 9a */
10999 /* The bytes 2..15 are the mantissa/fraction,
11000 * the 0,1 are the sign+exponent. */
11001 const U8* nvp = (const U8*)(&nv);
11002 HEXTRACT_IMPLICIT_BIT(nv);
11003 # undef HEXTRACT_HAS_TOP_NYBBLE
11004 HEXTRACT_BYTES_BE(2, 15);
11005 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN
11006 /* x86 80-bit "extended precision", 64 bits of mantissa / fraction /
11007 * significand, 15 bits of exponent, 1 bit of sign. NVSIZE can
11008 * be either 12 (ILP32, Solaris x86) or 16 (LP64, Linux and OS X),
11009 * meaning that 2 or 6 bytes are empty padding. */
11010 /* The bytes 7..0 are the mantissa/fraction */
11011 const U8* nvp = (const U8*)(&nv);
11012 # undef HEXTRACT_HAS_IMPLICIT_BIT
11013 # undef HEXTRACT_HAS_TOP_NYBBLE
11014 HEXTRACT_BYTES_LE(7, 0);
11015 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_BIG_ENDIAN
11016 /* Does this format ever happen? (Wikipedia says the Motorola
11017 * 6888x math coprocessors used format _like_ this but padded
11018 * to 96 bits with 16 unused bits between the exponent and the
11019 * mantissa.) */
11020 const U8* nvp = (const U8*)(&nv);
11021 # undef HEXTRACT_HAS_IMPLICIT_BIT
11022 # undef HEXTRACT_HAS_TOP_NYBBLE
11023 HEXTRACT_BYTES_BE(0, 7);
11024 # else
11025 # define HEXTRACT_FALLBACK
11026 /* Double-double format: two doubles next to each other.
11027 * The first double is the high-order one, exactly like
11028 * it would be for a "lone" double. The second double
11029 * is shifted down using the exponent so that that there
11030 * are no common bits. The tricky part is that the value
11031 * of the double-double is the SUM of the two doubles and
11032 * the second one can be also NEGATIVE.
11033 *
11034 * Because of this tricky construction the bytewise extraction we
11035 * use for the other long double formats doesn't work, we must
11036 * extract the values bit by bit.
11037 *
11038 * The little-endian double-double is used .. somewhere?
11039 *
11040 * The big endian double-double is used in e.g. PPC/Power (AIX)
11041 * and MIPS (SGI).
11042 *
11043 * The mantissa bits are in two separate stretches, e.g. for -0.1L:
11044 * 9a 99 99 99 99 99 59 bc 9a 99 99 99 99 99 b9 3f (LE)
11045 * 3f b9 99 99 99 99 99 9a bc 59 99 99 99 99 99 9a (BE)
11046 */
11047 # endif
11048 #else /* #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE) */
11049 /* Using normal doubles, not long doubles.
11050 *
11051 * We generate 4-bit xdigits (nybble/nibble) instead of 8-bit
11052 * bytes, since we might need to handle printf precision, and
11053 * also need to insert the radix. */
11054 # if NVSIZE == 8
11055 # ifdef HEXTRACT_LITTLE_ENDIAN
11056 /* 0 1 2 3 4 5 6 7 (MSB = 7, LSB = 0, 6+7 = exponent+sign) */
11057 const U8* nvp = (const U8*)(&nv);
11058 HEXTRACT_IMPLICIT_BIT(nv);
11059 HEXTRACT_TOP_NYBBLE(6);
11060 HEXTRACT_BYTES_LE(5, 0);
11061 # elif defined(HEXTRACT_BIG_ENDIAN)
11062 /* 7 6 5 4 3 2 1 0 (MSB = 7, LSB = 0, 6+7 = exponent+sign) */
11063 const U8* nvp = (const U8*)(&nv);
11064 HEXTRACT_IMPLICIT_BIT(nv);
11065 HEXTRACT_TOP_NYBBLE(1);
11066 HEXTRACT_BYTES_BE(2, 7);
11067 # elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_LE_BE
11068 /* 4 5 6 7 0 1 2 3 (MSB = 7, LSB = 0, 6:7 = nybble:exponent:sign) */
11069 const U8* nvp = (const U8*)(&nv);
11070 HEXTRACT_IMPLICIT_BIT(nv);
11071 HEXTRACT_TOP_NYBBLE(2); /* 6 */
11072 HEXTRACT_BYTE(1); /* 5 */
11073 HEXTRACT_BYTE(0); /* 4 */
11074 HEXTRACT_BYTE(7); /* 3 */
11075 HEXTRACT_BYTE(6); /* 2 */
11076 HEXTRACT_BYTE(5); /* 1 */
11077 HEXTRACT_BYTE(4); /* 0 */
11078 # elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_BE_LE
11079 /* 3 2 1 0 7 6 5 4 (MSB = 7, LSB = 0, 7:6 = sign:exponent:nybble) */
11080 const U8* nvp = (const U8*)(&nv);
11081 HEXTRACT_IMPLICIT_BIT(nv);
11082 HEXTRACT_TOP_NYBBLE(5); /* 6 */
11083 HEXTRACT_BYTE(6); /* 5 */
11084 HEXTRACT_BYTE(7); /* 4 */
11085 HEXTRACT_BYTE(0); /* 3 */
11086 HEXTRACT_BYTE(1); /* 2 */
11087 HEXTRACT_BYTE(2); /* 1 */
11088 HEXTRACT_BYTE(3); /* 0 */
11089 # else
11090 # define HEXTRACT_FALLBACK
11091 # endif
11092 # else
11093 # define HEXTRACT_FALLBACK
11094 # endif
11095 #endif /* #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE) #else */
11096 # ifdef HEXTRACT_FALLBACK
11097 # undef HEXTRACT_HAS_TOP_NYBBLE /* Meaningless, but consistent. */
11098 /* The fallback is used for the double-double format, and
11099 * for unknown long double formats, and for unknown double
11100 * formats, or in general unknown NV formats. */
11101 if (nv == (NV)0.0) {
11102 if (vend)
11103 *v++ = 0;
11104 else
11105 v++;
11106 *exponent = 0;
11107 }
11108 else {
11109 NV d = nv < 0 ? -nv : nv;
11110 NV e = (NV)1.0;
11111 U8 ha = 0x0; /* hexvalue accumulator */
11112 U8 hd = 0x8; /* hexvalue digit */
11113
11114 /* Shift d and e (and update exponent) so that e <= d < 2*e,
11115 * this is essentially manual frexp(). Multiplying by 0.5 and
11116 * doubling should be lossless in binary floating point. */
11117
11118 *exponent = 1;
11119
11120 while (e > d) {
11121 e *= (NV)0.5;
11122 (*exponent)--;
11123 }
11124 /* Now d >= e */
11125
11126 while (d >= e + e) {
11127 e += e;
11128 (*exponent)++;
11129 }
11130 /* Now e <= d < 2*e */
11131
11132 /* First extract the leading hexdigit (the implicit bit). */
11133 if (d >= e) {
11134 d -= e;
11135 if (vend)
11136 *v++ = 1;
11137 else
11138 v++;
11139 }
11140 else {
11141 if (vend)
11142 *v++ = 0;
11143 else
11144 v++;
11145 }
11146 e *= (NV)0.5;
11147
11148 /* Then extract the remaining hexdigits. */
11149 while (d > (NV)0.0) {
11150 if (d >= e) {
11151 ha |= hd;
11152 d -= e;
11153 }
11154 if (hd == 1) {
11155 /* Output or count in groups of four bits,
11156 * that is, when the hexdigit is down to one. */
11157 if (vend)
11158 *v++ = ha;
11159 else
11160 v++;
11161 /* Reset the hexvalue. */
11162 ha = 0x0;
11163 hd = 0x8;
11164 }
11165 else
11166 hd >>= 1;
11167 e *= (NV)0.5;
11168 }
11169
11170 /* Flush possible pending hexvalue. */
11171 if (ha) {
11172 if (vend)
11173 *v++ = ha;
11174 else
11175 v++;
11176 }
11177 }
11178 # endif
11179 }
11180 /* Croak for various reasons: if the output pointer escaped the
11181 * output buffer, if the extraction index escaped the extraction
11182 * buffer, or if the ending output pointer didn't match the
11183 * previously computed value. */
11184 if (v <= vhex || v - vhex >= VHEX_SIZE ||
11185 /* For double-double the ixmin and ixmax stay at zero,
11186 * which is convenient since the HEXTRACTSIZE is tricky
11187 * for double-double. */
11188 ixmin < 0 || ixmax >= NVSIZE ||
11189 (vend && v != vend)) {
11190 /* diag_listed_as: Hexadecimal float: internal error (%s) */
11191 Perl_croak(aTHX_ "Hexadecimal float: internal error (overflow)");
11192 }
11193 return v;
11194 }
11195
11196 void
11197 Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
11198 va_list *const args, SV **const svargs, const I32 svmax, bool *const maybe_tainted,
11199 const U32 flags)
11200 {
11201 char *p;
11202 char *q;
11203 const char *patend;
11204 STRLEN origlen;
11205 I32 svix = 0;
11206 static const char nullstr[] = "(null)";
11207 SV *argsv = NULL;
11208 bool has_utf8 = DO_UTF8(sv); /* has the result utf8? */
11209 const bool pat_utf8 = has_utf8; /* the pattern is in utf8? */
11210 SV *nsv = NULL;
11211 /* Times 4: a decimal digit takes more than 3 binary digits.
11212 * NV_DIG: mantissa takes than many decimal digits.
11213 * Plus 32: Playing safe. */
11214 char ebuf[IV_DIG * 4 + NV_DIG + 32];
11215 bool no_redundant_warning = FALSE; /* did we use any explicit format parameter index? */
11216 bool hexfp = FALSE; /* hexadecimal floating point? */
11217
11218 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
11219
11220 PERL_ARGS_ASSERT_SV_VCATPVFN_FLAGS;
11221 PERL_UNUSED_ARG(maybe_tainted);
11222
11223 if (flags & SV_GMAGIC)
11224 SvGETMAGIC(sv);
11225
11226 /* no matter what, this is a string now */
11227 (void)SvPV_force_nomg(sv, origlen);
11228
11229 /* special-case "", "%s", and "%-p" (SVf - see below) */
11230 if (patlen == 0) {
11231 if (svmax && ckWARN(WARN_REDUNDANT))
11232 Perl_warner(aTHX_ packWARN(WARN_REDUNDANT), "Redundant argument in %s",
11233 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
11234 return;
11235 }
11236 if (patlen == 2 && pat[0] == '%' && pat[1] == 's') {
11237 if (svmax > 1 && ckWARN(WARN_REDUNDANT))
11238 Perl_warner(aTHX_ packWARN(WARN_REDUNDANT), "Redundant argument in %s",
11239 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
11240
11241 if (args) {
11242 const char * const s = va_arg(*args, char*);
11243 sv_catpv_nomg(sv, s ? s : nullstr);
11244 }
11245 else if (svix < svmax) {
11246 /* we want get magic on the source but not the target. sv_catsv can't do that, though */
11247 SvGETMAGIC(*svargs);
11248 sv_catsv_nomg(sv, *svargs);
11249 }
11250 else
11251 S_vcatpvfn_missing_argument(aTHX);
11252 return;
11253 }
11254 if (args && patlen == 3 && pat[0] == '%' &&
11255 pat[1] == '-' && pat[2] == 'p') {
11256 if (svmax > 1 && ckWARN(WARN_REDUNDANT))
11257 Perl_warner(aTHX_ packWARN(WARN_REDUNDANT), "Redundant argument in %s",
11258 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
11259 argsv = MUTABLE_SV(va_arg(*args, void*));
11260 sv_catsv_nomg(sv, argsv);
11261 return;
11262 }
11263
11264 #if !defined(USE_LONG_DOUBLE) && !defined(USE_QUADMATH)
11265 /* special-case "%.<number>[gf]" */
11266 if ( !args && patlen <= 5 && pat[0] == '%' && pat[1] == '.'
11267 && (pat[patlen-1] == 'g' || pat[patlen-1] == 'f') ) {
11268 unsigned digits = 0;
11269 const char *pp;
11270
11271 pp = pat + 2;
11272 while (*pp >= '0' && *pp <= '9')
11273 digits = 10 * digits + (*pp++ - '0');
11274
11275 /* XXX: Why do this `svix < svmax` test? Couldn't we just
11276 format the first argument and WARN_REDUNDANT if svmax > 1?
11277 Munged by Nicholas Clark in v5.13.0-209-g95ea86d */
11278 if (pp - pat == (int)patlen - 1 && svix < svmax) {
11279 const NV nv = SvNV(*svargs);
11280 if (LIKELY(!Perl_isinfnan(nv))) {
11281 if (*pp == 'g') {
11282 /* Add check for digits != 0 because it seems that some
11283 gconverts are buggy in this case, and we don't yet have
11284 a Configure test for this. */
11285 if (digits && digits < sizeof(ebuf) - NV_DIG - 10) {
11286 /* 0, point, slack */
11287 STORE_LC_NUMERIC_SET_TO_NEEDED();
11288 SNPRINTF_G(nv, ebuf, size, digits);
11289 sv_catpv_nomg(sv, ebuf);
11290 if (*ebuf) /* May return an empty string for digits==0 */
11291 return;
11292 }
11293 } else if (!digits) {
11294 STRLEN l;
11295
11296 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
11297 sv_catpvn_nomg(sv, p, l);
11298 return;
11299 }
11300 }
11301 }
11302 }
11303 }
11304 #endif /* !USE_LONG_DOUBLE */
11305
11306 if (!args && svix < svmax && DO_UTF8(*svargs))
11307 has_utf8 = TRUE;
11308
11309 patend = (char*)pat + patlen;
11310 for (p = (char*)pat; p < patend; p = q) {
11311 bool alt = FALSE;
11312 bool left = FALSE;
11313 bool vectorize = FALSE;
11314 bool vectorarg = FALSE;
11315 bool vec_utf8 = FALSE;
11316 char fill = ' ';
11317 char plus = 0;
11318 char intsize = 0;
11319 STRLEN width = 0;
11320 STRLEN zeros = 0;
11321 bool has_precis = FALSE;
11322 STRLEN precis = 0;
11323 const I32 osvix = svix;
11324 bool is_utf8 = FALSE; /* is this item utf8? */
11325 #ifdef HAS_LDBL_SPRINTF_BUG
11326 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
11327 with sfio - Allen <allens@cpan.org> */
11328 bool fix_ldbl_sprintf_bug = FALSE;
11329 #endif
11330
11331 char esignbuf[4];
11332 U8 utf8buf[UTF8_MAXBYTES+1];
11333 STRLEN esignlen = 0;
11334
11335 const char *eptr = NULL;
11336 const char *fmtstart;
11337 STRLEN elen = 0;
11338 SV *vecsv = NULL;
11339 const U8 *vecstr = NULL;
11340 STRLEN veclen = 0;
11341 char c = 0;
11342 int i;
11343 unsigned base = 0;
11344 IV iv = 0;
11345 UV uv = 0;
11346 /* We need a long double target in case HAS_LONG_DOUBLE,
11347 * even without USE_LONG_DOUBLE, so that we can printf with
11348 * long double formats, even without NV being long double.
11349 * But we call the target 'fv' instead of 'nv', since most of
11350 * the time it is not (most compilers these days recognize
11351 * "long double", even if only as a synonym for "double").
11352 */
11353 #if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE && \
11354 defined(PERL_PRIgldbl) && !defined(USE_QUADMATH)
11355 long double fv;
11356 # ifdef Perl_isfinitel
11357 # define FV_ISFINITE(x) Perl_isfinitel(x)
11358 # endif
11359 # define FV_GF PERL_PRIgldbl
11360 # if defined(__VMS) && defined(__ia64) && defined(__IEEE_FLOAT)
11361 /* Work around breakage in OTS$CVT_FLOAT_T_X */
11362 # define NV_TO_FV(nv,fv) STMT_START { \
11363 double _dv = nv; \
11364 fv = Perl_isnan(_dv) ? LDBL_QNAN : _dv; \
11365 } STMT_END
11366 # else
11367 # define NV_TO_FV(nv,fv) (fv)=(nv)
11368 # endif
11369 #else
11370 NV fv;
11371 # define FV_GF NVgf
11372 # define NV_TO_FV(nv,fv) (fv)=(nv)
11373 #endif
11374 #ifndef FV_ISFINITE
11375 # define FV_ISFINITE(x) Perl_isfinite((NV)(x))
11376 #endif
11377 NV nv;
11378 STRLEN have;
11379 STRLEN need;
11380 STRLEN gap;
11381 const char *dotstr = ".";
11382 STRLEN dotstrlen = 1;
11383 I32 efix = 0; /* explicit format parameter index */
11384 I32 ewix = 0; /* explicit width index */
11385 I32 epix = 0; /* explicit precision index */
11386 I32 evix = 0; /* explicit vector index */
11387 bool asterisk = FALSE;
11388 bool infnan = FALSE;
11389
11390 /* echo everything up to the next format specification */
11391 for (q = p; q < patend && *q != '%'; ++q) ;
11392 if (q > p) {
11393 if (has_utf8 && !pat_utf8)
11394 sv_catpvn_nomg_utf8_upgrade(sv, p, q - p, nsv);
11395 else
11396 sv_catpvn_nomg(sv, p, q - p);
11397 p = q;
11398 }
11399 if (q++ >= patend)
11400 break;
11401
11402 fmtstart = q;
11403
11404 /*
11405 We allow format specification elements in this order:
11406 \d+\$ explicit format parameter index
11407 [-+ 0#]+ flags
11408 v|\*(\d+\$)?v vector with optional (optionally specified) arg
11409 0 flag (as above): repeated to allow "v02"
11410 \d+|\*(\d+\$)? width using optional (optionally specified) arg
11411 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
11412 [hlqLV] size
11413 [%bcdefginopsuxDFOUX] format (mandatory)
11414 */
11415
11416 if (args) {
11417 /*
11418 As of perl5.9.3, printf format checking is on by default.
11419 Internally, perl uses %p formats to provide an escape to
11420 some extended formatting. This block deals with those
11421 extensions: if it does not match, (char*)q is reset and
11422 the normal format processing code is used.
11423
11424 Currently defined extensions are:
11425 %p include pointer address (standard)
11426 %-p (SVf) include an SV (previously %_)
11427 %-<num>p include an SV with precision <num>
11428 %2p include a HEK
11429 %3p include a HEK with precision of 256
11430 %4p char* preceded by utf8 flag and length
11431 %<num>p (where num is 1 or > 4) reserved for future
11432 extensions
11433
11434 Robin Barker 2005-07-14 (but modified since)
11435
11436 %1p (VDf) removed. RMB 2007-10-19
11437 */
11438 char* r = q;
11439 bool sv = FALSE;
11440 STRLEN n = 0;
11441 if (*q == '-')
11442 sv = *q++;
11443 else if (strnEQ(q, UTF8f, sizeof(UTF8f)-1)) { /* UTF8f */
11444 /* The argument has already gone through cBOOL, so the cast
11445 is safe. */
11446 is_utf8 = (bool)va_arg(*args, int);
11447 elen = va_arg(*args, UV);
11448 if ((IV)elen < 0) {
11449 /* check if utf8 length is larger than 0 when cast to IV */
11450 assert( (IV)elen >= 0 ); /* in DEBUGGING build we want to crash */
11451 elen= 0; /* otherwise we want to treat this as an empty string */
11452 }
11453 eptr = va_arg(*args, char *);
11454 q += sizeof(UTF8f)-1;
11455 goto string;
11456 }
11457 n = expect_number(&q);
11458 if (*q++ == 'p') {
11459 if (sv) { /* SVf */
11460 if (n) {
11461 precis = n;
11462 has_precis = TRUE;
11463 }
11464 argsv = MUTABLE_SV(va_arg(*args, void*));
11465 eptr = SvPV_const(argsv, elen);
11466 if (DO_UTF8(argsv))
11467 is_utf8 = TRUE;
11468 goto string;
11469 }
11470 else if (n==2 || n==3) { /* HEKf */
11471 HEK * const hek = va_arg(*args, HEK *);
11472 eptr = HEK_KEY(hek);
11473 elen = HEK_LEN(hek);
11474 if (HEK_UTF8(hek)) is_utf8 = TRUE;
11475 if (n==3) precis = 256, has_precis = TRUE;
11476 goto string;
11477 }
11478 else if (n) {
11479 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
11480 "internal %%<num>p might conflict with future printf extensions");
11481 }
11482 }
11483 q = r;
11484 }
11485
11486 if ( (width = expect_number(&q)) ) {
11487 if (*q == '$') {
11488 ++q;
11489 efix = width;
11490 if (!no_redundant_warning)
11491 /* I've forgotten if it's a better
11492 micro-optimization to always set this or to
11493 only set it if it's unset */
11494 no_redundant_warning = TRUE;
11495 } else {
11496 goto gotwidth;
11497 }
11498 }
11499
11500 /* FLAGS */
11501
11502 while (*q) {
11503 switch (*q) {
11504 case ' ':
11505 case '+':
11506 if (plus == '+' && *q == ' ') /* '+' over ' ' */
11507 q++;
11508 else
11509 plus = *q++;
11510 continue;
11511
11512 case '-':
11513 left = TRUE;
11514 q++;
11515 continue;
11516
11517 case '0':
11518 fill = *q++;
11519 continue;
11520
11521 case '#':
11522 alt = TRUE;
11523 q++;
11524 continue;
11525
11526 default:
11527 break;
11528 }
11529 break;
11530 }
11531
11532 tryasterisk:
11533 if (*q == '*') {
11534 q++;
11535 if ( (ewix = expect_number(&q)) ) {
11536 if (*q++ == '$')
11537 no_redundant_warning = TRUE;
11538 else
11539 goto unknown;
11540 }
11541 asterisk = TRUE;
11542 }
11543 if (*q == 'v') {
11544 q++;
11545 if (vectorize)
11546 goto unknown;
11547 if ((vectorarg = asterisk)) {
11548 evix = ewix;
11549 ewix = 0;
11550 asterisk = FALSE;
11551 }
11552 vectorize = TRUE;
11553 goto tryasterisk;
11554 }
11555
11556 if (!asterisk)
11557 {
11558 if( *q == '0' )
11559 fill = *q++;
11560 width = expect_number(&q);
11561 }
11562
11563 if (vectorize && vectorarg) {
11564 /* vectorizing, but not with the default "." */
11565 if (args)
11566 vecsv = va_arg(*args, SV*);
11567 else if (evix) {
11568 vecsv = (evix > 0 && evix <= svmax)
11569 ? svargs[evix-1] : S_vcatpvfn_missing_argument(aTHX);
11570 } else {
11571 vecsv = svix < svmax
11572 ? svargs[svix++] : S_vcatpvfn_missing_argument(aTHX);
11573 }
11574 dotstr = SvPV_const(vecsv, dotstrlen);
11575 /* Keep the DO_UTF8 test *after* the SvPV call, else things go
11576 bad with tied or overloaded values that return UTF8. */
11577 if (DO_UTF8(vecsv))
11578 is_utf8 = TRUE;
11579 else if (has_utf8) {
11580 vecsv = sv_mortalcopy(vecsv);
11581 sv_utf8_upgrade(vecsv);
11582 dotstr = SvPV_const(vecsv, dotstrlen);
11583 is_utf8 = TRUE;
11584 }
11585 }
11586
11587 if (asterisk) {
11588 if (args)
11589 i = va_arg(*args, int);
11590 else
11591 i = (ewix ? ewix <= svmax : svix < svmax) ?
11592 SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
11593 left |= (i < 0);
11594 width = (i < 0) ? -i : i;
11595 }
11596 gotwidth:
11597
11598 /* PRECISION */
11599
11600 if (*q == '.') {
11601 q++;
11602 if (*q == '*') {
11603 q++;
11604 if ( ((epix = expect_number(&q))) && (*q++ != '$') )
11605 goto unknown;
11606 /* XXX: todo, support specified precision parameter */
11607 if (epix)
11608 goto unknown;
11609 if (args)
11610 i = va_arg(*args, int);
11611 else
11612 i = (ewix ? ewix <= svmax : svix < svmax)
11613 ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
11614 precis = i;
11615 has_precis = !(i < 0);
11616 }
11617 else {
11618 precis = 0;
11619 while (isDIGIT(*q))
11620 precis = precis * 10 + (*q++ - '0');
11621 has_precis = TRUE;
11622 }
11623 }
11624
11625 if (vectorize) {
11626 if (args) {
11627 VECTORIZE_ARGS
11628 }
11629 else if (efix ? (efix > 0 && efix <= svmax) : svix < svmax) {
11630 vecsv = svargs[efix ? efix-1 : svix++];
11631 vecstr = (U8*)SvPV_const(vecsv,veclen);
11632 vec_utf8 = DO_UTF8(vecsv);
11633
11634 /* if this is a version object, we need to convert
11635 * back into v-string notation and then let the
11636 * vectorize happen normally
11637 */
11638 if (sv_isobject(vecsv) && sv_derived_from(vecsv, "version")) {
11639 if ( hv_exists(MUTABLE_HV(SvRV(vecsv)), "alpha", 5 ) ) {
11640 Perl_ck_warner_d(aTHX_ packWARN(WARN_PRINTF),
11641 "vector argument not supported with alpha versions");
11642 goto vdblank;
11643 }
11644 vecsv = sv_newmortal();
11645 scan_vstring((char *)vecstr, (char *)vecstr + veclen,
11646 vecsv);
11647 vecstr = (U8*)SvPV_const(vecsv, veclen);
11648 vec_utf8 = DO_UTF8(vecsv);
11649 }
11650 }
11651 else {
11652 vdblank:
11653 vecstr = (U8*)"";
11654 veclen = 0;
11655 }
11656 }
11657
11658 /* SIZE */
11659
11660 switch (*q) {
11661 #ifdef WIN32
11662 case 'I': /* Ix, I32x, and I64x */
11663 # ifdef USE_64_BIT_INT
11664 if (q[1] == '6' && q[2] == '4') {
11665 q += 3;
11666 intsize = 'q';
11667 break;
11668 }
11669 # endif
11670 if (q[1] == '3' && q[2] == '2') {
11671 q += 3;
11672 break;
11673 }
11674 # ifdef USE_64_BIT_INT
11675 intsize = 'q';
11676 # endif
11677 q++;
11678 break;
11679 #endif
11680 #if (IVSIZE >= 8 || defined(HAS_LONG_DOUBLE)) || \
11681 (IVSIZE == 4 && !defined(HAS_LONG_DOUBLE))
11682 case 'L': /* Ld */
11683 /* FALLTHROUGH */
11684 # ifdef USE_QUADMATH
11685 case 'Q':
11686 /* FALLTHROUGH */
11687 # endif
11688 # if IVSIZE >= 8
11689 case 'q': /* qd */
11690 # endif
11691 intsize = 'q';
11692 q++;
11693 break;
11694 #endif
11695 case 'l':
11696 ++q;
11697 #if (IVSIZE >= 8 || defined(HAS_LONG_DOUBLE)) || \
11698 (IVSIZE == 4 && !defined(HAS_LONG_DOUBLE))
11699 if (*q == 'l') { /* lld, llf */
11700 intsize = 'q';
11701 ++q;
11702 }
11703 else
11704 #endif
11705 intsize = 'l';
11706 break;
11707 case 'h':
11708 if (*++q == 'h') { /* hhd, hhu */
11709 intsize = 'c';
11710 ++q;
11711 }
11712 else
11713 intsize = 'h';
11714 break;
11715 case 'V':
11716 case 'z':
11717 case 't':
11718 #ifdef I_STDINT
11719 case 'j':
11720 #endif
11721 intsize = *q++;
11722 break;
11723 }
11724
11725 /* CONVERSION */
11726
11727 if (*q == '%') {
11728 eptr = q++;
11729 elen = 1;
11730 if (vectorize) {
11731 c = '%';
11732 goto unknown;
11733 }
11734 goto string;
11735 }
11736
11737 if (!vectorize && !args) {
11738 if (efix) {
11739 const I32 i = efix-1;
11740 argsv = (i >= 0 && i < svmax)
11741 ? svargs[i] : S_vcatpvfn_missing_argument(aTHX);
11742 } else {
11743 argsv = (svix >= 0 && svix < svmax)
11744 ? svargs[svix++] : S_vcatpvfn_missing_argument(aTHX);
11745 }
11746 }
11747
11748 if (argsv && strchr("BbcDdiOopuUXx",*q)) {
11749 /* XXX va_arg(*args) case? need peek, use va_copy? */
11750 SvGETMAGIC(argsv);
11751 if (UNLIKELY(SvAMAGIC(argsv)))
11752 argsv = sv_2num(argsv);
11753 infnan = UNLIKELY(isinfnansv(argsv));
11754 }
11755
11756 switch (c = *q++) {
11757
11758 /* STRINGS */
11759
11760 case 'c':
11761 if (vectorize)
11762 goto unknown;
11763 if (infnan)
11764 Perl_croak(aTHX_ "Cannot printf %"NVgf" with '%c'",
11765 /* no va_arg() case */
11766 SvNV_nomg(argsv), (int)c);
11767 uv = (args) ? va_arg(*args, int) : SvIV_nomg(argsv);
11768 if ((uv > 255 ||
11769 (!UVCHR_IS_INVARIANT(uv) && SvUTF8(sv)))
11770 && !IN_BYTES) {
11771 eptr = (char*)utf8buf;
11772 elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf;
11773 is_utf8 = TRUE;
11774 }
11775 else {
11776 c = (char)uv;
11777 eptr = &c;
11778 elen = 1;
11779 }
11780 goto string;
11781
11782 case 's':
11783 if (vectorize)
11784 goto unknown;
11785 if (args) {
11786 eptr = va_arg(*args, char*);
11787 if (eptr)
11788 elen = strlen(eptr);
11789 else {
11790 eptr = (char *)nullstr;
11791 elen = sizeof nullstr - 1;
11792 }
11793 }
11794 else {
11795 eptr = SvPV_const(argsv, elen);
11796 if (DO_UTF8(argsv)) {
11797 STRLEN old_precis = precis;
11798 if (has_precis && precis < elen) {
11799 STRLEN ulen = sv_or_pv_len_utf8(argsv, eptr, elen);
11800 STRLEN p = precis > ulen ? ulen : precis;
11801 precis = sv_or_pv_pos_u2b(argsv, eptr, p, 0);
11802 /* sticks at end */
11803 }
11804 if (width) { /* fudge width (can't fudge elen) */
11805 if (has_precis && precis < elen)
11806 width += precis - old_precis;
11807 else
11808 width +=
11809 elen - sv_or_pv_len_utf8(argsv,eptr,elen);
11810 }
11811 is_utf8 = TRUE;
11812 }
11813 }
11814
11815 string:
11816 if (has_precis && precis < elen)
11817 elen = precis;
11818 break;
11819
11820 /* INTEGERS */
11821
11822 case 'p':
11823 if (infnan) {
11824 goto floating_point;
11825 }
11826 if (alt || vectorize)
11827 goto unknown;
11828 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
11829 base = 16;
11830 goto integer;
11831
11832 case 'D':
11833 #ifdef IV_IS_QUAD
11834 intsize = 'q';
11835 #else
11836 intsize = 'l';
11837 #endif
11838 /* FALLTHROUGH */
11839 case 'd':
11840 case 'i':
11841 if (infnan) {
11842 goto floating_point;
11843 }
11844 if (vectorize) {
11845 STRLEN ulen;
11846 if (!veclen)
11847 continue;
11848 if (vec_utf8)
11849 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
11850 UTF8_ALLOW_ANYUV);
11851 else {
11852 uv = *vecstr;
11853 ulen = 1;
11854 }
11855 vecstr += ulen;
11856 veclen -= ulen;
11857 if (plus)
11858 esignbuf[esignlen++] = plus;
11859 }
11860 else if (args) {
11861 switch (intsize) {
11862 case 'c': iv = (char)va_arg(*args, int); break;
11863 case 'h': iv = (short)va_arg(*args, int); break;
11864 case 'l': iv = va_arg(*args, long); break;
11865 case 'V': iv = va_arg(*args, IV); break;
11866 case 'z': iv = va_arg(*args, SSize_t); break;
11867 #ifdef HAS_PTRDIFF_T
11868 case 't': iv = va_arg(*args, ptrdiff_t); break;
11869 #endif
11870 default: iv = va_arg(*args, int); break;
11871 #ifdef I_STDINT
11872 case 'j': iv = va_arg(*args, intmax_t); break;
11873 #endif
11874 case 'q':
11875 #if IVSIZE >= 8
11876 iv = va_arg(*args, Quad_t); break;
11877 #else
11878 goto unknown;
11879 #endif
11880 }
11881 }
11882 else {
11883 IV tiv = SvIV_nomg(argsv); /* work around GCC bug #13488 */
11884 switch (intsize) {
11885 case 'c': iv = (char)tiv; break;
11886 case 'h': iv = (short)tiv; break;
11887 case 'l': iv = (long)tiv; break;
11888 case 'V':
11889 default: iv = tiv; break;
11890 case 'q':
11891 #if IVSIZE >= 8
11892 iv = (Quad_t)tiv; break;
11893 #else
11894 goto unknown;
11895 #endif
11896 }
11897 }
11898 if ( !vectorize ) /* we already set uv above */
11899 {
11900 if (iv >= 0) {
11901 uv = iv;
11902 if (plus)
11903 esignbuf[esignlen++] = plus;
11904 }
11905 else {
11906 uv = (iv == IV_MIN) ? (UV)iv : (UV)(-iv);
11907 esignbuf[esignlen++] = '-';
11908 }
11909 }
11910 base = 10;
11911 goto integer;
11912
11913 case 'U':
11914 #ifdef IV_IS_QUAD
11915 intsize = 'q';
11916 #else
11917 intsize = 'l';
11918 #endif
11919 /* FALLTHROUGH */
11920 case 'u':
11921 base = 10;
11922 goto uns_integer;
11923
11924 case 'B':
11925 case 'b':
11926 base = 2;
11927 goto uns_integer;
11928
11929 case 'O':
11930 #ifdef IV_IS_QUAD
11931 intsize = 'q';
11932 #else
11933 intsize = 'l';
11934 #endif
11935 /* FALLTHROUGH */
11936 case 'o':
11937 base = 8;
11938 goto uns_integer;
11939
11940 case 'X':
11941 case 'x':
11942 base = 16;
11943
11944 uns_integer:
11945 if (infnan) {
11946 goto floating_point;
11947 }
11948 if (vectorize) {
11949 STRLEN ulen;
11950 vector:
11951 if (!veclen)
11952 continue;
11953 if (vec_utf8)
11954 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
11955 UTF8_ALLOW_ANYUV);
11956 else {
11957 uv = *vecstr;
11958 ulen = 1;
11959 }
11960 vecstr += ulen;
11961 veclen -= ulen;
11962 }
11963 else if (args) {
11964 switch (intsize) {
11965 case 'c': uv = (unsigned char)va_arg(*args, unsigned); break;
11966 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
11967 case 'l': uv = va_arg(*args, unsigned long); break;
11968 case 'V': uv = va_arg(*args, UV); break;
11969 case 'z': uv = va_arg(*args, Size_t); break;
11970 #ifdef HAS_PTRDIFF_T
11971 case 't': uv = va_arg(*args, ptrdiff_t); break; /* will sign extend, but there is no uptrdiff_t, so oh well */
11972 #endif
11973 #ifdef I_STDINT
11974 case 'j': uv = va_arg(*args, uintmax_t); break;
11975 #endif
11976 default: uv = va_arg(*args, unsigned); break;
11977 case 'q':
11978 #if IVSIZE >= 8
11979 uv = va_arg(*args, Uquad_t); break;
11980 #else
11981 goto unknown;
11982 #endif
11983 }
11984 }
11985 else {
11986 UV tuv = SvUV_nomg(argsv); /* work around GCC bug #13488 */
11987 switch (intsize) {
11988 case 'c': uv = (unsigned char)tuv; break;
11989 case 'h': uv = (unsigned short)tuv; break;
11990 case 'l': uv = (unsigned long)tuv; break;
11991 case 'V':
11992 default: uv = tuv; break;
11993 case 'q':
11994 #if IVSIZE >= 8
11995 uv = (Uquad_t)tuv; break;
11996 #else
11997 goto unknown;
11998 #endif
11999 }
12000 }
12001
12002 integer:
12003 {
12004 char *ptr = ebuf + sizeof ebuf;
12005 bool tempalt = uv ? alt : FALSE; /* Vectors can't change alt */
12006 unsigned dig;
12007 zeros = 0;
12008
12009 switch (base) {
12010 case 16:
12011 p = (char *)((c == 'X') ? PL_hexdigit + 16 : PL_hexdigit);
12012 do {
12013 dig = uv & 15;
12014 *--ptr = p[dig];
12015 } while (uv >>= 4);
12016 if (tempalt) {
12017 esignbuf[esignlen++] = '0';
12018 esignbuf[esignlen++] = c; /* 'x' or 'X' */
12019 }
12020 break;
12021 case 8:
12022 do {
12023 dig = uv & 7;
12024 *--ptr = '0' + dig;
12025 } while (uv >>= 3);
12026 if (alt && *ptr != '0')
12027 *--ptr = '0';
12028 break;
12029 case 2:
12030 do {
12031 dig = uv & 1;
12032 *--ptr = '0' + dig;
12033 } while (uv >>= 1);
12034 if (tempalt) {
12035 esignbuf[esignlen++] = '0';
12036 esignbuf[esignlen++] = c;
12037 }
12038 break;
12039 default: /* it had better be ten or less */
12040 do {
12041 dig = uv % base;
12042 *--ptr = '0' + dig;
12043 } while (uv /= base);
12044 break;
12045 }
12046 elen = (ebuf + sizeof ebuf) - ptr;
12047 eptr = ptr;
12048 if (has_precis) {
12049 if (precis > elen)
12050 zeros = precis - elen;
12051 else if (precis == 0 && elen == 1 && *eptr == '0'
12052 && !(base == 8 && alt)) /* "%#.0o" prints "0" */
12053 elen = 0;
12054
12055 /* a precision nullifies the 0 flag. */
12056 if (fill == '0')
12057 fill = ' ';
12058 }
12059 }
12060 break;
12061
12062 /* FLOATING POINT */
12063
12064 floating_point:
12065
12066 case 'F':
12067 c = 'f'; /* maybe %F isn't supported here */
12068 /* FALLTHROUGH */
12069 case 'e': case 'E':
12070 case 'f':
12071 case 'g': case 'G':
12072 case 'a': case 'A':
12073 if (vectorize)
12074 goto unknown;
12075
12076 /* This is evil, but floating point is even more evil */
12077
12078 /* for SV-style calling, we can only get NV
12079 for C-style calling, we assume %f is double;
12080 for simplicity we allow any of %Lf, %llf, %qf for long double
12081 */
12082 switch (intsize) {
12083 case 'V':
12084 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
12085 intsize = 'q';
12086 #endif
12087 break;
12088 /* [perl #20339] - we should accept and ignore %lf rather than die */
12089 case 'l':
12090 /* FALLTHROUGH */
12091 default:
12092 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
12093 intsize = args ? 0 : 'q';
12094 #endif
12095 break;
12096 case 'q':
12097 #if defined(HAS_LONG_DOUBLE)
12098 break;
12099 #else
12100 /* FALLTHROUGH */
12101 #endif
12102 case 'c':
12103 case 'h':
12104 case 'z':
12105 case 't':
12106 case 'j':
12107 goto unknown;
12108 }
12109
12110 /* Now we need (long double) if intsize == 'q', else (double). */
12111 if (args) {
12112 /* Note: do not pull NVs off the va_list with va_arg()
12113 * (pull doubles instead) because if you have a build
12114 * with long doubles, you would always be pulling long
12115 * doubles, which would badly break anyone using only
12116 * doubles (i.e. the majority of builds). In other
12117 * words, you cannot mix doubles and long doubles.
12118 * The only case where you can pull off long doubles
12119 * is when the format specifier explicitly asks so with
12120 * e.g. "%Lg". */
12121 #ifdef USE_QUADMATH
12122 fv = intsize == 'q' ?
12123 va_arg(*args, NV) : va_arg(*args, double);
12124 nv = fv;
12125 #elif LONG_DOUBLESIZE > DOUBLESIZE
12126 if (intsize == 'q') {
12127 fv = va_arg(*args, long double);
12128 nv = fv;
12129 } else {
12130 nv = va_arg(*args, double);
12131 NV_TO_FV(nv, fv);
12132 }
12133 #else
12134 nv = va_arg(*args, double);
12135 fv = nv;
12136 #endif
12137 }
12138 else
12139 {
12140 if (!infnan) SvGETMAGIC(argsv);
12141 nv = SvNV_nomg(argsv);
12142 NV_TO_FV(nv, fv);
12143 }
12144
12145 need = 0;
12146 /* frexp() (or frexpl) has some unspecified behaviour for
12147 * nan/inf/-inf, so let's avoid calling that on non-finites. */
12148 if (isALPHA_FOLD_NE(c, 'e') && FV_ISFINITE(fv)) {
12149 i = PERL_INT_MIN;
12150 (void)Perl_frexp((NV)fv, &i);
12151 if (i == PERL_INT_MIN)
12152 Perl_die(aTHX_ "panic: frexp: %"FV_GF, fv);
12153 /* Do not set hexfp earlier since we want to printf
12154 * Inf/NaN for Inf/NaN, not their hexfp. */
12155 hexfp = isALPHA_FOLD_EQ(c, 'a');
12156 if (UNLIKELY(hexfp)) {
12157 /* This seriously overshoots in most cases, but
12158 * better the undershooting. Firstly, all bytes
12159 * of the NV are not mantissa, some of them are
12160 * exponent. Secondly, for the reasonably common
12161 * long doubles case, the "80-bit extended", two
12162 * or six bytes of the NV are unused. */
12163 need +=
12164 (fv < 0) ? 1 : 0 + /* possible unary minus */
12165 2 + /* "0x" */
12166 1 + /* the very unlikely carry */
12167 1 + /* "1" */
12168 1 + /* "." */
12169 2 * NVSIZE + /* 2 hexdigits for each byte */
12170 2 + /* "p+" */
12171 6 + /* exponent: sign, plus up to 16383 (quad fp) */
12172 1; /* \0 */
12173 #ifdef LONGDOUBLE_DOUBLEDOUBLE
12174 /* However, for the "double double", we need more.
12175 * Since each double has their own exponent, the
12176 * doubles may float (haha) rather far from each
12177 * other, and the number of required bits is much
12178 * larger, up to total of DOUBLEDOUBLE_MAXBITS bits.
12179 * See the definition of DOUBLEDOUBLE_MAXBITS.
12180 *
12181 * Need 2 hexdigits for each byte. */
12182 need += (DOUBLEDOUBLE_MAXBITS/8 + 1) * 2;
12183 /* the size for the exponent already added */
12184 #endif
12185 #ifdef USE_LOCALE_NUMERIC
12186 STORE_LC_NUMERIC_SET_TO_NEEDED();
12187 if (PL_numeric_radix_sv && IN_LC(LC_NUMERIC))
12188 need += SvLEN(PL_numeric_radix_sv);
12189 RESTORE_LC_NUMERIC();
12190 #endif
12191 }
12192 else if (i > 0) {
12193 need = BIT_DIGITS(i);
12194 } /* if i < 0, the number of digits is hard to predict. */
12195 }
12196 need += has_precis ? precis : 6; /* known default */
12197
12198 if (need < width)
12199 need = width;
12200
12201 #ifdef HAS_LDBL_SPRINTF_BUG
12202 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
12203 with sfio - Allen <allens@cpan.org> */
12204
12205 # ifdef DBL_MAX
12206 # define MY_DBL_MAX DBL_MAX
12207 # else /* XXX guessing! HUGE_VAL may be defined as infinity, so not using */
12208 # if DOUBLESIZE >= 8
12209 # define MY_DBL_MAX 1.7976931348623157E+308L
12210 # else
12211 # define MY_DBL_MAX 3.40282347E+38L
12212 # endif
12213 # endif
12214
12215 # ifdef HAS_LDBL_SPRINTF_BUG_LESS1 /* only between -1L & 1L - Allen */
12216 # define MY_DBL_MAX_BUG 1L
12217 # else
12218 # define MY_DBL_MAX_BUG MY_DBL_MAX
12219 # endif
12220
12221 # ifdef DBL_MIN
12222 # define MY_DBL_MIN DBL_MIN
12223 # else /* XXX guessing! -Allen */
12224 # if DOUBLESIZE >= 8
12225 # define MY_DBL_MIN 2.2250738585072014E-308L
12226 # else
12227 # define MY_DBL_MIN 1.17549435E-38L
12228 # endif
12229 # endif
12230
12231 if ((intsize == 'q') && (c == 'f') &&
12232 ((fv < MY_DBL_MAX_BUG) && (fv > -MY_DBL_MAX_BUG)) &&
12233 (need < DBL_DIG)) {
12234 /* it's going to be short enough that
12235 * long double precision is not needed */
12236
12237 if ((fv <= 0L) && (fv >= -0L))
12238 fix_ldbl_sprintf_bug = TRUE; /* 0 is 0 - easiest */
12239 else {
12240 /* would use Perl_fp_class as a double-check but not
12241 * functional on IRIX - see perl.h comments */
12242
12243 if ((fv >= MY_DBL_MIN) || (fv <= -MY_DBL_MIN)) {
12244 /* It's within the range that a double can represent */
12245 #if defined(DBL_MAX) && !defined(DBL_MIN)
12246 if ((fv >= ((long double)1/DBL_MAX)) ||
12247 (fv <= (-(long double)1/DBL_MAX)))
12248 #endif
12249 fix_ldbl_sprintf_bug = TRUE;
12250 }
12251 }
12252 if (fix_ldbl_sprintf_bug == TRUE) {
12253 double temp;
12254
12255 intsize = 0;
12256 temp = (double)fv;
12257 fv = (NV)temp;
12258 }
12259 }
12260
12261 # undef MY_DBL_MAX
12262 # undef MY_DBL_MAX_BUG
12263 # undef MY_DBL_MIN
12264
12265 #endif /* HAS_LDBL_SPRINTF_BUG */
12266
12267 need += 20; /* fudge factor */
12268 if (PL_efloatsize < need) {
12269 Safefree(PL_efloatbuf);
12270 PL_efloatsize = need + 20; /* more fudge */
12271 Newx(PL_efloatbuf, PL_efloatsize, char);
12272 PL_efloatbuf[0] = '\0';
12273 }
12274
12275 if ( !(width || left || plus || alt) && fill != '0'
12276 && has_precis && intsize != 'q' /* Shortcuts */
12277 && LIKELY(!Perl_isinfnan((NV)fv)) ) {
12278 /* See earlier comment about buggy Gconvert when digits,
12279 aka precis is 0 */
12280 if ( c == 'g' && precis ) {
12281 STORE_LC_NUMERIC_SET_TO_NEEDED();
12282 SNPRINTF_G(fv, PL_efloatbuf, PL_efloatsize, precis);
12283 /* May return an empty string for digits==0 */
12284 if (*PL_efloatbuf) {
12285 elen = strlen(PL_efloatbuf);
12286 goto float_converted;
12287 }
12288 } else if ( c == 'f' && !precis ) {
12289 if ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
12290 break;
12291 }
12292 }
12293
12294 if (UNLIKELY(hexfp)) {
12295 /* Hexadecimal floating point. */
12296 char* p = PL_efloatbuf;
12297 U8 vhex[VHEX_SIZE];
12298 U8* v = vhex; /* working pointer to vhex */
12299 U8* vend; /* pointer to one beyond last digit of vhex */
12300 U8* vfnz = NULL; /* first non-zero */
12301 U8* vlnz = NULL; /* last non-zero */
12302 const bool lower = (c == 'a');
12303 /* At output the values of vhex (up to vend) will
12304 * be mapped through the xdig to get the actual
12305 * human-readable xdigits. */
12306 const char* xdig = PL_hexdigit;
12307 int zerotail = 0; /* how many extra zeros to append */
12308 int exponent = 0; /* exponent of the floating point input */
12309 bool hexradix = FALSE; /* should we output the radix */
12310
12311 /* XXX: denormals, NaN, Inf.
12312 *
12313 * For example with denormals, (assuming the vanilla
12314 * 64-bit double): the exponent is zero. 1xp-1074 is
12315 * the smallest denormal and the smallest double, it
12316 * should be output as 0x0.0000000000001p-1022 to
12317 * match its internal structure. */
12318
12319 vend = S_hextract(aTHX_ nv, &exponent, vhex, NULL);
12320 S_hextract(aTHX_ nv, &exponent, vhex, vend);
12321
12322 #if NVSIZE > DOUBLESIZE
12323 # ifdef HEXTRACT_HAS_IMPLICIT_BIT
12324 /* In this case there is an implicit bit,
12325 * and therefore the exponent is shifted shift by one. */
12326 exponent--;
12327 # else
12328 /* In this case there is no implicit bit,
12329 * and the exponent is shifted by the first xdigit. */
12330 exponent -= 4;
12331 # endif
12332 #endif
12333
12334 if (fv < 0
12335 || Perl_signbit(nv)
12336 )
12337 *p++ = '-';
12338 else if (plus)
12339 *p++ = plus;
12340 *p++ = '0';
12341 if (lower) {
12342 *p++ = 'x';
12343 }
12344 else {
12345 *p++ = 'X';
12346 xdig += 16; /* Use uppercase hex. */
12347 }
12348
12349 /* Find the first non-zero xdigit. */
12350 for (v = vhex; v < vend; v++) {
12351 if (*v) {
12352 vfnz = v;
12353 break;
12354 }
12355 }
12356
12357 if (vfnz) {
12358 /* Find the last non-zero xdigit. */
12359 for (v = vend - 1; v >= vhex; v--) {
12360 if (*v) {
12361 vlnz = v;
12362 break;
12363 }
12364 }
12365
12366 #if NVSIZE == DOUBLESIZE
12367 if (fv != 0.0)
12368 exponent--;
12369 #endif
12370
12371 if (precis > 0) {
12372 if ((SSize_t)(precis + 1) < vend - vhex) {
12373 bool round;
12374
12375 v = vhex + precis + 1;
12376 /* Round away from zero: if the tail
12377 * beyond the precis xdigits is equal to
12378 * or greater than 0x8000... */
12379 round = *v > 0x8;
12380 if (!round && *v == 0x8) {
12381 for (v++; v < vend; v++) {
12382 if (*v) {
12383 round = TRUE;
12384 break;
12385 }
12386 }
12387 }
12388 if (round) {
12389 for (v = vhex + precis; v >= vhex; v--) {
12390 if (*v < 0xF) {
12391 (*v)++;
12392 break;
12393 }
12394 *v = 0;
12395 if (v == vhex) {
12396 /* If the carry goes all the way to
12397 * the front, we need to output
12398 * a single '1'. This goes against
12399 * the "xdigit and then radix"
12400 * but since this is "cannot happen"
12401 * category, that is probably good. */
12402 *p++ = xdig[1];
12403 }
12404 }
12405 }
12406 /* The new effective "last non zero". */
12407 vlnz = vhex + precis;
12408 }
12409 else {
12410 zerotail = precis - (vlnz - vhex);
12411 }
12412 }
12413
12414 v = vhex;
12415 *p++ = xdig[*v++];
12416
12417 /* If there are non-zero xdigits, the radix
12418 * is output after the first one. */
12419 if (vfnz < vlnz) {
12420 hexradix = TRUE;
12421 }
12422 }
12423 else {
12424 *p++ = '0';
12425 exponent = 0;
12426 zerotail = precis;
12427 }
12428
12429 /* The radix is always output if precis, or if alt. */
12430 if (precis > 0 || alt) {
12431 hexradix = TRUE;
12432 }
12433
12434 if (hexradix) {
12435 #ifndef USE_LOCALE_NUMERIC
12436 *p++ = '.';
12437 #else
12438 STORE_LC_NUMERIC_SET_TO_NEEDED();
12439 if (PL_numeric_radix_sv && IN_LC(LC_NUMERIC)) {
12440 STRLEN n;
12441 const char* r = SvPV(PL_numeric_radix_sv, n);
12442 Copy(r, p, n, char);
12443 p += n;
12444 }
12445 else {
12446 *p++ = '.';
12447 }
12448 RESTORE_LC_NUMERIC();
12449 #endif
12450 }
12451
12452 if (vlnz) {
12453 while (v <= vlnz)
12454 *p++ = xdig[*v++];
12455 }
12456
12457 if (zerotail > 0) {
12458 while (zerotail--) {
12459 *p++ = '0';
12460 }
12461 }
12462
12463 elen = p - PL_efloatbuf;
12464 elen += my_snprintf(p, PL_efloatsize - elen,
12465 "%c%+d", lower ? 'p' : 'P',
12466 exponent);
12467
12468 if (elen < width) {
12469 if (left) {
12470 /* Pad the back with spaces. */
12471 memset(PL_efloatbuf + elen, ' ', width - elen);
12472 }
12473 else if (fill == '0') {
12474 /* Insert the zeros between the "0x" and
12475 * the digits, otherwise we end up with
12476 * "0000xHHH..." */
12477 STRLEN nzero = width - elen;
12478 char* zerox = PL_efloatbuf + 2;
12479 Move(zerox, zerox + nzero, elen - 2, char);
12480 memset(zerox, fill, nzero);
12481 }
12482 else {
12483 /* Move it to the right. */
12484 Move(PL_efloatbuf, PL_efloatbuf + width - elen,
12485 elen, char);
12486 /* Pad the front with spaces. */
12487 memset(PL_efloatbuf, ' ', width - elen);
12488 }
12489 elen = width;
12490 }
12491 }
12492 else {
12493 elen = S_infnan_2pv(nv, PL_efloatbuf, PL_efloatsize, plus);
12494 if (elen) {
12495 /* Not affecting infnan output: precision, alt, fill. */
12496 if (elen < width) {
12497 if (left) {
12498 /* Pack the back with spaces. */
12499 memset(PL_efloatbuf + elen, ' ', width - elen);
12500 } else {
12501 /* Move it to the right. */
12502 Move(PL_efloatbuf, PL_efloatbuf + width - elen,
12503 elen, char);
12504 /* Pad the front with spaces. */
12505 memset(PL_efloatbuf, ' ', width - elen);
12506 }
12507 elen = width;
12508 }
12509 }
12510 }
12511
12512 if (elen == 0) {
12513 char *ptr = ebuf + sizeof ebuf;
12514 *--ptr = '\0';
12515 *--ptr = c;
12516 #if defined(USE_QUADMATH)
12517 if (intsize == 'q') {
12518 /* "g" -> "Qg" */
12519 *--ptr = 'Q';
12520 }
12521 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
12522 #elif defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
12523 /* Note that this is HAS_LONG_DOUBLE and PERL_PRIfldbl,
12524 * not USE_LONG_DOUBLE and NVff. In other words,
12525 * this needs to work without USE_LONG_DOUBLE. */
12526 if (intsize == 'q') {
12527 /* Copy the one or more characters in a long double
12528 * format before the 'base' ([efgEFG]) character to
12529 * the format string. */
12530 static char const ldblf[] = PERL_PRIfldbl;
12531 char const *p = ldblf + sizeof(ldblf) - 3;
12532 while (p >= ldblf) { *--ptr = *p--; }
12533 }
12534 #endif
12535 if (has_precis) {
12536 base = precis;
12537 do { *--ptr = '0' + (base % 10); } while (base /= 10);
12538 *--ptr = '.';
12539 }
12540 if (width) {
12541 base = width;
12542 do { *--ptr = '0' + (base % 10); } while (base /= 10);
12543 }
12544 if (fill == '0')
12545 *--ptr = fill;
12546 if (left)
12547 *--ptr = '-';
12548 if (plus)
12549 *--ptr = plus;
12550 if (alt)
12551 *--ptr = '#';
12552 *--ptr = '%';
12553
12554 /* No taint. Otherwise we are in the strange situation
12555 * where printf() taints but print($float) doesn't.
12556 * --jhi */
12557
12558 STORE_LC_NUMERIC_SET_TO_NEEDED();
12559
12560 /* hopefully the above makes ptr a very constrained format
12561 * that is safe to use, even though it's not literal */
12562 GCC_DIAG_IGNORE(-Wformat-nonliteral);
12563 #ifdef USE_QUADMATH
12564 {
12565 const char* qfmt = quadmath_format_single(ptr);
12566 if (!qfmt)
12567 Perl_croak_nocontext("panic: quadmath invalid format \"%s\"", ptr);
12568 elen = quadmath_snprintf(PL_efloatbuf, PL_efloatsize,
12569 qfmt, nv);
12570 if ((IV)elen == -1)
12571 Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", qfmt);
12572 if (qfmt != ptr)
12573 Safefree(qfmt);
12574 }
12575 #elif defined(HAS_LONG_DOUBLE)
12576 elen = ((intsize == 'q')
12577 ? my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
12578 : my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, (double)fv));
12579 #else
12580 elen = my_sprintf(PL_efloatbuf, ptr, fv);
12581 #endif
12582 GCC_DIAG_RESTORE;
12583 }
12584
12585 float_converted:
12586 eptr = PL_efloatbuf;
12587 assert((IV)elen > 0); /* here zero elen is bad */
12588
12589 #ifdef USE_LOCALE_NUMERIC
12590 /* If the decimal point character in the string is UTF-8, make the
12591 * output utf8 */
12592 if (PL_numeric_radix_sv && SvUTF8(PL_numeric_radix_sv)
12593 && instr(eptr, SvPVX_const(PL_numeric_radix_sv)))
12594 {
12595 is_utf8 = TRUE;
12596 }
12597 #endif
12598
12599 break;
12600
12601 /* SPECIAL */
12602
12603 case 'n':
12604 if (vectorize)
12605 goto unknown;
12606 i = SvCUR(sv) - origlen;
12607 if (args) {
12608 switch (intsize) {
12609 case 'c': *(va_arg(*args, char*)) = i; break;
12610 case 'h': *(va_arg(*args, short*)) = i; break;
12611 default: *(va_arg(*args, int*)) = i; break;
12612 case 'l': *(va_arg(*args, long*)) = i; break;
12613 case 'V': *(va_arg(*args, IV*)) = i; break;
12614 case 'z': *(va_arg(*args, SSize_t*)) = i; break;
12615 #ifdef HAS_PTRDIFF_T
12616 case 't': *(va_arg(*args, ptrdiff_t*)) = i; break;
12617 #endif
12618 #ifdef I_STDINT
12619 case 'j': *(va_arg(*args, intmax_t*)) = i; break;
12620 #endif
12621 case 'q':
12622 #if IVSIZE >= 8
12623 *(va_arg(*args, Quad_t*)) = i; break;
12624 #else
12625 goto unknown;
12626 #endif
12627 }
12628 }
12629 else
12630 sv_setuv_mg(argsv, has_utf8 ? (UV)sv_len_utf8(sv) : (UV)i);
12631 continue; /* not "break" */
12632
12633 /* UNKNOWN */
12634
12635 default:
12636 unknown:
12637 if (!args
12638 && (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)
12639 && ckWARN(WARN_PRINTF))
12640 {
12641 SV * const msg = sv_newmortal();
12642 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
12643 (PL_op->op_type == OP_PRTF) ? "" : "s");
12644 if (fmtstart < patend) {
12645 const char * const fmtend = q < patend ? q : patend;
12646 const char * f;
12647 sv_catpvs(msg, "\"%");
12648 for (f = fmtstart; f < fmtend; f++) {
12649 if (isPRINT(*f)) {
12650 sv_catpvn_nomg(msg, f, 1);
12651 } else {
12652 Perl_sv_catpvf(aTHX_ msg,
12653 "\\%03"UVof, (UV)*f & 0xFF);
12654 }
12655 }
12656 sv_catpvs(msg, "\"");
12657 } else {
12658 sv_catpvs(msg, "end of string");
12659 }
12660 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%"SVf, SVfARG(msg)); /* yes, this is reentrant */
12661 }
12662
12663 /* output mangled stuff ... */
12664 if (c == '\0')
12665 --q;
12666 eptr = p;
12667 elen = q - p;
12668
12669 /* ... right here, because formatting flags should not apply */
12670 SvGROW(sv, SvCUR(sv) + elen + 1);
12671 p = SvEND(sv);
12672 Copy(eptr, p, elen, char);
12673 p += elen;
12674 *p = '\0';
12675 SvCUR_set(sv, p - SvPVX_const(sv));
12676 svix = osvix;
12677 continue; /* not "break" */
12678 }
12679
12680 if (is_utf8 != has_utf8) {
12681 if (is_utf8) {
12682 if (SvCUR(sv))
12683 sv_utf8_upgrade(sv);
12684 }
12685 else {
12686 const STRLEN old_elen = elen;
12687 SV * const nsv = newSVpvn_flags(eptr, elen, SVs_TEMP);
12688 sv_utf8_upgrade(nsv);
12689 eptr = SvPVX_const(nsv);
12690 elen = SvCUR(nsv);
12691
12692 if (width) { /* fudge width (can't fudge elen) */
12693 width += elen - old_elen;
12694 }
12695 is_utf8 = TRUE;
12696 }
12697 }
12698
12699 assert((IV)elen >= 0); /* here zero elen is fine */
12700 have = esignlen + zeros + elen;
12701 if (have < zeros)
12702 croak_memory_wrap();
12703
12704 need = (have > width ? have : width);
12705 gap = need - have;
12706
12707 if (need >= (((STRLEN)~0) - SvCUR(sv) - dotstrlen - 1))
12708 croak_memory_wrap();
12709 SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1);
12710 p = SvEND(sv);
12711 if (esignlen && fill == '0') {
12712 int i;
12713 for (i = 0; i < (int)esignlen; i++)
12714 *p++ = esignbuf[i];
12715 }
12716 if (gap && !left) {
12717 memset(p, fill, gap);
12718 p += gap;
12719 }
12720 if (esignlen && fill != '0') {
12721 int i;
12722 for (i = 0; i < (int)esignlen; i++)
12723 *p++ = esignbuf[i];
12724 }
12725 if (zeros) {
12726 int i;
12727 for (i = zeros; i; i--)
12728 *p++ = '0';
12729 }
12730 if (elen) {
12731 Copy(eptr, p, elen, char);
12732 p += elen;
12733 }
12734 if (gap && left) {
12735 memset(p, ' ', gap);
12736 p += gap;
12737 }
12738 if (vectorize) {
12739 if (veclen) {
12740 Copy(dotstr, p, dotstrlen, char);
12741 p += dotstrlen;
12742 }
12743 else
12744 vectorize = FALSE; /* done iterating over vecstr */
12745 }
12746 if (is_utf8)
12747 has_utf8 = TRUE;
12748 if (has_utf8)
12749 SvUTF8_on(sv);
12750 *p = '\0';
12751 SvCUR_set(sv, p - SvPVX_const(sv));
12752 if (vectorize) {
12753 esignlen = 0;
12754 goto vector;
12755 }
12756 }
12757
12758 /* Now that we've consumed all our printf format arguments (svix)
12759 * do we have things left on the stack that we didn't use?
12760 */
12761 if (!no_redundant_warning && svmax >= svix + 1 && ckWARN(WARN_REDUNDANT)) {
12762 Perl_warner(aTHX_ packWARN(WARN_REDUNDANT), "Redundant argument in %s",
12763 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
12764 }
12765
12766 SvTAINT(sv);
12767
12768 RESTORE_LC_NUMERIC(); /* Done outside loop, so don't have to save/restore
12769 each iteration. */
12770 }
12771
12772 /* =========================================================================
12773
12774 =head1 Cloning an interpreter
12775
12776 =cut
12777
12778 All the macros and functions in this section are for the private use of
12779 the main function, perl_clone().
12780
12781 The foo_dup() functions make an exact copy of an existing foo thingy.
12782 During the course of a cloning, a hash table is used to map old addresses
12783 to new addresses. The table is created and manipulated with the
12784 ptr_table_* functions.
12785
12786 * =========================================================================*/
12787
12788
12789 #if defined(USE_ITHREADS)
12790
12791 /* XXX Remove this so it doesn't have to go thru the macro and return for nothing */
12792 #ifndef GpREFCNT_inc
12793 # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
12794 #endif
12795
12796
12797 /* Certain cases in Perl_ss_dup have been merged, by relying on the fact
12798 that currently av_dup, gv_dup and hv_dup are the same as sv_dup.
12799 If this changes, please unmerge ss_dup.
12800 Likewise, sv_dup_inc_multiple() relies on this fact. */
12801 #define sv_dup_inc_NN(s,t) SvREFCNT_inc_NN(sv_dup_inc(s,t))
12802 #define av_dup(s,t) MUTABLE_AV(sv_dup((const SV *)s,t))
12803 #define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t))
12804 #define hv_dup(s,t) MUTABLE_HV(sv_dup((const SV *)s,t))
12805 #define hv_dup_inc(s,t) MUTABLE_HV(sv_dup_inc((const SV *)s,t))
12806 #define cv_dup(s,t) MUTABLE_CV(sv_dup((const SV *)s,t))
12807 #define cv_dup_inc(s,t) MUTABLE_CV(sv_dup_inc((const SV *)s,t))
12808 #define io_dup(s,t) MUTABLE_IO(sv_dup((const SV *)s,t))
12809 #define io_dup_inc(s,t) MUTABLE_IO(sv_dup_inc((const SV *)s,t))
12810 #define gv_dup(s,t) MUTABLE_GV(sv_dup((const SV *)s,t))
12811 #define gv_dup_inc(s,t) MUTABLE_GV(sv_dup_inc((const SV *)s,t))
12812 #define SAVEPV(p) ((p) ? savepv(p) : NULL)
12813 #define SAVEPVN(p,n) ((p) ? savepvn(p,n) : NULL)
12814
12815 /* clone a parser */
12816
12817 yy_parser *
12818 Perl_parser_dup(pTHX_ const yy_parser *const proto, CLONE_PARAMS *const param)
12819 {
12820 yy_parser *parser;
12821
12822 PERL_ARGS_ASSERT_PARSER_DUP;
12823
12824 if (!proto)
12825 return NULL;
12826
12827 /* look for it in the table first */
12828 parser = (yy_parser *)ptr_table_fetch(PL_ptr_table, proto);
12829 if (parser)
12830 return parser;
12831
12832 /* create anew and remember what it is */
12833 Newxz(parser, 1, yy_parser);
12834 ptr_table_store(PL_ptr_table, proto, parser);
12835
12836 /* XXX these not yet duped */
12837 parser->old_parser = NULL;
12838 parser->stack = NULL;
12839 parser->ps = NULL;
12840 parser->stack_size = 0;
12841 /* XXX parser->stack->state = 0; */
12842
12843 /* XXX eventually, just Copy() most of the parser struct ? */
12844
12845 parser->lex_brackets = proto->lex_brackets;
12846 parser->lex_casemods = proto->lex_casemods;
12847 parser->lex_brackstack = savepvn(proto->lex_brackstack,
12848 (proto->lex_brackets < 120 ? 120 : proto->lex_brackets));
12849 parser->lex_casestack = savepvn(proto->lex_casestack,
12850 (proto->lex_casemods < 12 ? 12 : proto->lex_casemods));
12851 parser->lex_defer = proto->lex_defer;
12852 parser->lex_dojoin = proto->lex_dojoin;
12853 parser->lex_formbrack = proto->lex_formbrack;
12854 parser->lex_inpat = proto->lex_inpat;
12855 parser->lex_inwhat = proto->lex_inwhat;
12856 parser->lex_op = proto->lex_op;
12857 parser->lex_repl = sv_dup_inc(proto->lex_repl, param);
12858 parser->lex_starts = proto->lex_starts;
12859 parser->lex_stuff = sv_dup_inc(proto->lex_stuff, param);
12860 parser->multi_close = proto->multi_close;
12861 parser->multi_open = proto->multi_open;
12862 parser->multi_start = proto->multi_start;
12863 parser->multi_end = proto->multi_end;
12864 parser->preambled = proto->preambled;
12865 parser->sublex_info = proto->sublex_info; /* XXX not quite right */
12866 parser->linestr = sv_dup_inc(proto->linestr, param);
12867 parser->expect = proto->expect;
12868 parser->copline = proto->copline;
12869 parser->last_lop_op = proto->last_lop_op;
12870 parser->lex_state = proto->lex_state;
12871 parser->rsfp = fp_dup(proto->rsfp, '<', param);
12872 /* rsfp_filters entries have fake IoDIRP() */
12873 parser->rsfp_filters= av_dup_inc(proto->rsfp_filters, param);
12874 parser->in_my = proto->in_my;
12875 parser->in_my_stash = hv_dup(proto->in_my_stash, param);
12876 parser->error_count = proto->error_count;
12877
12878
12879 parser->linestr = sv_dup_inc(proto->linestr, param);
12880
12881 {
12882 char * const ols = SvPVX(proto->linestr);
12883 char * const ls = SvPVX(parser->linestr);
12884
12885 parser->bufptr = ls + (proto->bufptr >= ols ?
12886 proto->bufptr - ols : 0);
12887 parser->oldbufptr = ls + (proto->oldbufptr >= ols ?
12888 proto->oldbufptr - ols : 0);
12889 parser->oldoldbufptr= ls + (proto->oldoldbufptr >= ols ?
12890 proto->oldoldbufptr - ols : 0);
12891 parser->linestart = ls + (proto->linestart >= ols ?
12892 proto->linestart - ols : 0);
12893 parser->last_uni = ls + (proto->last_uni >= ols ?
12894 proto->last_uni - ols : 0);
12895 parser->last_lop = ls + (proto->last_lop >= ols ?
12896 proto->last_lop - ols : 0);
12897
12898 parser->bufend = ls + SvCUR(parser->linestr);
12899 }
12900
12901 Copy(proto->tokenbuf, parser->tokenbuf, 256, char);
12902
12903
12904 Copy(proto->nextval, parser->nextval, 5, YYSTYPE);
12905 Copy(proto->nexttype, parser->nexttype, 5, I32);
12906 parser->nexttoke = proto->nexttoke;
12907
12908 /* XXX should clone saved_curcop here, but we aren't passed
12909 * proto_perl; so do it in perl_clone_using instead */
12910
12911 return parser;
12912 }
12913
12914
12915 /* duplicate a file handle */
12916
12917 PerlIO *
12918 Perl_fp_dup(pTHX_ PerlIO *const fp, const char type, CLONE_PARAMS *const param)
12919 {
12920 PerlIO *ret;
12921
12922 PERL_ARGS_ASSERT_FP_DUP;
12923 PERL_UNUSED_ARG(type);
12924
12925 if (!fp)
12926 return (PerlIO*)NULL;
12927
12928 /* look for it in the table first */
12929 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
12930 if (ret)
12931 return ret;
12932
12933 /* create anew and remember what it is */
12934 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
12935 ptr_table_store(PL_ptr_table, fp, ret);
12936 return ret;
12937 }
12938
12939 /* duplicate a directory handle */
12940
12941 DIR *
12942 Perl_dirp_dup(pTHX_ DIR *const dp, CLONE_PARAMS *const param)
12943 {
12944 DIR *ret;
12945
12946 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
12947 DIR *pwd;
12948 const Direntry_t *dirent;
12949 char smallbuf[256];
12950 char *name = NULL;
12951 STRLEN len = 0;
12952 long pos;
12953 #endif
12954
12955 PERL_UNUSED_CONTEXT;
12956 PERL_ARGS_ASSERT_DIRP_DUP;
12957
12958 if (!dp)
12959 return (DIR*)NULL;
12960
12961 /* look for it in the table first */
12962 ret = (DIR*)ptr_table_fetch(PL_ptr_table, dp);
12963 if (ret)
12964 return ret;
12965
12966 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
12967
12968 PERL_UNUSED_ARG(param);
12969
12970 /* create anew */
12971
12972 /* open the current directory (so we can switch back) */
12973 if (!(pwd = PerlDir_open("."))) return (DIR *)NULL;
12974
12975 /* chdir to our dir handle and open the present working directory */
12976 if (fchdir(my_dirfd(dp)) < 0 || !(ret = PerlDir_open("."))) {
12977 PerlDir_close(pwd);
12978 return (DIR *)NULL;
12979 }
12980 /* Now we should have two dir handles pointing to the same dir. */
12981
12982 /* Be nice to the calling code and chdir back to where we were. */
12983 /* XXX If this fails, then what? */
12984 PERL_UNUSED_RESULT(fchdir(my_dirfd(pwd)));
12985
12986 /* We have no need of the pwd handle any more. */
12987 PerlDir_close(pwd);
12988
12989 #ifdef DIRNAMLEN
12990 # define d_namlen(d) (d)->d_namlen
12991 #else
12992 # define d_namlen(d) strlen((d)->d_name)
12993 #endif
12994 /* Iterate once through dp, to get the file name at the current posi-
12995 tion. Then step back. */
12996 pos = PerlDir_tell(dp);
12997 if ((dirent = PerlDir_read(dp))) {
12998 len = d_namlen(dirent);
12999 if (len <= sizeof smallbuf) name = smallbuf;
13000 else Newx(name, len, char);
13001 Move(dirent->d_name, name, len, char);
13002 }
13003 PerlDir_seek(dp, pos);
13004
13005 /* Iterate through the new dir handle, till we find a file with the
13006 right name. */
13007 if (!dirent) /* just before the end */
13008 for(;;) {
13009 pos = PerlDir_tell(ret);
13010 if (PerlDir_read(ret)) continue; /* not there yet */
13011 PerlDir_seek(ret, pos); /* step back */
13012 break;
13013 }
13014 else {
13015 const long pos0 = PerlDir_tell(ret);
13016 for(;;) {
13017 pos = PerlDir_tell(ret);
13018 if ((dirent = PerlDir_read(ret))) {
13019 if (len == (STRLEN)d_namlen(dirent)
13020 && memEQ(name, dirent->d_name, len)) {
13021 /* found it */
13022 PerlDir_seek(ret, pos); /* step back */
13023 break;
13024 }
13025 /* else we are not there yet; keep iterating */
13026 }
13027 else { /* This is not meant to happen. The best we can do is
13028 reset the iterator to the beginning. */
13029 PerlDir_seek(ret, pos0);
13030 break;
13031 }
13032 }
13033 }
13034 #undef d_namlen
13035
13036 if (name && name != smallbuf)
13037 Safefree(name);
13038 #endif
13039
13040 #ifdef WIN32
13041 ret = win32_dirp_dup(dp, param);
13042 #endif
13043
13044 /* pop it in the pointer table */
13045 if (ret)
13046 ptr_table_store(PL_ptr_table, dp, ret);
13047
13048 return ret;
13049 }
13050
13051 /* duplicate a typeglob */
13052
13053 GP *
13054 Perl_gp_dup(pTHX_ GP *const gp, CLONE_PARAMS *const param)
13055 {
13056 GP *ret;
13057
13058 PERL_ARGS_ASSERT_GP_DUP;
13059
13060 if (!gp)
13061 return (GP*)NULL;
13062 /* look for it in the table first */
13063 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
13064 if (ret)
13065 return ret;
13066
13067 /* create anew and remember what it is */
13068 Newxz(ret, 1, GP);
13069 ptr_table_store(PL_ptr_table, gp, ret);
13070
13071 /* clone */
13072 /* ret->gp_refcnt must be 0 before any other dups are called. We're relying
13073 on Newxz() to do this for us. */
13074 ret->gp_sv = sv_dup_inc(gp->gp_sv, param);
13075 ret->gp_io = io_dup_inc(gp->gp_io, param);
13076 ret->gp_form = cv_dup_inc(gp->gp_form, param);
13077 ret->gp_av = av_dup_inc(gp->gp_av, param);
13078 ret->gp_hv = hv_dup_inc(gp->gp_hv, param);
13079 ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
13080 ret->gp_cv = cv_dup_inc(gp->gp_cv, param);
13081 ret->gp_cvgen = gp->gp_cvgen;
13082 ret->gp_line = gp->gp_line;
13083 ret->gp_file_hek = hek_dup(gp->gp_file_hek, param);
13084 return ret;
13085 }
13086
13087 /* duplicate a chain of magic */
13088
13089 MAGIC *
13090 Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *const param)
13091 {
13092 MAGIC *mgret = NULL;
13093 MAGIC **mgprev_p = &mgret;
13094
13095 PERL_ARGS_ASSERT_MG_DUP;
13096
13097 for (; mg; mg = mg->mg_moremagic) {
13098 MAGIC *nmg;
13099
13100 if ((param->flags & CLONEf_JOIN_IN)
13101 && mg->mg_type == PERL_MAGIC_backref)
13102 /* when joining, we let the individual SVs add themselves to
13103 * backref as needed. */
13104 continue;
13105
13106 Newx(nmg, 1, MAGIC);
13107 *mgprev_p = nmg;
13108 mgprev_p = &(nmg->mg_moremagic);
13109
13110 /* There was a comment "XXX copy dynamic vtable?" but as we don't have
13111 dynamic vtables, I'm not sure why Sarathy wrote it. The comment dates
13112 from the original commit adding Perl_mg_dup() - revision 4538.
13113 Similarly there is the annotation "XXX random ptr?" next to the
13114 assignment to nmg->mg_ptr. */
13115 *nmg = *mg;
13116
13117 /* FIXME for plugins
13118 if (nmg->mg_type == PERL_MAGIC_qr) {
13119 nmg->mg_obj = MUTABLE_SV(CALLREGDUPE((REGEXP*)nmg->mg_obj, param));
13120 }
13121 else
13122 */
13123 nmg->mg_obj = (nmg->mg_flags & MGf_REFCOUNTED)
13124 ? nmg->mg_type == PERL_MAGIC_backref
13125 /* The backref AV has its reference
13126 * count deliberately bumped by 1 */
13127 ? SvREFCNT_inc(av_dup_inc((const AV *)
13128 nmg->mg_obj, param))
13129 : sv_dup_inc(nmg->mg_obj, param)
13130 : sv_dup(nmg->mg_obj, param);
13131
13132 if (nmg->mg_ptr && nmg->mg_type != PERL_MAGIC_regex_global) {
13133 if (nmg->mg_len > 0) {
13134 nmg->mg_ptr = SAVEPVN(nmg->mg_ptr, nmg->mg_len);
13135 if (nmg->mg_type == PERL_MAGIC_overload_table &&
13136 AMT_AMAGIC((AMT*)nmg->mg_ptr))
13137 {
13138 AMT * const namtp = (AMT*)nmg->mg_ptr;
13139 sv_dup_inc_multiple((SV**)(namtp->table),
13140 (SV**)(namtp->table), NofAMmeth, param);
13141 }
13142 }
13143 else if (nmg->mg_len == HEf_SVKEY)
13144 nmg->mg_ptr = (char*)sv_dup_inc((const SV *)nmg->mg_ptr, param);
13145 }
13146 if ((nmg->mg_flags & MGf_DUP) && nmg->mg_virtual && nmg->mg_virtual->svt_dup) {
13147 nmg->mg_virtual->svt_dup(aTHX_ nmg, param);
13148 }
13149 }
13150 return mgret;
13151 }
13152
13153 #endif /* USE_ITHREADS */
13154
13155 struct ptr_tbl_arena {
13156 struct ptr_tbl_arena *next;
13157 struct ptr_tbl_ent array[1023/3]; /* as ptr_tbl_ent has 3 pointers. */
13158 };
13159
13160 /* create a new pointer-mapping table */
13161
13162 PTR_TBL_t *
13163 Perl_ptr_table_new(pTHX)
13164 {
13165 PTR_TBL_t *tbl;
13166 PERL_UNUSED_CONTEXT;
13167
13168 Newx(tbl, 1, PTR_TBL_t);
13169 tbl->tbl_max = 511;
13170 tbl->tbl_items = 0;
13171 tbl->tbl_arena = NULL;
13172 tbl->tbl_arena_next = NULL;
13173 tbl->tbl_arena_end = NULL;
13174 Newxz(tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
13175 return tbl;
13176 }
13177
13178 #define PTR_TABLE_HASH(ptr) \
13179 ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17)))
13180
13181 /* map an existing pointer using a table */
13182
13183 STATIC PTR_TBL_ENT_t *
13184 S_ptr_table_find(PTR_TBL_t *const tbl, const void *const sv)
13185 {
13186 PTR_TBL_ENT_t *tblent;
13187 const UV hash = PTR_TABLE_HASH(sv);
13188
13189 PERL_ARGS_ASSERT_PTR_TABLE_FIND;
13190
13191 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
13192 for (; tblent; tblent = tblent->next) {
13193 if (tblent->oldval == sv)
13194 return tblent;
13195 }
13196 return NULL;
13197 }
13198
13199 void *
13200 Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *const tbl, const void *const sv)
13201 {
13202 PTR_TBL_ENT_t const *const tblent = ptr_table_find(tbl, sv);
13203
13204 PERL_ARGS_ASSERT_PTR_TABLE_FETCH;
13205 PERL_UNUSED_CONTEXT;
13206
13207 return tblent ? tblent->newval : NULL;
13208 }
13209
13210 /* add a new entry to a pointer-mapping table 'tbl'. In hash terms, 'oldsv' is
13211 * the key; 'newsv' is the value. The names "old" and "new" are specific to
13212 * the core's typical use of ptr_tables in thread cloning. */
13213
13214 void
13215 Perl_ptr_table_store(pTHX_ PTR_TBL_t *const tbl, const void *const oldsv, void *const newsv)
13216 {
13217 PTR_TBL_ENT_t *tblent = ptr_table_find(tbl, oldsv);
13218
13219 PERL_ARGS_ASSERT_PTR_TABLE_STORE;
13220 PERL_UNUSED_CONTEXT;
13221
13222 if (tblent) {
13223 tblent->newval = newsv;
13224 } else {
13225 const UV entry = PTR_TABLE_HASH(oldsv) & tbl->tbl_max;
13226
13227 if (tbl->tbl_arena_next == tbl->tbl_arena_end) {
13228 struct ptr_tbl_arena *new_arena;
13229
13230 Newx(new_arena, 1, struct ptr_tbl_arena);
13231 new_arena->next = tbl->tbl_arena;
13232 tbl->tbl_arena = new_arena;
13233 tbl->tbl_arena_next = new_arena->array;
13234 tbl->tbl_arena_end = C_ARRAY_END(new_arena->array);
13235 }
13236
13237 tblent = tbl->tbl_arena_next++;
13238
13239 tblent->oldval = oldsv;
13240 tblent->newval = newsv;
13241 tblent->next = tbl->tbl_ary[entry];
13242 tbl->tbl_ary[entry] = tblent;
13243 tbl->tbl_items++;
13244 if (tblent->next && tbl->tbl_items > tbl->tbl_max)
13245 ptr_table_split(tbl);
13246 }
13247 }
13248
13249 /* double the hash bucket size of an existing ptr table */
13250
13251 void
13252 Perl_ptr_table_split(pTHX_ PTR_TBL_t *const tbl)
13253 {
13254 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
13255 const UV oldsize = tbl->tbl_max + 1;
13256 UV newsize = oldsize * 2;
13257 UV i;
13258
13259 PERL_ARGS_ASSERT_PTR_TABLE_SPLIT;
13260 PERL_UNUSED_CONTEXT;
13261
13262 Renew(ary, newsize, PTR_TBL_ENT_t*);
13263 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
13264 tbl->tbl_max = --newsize;
13265 tbl->tbl_ary = ary;
13266 for (i=0; i < oldsize; i++, ary++) {
13267 PTR_TBL_ENT_t **entp = ary;
13268 PTR_TBL_ENT_t *ent = *ary;
13269 PTR_TBL_ENT_t **curentp;
13270 if (!ent)
13271 continue;
13272 curentp = ary + oldsize;
13273 do {
13274 if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
13275 *entp = ent->next;
13276 ent->next = *curentp;
13277 *curentp = ent;
13278 }
13279 else
13280 entp = &ent->next;
13281 ent = *entp;
13282 } while (ent);
13283 }
13284 }
13285
13286 /* remove all the entries from a ptr table */
13287 /* Deprecated - will be removed post 5.14 */
13288
13289 void
13290 Perl_ptr_table_clear(pTHX_ PTR_TBL_t *const tbl)
13291 {
13292 PERL_UNUSED_CONTEXT;
13293 if (tbl && tbl->tbl_items) {
13294 struct ptr_tbl_arena *arena = tbl->tbl_arena;
13295
13296 Zero(tbl->tbl_ary, tbl->tbl_max + 1, struct ptr_tbl_ent **);
13297
13298 while (arena) {
13299 struct ptr_tbl_arena *next = arena->next;
13300
13301 Safefree(arena);
13302 arena = next;
13303 };
13304
13305 tbl->tbl_items = 0;
13306 tbl->tbl_arena = NULL;
13307 tbl->tbl_arena_next = NULL;
13308 tbl->tbl_arena_end = NULL;
13309 }
13310 }
13311
13312 /* clear and free a ptr table */
13313
13314 void
13315 Perl_ptr_table_free(pTHX_ PTR_TBL_t *const tbl)
13316 {
13317 struct ptr_tbl_arena *arena;
13318
13319 PERL_UNUSED_CONTEXT;
13320
13321 if (!tbl) {
13322 return;
13323 }
13324
13325 arena = tbl->tbl_arena;
13326
13327 while (arena) {
13328 struct ptr_tbl_arena *next = arena->next;
13329
13330 Safefree(arena);
13331 arena = next;
13332 }
13333
13334 Safefree(tbl->tbl_ary);
13335 Safefree(tbl);
13336 }
13337
13338 #if defined(USE_ITHREADS)
13339
13340 void
13341 Perl_rvpv_dup(pTHX_ SV *const dstr, const SV *const sstr, CLONE_PARAMS *const param)
13342 {
13343 PERL_ARGS_ASSERT_RVPV_DUP;
13344
13345 assert(!isREGEXP(sstr));
13346 if (SvROK(sstr)) {
13347 if (SvWEAKREF(sstr)) {
13348 SvRV_set(dstr, sv_dup(SvRV_const(sstr), param));
13349 if (param->flags & CLONEf_JOIN_IN) {
13350 /* if joining, we add any back references individually rather
13351 * than copying the whole backref array */
13352 Perl_sv_add_backref(aTHX_ SvRV(dstr), dstr);
13353 }
13354 }
13355 else
13356 SvRV_set(dstr, sv_dup_inc(SvRV_const(sstr), param));
13357 }
13358 else if (SvPVX_const(sstr)) {
13359 /* Has something there */
13360 if (SvLEN(sstr)) {
13361 /* Normal PV - clone whole allocated space */
13362 SvPV_set(dstr, SAVEPVN(SvPVX_const(sstr), SvLEN(sstr)-1));
13363 /* sstr may not be that normal, but actually copy on write.
13364 But we are a true, independent SV, so: */
13365 SvIsCOW_off(dstr);
13366 }
13367 else {
13368 /* Special case - not normally malloced for some reason */
13369 if (isGV_with_GP(sstr)) {
13370 /* Don't need to do anything here. */
13371 }
13372 else if ((SvIsCOW(sstr))) {
13373 /* A "shared" PV - clone it as "shared" PV */
13374 SvPV_set(dstr,
13375 HEK_KEY(hek_dup(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)),
13376 param)));
13377 }
13378 else {
13379 /* Some other special case - random pointer */
13380 SvPV_set(dstr, (char *) SvPVX_const(sstr));
13381 }
13382 }
13383 }
13384 else {
13385 /* Copy the NULL */
13386 SvPV_set(dstr, NULL);
13387 }
13388 }
13389
13390 /* duplicate a list of SVs. source and dest may point to the same memory. */
13391 static SV **
13392 S_sv_dup_inc_multiple(pTHX_ SV *const *source, SV **dest,
13393 SSize_t items, CLONE_PARAMS *const param)
13394 {
13395 PERL_ARGS_ASSERT_SV_DUP_INC_MULTIPLE;
13396
13397 while (items-- > 0) {
13398 *dest++ = sv_dup_inc(*source++, param);
13399 }
13400
13401 return dest;
13402 }
13403
13404 /* duplicate an SV of any type (including AV, HV etc) */
13405
13406 static SV *
13407 S_sv_dup_common(pTHX_ const SV *const sstr, CLONE_PARAMS *const param)
13408 {
13409 dVAR;
13410 SV *dstr;
13411
13412 PERL_ARGS_ASSERT_SV_DUP_COMMON;
13413
13414 if (SvTYPE(sstr) == (svtype)SVTYPEMASK) {
13415 #ifdef DEBUG_LEAKING_SCALARS_ABORT
13416 abort();
13417 #endif
13418 return NULL;
13419 }
13420 /* look for it in the table first */
13421 dstr = MUTABLE_SV(ptr_table_fetch(PL_ptr_table, sstr));
13422 if (dstr)
13423 return dstr;
13424
13425 if(param->flags & CLONEf_JOIN_IN) {
13426 /** We are joining here so we don't want do clone
13427 something that is bad **/
13428 if (SvTYPE(sstr) == SVt_PVHV) {
13429 const HEK * const hvname = HvNAME_HEK(sstr);
13430 if (hvname) {
13431 /** don't clone stashes if they already exist **/
13432 dstr = MUTABLE_SV(gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname),
13433 HEK_UTF8(hvname) ? SVf_UTF8 : 0));
13434 ptr_table_store(PL_ptr_table, sstr, dstr);
13435 return dstr;
13436 }
13437 }
13438 else if (SvTYPE(sstr) == SVt_PVGV && !SvFAKE(sstr)) {
13439 HV *stash = GvSTASH(sstr);
13440 const HEK * hvname;
13441 if (stash && (hvname = HvNAME_HEK(stash))) {
13442 /** don't clone GVs if they already exist **/
13443 SV **svp;
13444 stash = gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname),
13445 HEK_UTF8(hvname) ? SVf_UTF8 : 0);
13446 svp = hv_fetch(
13447 stash, GvNAME(sstr),
13448 GvNAMEUTF8(sstr)
13449 ? -GvNAMELEN(sstr)
13450 : GvNAMELEN(sstr),
13451 0
13452 );
13453 if (svp && *svp && SvTYPE(*svp) == SVt_PVGV) {
13454 ptr_table_store(PL_ptr_table, sstr, *svp);
13455 return *svp;
13456 }
13457 }
13458 }
13459 }
13460
13461 /* create anew and remember what it is */
13462 new_SV(dstr);
13463
13464 #ifdef DEBUG_LEAKING_SCALARS
13465 dstr->sv_debug_optype = sstr->sv_debug_optype;
13466 dstr->sv_debug_line = sstr->sv_debug_line;
13467 dstr->sv_debug_inpad = sstr->sv_debug_inpad;
13468 dstr->sv_debug_parent = (SV*)sstr;
13469 FREE_SV_DEBUG_FILE(dstr);
13470 dstr->sv_debug_file = savesharedpv(sstr->sv_debug_file);
13471 #endif
13472
13473 ptr_table_store(PL_ptr_table, sstr, dstr);
13474
13475 /* clone */
13476 SvFLAGS(dstr) = SvFLAGS(sstr);
13477 SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */
13478 SvREFCNT(dstr) = 0; /* must be before any other dups! */
13479
13480 #ifdef DEBUGGING
13481 if (SvANY(sstr) && PL_watch_pvx && SvPVX_const(sstr) == PL_watch_pvx)
13482 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
13483 (void*)PL_watch_pvx, SvPVX_const(sstr));
13484 #endif
13485
13486 /* don't clone objects whose class has asked us not to */
13487 if (SvOBJECT(sstr)
13488 && ! (SvFLAGS(SvSTASH(sstr)) & SVphv_CLONEABLE))
13489 {
13490 SvFLAGS(dstr) = 0;
13491 return dstr;
13492 }
13493
13494 switch (SvTYPE(sstr)) {
13495 case SVt_NULL:
13496 SvANY(dstr) = NULL;
13497 break;
13498 case SVt_IV:
13499 SET_SVANY_FOR_BODYLESS_IV(dstr);
13500 if(SvROK(sstr)) {
13501 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
13502 } else {
13503 SvIV_set(dstr, SvIVX(sstr));
13504 }
13505 break;
13506 case SVt_NV:
13507 #if NVSIZE <= IVSIZE
13508 SET_SVANY_FOR_BODYLESS_NV(dstr);
13509 #else
13510 SvANY(dstr) = new_XNV();
13511 #endif
13512 SvNV_set(dstr, SvNVX(sstr));
13513 break;
13514 default:
13515 {
13516 /* These are all the types that need complex bodies allocating. */
13517 void *new_body;
13518 const svtype sv_type = SvTYPE(sstr);
13519 const struct body_details *const sv_type_details
13520 = bodies_by_type + sv_type;
13521
13522 switch (sv_type) {
13523 default:
13524 Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(sstr));
13525 break;
13526
13527 case SVt_PVGV:
13528 case SVt_PVIO:
13529 case SVt_PVFM:
13530 case SVt_PVHV:
13531 case SVt_PVAV:
13532 case SVt_PVCV:
13533 case SVt_PVLV:
13534 case SVt_REGEXP:
13535 case SVt_PVMG:
13536 case SVt_PVNV:
13537 case SVt_PVIV:
13538 case SVt_INVLIST:
13539 case SVt_PV:
13540 assert(sv_type_details->body_size);
13541 if (sv_type_details->arena) {
13542 new_body_inline(new_body, sv_type);
13543 new_body
13544 = (void*)((char*)new_body - sv_type_details->offset);
13545 } else {
13546 new_body = new_NOARENA(sv_type_details);
13547 }
13548 }
13549 assert(new_body);
13550 SvANY(dstr) = new_body;
13551
13552 #ifndef PURIFY
13553 Copy(((char*)SvANY(sstr)) + sv_type_details->offset,
13554 ((char*)SvANY(dstr)) + sv_type_details->offset,
13555 sv_type_details->copy, char);
13556 #else
13557 Copy(((char*)SvANY(sstr)),
13558 ((char*)SvANY(dstr)),
13559 sv_type_details->body_size + sv_type_details->offset, char);
13560 #endif
13561
13562 if (sv_type != SVt_PVAV && sv_type != SVt_PVHV
13563 && !isGV_with_GP(dstr)
13564 && !isREGEXP(dstr)
13565 && !(sv_type == SVt_PVIO && !(IoFLAGS(dstr) & IOf_FAKE_DIRP)))
13566 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
13567
13568 /* The Copy above means that all the source (unduplicated) pointers
13569 are now in the destination. We can check the flags and the
13570 pointers in either, but it's possible that there's less cache
13571 missing by always going for the destination.
13572 FIXME - instrument and check that assumption */
13573 if (sv_type >= SVt_PVMG) {
13574 if (SvMAGIC(dstr))
13575 SvMAGIC_set(dstr, mg_dup(SvMAGIC(dstr), param));
13576 if (SvOBJECT(dstr) && SvSTASH(dstr))
13577 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(dstr), param));
13578 else SvSTASH_set(dstr, 0); /* don't copy DESTROY cache */
13579 }
13580
13581 /* The cast silences a GCC warning about unhandled types. */
13582 switch ((int)sv_type) {
13583 case SVt_PV:
13584 break;
13585 case SVt_PVIV:
13586 break;
13587 case SVt_PVNV:
13588 break;
13589 case SVt_PVMG:
13590 break;
13591 case SVt_REGEXP:
13592 duprex:
13593 /* FIXME for plugins */
13594 dstr->sv_u.svu_rx = ((REGEXP *)dstr)->sv_any;
13595 re_dup_guts((REGEXP*) sstr, (REGEXP*) dstr, param);
13596 break;
13597 case SVt_PVLV:
13598 /* XXX LvTARGOFF sometimes holds PMOP* when DEBUGGING */
13599 if (LvTYPE(dstr) == 't') /* for tie: unrefcnted fake (SV**) */
13600 LvTARG(dstr) = dstr;
13601 else if (LvTYPE(dstr) == 'T') /* for tie: fake HE */
13602 LvTARG(dstr) = MUTABLE_SV(he_dup((HE*)LvTARG(dstr), 0, param));
13603 else
13604 LvTARG(dstr) = sv_dup_inc(LvTARG(dstr), param);
13605 if (isREGEXP(sstr)) goto duprex;
13606 case SVt_PVGV:
13607 /* non-GP case already handled above */
13608 if(isGV_with_GP(sstr)) {
13609 GvNAME_HEK(dstr) = hek_dup(GvNAME_HEK(dstr), param);
13610 /* Don't call sv_add_backref here as it's going to be
13611 created as part of the magic cloning of the symbol
13612 table--unless this is during a join and the stash
13613 is not actually being cloned. */
13614 /* Danger Will Robinson - GvGP(dstr) isn't initialised
13615 at the point of this comment. */
13616 GvSTASH(dstr) = hv_dup(GvSTASH(dstr), param);
13617 if (param->flags & CLONEf_JOIN_IN)
13618 Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dstr)), dstr);
13619 GvGP_set(dstr, gp_dup(GvGP(sstr), param));
13620 (void)GpREFCNT_inc(GvGP(dstr));
13621 }
13622 break;
13623 case SVt_PVIO:
13624 /* PL_parser->rsfp_filters entries have fake IoDIRP() */
13625 if(IoFLAGS(dstr) & IOf_FAKE_DIRP) {
13626 /* I have no idea why fake dirp (rsfps)
13627 should be treated differently but otherwise
13628 we end up with leaks -- sky*/
13629 IoTOP_GV(dstr) = gv_dup_inc(IoTOP_GV(dstr), param);
13630 IoFMT_GV(dstr) = gv_dup_inc(IoFMT_GV(dstr), param);
13631 IoBOTTOM_GV(dstr) = gv_dup_inc(IoBOTTOM_GV(dstr), param);
13632 } else {
13633 IoTOP_GV(dstr) = gv_dup(IoTOP_GV(dstr), param);
13634 IoFMT_GV(dstr) = gv_dup(IoFMT_GV(dstr), param);
13635 IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(dstr), param);
13636 if (IoDIRP(dstr)) {
13637 IoDIRP(dstr) = dirp_dup(IoDIRP(dstr), param);
13638 } else {
13639 NOOP;
13640 /* IoDIRP(dstr) is already a copy of IoDIRP(sstr) */
13641 }
13642 IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(dstr), param);
13643 }
13644 if (IoOFP(dstr) == IoIFP(sstr))
13645 IoOFP(dstr) = IoIFP(dstr);
13646 else
13647 IoOFP(dstr) = fp_dup(IoOFP(dstr), IoTYPE(dstr), param);
13648 IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(dstr));
13649 IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(dstr));
13650 IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(dstr));
13651 break;
13652 case SVt_PVAV:
13653 /* avoid cloning an empty array */
13654 if (AvARRAY((const AV *)sstr) && AvFILLp((const AV *)sstr) >= 0) {
13655 SV **dst_ary, **src_ary;
13656 SSize_t items = AvFILLp((const AV *)sstr) + 1;
13657
13658 src_ary = AvARRAY((const AV *)sstr);
13659 Newxz(dst_ary, AvMAX((const AV *)sstr)+1, SV*);
13660 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
13661 AvARRAY(MUTABLE_AV(dstr)) = dst_ary;
13662 AvALLOC((const AV *)dstr) = dst_ary;
13663 if (AvREAL((const AV *)sstr)) {
13664 dst_ary = sv_dup_inc_multiple(src_ary, dst_ary, items,
13665 param);
13666 }
13667 else {
13668 while (items-- > 0)
13669 *dst_ary++ = sv_dup(*src_ary++, param);
13670 }
13671 items = AvMAX((const AV *)sstr) - AvFILLp((const AV *)sstr);
13672 while (items-- > 0) {
13673 *dst_ary++ = NULL;
13674 }
13675 }
13676 else {
13677 AvARRAY(MUTABLE_AV(dstr)) = NULL;
13678 AvALLOC((const AV *)dstr) = (SV**)NULL;
13679 AvMAX( (const AV *)dstr) = -1;
13680 AvFILLp((const AV *)dstr) = -1;
13681 }
13682 break;
13683 case SVt_PVHV:
13684 if (HvARRAY((const HV *)sstr)) {
13685 STRLEN i = 0;
13686 const bool sharekeys = !!HvSHAREKEYS(sstr);
13687 XPVHV * const dxhv = (XPVHV*)SvANY(dstr);
13688 XPVHV * const sxhv = (XPVHV*)SvANY(sstr);
13689 char *darray;
13690 Newx(darray, PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1)
13691 + (SvOOK(sstr) ? sizeof(struct xpvhv_aux) : 0),
13692 char);
13693 HvARRAY(dstr) = (HE**)darray;
13694 while (i <= sxhv->xhv_max) {
13695 const HE * const source = HvARRAY(sstr)[i];
13696 HvARRAY(dstr)[i] = source
13697 ? he_dup(source, sharekeys, param) : 0;
13698 ++i;
13699 }
13700 if (SvOOK(sstr)) {
13701 const struct xpvhv_aux * const saux = HvAUX(sstr);
13702 struct xpvhv_aux * const daux = HvAUX(dstr);
13703 /* This flag isn't copied. */
13704 SvOOK_on(dstr);
13705
13706 if (saux->xhv_name_count) {
13707 HEK ** const sname = saux->xhv_name_u.xhvnameu_names;
13708 const I32 count
13709 = saux->xhv_name_count < 0
13710 ? -saux->xhv_name_count
13711 : saux->xhv_name_count;
13712 HEK **shekp = sname + count;
13713 HEK **dhekp;
13714 Newx(daux->xhv_name_u.xhvnameu_names, count, HEK *);
13715 dhekp = daux->xhv_name_u.xhvnameu_names + count;
13716 while (shekp-- > sname) {
13717 dhekp--;
13718 *dhekp = hek_dup(*shekp, param);
13719 }
13720 }
13721 else {
13722 daux->xhv_name_u.xhvnameu_name
13723 = hek_dup(saux->xhv_name_u.xhvnameu_name,
13724 param);
13725 }
13726 daux->xhv_name_count = saux->xhv_name_count;
13727
13728 daux->xhv_fill_lazy = saux->xhv_fill_lazy;
13729 daux->xhv_aux_flags = saux->xhv_aux_flags;
13730 #ifdef PERL_HASH_RANDOMIZE_KEYS
13731 daux->xhv_rand = saux->xhv_rand;
13732 daux->xhv_last_rand = saux->xhv_last_rand;
13733 #endif
13734 daux->xhv_riter = saux->xhv_riter;
13735 daux->xhv_eiter = saux->xhv_eiter
13736 ? he_dup(saux->xhv_eiter,
13737 cBOOL(HvSHAREKEYS(sstr)), param) : 0;
13738 /* backref array needs refcnt=2; see sv_add_backref */
13739 daux->xhv_backreferences =
13740 (param->flags & CLONEf_JOIN_IN)
13741 /* when joining, we let the individual GVs and
13742 * CVs add themselves to backref as
13743 * needed. This avoids pulling in stuff
13744 * that isn't required, and simplifies the
13745 * case where stashes aren't cloned back
13746 * if they already exist in the parent
13747 * thread */
13748 ? NULL
13749 : saux->xhv_backreferences
13750 ? (SvTYPE(saux->xhv_backreferences) == SVt_PVAV)
13751 ? MUTABLE_AV(SvREFCNT_inc(
13752 sv_dup_inc((const SV *)
13753 saux->xhv_backreferences, param)))
13754 : MUTABLE_AV(sv_dup((const SV *)
13755 saux->xhv_backreferences, param))
13756 : 0;
13757
13758 daux->xhv_mro_meta = saux->xhv_mro_meta
13759 ? mro_meta_dup(saux->xhv_mro_meta, param)
13760 : 0;
13761
13762 /* Record stashes for possible cloning in Perl_clone(). */
13763 if (HvNAME(sstr))
13764 av_push(param->stashes, dstr);
13765 }
13766 }
13767 else
13768 HvARRAY(MUTABLE_HV(dstr)) = NULL;
13769 break;
13770 case SVt_PVCV:
13771 if (!(param->flags & CLONEf_COPY_STACKS)) {
13772 CvDEPTH(dstr) = 0;
13773 }
13774 /* FALLTHROUGH */
13775 case SVt_PVFM:
13776 /* NOTE: not refcounted */
13777 SvANY(MUTABLE_CV(dstr))->xcv_stash =
13778 hv_dup(CvSTASH(dstr), param);
13779 if ((param->flags & CLONEf_JOIN_IN) && CvSTASH(dstr))
13780 Perl_sv_add_backref(aTHX_ MUTABLE_SV(CvSTASH(dstr)), dstr);
13781 if (!CvISXSUB(dstr)) {
13782 OP_REFCNT_LOCK;
13783 CvROOT(dstr) = OpREFCNT_inc(CvROOT(dstr));
13784 OP_REFCNT_UNLOCK;
13785 CvSLABBED_off(dstr);
13786 } else if (CvCONST(dstr)) {
13787 CvXSUBANY(dstr).any_ptr =
13788 sv_dup_inc((const SV *)CvXSUBANY(dstr).any_ptr, param);
13789 }
13790 assert(!CvSLABBED(dstr));
13791 if (CvDYNFILE(dstr)) CvFILE(dstr) = SAVEPV(CvFILE(dstr));
13792 if (CvNAMED(dstr))
13793 SvANY((CV *)dstr)->xcv_gv_u.xcv_hek =
13794 hek_dup(CvNAME_HEK((CV *)sstr), param);
13795 /* don't dup if copying back - CvGV isn't refcounted, so the
13796 * duped GV may never be freed. A bit of a hack! DAPM */
13797 else
13798 SvANY(MUTABLE_CV(dstr))->xcv_gv_u.xcv_gv =
13799 CvCVGV_RC(dstr)
13800 ? gv_dup_inc(CvGV(sstr), param)
13801 : (param->flags & CLONEf_JOIN_IN)
13802 ? NULL
13803 : gv_dup(CvGV(sstr), param);
13804
13805 if (!CvISXSUB(sstr)) {
13806 PADLIST * padlist = CvPADLIST(sstr);
13807 if(padlist)
13808 padlist = padlist_dup(padlist, param);
13809 CvPADLIST_set(dstr, padlist);
13810 } else
13811 /* unthreaded perl can't sv_dup so we dont support unthreaded's CvHSCXT */
13812 PoisonPADLIST(dstr);
13813
13814 CvOUTSIDE(dstr) =
13815 CvWEAKOUTSIDE(sstr)
13816 ? cv_dup( CvOUTSIDE(dstr), param)
13817 : cv_dup_inc(CvOUTSIDE(dstr), param);
13818 break;
13819 }
13820 }
13821 }
13822
13823 return dstr;
13824 }
13825
13826 SV *
13827 Perl_sv_dup_inc(pTHX_ const SV *const sstr, CLONE_PARAMS *const param)
13828 {
13829 PERL_ARGS_ASSERT_SV_DUP_INC;
13830 return sstr ? SvREFCNT_inc(sv_dup_common(sstr, param)) : NULL;
13831 }
13832
13833 SV *
13834 Perl_sv_dup(pTHX_ const SV *const sstr, CLONE_PARAMS *const param)
13835 {
13836 SV *dstr = sstr ? sv_dup_common(sstr, param) : NULL;
13837 PERL_ARGS_ASSERT_SV_DUP;
13838
13839 /* Track every SV that (at least initially) had a reference count of 0.
13840 We need to do this by holding an actual reference to it in this array.
13841 If we attempt to cheat, turn AvREAL_off(), and store only pointers
13842 (akin to the stashes hash, and the perl stack), we come unstuck if
13843 a weak reference (or other SV legitimately SvREFCNT() == 0 for this
13844 thread) is manipulated in a CLONE method, because CLONE runs before the
13845 unreferenced array is walked to find SVs still with SvREFCNT() == 0
13846 (and fix things up by giving each a reference via the temps stack).
13847 Instead, during CLONE, if the 0-referenced SV has SvREFCNT_inc() and
13848 then SvREFCNT_dec(), it will be cleaned up (and added to the free list)
13849 before the walk of unreferenced happens and a reference to that is SV
13850 added to the temps stack. At which point we have the same SV considered
13851 to be in use, and free to be re-used. Not good.
13852 */
13853 if (dstr && !(param->flags & CLONEf_COPY_STACKS) && !SvREFCNT(dstr)) {
13854 assert(param->unreferenced);
13855 av_push(param->unreferenced, SvREFCNT_inc(dstr));
13856 }
13857
13858 return dstr;
13859 }
13860
13861 /* duplicate a context */
13862
13863 PERL_CONTEXT *
13864 Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
13865 {
13866 PERL_CONTEXT *ncxs;
13867
13868 PERL_ARGS_ASSERT_CX_DUP;
13869
13870 if (!cxs)
13871 return (PERL_CONTEXT*)NULL;
13872
13873 /* look for it in the table first */
13874 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
13875 if (ncxs)
13876 return ncxs;
13877
13878 /* create anew and remember what it is */
13879 Newx(ncxs, max + 1, PERL_CONTEXT);
13880 ptr_table_store(PL_ptr_table, cxs, ncxs);
13881 Copy(cxs, ncxs, max + 1, PERL_CONTEXT);
13882
13883 while (ix >= 0) {
13884 PERL_CONTEXT * const ncx = &ncxs[ix];
13885 if (CxTYPE(ncx) == CXt_SUBST) {
13886 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
13887 }
13888 else {
13889 ncx->blk_oldcop = (COP*)any_dup(ncx->blk_oldcop, param->proto_perl);
13890 switch (CxTYPE(ncx)) {
13891 case CXt_SUB:
13892 ncx->blk_sub.cv = (ncx->blk_sub.olddepth == 0
13893 ? cv_dup_inc(ncx->blk_sub.cv, param)
13894 : cv_dup(ncx->blk_sub.cv,param));
13895 if(CxHASARGS(ncx)){
13896 ncx->blk_sub.argarray = av_dup_inc(ncx->blk_sub.argarray,param);
13897 ncx->blk_sub.savearray = av_dup_inc(ncx->blk_sub.savearray,param);
13898 } else {
13899 ncx->blk_sub.argarray = NULL;
13900 ncx->blk_sub.savearray = NULL;
13901 }
13902 ncx->blk_sub.oldcomppad = (PAD*)ptr_table_fetch(PL_ptr_table,
13903 ncx->blk_sub.oldcomppad);
13904 break;
13905 case CXt_EVAL:
13906 ncx->blk_eval.old_namesv = sv_dup_inc(ncx->blk_eval.old_namesv,
13907 param);
13908 ncx->blk_eval.cur_text = sv_dup(ncx->blk_eval.cur_text, param);
13909 ncx->blk_eval.cv = cv_dup(ncx->blk_eval.cv, param);
13910 break;
13911 case CXt_LOOP_LAZYSV:
13912 ncx->blk_loop.state_u.lazysv.end
13913 = sv_dup_inc(ncx->blk_loop.state_u.lazysv.end, param);
13914 /* Fallthrough: duplicate lazysv.cur by using the ary.ary
13915 duplication code instead.
13916 We are taking advantage of (1) av_dup_inc and sv_dup_inc
13917 actually being the same function, and (2) order
13918 equivalence of the two unions.
13919 We can assert the later [but only at run time :-(] */
13920 assert ((void *) &ncx->blk_loop.state_u.ary.ary ==
13921 (void *) &ncx->blk_loop.state_u.lazysv.cur);
13922 /* FALLTHROUGH */
13923 case CXt_LOOP_FOR:
13924 ncx->blk_loop.state_u.ary.ary
13925 = av_dup_inc(ncx->blk_loop.state_u.ary.ary, param);
13926 /* FALLTHROUGH */
13927 case CXt_LOOP_LAZYIV:
13928 case CXt_LOOP_PLAIN:
13929 /* code common to all CXt_LOOP_* types */
13930 if (CxPADLOOP(ncx)) {
13931 ncx->blk_loop.itervar_u.oldcomppad
13932 = (PAD*)ptr_table_fetch(PL_ptr_table,
13933 ncx->blk_loop.itervar_u.oldcomppad);
13934 } else {
13935 ncx->blk_loop.itervar_u.gv
13936 = gv_dup((const GV *)ncx->blk_loop.itervar_u.gv,
13937 param);
13938 }
13939 break;
13940 case CXt_FORMAT:
13941 ncx->blk_format.cv = cv_dup(ncx->blk_format.cv, param);
13942 ncx->blk_format.gv = gv_dup(ncx->blk_format.gv, param);
13943 ncx->blk_format.dfoutgv = gv_dup_inc(ncx->blk_format.dfoutgv,
13944 param);
13945 break;
13946 case CXt_BLOCK:
13947 case CXt_NULL:
13948 case CXt_WHEN:
13949 case CXt_GIVEN:
13950 break;
13951 }
13952 }
13953 --ix;
13954 }
13955 return ncxs;
13956 }
13957
13958 /* duplicate a stack info structure */
13959
13960 PERL_SI *
13961 Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
13962 {
13963 PERL_SI *nsi;
13964
13965 PERL_ARGS_ASSERT_SI_DUP;
13966
13967 if (!si)
13968 return (PERL_SI*)NULL;
13969
13970 /* look for it in the table first */
13971 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
13972 if (nsi)
13973 return nsi;
13974
13975 /* create anew and remember what it is */
13976 Newxz(nsi, 1, PERL_SI);
13977 ptr_table_store(PL_ptr_table, si, nsi);
13978
13979 nsi->si_stack = av_dup_inc(si->si_stack, param);
13980 nsi->si_cxix = si->si_cxix;
13981 nsi->si_cxmax = si->si_cxmax;
13982 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
13983 nsi->si_type = si->si_type;
13984 nsi->si_prev = si_dup(si->si_prev, param);
13985 nsi->si_next = si_dup(si->si_next, param);
13986 nsi->si_markoff = si->si_markoff;
13987
13988 return nsi;
13989 }
13990
13991 #define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
13992 #define TOPINT(ss,ix) ((ss)[ix].any_i32)
13993 #define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
13994 #define TOPLONG(ss,ix) ((ss)[ix].any_long)
13995 #define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
13996 #define TOPIV(ss,ix) ((ss)[ix].any_iv)
13997 #define POPUV(ss,ix) ((ss)[--(ix)].any_uv)
13998 #define TOPUV(ss,ix) ((ss)[ix].any_uv)
13999 #define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool)
14000 #define TOPBOOL(ss,ix) ((ss)[ix].any_bool)
14001 #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
14002 #define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
14003 #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
14004 #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
14005 #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
14006 #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
14007
14008 /* XXXXX todo */
14009 #define pv_dup_inc(p) SAVEPV(p)
14010 #define pv_dup(p) SAVEPV(p)
14011 #define svp_dup_inc(p,pp) any_dup(p,pp)
14012
14013 /* map any object to the new equivent - either something in the
14014 * ptr table, or something in the interpreter structure
14015 */
14016
14017 void *
14018 Perl_any_dup(pTHX_ void *v, const PerlInterpreter *proto_perl)
14019 {
14020 void *ret;
14021
14022 PERL_ARGS_ASSERT_ANY_DUP;
14023
14024 if (!v)
14025 return (void*)NULL;
14026
14027 /* look for it in the table first */
14028 ret = ptr_table_fetch(PL_ptr_table, v);
14029 if (ret)
14030 return ret;
14031
14032 /* see if it is part of the interpreter structure */
14033 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
14034 ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
14035 else {
14036 ret = v;
14037 }
14038
14039 return ret;
14040 }
14041
14042 /* duplicate the save stack */
14043
14044 ANY *
14045 Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
14046 {
14047 dVAR;
14048 ANY * const ss = proto_perl->Isavestack;
14049 const I32 max = proto_perl->Isavestack_max;
14050 I32 ix = proto_perl->Isavestack_ix;
14051 ANY *nss;
14052 const SV *sv;
14053 const GV *gv;
14054 const AV *av;
14055 const HV *hv;
14056 void* ptr;
14057 int intval;
14058 long longval;
14059 GP *gp;
14060 IV iv;
14061 I32 i;
14062 char *c = NULL;
14063 void (*dptr) (void*);
14064 void (*dxptr) (pTHX_ void*);
14065
14066 PERL_ARGS_ASSERT_SS_DUP;
14067
14068 Newxz(nss, max, ANY);
14069
14070 while (ix > 0) {
14071 const UV uv = POPUV(ss,ix);
14072 const U8 type = (U8)uv & SAVE_MASK;
14073
14074 TOPUV(nss,ix) = uv;
14075 switch (type) {
14076 case SAVEt_CLEARSV:
14077 case SAVEt_CLEARPADRANGE:
14078 break;
14079 case SAVEt_HELEM: /* hash element */
14080 case SAVEt_SV: /* scalar reference */
14081 sv = (const SV *)POPPTR(ss,ix);
14082 TOPPTR(nss,ix) = SvREFCNT_inc(sv_dup_inc(sv, param));
14083 /* FALLTHROUGH */
14084 case SAVEt_ITEM: /* normal string */
14085 case SAVEt_GVSV: /* scalar slot in GV */
14086 sv = (const SV *)POPPTR(ss,ix);
14087 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
14088 if (type == SAVEt_SV)
14089 break;
14090 /* FALLTHROUGH */
14091 case SAVEt_FREESV:
14092 case SAVEt_MORTALIZESV:
14093 case SAVEt_READONLY_OFF:
14094 sv = (const SV *)POPPTR(ss,ix);
14095 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
14096 break;
14097 case SAVEt_FREEPADNAME:
14098 ptr = POPPTR(ss,ix);
14099 TOPPTR(nss,ix) = padname_dup((PADNAME *)ptr, param);
14100 PadnameREFCNT((PADNAME *)TOPPTR(nss,ix))++;
14101 break;
14102 case SAVEt_SHARED_PVREF: /* char* in shared space */
14103 c = (char*)POPPTR(ss,ix);
14104 TOPPTR(nss,ix) = savesharedpv(c);
14105 ptr = POPPTR(ss,ix);
14106 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
14107 break;
14108 case SAVEt_GENERIC_SVREF: /* generic sv */
14109 case SAVEt_SVREF: /* scalar reference */
14110 sv = (const SV *)POPPTR(ss,ix);
14111 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
14112 if (type == SAVEt_SVREF)
14113 SvREFCNT_inc_simple_void((SV *)TOPPTR(nss,ix));
14114 ptr = POPPTR(ss,ix);
14115 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
14116 break;
14117 case SAVEt_GVSLOT: /* any slot in GV */
14118 sv = (const SV *)POPPTR(ss,ix);
14119 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
14120 ptr = POPPTR(ss,ix);
14121 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
14122 sv = (const SV *)POPPTR(ss,ix);
14123 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
14124 break;
14125 case SAVEt_HV: /* hash reference */
14126 case SAVEt_AV: /* array reference */
14127 sv = (const SV *) POPPTR(ss,ix);
14128 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
14129 /* FALLTHROUGH */
14130 case SAVEt_COMPPAD:
14131 case SAVEt_NSTAB:
14132 sv = (const SV *) POPPTR(ss,ix);
14133 TOPPTR(nss,ix) = sv_dup(sv, param);
14134 break;
14135 case SAVEt_INT: /* int reference */
14136 ptr = POPPTR(ss,ix);
14137 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
14138 intval = (int)POPINT(ss,ix);
14139 TOPINT(nss,ix) = intval;
14140 break;
14141 case SAVEt_LONG: /* long reference */
14142 ptr = POPPTR(ss,ix);
14143 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
14144 longval = (long)POPLONG(ss,ix);
14145 TOPLONG(nss,ix) = longval;
14146 break;
14147 case SAVEt_I32: /* I32 reference */
14148 ptr = POPPTR(ss,ix);
14149 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
14150 i = POPINT(ss,ix);
14151 TOPINT(nss,ix) = i;
14152 break;
14153 case SAVEt_IV: /* IV reference */
14154 case SAVEt_STRLEN: /* STRLEN/size_t ref */
14155 ptr = POPPTR(ss,ix);
14156 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
14157 iv = POPIV(ss,ix);
14158 TOPIV(nss,ix) = iv;
14159 break;
14160 case SAVEt_HPTR: /* HV* reference */
14161 case SAVEt_APTR: /* AV* reference */
14162 case SAVEt_SPTR: /* SV* reference */
14163 ptr = POPPTR(ss,ix);
14164 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
14165 sv = (const SV *)POPPTR(ss,ix);
14166 TOPPTR(nss,ix) = sv_dup(sv, param);
14167 break;
14168 case SAVEt_VPTR: /* random* reference */
14169 ptr = POPPTR(ss,ix);
14170 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
14171 /* FALLTHROUGH */
14172 case SAVEt_INT_SMALL:
14173 case SAVEt_I32_SMALL:
14174 case SAVEt_I16: /* I16 reference */
14175 case SAVEt_I8: /* I8 reference */
14176 case SAVEt_BOOL:
14177 ptr = POPPTR(ss,ix);
14178 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
14179 break;
14180 case SAVEt_GENERIC_PVREF: /* generic char* */
14181 case SAVEt_PPTR: /* char* reference */
14182 ptr = POPPTR(ss,ix);
14183 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
14184 c = (char*)POPPTR(ss,ix);
14185 TOPPTR(nss,ix) = pv_dup(c);
14186 break;
14187 case SAVEt_GP: /* scalar reference */
14188 gp = (GP*)POPPTR(ss,ix);
14189 TOPPTR(nss,ix) = gp = gp_dup(gp, param);
14190 (void)GpREFCNT_inc(gp);
14191 gv = (const GV *)POPPTR(ss,ix);
14192 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
14193 break;
14194 case SAVEt_FREEOP:
14195 ptr = POPPTR(ss,ix);
14196 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
14197 /* these are assumed to be refcounted properly */
14198 OP *o;
14199 switch (((OP*)ptr)->op_type) {
14200 case OP_LEAVESUB:
14201 case OP_LEAVESUBLV:
14202 case OP_LEAVEEVAL:
14203 case OP_LEAVE:
14204 case OP_SCOPE:
14205 case OP_LEAVEWRITE:
14206 TOPPTR(nss,ix) = ptr;
14207 o = (OP*)ptr;
14208 OP_REFCNT_LOCK;
14209 (void) OpREFCNT_inc(o);
14210 OP_REFCNT_UNLOCK;
14211 break;
14212 default:
14213 TOPPTR(nss,ix) = NULL;
14214 break;
14215 }
14216 }
14217 else
14218 TOPPTR(nss,ix) = NULL;
14219 break;
14220 case SAVEt_FREECOPHH:
14221 ptr = POPPTR(ss,ix);
14222 TOPPTR(nss,ix) = cophh_copy((COPHH *)ptr);
14223 break;
14224 case SAVEt_ADELETE:
14225 av = (const AV *)POPPTR(ss,ix);
14226 TOPPTR(nss,ix) = av_dup_inc(av, param);
14227 i = POPINT(ss,ix);
14228 TOPINT(nss,ix) = i;
14229 break;
14230 case SAVEt_DELETE:
14231 hv = (const HV *)POPPTR(ss,ix);
14232 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
14233 i = POPINT(ss,ix);
14234 TOPINT(nss,ix) = i;
14235 /* FALLTHROUGH */
14236 case SAVEt_FREEPV:
14237 c = (char*)POPPTR(ss,ix);
14238 TOPPTR(nss,ix) = pv_dup_inc(c);
14239 break;
14240 case SAVEt_STACK_POS: /* Position on Perl stack */
14241 i = POPINT(ss,ix);
14242 TOPINT(nss,ix) = i;
14243 break;
14244 case SAVEt_DESTRUCTOR:
14245 ptr = POPPTR(ss,ix);
14246 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
14247 dptr = POPDPTR(ss,ix);
14248 TOPDPTR(nss,ix) = DPTR2FPTR(void (*)(void*),
14249 any_dup(FPTR2DPTR(void *, dptr),
14250 proto_perl));
14251 break;
14252 case SAVEt_DESTRUCTOR_X:
14253 ptr = POPPTR(ss,ix);
14254 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
14255 dxptr = POPDXPTR(ss,ix);
14256 TOPDXPTR(nss,ix) = DPTR2FPTR(void (*)(pTHX_ void*),
14257 any_dup(FPTR2DPTR(void *, dxptr),
14258 proto_perl));
14259 break;
14260 case SAVEt_REGCONTEXT:
14261 case SAVEt_ALLOC:
14262 ix -= uv >> SAVE_TIGHT_SHIFT;
14263 break;
14264 case SAVEt_AELEM: /* array element */
14265 sv = (const SV *)POPPTR(ss,ix);
14266 TOPPTR(nss,ix) = SvREFCNT_inc(sv_dup_inc(sv, param));
14267 i = POPINT(ss,ix);
14268 TOPINT(nss,ix) = i;
14269 av = (const AV *)POPPTR(ss,ix);
14270 TOPPTR(nss,ix) = av_dup_inc(av, param);
14271 break;
14272 case SAVEt_OP:
14273 ptr = POPPTR(ss,ix);
14274 TOPPTR(nss,ix) = ptr;
14275 break;
14276 case SAVEt_HINTS:
14277 ptr = POPPTR(ss,ix);
14278 ptr = cophh_copy((COPHH*)ptr);
14279 TOPPTR(nss,ix) = ptr;
14280 i = POPINT(ss,ix);
14281 TOPINT(nss,ix) = i;
14282 if (i & HINT_LOCALIZE_HH) {
14283 hv = (const HV *)POPPTR(ss,ix);
14284 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
14285 }
14286 break;
14287 case SAVEt_PADSV_AND_MORTALIZE:
14288 longval = (long)POPLONG(ss,ix);
14289 TOPLONG(nss,ix) = longval;
14290 ptr = POPPTR(ss,ix);
14291 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
14292 sv = (const SV *)POPPTR(ss,ix);
14293 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
14294 break;
14295 case SAVEt_SET_SVFLAGS:
14296 i = POPINT(ss,ix);
14297 TOPINT(nss,ix) = i;
14298 i = POPINT(ss,ix);
14299 TOPINT(nss,ix) = i;
14300 sv = (const SV *)POPPTR(ss,ix);
14301 TOPPTR(nss,ix) = sv_dup(sv, param);
14302 break;
14303 case SAVEt_COMPILE_WARNINGS:
14304 ptr = POPPTR(ss,ix);
14305 TOPPTR(nss,ix) = DUP_WARNINGS((STRLEN*)ptr);
14306 break;
14307 case SAVEt_PARSER:
14308 ptr = POPPTR(ss,ix);
14309 TOPPTR(nss,ix) = parser_dup((const yy_parser*)ptr, param);
14310 break;
14311 case SAVEt_GP_ALIASED_SV: {
14312 GP * gp_ptr = (GP *)POPPTR(ss,ix);
14313 GP * new_gp_ptr = gp_dup(gp_ptr, param);
14314 TOPPTR(nss,ix) = new_gp_ptr;
14315 new_gp_ptr->gp_refcnt++;
14316 break;
14317 }
14318 default:
14319 Perl_croak(aTHX_
14320 "panic: ss_dup inconsistency (%"IVdf")", (IV) type);
14321 }
14322 }
14323
14324 return nss;
14325 }
14326
14327
14328 /* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
14329 * flag to the result. This is done for each stash before cloning starts,
14330 * so we know which stashes want their objects cloned */
14331
14332 static void
14333 do_mark_cloneable_stash(pTHX_ SV *const sv)
14334 {
14335 const HEK * const hvname = HvNAME_HEK((const HV *)sv);
14336 if (hvname) {
14337 GV* const cloner = gv_fetchmethod_autoload(MUTABLE_HV(sv), "CLONE_SKIP", 0);
14338 SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
14339 if (cloner && GvCV(cloner)) {
14340 dSP;
14341 UV status;
14342
14343 ENTER;
14344 SAVETMPS;
14345 PUSHMARK(SP);
14346 mXPUSHs(newSVhek(hvname));
14347 PUTBACK;
14348 call_sv(MUTABLE_SV(GvCV(cloner)), G_SCALAR);
14349 SPAGAIN;
14350 status = POPu;
14351 PUTBACK;
14352 FREETMPS;
14353 LEAVE;
14354 if (status)
14355 SvFLAGS(sv) &= ~SVphv_CLONEABLE;
14356 }
14357 }
14358 }
14359
14360
14361
14362 /*
14363 =for apidoc perl_clone
14364
14365 Create and return a new interpreter by cloning the current one.
14366
14367 perl_clone takes these flags as parameters:
14368
14369 CLONEf_COPY_STACKS - is used to, well, copy the stacks also,
14370 without it we only clone the data and zero the stacks,
14371 with it we copy the stacks and the new perl interpreter is
14372 ready to run at the exact same point as the previous one.
14373 The pseudo-fork code uses COPY_STACKS while the
14374 threads->create doesn't.
14375
14376 CLONEf_KEEP_PTR_TABLE -
14377 perl_clone keeps a ptr_table with the pointer of the old
14378 variable as a key and the new variable as a value,
14379 this allows it to check if something has been cloned and not
14380 clone it again but rather just use the value and increase the
14381 refcount. If KEEP_PTR_TABLE is not set then perl_clone will kill
14382 the ptr_table using the function
14383 C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>,
14384 reason to keep it around is if you want to dup some of your own
14385 variable who are outside the graph perl scans, example of this
14386 code is in threads.xs create.
14387
14388 CLONEf_CLONE_HOST -
14389 This is a win32 thing, it is ignored on unix, it tells perls
14390 win32host code (which is c++) to clone itself, this is needed on
14391 win32 if you want to run two threads at the same time,
14392 if you just want to do some stuff in a separate perl interpreter
14393 and then throw it away and return to the original one,
14394 you don't need to do anything.
14395
14396 =cut
14397 */
14398
14399 /* XXX the above needs expanding by someone who actually understands it ! */
14400 EXTERN_C PerlInterpreter *
14401 perl_clone_host(PerlInterpreter* proto_perl, UV flags);
14402
14403 PerlInterpreter *
14404 perl_clone(PerlInterpreter *proto_perl, UV flags)
14405 {
14406 dVAR;
14407 #ifdef PERL_IMPLICIT_SYS
14408
14409 PERL_ARGS_ASSERT_PERL_CLONE;
14410
14411 /* perlhost.h so we need to call into it
14412 to clone the host, CPerlHost should have a c interface, sky */
14413
14414 if (flags & CLONEf_CLONE_HOST) {
14415 return perl_clone_host(proto_perl,flags);
14416 }
14417 return perl_clone_using(proto_perl, flags,
14418 proto_perl->IMem,
14419 proto_perl->IMemShared,
14420 proto_perl->IMemParse,
14421 proto_perl->IEnv,
14422 proto_perl->IStdIO,
14423 proto_perl->ILIO,
14424 proto_perl->IDir,
14425 proto_perl->ISock,
14426 proto_perl->IProc);
14427 }
14428
14429 PerlInterpreter *
14430 perl_clone_using(PerlInterpreter *proto_perl, UV flags,
14431 struct IPerlMem* ipM, struct IPerlMem* ipMS,
14432 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
14433 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
14434 struct IPerlDir* ipD, struct IPerlSock* ipS,
14435 struct IPerlProc* ipP)
14436 {
14437 /* XXX many of the string copies here can be optimized if they're
14438 * constants; they need to be allocated as common memory and just
14439 * their pointers copied. */
14440
14441 IV i;
14442 CLONE_PARAMS clone_params;
14443 CLONE_PARAMS* const param = &clone_params;
14444
14445 PerlInterpreter * const my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
14446
14447 PERL_ARGS_ASSERT_PERL_CLONE_USING;
14448 #else /* !PERL_IMPLICIT_SYS */
14449 IV i;
14450 CLONE_PARAMS clone_params;
14451 CLONE_PARAMS* param = &clone_params;
14452 PerlInterpreter * const my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
14453
14454 PERL_ARGS_ASSERT_PERL_CLONE;
14455 #endif /* PERL_IMPLICIT_SYS */
14456
14457 /* for each stash, determine whether its objects should be cloned */
14458 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
14459 PERL_SET_THX(my_perl);
14460
14461 #ifdef DEBUGGING
14462 PoisonNew(my_perl, 1, PerlInterpreter);
14463 PL_op = NULL;
14464 PL_curcop = NULL;
14465 PL_defstash = NULL; /* may be used by perl malloc() */
14466 PL_markstack = 0;
14467 PL_scopestack = 0;
14468 PL_scopestack_name = 0;
14469 PL_savestack = 0;
14470 PL_savestack_ix = 0;
14471 PL_savestack_max = -1;
14472 PL_sig_pending = 0;
14473 PL_parser = NULL;
14474 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
14475 Zero(&PL_padname_undef, 1, PADNAME);
14476 Zero(&PL_padname_const, 1, PADNAME);
14477 # ifdef DEBUG_LEAKING_SCALARS
14478 PL_sv_serial = (((UV)my_perl >> 2) & 0xfff) * 1000000;
14479 # endif
14480 #else /* !DEBUGGING */
14481 Zero(my_perl, 1, PerlInterpreter);
14482 #endif /* DEBUGGING */
14483
14484 #ifdef PERL_IMPLICIT_SYS
14485 /* host pointers */
14486 PL_Mem = ipM;
14487 PL_MemShared = ipMS;
14488 PL_MemParse = ipMP;
14489 PL_Env = ipE;
14490 PL_StdIO = ipStd;
14491 PL_LIO = ipLIO;
14492 PL_Dir = ipD;
14493 PL_Sock = ipS;
14494 PL_Proc = ipP;
14495 #endif /* PERL_IMPLICIT_SYS */
14496
14497
14498 param->flags = flags;
14499 /* Nothing in the core code uses this, but we make it available to
14500 extensions (using mg_dup). */
14501 param->proto_perl = proto_perl;
14502 /* Likely nothing will use this, but it is initialised to be consistent
14503 with Perl_clone_params_new(). */
14504 param->new_perl = my_perl;
14505 param->unreferenced = NULL;
14506
14507
14508 INIT_TRACK_MEMPOOL(my_perl->Imemory_debug_header, my_perl);
14509
14510 PL_body_arenas = NULL;
14511 Zero(&PL_body_roots, 1, PL_body_roots);
14512
14513 PL_sv_count = 0;
14514 PL_sv_root = NULL;
14515 PL_sv_arenaroot = NULL;
14516
14517 PL_debug = proto_perl->Idebug;
14518
14519 /* dbargs array probably holds garbage */
14520 PL_dbargs = NULL;
14521
14522 PL_compiling = proto_perl->Icompiling;
14523
14524 /* pseudo environmental stuff */
14525 PL_origargc = proto_perl->Iorigargc;
14526 PL_origargv = proto_perl->Iorigargv;
14527
14528 #ifndef NO_TAINT_SUPPORT
14529 /* Set tainting stuff before PerlIO_debug can possibly get called */
14530 PL_tainting = proto_perl->Itainting;
14531 PL_taint_warn = proto_perl->Itaint_warn;
14532 #else
14533 PL_tainting = FALSE;
14534 PL_taint_warn = FALSE;
14535 #endif
14536
14537 PL_minus_c = proto_perl->Iminus_c;
14538
14539 PL_localpatches = proto_perl->Ilocalpatches;
14540 PL_splitstr = proto_perl->Isplitstr;
14541 PL_minus_n = proto_perl->Iminus_n;
14542 PL_minus_p = proto_perl->Iminus_p;
14543 PL_minus_l = proto_perl->Iminus_l;
14544 PL_minus_a = proto_perl->Iminus_a;
14545 PL_minus_E = proto_perl->Iminus_E;
14546 PL_minus_F = proto_perl->Iminus_F;
14547 PL_doswitches = proto_perl->Idoswitches;
14548 PL_dowarn = proto_perl->Idowarn;
14549 PL_sawalias = proto_perl->Isawalias;
14550 #ifdef PERL_SAWAMPERSAND
14551 PL_sawampersand = proto_perl->Isawampersand;
14552 #endif
14553 PL_unsafe = proto_perl->Iunsafe;
14554 PL_perldb = proto_perl->Iperldb;
14555 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
14556 PL_exit_flags = proto_perl->Iexit_flags;
14557
14558 /* XXX time(&PL_basetime) when asked for? */
14559 PL_basetime = proto_perl->Ibasetime;
14560
14561 PL_maxsysfd = proto_perl->Imaxsysfd;
14562 PL_statusvalue = proto_perl->Istatusvalue;
14563 #ifdef __VMS
14564 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
14565 #else
14566 PL_statusvalue_posix = proto_perl->Istatusvalue_posix;
14567 #endif
14568
14569 /* RE engine related */
14570 PL_regmatch_slab = NULL;
14571 PL_reg_curpm = NULL;
14572
14573 PL_sub_generation = proto_perl->Isub_generation;
14574
14575 /* funky return mechanisms */
14576 PL_forkprocess = proto_perl->Iforkprocess;
14577
14578 /* internal state */
14579 PL_maxo = proto_perl->Imaxo;
14580
14581 PL_main_start = proto_perl->Imain_start;
14582 PL_eval_root = proto_perl->Ieval_root;
14583 PL_eval_start = proto_perl->Ieval_start;
14584
14585 PL_filemode = proto_perl->Ifilemode;
14586 PL_lastfd = proto_perl->Ilastfd;
14587 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
14588 PL_Argv = NULL;
14589 PL_Cmd = NULL;
14590 PL_gensym = proto_perl->Igensym;
14591
14592 PL_laststatval = proto_perl->Ilaststatval;
14593 PL_laststype = proto_perl->Ilaststype;
14594 PL_mess_sv = NULL;
14595
14596 PL_profiledata = NULL;
14597
14598 PL_generation = proto_perl->Igeneration;
14599
14600 PL_in_clean_objs = proto_perl->Iin_clean_objs;
14601 PL_in_clean_all = proto_perl->Iin_clean_all;
14602
14603 PL_delaymagic_uid = proto_perl->Idelaymagic_uid;
14604 PL_delaymagic_euid = proto_perl->Idelaymagic_euid;
14605 PL_delaymagic_gid = proto_perl->Idelaymagic_gid;
14606 PL_delaymagic_egid = proto_perl->Idelaymagic_egid;
14607 PL_nomemok = proto_perl->Inomemok;
14608 PL_an = proto_perl->Ian;
14609 PL_evalseq = proto_perl->Ievalseq;
14610 PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */
14611 PL_origalen = proto_perl->Iorigalen;
14612
14613 PL_sighandlerp = proto_perl->Isighandlerp;
14614
14615 PL_runops = proto_perl->Irunops;
14616
14617 PL_subline = proto_perl->Isubline;
14618
14619 PL_cv_has_eval = proto_perl->Icv_has_eval;
14620
14621 #ifdef FCRYPT
14622 PL_cryptseen = proto_perl->Icryptseen;
14623 #endif
14624
14625 #ifdef USE_LOCALE_COLLATE
14626 PL_collation_ix = proto_perl->Icollation_ix;
14627 PL_collation_standard = proto_perl->Icollation_standard;
14628 PL_collxfrm_base = proto_perl->Icollxfrm_base;
14629 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
14630 #endif /* USE_LOCALE_COLLATE */
14631
14632 #ifdef USE_LOCALE_NUMERIC
14633 PL_numeric_standard = proto_perl->Inumeric_standard;
14634 PL_numeric_local = proto_perl->Inumeric_local;
14635 #endif /* !USE_LOCALE_NUMERIC */
14636
14637 /* Did the locale setup indicate UTF-8? */
14638 PL_utf8locale = proto_perl->Iutf8locale;
14639 PL_in_utf8_CTYPE_locale = proto_perl->Iin_utf8_CTYPE_locale;
14640 /* Unicode features (see perlrun/-C) */
14641 PL_unicode = proto_perl->Iunicode;
14642
14643 /* Pre-5.8 signals control */
14644 PL_signals = proto_perl->Isignals;
14645
14646 /* times() ticks per second */
14647 PL_clocktick = proto_perl->Iclocktick;
14648
14649 /* Recursion stopper for PerlIO_find_layer */
14650 PL_in_load_module = proto_perl->Iin_load_module;
14651
14652 /* sort() routine */
14653 PL_sort_RealCmp = proto_perl->Isort_RealCmp;
14654
14655 /* Not really needed/useful since the reenrant_retint is "volatile",
14656 * but do it for consistency's sake. */
14657 PL_reentrant_retint = proto_perl->Ireentrant_retint;
14658
14659 /* Hooks to shared SVs and locks. */
14660 PL_sharehook = proto_perl->Isharehook;
14661 PL_lockhook = proto_perl->Ilockhook;
14662 PL_unlockhook = proto_perl->Iunlockhook;
14663 PL_threadhook = proto_perl->Ithreadhook;
14664 PL_destroyhook = proto_perl->Idestroyhook;
14665 PL_signalhook = proto_perl->Isignalhook;
14666
14667 PL_globhook = proto_perl->Iglobhook;
14668
14669 /* swatch cache */
14670 PL_last_swash_hv = NULL; /* reinits on demand */
14671 PL_last_swash_klen = 0;
14672 PL_last_swash_key[0]= '\0';
14673 PL_last_swash_tmps = (U8*)NULL;
14674 PL_last_swash_slen = 0;
14675
14676 PL_srand_called = proto_perl->Isrand_called;
14677 Copy(&(proto_perl->Irandom_state), &PL_random_state, 1, PL_RANDOM_STATE_TYPE);
14678
14679 if (flags & CLONEf_COPY_STACKS) {
14680 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
14681 PL_tmps_ix = proto_perl->Itmps_ix;
14682 PL_tmps_max = proto_perl->Itmps_max;
14683 PL_tmps_floor = proto_perl->Itmps_floor;
14684
14685 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
14686 * NOTE: unlike the others! */
14687 PL_scopestack_ix = proto_perl->Iscopestack_ix;
14688 PL_scopestack_max = proto_perl->Iscopestack_max;
14689
14690 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
14691 * NOTE: unlike the others! */
14692 PL_savestack_ix = proto_perl->Isavestack_ix;
14693 PL_savestack_max = proto_perl->Isavestack_max;
14694 }
14695
14696 PL_start_env = proto_perl->Istart_env; /* XXXXXX */
14697 PL_top_env = &PL_start_env;
14698
14699 PL_op = proto_perl->Iop;
14700
14701 PL_Sv = NULL;
14702 PL_Xpv = (XPV*)NULL;
14703 my_perl->Ina = proto_perl->Ina;
14704
14705 PL_statbuf = proto_perl->Istatbuf;
14706 PL_statcache = proto_perl->Istatcache;
14707
14708 #ifndef NO_TAINT_SUPPORT
14709 PL_tainted = proto_perl->Itainted;
14710 #else
14711 PL_tainted = FALSE;
14712 #endif
14713 PL_curpm = proto_perl->Icurpm; /* XXX No PMOP ref count */
14714
14715 PL_chopset = proto_perl->Ichopset; /* XXX never deallocated */
14716
14717 PL_restartjmpenv = proto_perl->Irestartjmpenv;
14718 PL_restartop = proto_perl->Irestartop;
14719 PL_in_eval = proto_perl->Iin_eval;
14720 PL_delaymagic = proto_perl->Idelaymagic;
14721 PL_phase = proto_perl->Iphase;
14722 PL_localizing = proto_perl->Ilocalizing;
14723
14724 PL_hv_fetch_ent_mh = NULL;
14725 PL_modcount = proto_perl->Imodcount;
14726 PL_lastgotoprobe = NULL;
14727 PL_dumpindent = proto_perl->Idumpindent;
14728
14729 PL_efloatbuf = NULL; /* reinits on demand */
14730 PL_efloatsize = 0; /* reinits on demand */
14731
14732 /* regex stuff */
14733
14734 PL_colorset = 0; /* reinits PL_colors[] */
14735 /*PL_colors[6] = {0,0,0,0,0,0};*/
14736
14737 /* Pluggable optimizer */
14738 PL_peepp = proto_perl->Ipeepp;
14739 PL_rpeepp = proto_perl->Irpeepp;
14740 /* op_free() hook */
14741 PL_opfreehook = proto_perl->Iopfreehook;
14742
14743 #ifdef USE_REENTRANT_API
14744 /* XXX: things like -Dm will segfault here in perlio, but doing
14745 * PERL_SET_CONTEXT(proto_perl);
14746 * breaks too many other things
14747 */
14748 Perl_reentrant_init(aTHX);
14749 #endif
14750
14751 /* create SV map for pointer relocation */
14752 PL_ptr_table = ptr_table_new();
14753
14754 /* initialize these special pointers as early as possible */
14755 init_constants();
14756 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
14757 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
14758 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
14759 ptr_table_store(PL_ptr_table, &proto_perl->Ipadname_const,
14760 &PL_padname_const);
14761
14762 /* create (a non-shared!) shared string table */
14763 PL_strtab = newHV();
14764 HvSHAREKEYS_off(PL_strtab);
14765 hv_ksplit(PL_strtab, HvTOTALKEYS(proto_perl->Istrtab));
14766 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
14767
14768 Zero(PL_sv_consts, SV_CONSTS_COUNT, SV*);
14769
14770 /* This PV will be free'd special way so must set it same way op.c does */
14771 PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file);
14772 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file);
14773
14774 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
14775 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
14776 CopHINTHASH_set(&PL_compiling, cophh_copy(CopHINTHASH_get(&PL_compiling)));
14777 PL_curcop = (COP*)any_dup(proto_perl->Icurcop, proto_perl);
14778
14779 param->stashes = newAV(); /* Setup array of objects to call clone on */
14780 /* This makes no difference to the implementation, as it always pushes
14781 and shifts pointers to other SVs without changing their reference
14782 count, with the array becoming empty before it is freed. However, it
14783 makes it conceptually clear what is going on, and will avoid some
14784 work inside av.c, filling slots between AvFILL() and AvMAX() with
14785 &PL_sv_undef, and SvREFCNT_dec()ing those. */
14786 AvREAL_off(param->stashes);
14787
14788 if (!(flags & CLONEf_COPY_STACKS)) {
14789 param->unreferenced = newAV();
14790 }
14791
14792 #ifdef PERLIO_LAYERS
14793 /* Clone PerlIO tables as soon as we can handle general xx_dup() */
14794 PerlIO_clone(aTHX_ proto_perl, param);
14795 #endif
14796
14797 PL_envgv = gv_dup_inc(proto_perl->Ienvgv, param);
14798 PL_incgv = gv_dup_inc(proto_perl->Iincgv, param);
14799 PL_hintgv = gv_dup_inc(proto_perl->Ihintgv, param);
14800 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
14801 PL_xsubfilename = proto_perl->Ixsubfilename;
14802 PL_diehook = sv_dup_inc(proto_perl->Idiehook, param);
14803 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param);
14804
14805 /* switches */
14806 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param);
14807 PL_inplace = SAVEPV(proto_perl->Iinplace);
14808 PL_e_script = sv_dup_inc(proto_perl->Ie_script, param);
14809
14810 /* magical thingies */
14811
14812 PL_encoding = sv_dup(proto_perl->Iencoding, param);
14813 PL_lex_encoding = sv_dup(proto_perl->Ilex_encoding, param);
14814
14815 sv_setpvs(PERL_DEBUG_PAD(0), ""); /* For regex debugging. */
14816 sv_setpvs(PERL_DEBUG_PAD(1), ""); /* ext/re needs these */
14817 sv_setpvs(PERL_DEBUG_PAD(2), ""); /* even without DEBUGGING. */
14818
14819
14820 /* Clone the regex array */
14821 /* ORANGE FIXME for plugins, probably in the SV dup code.
14822 newSViv(PTR2IV(CALLREGDUPE(
14823 INT2PTR(REGEXP *, SvIVX(regex)), param))))
14824 */
14825 PL_regex_padav = av_dup_inc(proto_perl->Iregex_padav, param);
14826 PL_regex_pad = AvARRAY(PL_regex_padav);
14827
14828 PL_stashpadmax = proto_perl->Istashpadmax;
14829 PL_stashpadix = proto_perl->Istashpadix ;
14830 Newx(PL_stashpad, PL_stashpadmax, HV *);
14831 {
14832 PADOFFSET o = 0;
14833 for (; o < PL_stashpadmax; ++o)
14834 PL_stashpad[o] = hv_dup(proto_perl->Istashpad[o], param);
14835 }
14836
14837 /* shortcuts to various I/O objects */
14838 PL_ofsgv = gv_dup_inc(proto_perl->Iofsgv, param);
14839 PL_stdingv = gv_dup(proto_perl->Istdingv, param);
14840 PL_stderrgv = gv_dup(proto_perl->Istderrgv, param);
14841 PL_defgv = gv_dup(proto_perl->Idefgv, param);
14842 PL_argvgv = gv_dup_inc(proto_perl->Iargvgv, param);
14843 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param);
14844 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param);
14845
14846 /* shortcuts to regexp stuff */
14847 PL_replgv = gv_dup_inc(proto_perl->Ireplgv, param);
14848
14849 /* shortcuts to misc objects */
14850 PL_errgv = gv_dup(proto_perl->Ierrgv, param);
14851
14852 /* shortcuts to debugging objects */
14853 PL_DBgv = gv_dup_inc(proto_perl->IDBgv, param);
14854 PL_DBline = gv_dup_inc(proto_perl->IDBline, param);
14855 PL_DBsub = gv_dup_inc(proto_perl->IDBsub, param);
14856 PL_DBsingle = sv_dup(proto_perl->IDBsingle, param);
14857 PL_DBtrace = sv_dup(proto_perl->IDBtrace, param);
14858 PL_DBsignal = sv_dup(proto_perl->IDBsignal, param);
14859 Copy(proto_perl->IDBcontrol, PL_DBcontrol, DBVARMG_COUNT, IV);
14860
14861 /* symbol tables */
14862 PL_defstash = hv_dup_inc(proto_perl->Idefstash, param);
14863 PL_curstash = hv_dup_inc(proto_perl->Icurstash, param);
14864 PL_debstash = hv_dup(proto_perl->Idebstash, param);
14865 PL_globalstash = hv_dup(proto_perl->Iglobalstash, param);
14866 PL_curstname = sv_dup_inc(proto_perl->Icurstname, param);
14867
14868 PL_beginav = av_dup_inc(proto_perl->Ibeginav, param);
14869 PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param);
14870 PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param);
14871 PL_unitcheckav = av_dup_inc(proto_perl->Iunitcheckav, param);
14872 PL_unitcheckav_save = av_dup_inc(proto_perl->Iunitcheckav_save, param);
14873 PL_endav = av_dup_inc(proto_perl->Iendav, param);
14874 PL_checkav = av_dup_inc(proto_perl->Icheckav, param);
14875 PL_initav = av_dup_inc(proto_perl->Iinitav, param);
14876 PL_savebegin = proto_perl->Isavebegin;
14877
14878 PL_isarev = hv_dup_inc(proto_perl->Iisarev, param);
14879
14880 /* subprocess state */
14881 PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param);
14882
14883 if (proto_perl->Iop_mask)
14884 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
14885 else
14886 PL_op_mask = NULL;
14887 /* PL_asserting = proto_perl->Iasserting; */
14888
14889 /* current interpreter roots */
14890 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param);
14891 OP_REFCNT_LOCK;
14892 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
14893 OP_REFCNT_UNLOCK;
14894
14895 /* runtime control stuff */
14896 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
14897
14898 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param);
14899
14900 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param);
14901
14902 /* interpreter atexit processing */
14903 PL_exitlistlen = proto_perl->Iexitlistlen;
14904 if (PL_exitlistlen) {
14905 Newx(PL_exitlist, PL_exitlistlen, PerlExitListEntry);
14906 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
14907 }
14908 else
14909 PL_exitlist = (PerlExitListEntry*)NULL;
14910
14911 PL_my_cxt_size = proto_perl->Imy_cxt_size;
14912 if (PL_my_cxt_size) {
14913 Newx(PL_my_cxt_list, PL_my_cxt_size, void *);
14914 Copy(proto_perl->Imy_cxt_list, PL_my_cxt_list, PL_my_cxt_size, void *);
14915 #ifdef PERL_GLOBAL_STRUCT_PRIVATE
14916 Newx(PL_my_cxt_keys, PL_my_cxt_size, const char *);
14917 Copy(proto_perl->Imy_cxt_keys, PL_my_cxt_keys, PL_my_cxt_size, char *);
14918 #endif
14919 }
14920 else {
14921 PL_my_cxt_list = (void**)NULL;
14922 #ifdef PERL_GLOBAL_STRUCT_PRIVATE
14923 PL_my_cxt_keys = (const char**)NULL;
14924 #endif
14925 }
14926 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param);
14927 PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param);
14928 PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param);
14929 PL_custom_ops = hv_dup_inc(proto_perl->Icustom_ops, param);
14930
14931 PL_compcv = cv_dup(proto_perl->Icompcv, param);
14932
14933 PAD_CLONE_VARS(proto_perl, param);
14934
14935 #ifdef HAVE_INTERP_INTERN
14936 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
14937 #endif
14938
14939 PL_DBcv = cv_dup(proto_perl->IDBcv, param);
14940
14941 #ifdef PERL_USES_PL_PIDSTATUS
14942 PL_pidstatus = newHV(); /* XXX flag for cloning? */
14943 #endif
14944 PL_osname = SAVEPV(proto_perl->Iosname);
14945 PL_parser = parser_dup(proto_perl->Iparser, param);
14946
14947 /* XXX this only works if the saved cop has already been cloned */
14948 if (proto_perl->Iparser) {
14949 PL_parser->saved_curcop = (COP*)any_dup(
14950 proto_perl->Iparser->saved_curcop,
14951 proto_perl);
14952 }
14953
14954 PL_subname = sv_dup_inc(proto_perl->Isubname, param);
14955
14956 #ifdef USE_LOCALE_CTYPE
14957 /* Should we warn if uses locale? */
14958 PL_warn_locale = sv_dup_inc(proto_perl->Iwarn_locale, param);
14959 #endif
14960
14961 #ifdef USE_LOCALE_COLLATE
14962 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
14963 #endif /* USE_LOCALE_COLLATE */
14964
14965 #ifdef USE_LOCALE_NUMERIC
14966 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
14967 PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param);
14968 #endif /* !USE_LOCALE_NUMERIC */
14969
14970 /* Unicode inversion lists */
14971 PL_Latin1 = sv_dup_inc(proto_perl->ILatin1, param);
14972 PL_UpperLatin1 = sv_dup_inc(proto_perl->IUpperLatin1, param);
14973 PL_AboveLatin1 = sv_dup_inc(proto_perl->IAboveLatin1, param);
14974 PL_InBitmap = sv_dup_inc(proto_perl->IInBitmap, param);
14975
14976 PL_NonL1NonFinalFold = sv_dup_inc(proto_perl->INonL1NonFinalFold, param);
14977 PL_HasMultiCharFold = sv_dup_inc(proto_perl->IHasMultiCharFold, param);
14978
14979 /* utf8 character class swashes */
14980 for (i = 0; i < POSIX_SWASH_COUNT; i++) {
14981 PL_utf8_swash_ptrs[i] = sv_dup_inc(proto_perl->Iutf8_swash_ptrs[i], param);
14982 }
14983 for (i = 0; i < POSIX_CC_COUNT; i++) {
14984 PL_XPosix_ptrs[i] = sv_dup_inc(proto_perl->IXPosix_ptrs[i], param);
14985 }
14986 PL_GCB_invlist = sv_dup_inc(proto_perl->IGCB_invlist, param);
14987 PL_SB_invlist = sv_dup_inc(proto_perl->ISB_invlist, param);
14988 PL_WB_invlist = sv_dup_inc(proto_perl->IWB_invlist, param);
14989 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param);
14990 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param);
14991 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param);
14992 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param);
14993 PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param);
14994 PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param);
14995 PL_utf8_xidstart = sv_dup_inc(proto_perl->Iutf8_xidstart, param);
14996 PL_utf8_perl_idstart = sv_dup_inc(proto_perl->Iutf8_perl_idstart, param);
14997 PL_utf8_perl_idcont = sv_dup_inc(proto_perl->Iutf8_perl_idcont, param);
14998 PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param);
14999 PL_utf8_xidcont = sv_dup_inc(proto_perl->Iutf8_xidcont, param);
15000 PL_utf8_foldable = sv_dup_inc(proto_perl->Iutf8_foldable, param);
15001 PL_utf8_charname_begin = sv_dup_inc(proto_perl->Iutf8_charname_begin, param);
15002 PL_utf8_charname_continue = sv_dup_inc(proto_perl->Iutf8_charname_continue, param);
15003
15004 if (proto_perl->Ipsig_pend) {
15005 Newxz(PL_psig_pend, SIG_SIZE, int);
15006 }
15007 else {
15008 PL_psig_pend = (int*)NULL;
15009 }
15010
15011 if (proto_perl->Ipsig_name) {
15012 Newx(PL_psig_name, 2 * SIG_SIZE, SV*);
15013 sv_dup_inc_multiple(proto_perl->Ipsig_name, PL_psig_name, 2 * SIG_SIZE,
15014 param);
15015 PL_psig_ptr = PL_psig_name + SIG_SIZE;
15016 }
15017 else {
15018 PL_psig_ptr = (SV**)NULL;
15019 PL_psig_name = (SV**)NULL;
15020 }
15021
15022 if (flags & CLONEf_COPY_STACKS) {
15023 Newx(PL_tmps_stack, PL_tmps_max, SV*);
15024 sv_dup_inc_multiple(proto_perl->Itmps_stack, PL_tmps_stack,
15025 PL_tmps_ix+1, param);
15026
15027 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
15028 i = proto_perl->Imarkstack_max - proto_perl->Imarkstack;
15029 Newxz(PL_markstack, i, I32);
15030 PL_markstack_max = PL_markstack + (proto_perl->Imarkstack_max
15031 - proto_perl->Imarkstack);
15032 PL_markstack_ptr = PL_markstack + (proto_perl->Imarkstack_ptr
15033 - proto_perl->Imarkstack);
15034 Copy(proto_perl->Imarkstack, PL_markstack,
15035 PL_markstack_ptr - PL_markstack + 1, I32);
15036
15037 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
15038 * NOTE: unlike the others! */
15039 Newxz(PL_scopestack, PL_scopestack_max, I32);
15040 Copy(proto_perl->Iscopestack, PL_scopestack, PL_scopestack_ix, I32);
15041
15042 #ifdef DEBUGGING
15043 Newxz(PL_scopestack_name, PL_scopestack_max, const char *);
15044 Copy(proto_perl->Iscopestack_name, PL_scopestack_name, PL_scopestack_ix, const char *);
15045 #endif
15046 /* reset stack AV to correct length before its duped via
15047 * PL_curstackinfo */
15048 AvFILLp(proto_perl->Icurstack) =
15049 proto_perl->Istack_sp - proto_perl->Istack_base;
15050
15051 /* NOTE: si_dup() looks at PL_markstack */
15052 PL_curstackinfo = si_dup(proto_perl->Icurstackinfo, param);
15053
15054 /* PL_curstack = PL_curstackinfo->si_stack; */
15055 PL_curstack = av_dup(proto_perl->Icurstack, param);
15056 PL_mainstack = av_dup(proto_perl->Imainstack, param);
15057
15058 /* next PUSHs() etc. set *(PL_stack_sp+1) */
15059 PL_stack_base = AvARRAY(PL_curstack);
15060 PL_stack_sp = PL_stack_base + (proto_perl->Istack_sp
15061 - proto_perl->Istack_base);
15062 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
15063
15064 /*Newxz(PL_savestack, PL_savestack_max, ANY);*/
15065 PL_savestack = ss_dup(proto_perl, param);
15066 }
15067 else {
15068 init_stacks();
15069 ENTER; /* perl_destruct() wants to LEAVE; */
15070 }
15071
15072 PL_statgv = gv_dup(proto_perl->Istatgv, param);
15073 PL_statname = sv_dup_inc(proto_perl->Istatname, param);
15074
15075 PL_rs = sv_dup_inc(proto_perl->Irs, param);
15076 PL_last_in_gv = gv_dup(proto_perl->Ilast_in_gv, param);
15077 PL_defoutgv = gv_dup_inc(proto_perl->Idefoutgv, param);
15078 PL_toptarget = sv_dup_inc(proto_perl->Itoptarget, param);
15079 PL_bodytarget = sv_dup_inc(proto_perl->Ibodytarget, param);
15080 PL_formtarget = sv_dup(proto_perl->Iformtarget, param);
15081
15082 PL_errors = sv_dup_inc(proto_perl->Ierrors, param);
15083
15084 PL_sortcop = (OP*)any_dup(proto_perl->Isortcop, proto_perl);
15085 PL_firstgv = gv_dup_inc(proto_perl->Ifirstgv, param);
15086 PL_secondgv = gv_dup_inc(proto_perl->Isecondgv, param);
15087
15088 PL_stashcache = newHV();
15089
15090 PL_watchaddr = (char **) ptr_table_fetch(PL_ptr_table,
15091 proto_perl->Iwatchaddr);
15092 PL_watchok = PL_watchaddr ? * PL_watchaddr : NULL;
15093 if (PL_debug && PL_watchaddr) {
15094 PerlIO_printf(Perl_debug_log,
15095 "WATCHING: %"UVxf" cloned as %"UVxf" with value %"UVxf"\n",
15096 PTR2UV(proto_perl->Iwatchaddr), PTR2UV(PL_watchaddr),
15097 PTR2UV(PL_watchok));
15098 }
15099
15100 PL_registered_mros = hv_dup_inc(proto_perl->Iregistered_mros, param);
15101 PL_blockhooks = av_dup_inc(proto_perl->Iblockhooks, param);
15102 PL_utf8_foldclosures = hv_dup_inc(proto_perl->Iutf8_foldclosures, param);
15103
15104 /* Call the ->CLONE method, if it exists, for each of the stashes
15105 identified by sv_dup() above.
15106 */
15107 while(av_tindex(param->stashes) != -1) {
15108 HV* const stash = MUTABLE_HV(av_shift(param->stashes));
15109 GV* const cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
15110 if (cloner && GvCV(cloner)) {
15111 dSP;
15112 ENTER;
15113 SAVETMPS;
15114 PUSHMARK(SP);
15115 mXPUSHs(newSVhek(HvNAME_HEK(stash)));
15116 PUTBACK;
15117 call_sv(MUTABLE_SV(GvCV(cloner)), G_DISCARD);
15118 FREETMPS;
15119 LEAVE;
15120 }
15121 }
15122
15123 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
15124 ptr_table_free(PL_ptr_table);
15125 PL_ptr_table = NULL;
15126 }
15127
15128 if (!(flags & CLONEf_COPY_STACKS)) {
15129 unreferenced_to_tmp_stack(param->unreferenced);
15130 }
15131
15132 SvREFCNT_dec(param->stashes);
15133
15134 /* orphaned? eg threads->new inside BEGIN or use */
15135 if (PL_compcv && ! SvREFCNT(PL_compcv)) {
15136 SvREFCNT_inc_simple_void(PL_compcv);
15137 SAVEFREESV(PL_compcv);
15138 }
15139
15140 return my_perl;
15141 }
15142
15143 static void
15144 S_unreferenced_to_tmp_stack(pTHX_ AV *const unreferenced)
15145 {
15146 PERL_ARGS_ASSERT_UNREFERENCED_TO_TMP_STACK;
15147
15148 if (AvFILLp(unreferenced) > -1) {
15149 SV **svp = AvARRAY(unreferenced);
15150 SV **const last = svp + AvFILLp(unreferenced);
15151 SSize_t count = 0;
15152
15153 do {
15154 if (SvREFCNT(*svp) == 1)
15155 ++count;
15156 } while (++svp <= last);
15157
15158 EXTEND_MORTAL(count);
15159 svp = AvARRAY(unreferenced);
15160
15161 do {
15162 if (SvREFCNT(*svp) == 1) {
15163 /* Our reference is the only one to this SV. This means that
15164 in this thread, the scalar effectively has a 0 reference.
15165 That doesn't work (cleanup never happens), so donate our
15166 reference to it onto the save stack. */
15167 PL_tmps_stack[++PL_tmps_ix] = *svp;
15168 } else {
15169 /* As an optimisation, because we are already walking the
15170 entire array, instead of above doing either
15171 SvREFCNT_inc(*svp) or *svp = &PL_sv_undef, we can instead
15172 release our reference to the scalar, so that at the end of
15173 the array owns zero references to the scalars it happens to
15174 point to. We are effectively converting the array from
15175 AvREAL() on to AvREAL() off. This saves the av_clear()
15176 (triggered by the SvREFCNT_dec(unreferenced) below) from
15177 walking the array a second time. */
15178 SvREFCNT_dec(*svp);
15179 }
15180
15181 } while (++svp <= last);
15182 AvREAL_off(unreferenced);
15183 }
15184 SvREFCNT_dec_NN(unreferenced);
15185 }
15186
15187 void
15188 Perl_clone_params_del(CLONE_PARAMS *param)
15189 {
15190 /* This seemingly funky ordering keeps the build with PERL_GLOBAL_STRUCT
15191 happy: */
15192 PerlInterpreter *const to = param->new_perl;
15193 dTHXa(to);
15194 PerlInterpreter *const was = PERL_GET_THX;
15195
15196 PERL_ARGS_ASSERT_CLONE_PARAMS_DEL;
15197
15198 if (was != to) {
15199 PERL_SET_THX(to);
15200 }
15201
15202 SvREFCNT_dec(param->stashes);
15203 if (param->unreferenced)
15204 unreferenced_to_tmp_stack(param->unreferenced);
15205
15206 Safefree(param);
15207
15208 if (was != to) {
15209 PERL_SET_THX(was);
15210 }
15211 }
15212
15213 CLONE_PARAMS *
15214 Perl_clone_params_new(PerlInterpreter *const from, PerlInterpreter *const to)
15215 {
15216 dVAR;
15217 /* Need to play this game, as newAV() can call safesysmalloc(), and that
15218 does a dTHX; to get the context from thread local storage.
15219 FIXME - under PERL_CORE Newx(), Safefree() and friends should expand to
15220 a version that passes in my_perl. */
15221 PerlInterpreter *const was = PERL_GET_THX;
15222 CLONE_PARAMS *param;
15223
15224 PERL_ARGS_ASSERT_CLONE_PARAMS_NEW;
15225
15226 if (was != to) {
15227 PERL_SET_THX(to);
15228 }
15229
15230 /* Given that we've set the context, we can do this unshared. */
15231 Newx(param, 1, CLONE_PARAMS);
15232
15233 param->flags = 0;
15234 param->proto_perl = from;
15235 param->new_perl = to;
15236 param->stashes = (AV *)Perl_newSV_type(to, SVt_PVAV);
15237 AvREAL_off(param->stashes);
15238 param->unreferenced = (AV *)Perl_newSV_type(to, SVt_PVAV);
15239
15240 if (was != to) {
15241 PERL_SET_THX(was);
15242 }
15243 return param;
15244 }
15245
15246 #endif /* USE_ITHREADS */
15247
15248 void
15249 Perl_init_constants(pTHX)
15250 {
15251 SvREFCNT(&PL_sv_undef) = SvREFCNT_IMMORTAL;
15252 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVf_PROTECT|SVt_NULL;
15253 SvANY(&PL_sv_undef) = NULL;
15254
15255 SvANY(&PL_sv_no) = new_XPVNV();
15256 SvREFCNT(&PL_sv_no) = SvREFCNT_IMMORTAL;
15257 SvFLAGS(&PL_sv_no) = SVt_PVNV|SVf_READONLY|SVf_PROTECT
15258 |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
15259 |SVp_POK|SVf_POK;
15260
15261 SvANY(&PL_sv_yes) = new_XPVNV();
15262 SvREFCNT(&PL_sv_yes) = SvREFCNT_IMMORTAL;
15263 SvFLAGS(&PL_sv_yes) = SVt_PVNV|SVf_READONLY|SVf_PROTECT
15264 |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
15265 |SVp_POK|SVf_POK;
15266
15267 SvPV_set(&PL_sv_no, (char*)PL_No);
15268 SvCUR_set(&PL_sv_no, 0);
15269 SvLEN_set(&PL_sv_no, 0);
15270 SvIV_set(&PL_sv_no, 0);
15271 SvNV_set(&PL_sv_no, 0);
15272
15273 SvPV_set(&PL_sv_yes, (char*)PL_Yes);
15274 SvCUR_set(&PL_sv_yes, 1);
15275 SvLEN_set(&PL_sv_yes, 0);
15276 SvIV_set(&PL_sv_yes, 1);
15277 SvNV_set(&PL_sv_yes, 1);
15278
15279 PadnamePV(&PL_padname_const) = (char *)PL_No;
15280 }
15281
15282 /*
15283 =head1 Unicode Support
15284
15285 =for apidoc sv_recode_to_utf8
15286
15287 The encoding is assumed to be an Encode object, on entry the PV
15288 of the sv is assumed to be octets in that encoding, and the sv
15289 will be converted into Unicode (and UTF-8).
15290
15291 If the sv already is UTF-8 (or if it is not POK), or if the encoding
15292 is not a reference, nothing is done to the sv. If the encoding is not
15293 an C<Encode::XS> Encoding object, bad things will happen.
15294 (See F<lib/encoding.pm> and L<Encode>.)
15295
15296 The PV of the sv is returned.
15297
15298 =cut */
15299
15300 char *
15301 Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
15302 {
15303 PERL_ARGS_ASSERT_SV_RECODE_TO_UTF8;
15304
15305 if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
15306 SV *uni;
15307 STRLEN len;
15308 const char *s;
15309 dSP;
15310 SV *nsv = sv;
15311 ENTER;
15312 PUSHSTACK;
15313 SAVETMPS;
15314 if (SvPADTMP(nsv)) {
15315 nsv = sv_newmortal();
15316 SvSetSV_nosteal(nsv, sv);
15317 }
15318 save_re_context();
15319 PUSHMARK(sp);
15320 EXTEND(SP, 3);
15321 PUSHs(encoding);
15322 PUSHs(nsv);
15323 /*
15324 NI-S 2002/07/09
15325 Passing sv_yes is wrong - it needs to be or'ed set of constants
15326 for Encode::XS, while UTf-8 decode (currently) assumes a true value means
15327 remove converted chars from source.
15328
15329 Both will default the value - let them.
15330
15331 XPUSHs(&PL_sv_yes);
15332 */
15333 PUTBACK;
15334 call_method("decode", G_SCALAR);
15335 SPAGAIN;
15336 uni = POPs;
15337 PUTBACK;
15338 s = SvPV_const(uni, len);
15339 if (s != SvPVX_const(sv)) {
15340 SvGROW(sv, len + 1);
15341 Move(s, SvPVX(sv), len + 1, char);
15342 SvCUR_set(sv, len);
15343 }
15344 FREETMPS;
15345 POPSTACK;
15346 LEAVE;
15347 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
15348 /* clear pos and any utf8 cache */
15349 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
15350 if (mg)
15351 mg->mg_len = -1;
15352 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
15353 magic_setutf8(sv,mg); /* clear UTF8 cache */
15354 }
15355 SvUTF8_on(sv);
15356 return SvPVX(sv);
15357 }
15358 return SvPOKp(sv) ? SvPVX(sv) : NULL;
15359 }
15360
15361 /*
15362 =for apidoc sv_cat_decode
15363
15364 The encoding is assumed to be an Encode object, the PV of the ssv is
15365 assumed to be octets in that encoding and decoding the input starts
15366 from the position which (PV + *offset) pointed to. The dsv will be
15367 concatenated the decoded UTF-8 string from ssv. Decoding will terminate
15368 when the string tstr appears in decoding output or the input ends on
15369 the PV of the ssv. The value which the offset points will be modified
15370 to the last input position on the ssv.
15371
15372 Returns TRUE if the terminator was found, else returns FALSE.
15373
15374 =cut */
15375
15376 bool
15377 Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
15378 SV *ssv, int *offset, char *tstr, int tlen)
15379 {
15380 bool ret = FALSE;
15381
15382 PERL_ARGS_ASSERT_SV_CAT_DECODE;
15383
15384 if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding)) {
15385 SV *offsv;
15386 dSP;
15387 ENTER;
15388 SAVETMPS;
15389 save_re_context();
15390 PUSHMARK(sp);
15391 EXTEND(SP, 6);
15392 PUSHs(encoding);
15393 PUSHs(dsv);
15394 PUSHs(ssv);
15395 offsv = newSViv(*offset);
15396 mPUSHs(offsv);
15397 mPUSHp(tstr, tlen);
15398 PUTBACK;
15399 call_method("cat_decode", G_SCALAR);
15400 SPAGAIN;
15401 ret = SvTRUE(TOPs);
15402 *offset = SvIV(offsv);
15403 PUTBACK;
15404 FREETMPS;
15405 LEAVE;
15406 }
15407 else
15408 Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
15409 return ret;
15410
15411 }
15412
15413 /* ---------------------------------------------------------------------
15414 *
15415 * support functions for report_uninit()
15416 */
15417
15418 /* the maxiumum size of array or hash where we will scan looking
15419 * for the undefined element that triggered the warning */
15420
15421 #define FUV_MAX_SEARCH_SIZE 1000
15422
15423 /* Look for an entry in the hash whose value has the same SV as val;
15424 * If so, return a mortal copy of the key. */
15425
15426 STATIC SV*
15427 S_find_hash_subscript(pTHX_ const HV *const hv, const SV *const val)
15428 {
15429 dVAR;
15430 HE **array;
15431 I32 i;
15432
15433 PERL_ARGS_ASSERT_FIND_HASH_SUBSCRIPT;
15434
15435 if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
15436 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
15437 return NULL;
15438
15439 array = HvARRAY(hv);
15440
15441 for (i=HvMAX(hv); i>=0; i--) {
15442 HE *entry;
15443 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
15444 if (HeVAL(entry) != val)
15445 continue;
15446 if ( HeVAL(entry) == &PL_sv_undef ||
15447 HeVAL(entry) == &PL_sv_placeholder)
15448 continue;
15449 if (!HeKEY(entry))
15450 return NULL;
15451 if (HeKLEN(entry) == HEf_SVKEY)
15452 return sv_mortalcopy(HeKEY_sv(entry));
15453 return sv_2mortal(newSVhek(HeKEY_hek(entry)));
15454 }
15455 }
15456 return NULL;
15457 }
15458
15459 /* Look for an entry in the array whose value has the same SV as val;
15460 * If so, return the index, otherwise return -1. */
15461
15462 STATIC I32
15463 S_find_array_subscript(pTHX_ const AV *const av, const SV *const val)
15464 {
15465 PERL_ARGS_ASSERT_FIND_ARRAY_SUBSCRIPT;
15466
15467 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
15468 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
15469 return -1;
15470
15471 if (val != &PL_sv_undef) {
15472 SV ** const svp = AvARRAY(av);
15473 I32 i;
15474
15475 for (i=AvFILLp(av); i>=0; i--)
15476 if (svp[i] == val)
15477 return i;
15478 }
15479 return -1;
15480 }
15481
15482 /* varname(): return the name of a variable, optionally with a subscript.
15483 * If gv is non-zero, use the name of that global, along with gvtype (one
15484 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
15485 * targ. Depending on the value of the subscript_type flag, return:
15486 */
15487
15488 #define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
15489 #define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
15490 #define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
15491 #define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
15492
15493 SV*
15494 Perl_varname(pTHX_ const GV *const gv, const char gvtype, PADOFFSET targ,
15495 const SV *const keyname, I32 aindex, int subscript_type)
15496 {
15497
15498 SV * const name = sv_newmortal();
15499 if (gv && isGV(gv)) {
15500 char buffer[2];
15501 buffer[0] = gvtype;
15502 buffer[1] = 0;
15503
15504 /* as gv_fullname4(), but add literal '^' for $^FOO names */
15505
15506 gv_fullname4(name, gv, buffer, 0);
15507
15508 if ((unsigned int)SvPVX(name)[1] <= 26) {
15509 buffer[0] = '^';
15510 buffer[1] = SvPVX(name)[1] + 'A' - 1;
15511
15512 /* Swap the 1 unprintable control character for the 2 byte pretty
15513 version - ie substr($name, 1, 1) = $buffer; */
15514 sv_insert(name, 1, 1, buffer, 2);
15515 }
15516 }
15517 else {
15518 CV * const cv = gv ? ((CV *)gv) : find_runcv(NULL);
15519 PADNAME *sv;
15520
15521 assert(!cv || SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM);
15522
15523 if (!cv || !CvPADLIST(cv))
15524 return NULL;
15525 sv = padnamelist_fetch(PadlistNAMES(CvPADLIST(cv)), targ);
15526 sv_setpvn(name, PadnamePV(sv), PadnameLEN(sv));
15527 SvUTF8_on(name);
15528 }
15529
15530 if (subscript_type == FUV_SUBSCRIPT_HASH) {
15531 SV * const sv = newSV(0);
15532 *SvPVX(name) = '$';
15533 Perl_sv_catpvf(aTHX_ name, "{%s}",
15534 pv_pretty(sv, SvPVX_const(keyname), SvCUR(keyname), 32, NULL, NULL,
15535 PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_UNI_DETECT ));
15536 SvREFCNT_dec_NN(sv);
15537 }
15538 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
15539 *SvPVX(name) = '$';
15540 Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
15541 }
15542 else if (subscript_type == FUV_SUBSCRIPT_WITHIN) {
15543 /* We know that name has no magic, so can use 0 instead of SV_GMAGIC */
15544 Perl_sv_insert_flags(aTHX_ name, 0, 0, STR_WITH_LEN("within "), 0);
15545 }
15546
15547 return name;
15548 }
15549
15550
15551 /*
15552 =for apidoc find_uninit_var
15553
15554 Find the name of the undefined variable (if any) that caused the operator
15555 to issue a "Use of uninitialized value" warning.
15556 If match is true, only return a name if its value matches uninit_sv.
15557 So roughly speaking, if a unary operator (such as OP_COS) generates a
15558 warning, then following the direct child of the op may yield an
15559 OP_PADSV or OP_GV that gives the name of the undefined variable. On the
15560 other hand, with OP_ADD there are two branches to follow, so we only print
15561 the variable name if we get an exact match.
15562 desc_p points to a string pointer holding the description of the op.
15563 This may be updated if needed.
15564
15565 The name is returned as a mortal SV.
15566
15567 Assumes that PL_op is the op that originally triggered the error, and that
15568 PL_comppad/PL_curpad points to the currently executing pad.
15569
15570 =cut
15571 */
15572
15573 STATIC SV *
15574 S_find_uninit_var(pTHX_ const OP *const obase, const SV *const uninit_sv,
15575 bool match, const char **desc_p)
15576 {
15577 dVAR;
15578 SV *sv;
15579 const GV *gv;
15580 const OP *o, *o2, *kid;
15581
15582 PERL_ARGS_ASSERT_FIND_UNINIT_VAR;
15583
15584 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
15585 uninit_sv == &PL_sv_placeholder)))
15586 return NULL;
15587
15588 switch (obase->op_type) {
15589
15590 case OP_RV2AV:
15591 case OP_RV2HV:
15592 case OP_PADAV:
15593 case OP_PADHV:
15594 {
15595 const bool pad = ( obase->op_type == OP_PADAV
15596 || obase->op_type == OP_PADHV
15597 || obase->op_type == OP_PADRANGE
15598 );
15599
15600 const bool hash = ( obase->op_type == OP_PADHV
15601 || obase->op_type == OP_RV2HV
15602 || (obase->op_type == OP_PADRANGE
15603 && SvTYPE(PAD_SVl(obase->op_targ)) == SVt_PVHV)
15604 );
15605 I32 index = 0;
15606 SV *keysv = NULL;
15607 int subscript_type = FUV_SUBSCRIPT_WITHIN;
15608
15609 if (pad) { /* @lex, %lex */
15610 sv = PAD_SVl(obase->op_targ);
15611 gv = NULL;
15612 }
15613 else {
15614 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
15615 /* @global, %global */
15616 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
15617 if (!gv)
15618 break;
15619 sv = hash ? MUTABLE_SV(GvHV(gv)): MUTABLE_SV(GvAV(gv));
15620 }
15621 else if (obase == PL_op) /* @{expr}, %{expr} */
15622 return find_uninit_var(cUNOPx(obase)->op_first,
15623 uninit_sv, match, desc_p);
15624 else /* @{expr}, %{expr} as a sub-expression */
15625 return NULL;
15626 }
15627
15628 /* attempt to find a match within the aggregate */
15629 if (hash) {
15630 keysv = find_hash_subscript((const HV*)sv, uninit_sv);
15631 if (keysv)
15632 subscript_type = FUV_SUBSCRIPT_HASH;
15633 }
15634 else {
15635 index = find_array_subscript((const AV *)sv, uninit_sv);
15636 if (index >= 0)
15637 subscript_type = FUV_SUBSCRIPT_ARRAY;
15638 }
15639
15640 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
15641 break;
15642
15643 return varname(gv, (char)(hash ? '%' : '@'), obase->op_targ,
15644 keysv, index, subscript_type);
15645 }
15646
15647 case OP_RV2SV:
15648 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
15649 /* $global */
15650 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
15651 if (!gv || !GvSTASH(gv))
15652 break;
15653 if (match && (GvSV(gv) != uninit_sv))
15654 break;
15655 return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
15656 }
15657 /* ${expr} */
15658 return find_uninit_var(cUNOPx(obase)->op_first, uninit_sv, 1, desc_p);
15659
15660 case OP_PADSV:
15661 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
15662 break;
15663 return varname(NULL, '$', obase->op_targ,
15664 NULL, 0, FUV_SUBSCRIPT_NONE);
15665
15666 case OP_GVSV:
15667 gv = cGVOPx_gv(obase);
15668 if (!gv || (match && GvSV(gv) != uninit_sv) || !GvSTASH(gv))
15669 break;
15670 return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
15671
15672 case OP_AELEMFAST_LEX:
15673 if (match) {
15674 SV **svp;
15675 AV *av = MUTABLE_AV(PAD_SV(obase->op_targ));
15676 if (!av || SvRMAGICAL(av))
15677 break;
15678 svp = av_fetch(av, (I8)obase->op_private, FALSE);
15679 if (!svp || *svp != uninit_sv)
15680 break;
15681 }
15682 return varname(NULL, '$', obase->op_targ,
15683 NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
15684 case OP_AELEMFAST:
15685 {
15686 gv = cGVOPx_gv(obase);
15687 if (!gv)
15688 break;
15689 if (match) {
15690 SV **svp;
15691 AV *const av = GvAV(gv);
15692 if (!av || SvRMAGICAL(av))
15693 break;
15694 svp = av_fetch(av, (I8)obase->op_private, FALSE);
15695 if (!svp || *svp != uninit_sv)
15696 break;
15697 }
15698 return varname(gv, '$', 0,
15699 NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
15700 }
15701 NOT_REACHED; /* NOTREACHED */
15702
15703 case OP_EXISTS:
15704 o = cUNOPx(obase)->op_first;
15705 if (!o || o->op_type != OP_NULL ||
15706 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
15707 break;
15708 return find_uninit_var(cBINOPo->op_last, uninit_sv, match, desc_p);
15709
15710 case OP_AELEM:
15711 case OP_HELEM:
15712 {
15713 bool negate = FALSE;
15714
15715 if (PL_op == obase)
15716 /* $a[uninit_expr] or $h{uninit_expr} */
15717 return find_uninit_var(cBINOPx(obase)->op_last,
15718 uninit_sv, match, desc_p);
15719
15720 gv = NULL;
15721 o = cBINOPx(obase)->op_first;
15722 kid = cBINOPx(obase)->op_last;
15723
15724 /* get the av or hv, and optionally the gv */
15725 sv = NULL;
15726 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
15727 sv = PAD_SV(o->op_targ);
15728 }
15729 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
15730 && cUNOPo->op_first->op_type == OP_GV)
15731 {
15732 gv = cGVOPx_gv(cUNOPo->op_first);
15733 if (!gv)
15734 break;
15735 sv = o->op_type
15736 == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(GvAV(gv));
15737 }
15738 if (!sv)
15739 break;
15740
15741 if (kid && kid->op_type == OP_NEGATE) {
15742 negate = TRUE;
15743 kid = cUNOPx(kid)->op_first;
15744 }
15745
15746 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
15747 /* index is constant */
15748 SV* kidsv;
15749 if (negate) {
15750 kidsv = newSVpvs_flags("-", SVs_TEMP);
15751 sv_catsv(kidsv, cSVOPx_sv(kid));
15752 }
15753 else
15754 kidsv = cSVOPx_sv(kid);
15755 if (match) {
15756 if (SvMAGICAL(sv))
15757 break;
15758 if (obase->op_type == OP_HELEM) {
15759 HE* he = hv_fetch_ent(MUTABLE_HV(sv), kidsv, 0, 0);
15760 if (!he || HeVAL(he) != uninit_sv)
15761 break;
15762 }
15763 else {
15764 SV * const opsv = cSVOPx_sv(kid);
15765 const IV opsviv = SvIV(opsv);
15766 SV * const * const svp = av_fetch(MUTABLE_AV(sv),
15767 negate ? - opsviv : opsviv,
15768 FALSE);
15769 if (!svp || *svp != uninit_sv)
15770 break;
15771 }
15772 }
15773 if (obase->op_type == OP_HELEM)
15774 return varname(gv, '%', o->op_targ,
15775 kidsv, 0, FUV_SUBSCRIPT_HASH);
15776 else
15777 return varname(gv, '@', o->op_targ, NULL,
15778 negate ? - SvIV(cSVOPx_sv(kid)) : SvIV(cSVOPx_sv(kid)),
15779 FUV_SUBSCRIPT_ARRAY);
15780 }
15781 else {
15782 /* index is an expression;
15783 * attempt to find a match within the aggregate */
15784 if (obase->op_type == OP_HELEM) {
15785 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
15786 if (keysv)
15787 return varname(gv, '%', o->op_targ,
15788 keysv, 0, FUV_SUBSCRIPT_HASH);
15789 }
15790 else {
15791 const I32 index
15792 = find_array_subscript((const AV *)sv, uninit_sv);
15793 if (index >= 0)
15794 return varname(gv, '@', o->op_targ,
15795 NULL, index, FUV_SUBSCRIPT_ARRAY);
15796 }
15797 if (match)
15798 break;
15799 return varname(gv,
15800 (char)((o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
15801 ? '@' : '%'),
15802 o->op_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
15803 }
15804 NOT_REACHED; /* NOTREACHED */
15805 }
15806
15807 case OP_MULTIDEREF: {
15808 /* If we were executing OP_MULTIDEREF when the undef warning
15809 * triggered, then it must be one of the index values within
15810 * that triggered it. If not, then the only possibility is that
15811 * the value retrieved by the last aggregate lookup might be the
15812 * culprit. For the former, we set PL_multideref_pc each time before
15813 * using an index, so work though the item list until we reach
15814 * that point. For the latter, just work through the entire item
15815 * list; the last aggregate retrieved will be the candidate.
15816 */
15817
15818 /* the named aggregate, if any */
15819 PADOFFSET agg_targ = 0;
15820 GV *agg_gv = NULL;
15821 /* the last-seen index */
15822 UV index_type;
15823 PADOFFSET index_targ;
15824 GV *index_gv;
15825 IV index_const_iv = 0; /* init for spurious compiler warn */
15826 SV *index_const_sv;
15827 int depth = 0; /* how many array/hash lookups we've done */
15828
15829 UNOP_AUX_item *items = cUNOP_AUXx(obase)->op_aux;
15830 UNOP_AUX_item *last = NULL;
15831 UV actions = items->uv;
15832 bool is_hv;
15833
15834 if (PL_op == obase) {
15835 last = PL_multideref_pc;
15836 assert(last >= items && last <= items + items[-1].uv);
15837 }
15838
15839 assert(actions);
15840
15841 while (1) {
15842 is_hv = FALSE;
15843 switch (actions & MDEREF_ACTION_MASK) {
15844
15845 case MDEREF_reload:
15846 actions = (++items)->uv;
15847 continue;
15848
15849 case MDEREF_HV_padhv_helem: /* $lex{...} */
15850 is_hv = TRUE;
15851 /* FALLTHROUGH */
15852 case MDEREF_AV_padav_aelem: /* $lex[...] */
15853 agg_targ = (++items)->pad_offset;
15854 agg_gv = NULL;
15855 break;
15856
15857 case MDEREF_HV_gvhv_helem: /* $pkg{...} */
15858 is_hv = TRUE;
15859 /* FALLTHROUGH */
15860 case MDEREF_AV_gvav_aelem: /* $pkg[...] */
15861 agg_targ = 0;
15862 agg_gv = (GV*)UNOP_AUX_item_sv(++items);
15863 assert(isGV_with_GP(agg_gv));
15864 break;
15865
15866 case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */
15867 case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */
15868 ++items;
15869 /* FALLTHROUGH */
15870 case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */
15871 case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */
15872 agg_targ = 0;
15873 agg_gv = NULL;
15874 is_hv = TRUE;
15875 break;
15876
15877 case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */
15878 case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */
15879 ++items;
15880 /* FALLTHROUGH */
15881 case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */
15882 case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */
15883 agg_targ = 0;
15884 agg_gv = NULL;
15885 } /* switch */
15886
15887 index_targ = 0;
15888 index_gv = NULL;
15889 index_const_sv = NULL;
15890
15891 index_type = (actions & MDEREF_INDEX_MASK);
15892 switch (index_type) {
15893 case MDEREF_INDEX_none:
15894 break;
15895 case MDEREF_INDEX_const:
15896 if (is_hv)
15897 index_const_sv = UNOP_AUX_item_sv(++items)
15898 else
15899 index_const_iv = (++items)->iv;
15900 break;
15901 case MDEREF_INDEX_padsv:
15902 index_targ = (++items)->pad_offset;
15903 break;
15904 case MDEREF_INDEX_gvsv:
15905 index_gv = (GV*)UNOP_AUX_item_sv(++items);
15906 assert(isGV_with_GP(index_gv));
15907 break;
15908 }
15909
15910 if (index_type != MDEREF_INDEX_none)
15911 depth++;
15912
15913 if ( index_type == MDEREF_INDEX_none
15914 || (actions & MDEREF_FLAG_last)
15915 || (last && items == last)
15916 )
15917 break;
15918
15919 actions >>= MDEREF_SHIFT;
15920 } /* while */
15921
15922 if (PL_op == obase) {
15923 /* index was undef */
15924
15925 *desc_p = ( (actions & MDEREF_FLAG_last)
15926 && (obase->op_private
15927 & (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE)))
15928 ?
15929 (obase->op_private & OPpMULTIDEREF_EXISTS)
15930 ? "exists"
15931 : "delete"
15932 : is_hv ? "hash element" : "array element";
15933 assert(index_type != MDEREF_INDEX_none);
15934 if (index_gv)
15935 return varname(index_gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
15936 if (index_targ)
15937 return varname(NULL, '$', index_targ,
15938 NULL, 0, FUV_SUBSCRIPT_NONE);
15939 assert(is_hv); /* AV index is an IV and can't be undef */
15940 /* can a const HV index ever be undef? */
15941 return NULL;
15942 }
15943
15944 /* the SV returned by pp_multideref() was undef, if anything was */
15945
15946 if (depth != 1)
15947 break;
15948
15949 if (agg_targ)
15950 sv = PAD_SV(agg_targ);
15951 else if (agg_gv)
15952 sv = is_hv ? MUTABLE_SV(GvHV(agg_gv)) : MUTABLE_SV(GvAV(agg_gv));
15953 else
15954 break;
15955
15956 if (index_type == MDEREF_INDEX_const) {
15957 if (match) {
15958 if (SvMAGICAL(sv))
15959 break;
15960 if (is_hv) {
15961 HE* he = hv_fetch_ent(MUTABLE_HV(sv), index_const_sv, 0, 0);
15962 if (!he || HeVAL(he) != uninit_sv)
15963 break;
15964 }
15965 else {
15966 SV * const * const svp =
15967 av_fetch(MUTABLE_AV(sv), index_const_iv, FALSE);
15968 if (!svp || *svp != uninit_sv)
15969 break;
15970 }
15971 }
15972 return is_hv
15973 ? varname(agg_gv, '%', agg_targ,
15974 index_const_sv, 0, FUV_SUBSCRIPT_HASH)
15975 : varname(agg_gv, '@', agg_targ,
15976 NULL, index_const_iv, FUV_SUBSCRIPT_ARRAY);
15977 }
15978 else {
15979 /* index is an var */
15980 if (is_hv) {
15981 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
15982 if (keysv)
15983 return varname(agg_gv, '%', agg_targ,
15984 keysv, 0, FUV_SUBSCRIPT_HASH);
15985 }
15986 else {
15987 const I32 index
15988 = find_array_subscript((const AV *)sv, uninit_sv);
15989 if (index >= 0)
15990 return varname(agg_gv, '@', agg_targ,
15991 NULL, index, FUV_SUBSCRIPT_ARRAY);
15992 }
15993 if (match)
15994 break;
15995 return varname(agg_gv,
15996 is_hv ? '%' : '@',
15997 agg_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
15998 }
15999 NOT_REACHED; /* NOTREACHED */
16000 }
16001
16002 case OP_AASSIGN:
16003 /* only examine RHS */
16004 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv,
16005 match, desc_p);
16006
16007 case OP_OPEN:
16008 o = cUNOPx(obase)->op_first;
16009 if ( o->op_type == OP_PUSHMARK
16010 || (o->op_type == OP_NULL && o->op_targ == OP_PUSHMARK)
16011 )
16012 o = OpSIBLING(o);
16013
16014 if (!OpHAS_SIBLING(o)) {
16015 /* one-arg version of open is highly magical */
16016
16017 if (o->op_type == OP_GV) { /* open FOO; */
16018 gv = cGVOPx_gv(o);
16019 if (match && GvSV(gv) != uninit_sv)
16020 break;
16021 return varname(gv, '$', 0,
16022 NULL, 0, FUV_SUBSCRIPT_NONE);
16023 }
16024 /* other possibilities not handled are:
16025 * open $x; or open my $x; should return '${*$x}'
16026 * open expr; should return '$'.expr ideally
16027 */
16028 break;
16029 }
16030 goto do_op;
16031
16032 /* ops where $_ may be an implicit arg */
16033 case OP_TRANS:
16034 case OP_TRANSR:
16035 case OP_SUBST:
16036 case OP_MATCH:
16037 if ( !(obase->op_flags & OPf_STACKED)) {
16038 if (uninit_sv == DEFSV)
16039 return newSVpvs_flags("$_", SVs_TEMP);
16040 else if (obase->op_targ
16041 && uninit_sv == PAD_SVl(obase->op_targ))
16042 return varname(NULL, '$', obase->op_targ, NULL, 0,
16043 FUV_SUBSCRIPT_NONE);
16044 }
16045 goto do_op;
16046
16047 case OP_PRTF:
16048 case OP_PRINT:
16049 case OP_SAY:
16050 match = 1; /* print etc can return undef on defined args */
16051 /* skip filehandle as it can't produce 'undef' warning */
16052 o = cUNOPx(obase)->op_first;
16053 if ((obase->op_flags & OPf_STACKED)
16054 &&
16055 ( o->op_type == OP_PUSHMARK
16056 || (o->op_type == OP_NULL && o->op_targ == OP_PUSHMARK)))
16057 o = OpSIBLING(OpSIBLING(o));
16058 goto do_op2;
16059
16060
16061 case OP_ENTEREVAL: /* could be eval $undef or $x='$undef'; eval $x */
16062 case OP_CUSTOM: /* XS or custom code could trigger random warnings */
16063
16064 /* the following ops are capable of returning PL_sv_undef even for
16065 * defined arg(s) */
16066
16067 case OP_BACKTICK:
16068 case OP_PIPE_OP:
16069 case OP_FILENO:
16070 case OP_BINMODE:
16071 case OP_TIED:
16072 case OP_GETC:
16073 case OP_SYSREAD:
16074 case OP_SEND:
16075 case OP_IOCTL:
16076 case OP_SOCKET:
16077 case OP_SOCKPAIR:
16078 case OP_BIND:
16079 case OP_CONNECT:
16080 case OP_LISTEN:
16081 case OP_ACCEPT:
16082 case OP_SHUTDOWN:
16083 case OP_SSOCKOPT:
16084 case OP_GETPEERNAME:
16085 case OP_FTRREAD:
16086 case OP_FTRWRITE:
16087 case OP_FTREXEC:
16088 case OP_FTROWNED:
16089 case OP_FTEREAD:
16090 case OP_FTEWRITE:
16091 case OP_FTEEXEC:
16092 case OP_FTEOWNED:
16093 case OP_FTIS:
16094 case OP_FTZERO:
16095 case OP_FTSIZE:
16096 case OP_FTFILE:
16097 case OP_FTDIR:
16098 case OP_FTLINK:
16099 case OP_FTPIPE:
16100 case OP_FTSOCK:
16101 case OP_FTBLK:
16102 case OP_FTCHR:
16103 case OP_FTTTY:
16104 case OP_FTSUID:
16105 case OP_FTSGID:
16106 case OP_FTSVTX:
16107 case OP_FTTEXT:
16108 case OP_FTBINARY:
16109 case OP_FTMTIME:
16110 case OP_FTATIME:
16111 case OP_FTCTIME:
16112 case OP_READLINK:
16113 case OP_OPEN_DIR:
16114 case OP_READDIR:
16115 case OP_TELLDIR:
16116 case OP_SEEKDIR:
16117 case OP_REWINDDIR:
16118 case OP_CLOSEDIR:
16119 case OP_GMTIME:
16120 case OP_ALARM:
16121 case OP_SEMGET:
16122 case OP_GETLOGIN:
16123 case OP_UNDEF:
16124 case OP_SUBSTR:
16125 case OP_AEACH:
16126 case OP_EACH:
16127 case OP_SORT:
16128 case OP_CALLER:
16129 case OP_DOFILE:
16130 case OP_PROTOTYPE:
16131 case OP_NCMP:
16132 case OP_SMARTMATCH:
16133 case OP_UNPACK:
16134 case OP_SYSOPEN:
16135 case OP_SYSSEEK:
16136 match = 1;
16137 goto do_op;
16138
16139 case OP_ENTERSUB:
16140 case OP_GOTO:
16141 /* XXX tmp hack: these two may call an XS sub, and currently
16142 XS subs don't have a SUB entry on the context stack, so CV and
16143 pad determination goes wrong, and BAD things happen. So, just
16144 don't try to determine the value under those circumstances.
16145 Need a better fix at dome point. DAPM 11/2007 */
16146 break;
16147
16148 case OP_FLIP:
16149 case OP_FLOP:
16150 {
16151 GV * const gv = gv_fetchpvs(".", GV_NOTQUAL, SVt_PV);
16152 if (gv && GvSV(gv) == uninit_sv)
16153 return newSVpvs_flags("$.", SVs_TEMP);
16154 goto do_op;
16155 }
16156
16157 case OP_POS:
16158 /* def-ness of rval pos() is independent of the def-ness of its arg */
16159 if ( !(obase->op_flags & OPf_MOD))
16160 break;
16161
16162 case OP_SCHOMP:
16163 case OP_CHOMP:
16164 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
16165 return newSVpvs_flags("${$/}", SVs_TEMP);
16166 /* FALLTHROUGH */
16167
16168 default:
16169 do_op:
16170 if (!(obase->op_flags & OPf_KIDS))
16171 break;
16172 o = cUNOPx(obase)->op_first;
16173
16174 do_op2:
16175 if (!o)
16176 break;
16177
16178 /* This loop checks all the kid ops, skipping any that cannot pos-
16179 * sibly be responsible for the uninitialized value; i.e., defined
16180 * constants and ops that return nothing. If there is only one op
16181 * left that is not skipped, then we *know* it is responsible for
16182 * the uninitialized value. If there is more than one op left, we
16183 * have to look for an exact match in the while() loop below.
16184 * Note that we skip padrange, because the individual pad ops that
16185 * it replaced are still in the tree, so we work on them instead.
16186 */
16187 o2 = NULL;
16188 for (kid=o; kid; kid = OpSIBLING(kid)) {
16189 const OPCODE type = kid->op_type;
16190 if ( (type == OP_CONST && SvOK(cSVOPx_sv(kid)))
16191 || (type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
16192 || (type == OP_PUSHMARK)
16193 || (type == OP_PADRANGE)
16194 )
16195 continue;
16196
16197 if (o2) { /* more than one found */
16198 o2 = NULL;
16199 break;
16200 }
16201 o2 = kid;
16202 }
16203 if (o2)
16204 return find_uninit_var(o2, uninit_sv, match, desc_p);
16205
16206 /* scan all args */
16207 while (o) {
16208 sv = find_uninit_var(o, uninit_sv, 1, desc_p);
16209 if (sv)
16210 return sv;
16211 o = OpSIBLING(o);
16212 }
16213 break;
16214 }
16215 return NULL;
16216 }
16217
16218
16219 /*
16220 =for apidoc report_uninit
16221
16222 Print appropriate "Use of uninitialized variable" warning.
16223
16224 =cut
16225 */
16226
16227 void
16228 Perl_report_uninit(pTHX_ const SV *uninit_sv)
16229 {
16230 if (PL_op) {
16231 SV* varname = NULL;
16232 const char *desc;
16233
16234 desc = PL_op->op_type == OP_STRINGIFY && PL_op->op_folded
16235 ? "join or string"
16236 : OP_DESC(PL_op);
16237 if (uninit_sv && PL_curpad) {
16238 varname = find_uninit_var(PL_op, uninit_sv, 0, &desc);
16239 if (varname)
16240 sv_insert(varname, 0, 0, " ", 1);
16241 }
16242 /* PL_warn_uninit_sv is constant */
16243 GCC_DIAG_IGNORE(-Wformat-nonliteral);
16244 /* diag_listed_as: Use of uninitialized value%s */
16245 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit_sv,
16246 SVfARG(varname ? varname : &PL_sv_no),
16247 " in ", desc);
16248 GCC_DIAG_RESTORE;
16249 }
16250 else {
16251 /* PL_warn_uninit is constant */
16252 GCC_DIAG_IGNORE(-Wformat-nonliteral);
16253 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
16254 "", "", "");
16255 GCC_DIAG_RESTORE;
16256 }
16257 }
16258
16259 /*
16260 * ex: set ts=8 sts=4 sw=4 et:
16261 */