This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add basic usage info to Porting/corelist.pl
[perl5.git] / sv.c
CommitLineData
a0d0e21e 1/* sv.c
79072805 2 *
1129b882 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
83706693
RGS
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall
5 * and others
79072805
LW
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 *
4ac71550
TC
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/*
645c22ef
DM
20 *
21 *
5e045b90
AMS
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.
79072805
LW
28 */
29
30#include "EXTERN.h"
864dbfa3 31#define PERL_IN_SV_C
79072805 32#include "perl.h"
d2f185dc 33#include "regcomp.h"
79072805 34
51371543 35#define FCALL *f
2c5424a7 36
2f8ed50e
OS
37#ifdef __Lynx__
38/* Missing proto on LynxOS */
39 char *gconvert(double, int, int, char *);
40#endif
41
e23c8137 42#ifdef PERL_UTF8_CACHE_ASSERT
ab455f60 43/* if adding more checks watch out for the following tests:
e23c8137
JH
44 * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
45 * lib/utf8.t lib/Unicode/Collate/t/index.t
46 * --jhi
47 */
6f207bd3 48# define ASSERT_UTF8_CACHE(cache) \
ab455f60
NC
49 STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); \
50 assert((cache)[2] <= (cache)[3]); \
51 assert((cache)[3] <= (cache)[1]);} \
52 } STMT_END
e23c8137 53#else
6f207bd3 54# define ASSERT_UTF8_CACHE(cache) NOOP
e23c8137
JH
55#endif
56
f8c7b90f 57#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 58#define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
607fa7f2 59#define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next))
b5ccf5f2 60/* This is a pessimistic view. Scalar must be purely a read-write PV to copy-
765f542d 61 on-write. */
765f542d 62#endif
645c22ef
DM
63
64/* ============================================================================
65
66=head1 Allocation and deallocation of SVs.
67
d2a0f284
JC
68An SV (or AV, HV, etc.) is allocated in two parts: the head (struct
69sv, av, hv...) contains type and reference count information, and for
70many types, a pointer to the body (struct xrv, xpv, xpviv...), which
71contains fields specific to each type. Some types store all they need
72in the head, so don't have a body.
73
74In all but the most memory-paranoid configuations (ex: PURIFY), heads
75and bodies are allocated out of arenas, which by default are
76approximately 4K chunks of memory parcelled up into N heads or bodies.
93e68bfb
JC
77Sv-bodies are allocated by their sv-type, guaranteeing size
78consistency needed to allocate safely from arrays.
79
d2a0f284
JC
80For SV-heads, the first slot in each arena is reserved, and holds a
81link to the next arena, some flags, and a note of the number of slots.
82Snaked through each arena chain is a linked list of free items; when
83this becomes empty, an extra arena is allocated and divided up into N
84items which are threaded into the free list.
85
86SV-bodies are similar, but they use arena-sets by default, which
87separate the link and info from the arena itself, and reclaim the 1st
88slot in the arena. SV-bodies are further described later.
645c22ef
DM
89
90The following global variables are associated with arenas:
91
92 PL_sv_arenaroot pointer to list of SV arenas
93 PL_sv_root pointer to list of free SV structures
94
d2a0f284
JC
95 PL_body_arenas head of linked-list of body arenas
96 PL_body_roots[] array of pointers to list of free bodies of svtype
97 arrays are indexed by the svtype needed
93e68bfb 98
d2a0f284
JC
99A few special SV heads are not allocated from an arena, but are
100instead directly created in the interpreter structure, eg PL_sv_undef.
93e68bfb
JC
101The size of arenas can be changed from the default by setting
102PERL_ARENA_SIZE appropriately at compile time.
645c22ef
DM
103
104The SV arena serves the secondary purpose of allowing still-live SVs
105to be located and destroyed during final cleanup.
106
107At the lowest level, the macros new_SV() and del_SV() grab and free
108an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
109to return the SV to the free list with error checking.) new_SV() calls
110more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
111SVs in the free list have their SvTYPE field set to all ones.
112
ff276b08 113At the time of very final cleanup, sv_free_arenas() is called from
645c22ef 114perl_destruct() to physically free all the arenas allocated since the
6a93a7e5 115start of the interpreter.
645c22ef 116
645c22ef
DM
117The function visit() scans the SV arenas list, and calls a specified
118function for each SV it finds which is still live - ie which has an SvTYPE
119other than all 1's, and a non-zero SvREFCNT. visit() is used by the
120following functions (specified as [function that calls visit()] / [function
121called by visit() for each SV]):
122
123 sv_report_used() / do_report_used()
f2524eef 124 dump all remaining SVs (debugging aid)
645c22ef
DM
125
126 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
127 Attempt to free all objects pointed to by RVs,
128 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
129 try to do the same for all objects indirectly
130 referenced by typeglobs too. Called once from
131 perl_destruct(), prior to calling sv_clean_all()
132 below.
133
134 sv_clean_all() / do_clean_all()
135 SvREFCNT_dec(sv) each remaining SV, possibly
136 triggering an sv_free(). It also sets the
137 SVf_BREAK flag on the SV to indicate that the
138 refcnt has been artificially lowered, and thus
139 stopping sv_free() from giving spurious warnings
140 about SVs which unexpectedly have a refcnt
141 of zero. called repeatedly from perl_destruct()
142 until there are no SVs left.
143
93e68bfb 144=head2 Arena allocator API Summary
645c22ef
DM
145
146Private API to rest of sv.c
147
148 new_SV(), del_SV(),
149
150 new_XIV(), del_XIV(),
151 new_XNV(), del_XNV(),
152 etc
153
154Public API:
155
8cf8f3d1 156 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
645c22ef 157
645c22ef
DM
158=cut
159
3e8320cc 160 * ========================================================================= */
645c22ef 161
4561caa4
CS
162/*
163 * "A time to plant, and a time to uproot what was planted..."
164 */
165
77354fb4 166void
de37a194 167Perl_offer_nice_chunk(pTHX_ void *const chunk, const U32 chunk_size)
77354fb4 168{
97aff369 169 dVAR;
77354fb4
NC
170 void *new_chunk;
171 U32 new_chunk_size;
7918f24d
NC
172
173 PERL_ARGS_ASSERT_OFFER_NICE_CHUNK;
174
77354fb4
NC
175 new_chunk = (void *)(chunk);
176 new_chunk_size = (chunk_size);
177 if (new_chunk_size > PL_nice_chunk_size) {
178 Safefree(PL_nice_chunk);
179 PL_nice_chunk = (char *) new_chunk;
180 PL_nice_chunk_size = new_chunk_size;
181 } else {
182 Safefree(chunk);
183 }
77354fb4 184}
cac9b346 185
d7a2c63c
MHM
186#ifdef PERL_MEM_LOG
187# define MEM_LOG_NEW_SV(sv, file, line, func) \
188 Perl_mem_log_new_sv(sv, file, line, func)
189# define MEM_LOG_DEL_SV(sv, file, line, func) \
190 Perl_mem_log_del_sv(sv, file, line, func)
191#else
192# define MEM_LOG_NEW_SV(sv, file, line, func) NOOP
193# define MEM_LOG_DEL_SV(sv, file, line, func) NOOP
194#endif
195
fd0854ff 196#ifdef DEBUG_LEAKING_SCALARS
22162ca8 197# define FREE_SV_DEBUG_FILE(sv) Safefree((sv)->sv_debug_file)
d7a2c63c
MHM
198# define DEBUG_SV_SERIAL(sv) \
199 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) del_SV\n", \
200 PTR2UV(sv), (long)(sv)->sv_debug_serial))
fd0854ff
DM
201#else
202# define FREE_SV_DEBUG_FILE(sv)
d7a2c63c 203# define DEBUG_SV_SERIAL(sv) NOOP
fd0854ff
DM
204#endif
205
48614a46
NC
206#ifdef PERL_POISON
207# define SvARENA_CHAIN(sv) ((sv)->sv_u.svu_rv)
daba3364 208# define SvARENA_CHAIN_SET(sv,val) (sv)->sv_u.svu_rv = MUTABLE_SV((val))
48614a46
NC
209/* Whilst I'd love to do this, it seems that things like to check on
210 unreferenced scalars
7e337ee0 211# define POSION_SV_HEAD(sv) PoisonNew(sv, 1, struct STRUCT_SV)
48614a46 212*/
7e337ee0
JH
213# define POSION_SV_HEAD(sv) PoisonNew(&SvANY(sv), 1, void *), \
214 PoisonNew(&SvREFCNT(sv), 1, U32)
48614a46
NC
215#else
216# define SvARENA_CHAIN(sv) SvANY(sv)
3eef1deb 217# define SvARENA_CHAIN_SET(sv,val) SvANY(sv) = (void *)(val)
48614a46
NC
218# define POSION_SV_HEAD(sv)
219#endif
220
990198f0
DM
221/* Mark an SV head as unused, and add to free list.
222 *
223 * If SVf_BREAK is set, skip adding it to the free list, as this SV had
224 * its refcount artificially decremented during global destruction, so
225 * there may be dangling pointers to it. The last thing we want in that
226 * case is for it to be reused. */
227
053fc874
GS
228#define plant_SV(p) \
229 STMT_START { \
990198f0 230 const U32 old_flags = SvFLAGS(p); \
d7a2c63c
MHM
231 MEM_LOG_DEL_SV(p, __FILE__, __LINE__, FUNCTION__); \
232 DEBUG_SV_SERIAL(p); \
fd0854ff 233 FREE_SV_DEBUG_FILE(p); \
48614a46 234 POSION_SV_HEAD(p); \
053fc874 235 SvFLAGS(p) = SVTYPEMASK; \
990198f0 236 if (!(old_flags & SVf_BREAK)) { \
3eef1deb 237 SvARENA_CHAIN_SET(p, PL_sv_root); \
990198f0
DM
238 PL_sv_root = (p); \
239 } \
053fc874
GS
240 --PL_sv_count; \
241 } STMT_END
a0d0e21e 242
053fc874
GS
243#define uproot_SV(p) \
244 STMT_START { \
245 (p) = PL_sv_root; \
daba3364 246 PL_sv_root = MUTABLE_SV(SvARENA_CHAIN(p)); \
053fc874
GS
247 ++PL_sv_count; \
248 } STMT_END
249
645c22ef 250
cac9b346
NC
251/* make some more SVs by adding another arena */
252
cac9b346
NC
253STATIC SV*
254S_more_sv(pTHX)
255{
97aff369 256 dVAR;
cac9b346
NC
257 SV* sv;
258
259 if (PL_nice_chunk) {
260 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
bd61b366 261 PL_nice_chunk = NULL;
cac9b346
NC
262 PL_nice_chunk_size = 0;
263 }
264 else {
265 char *chunk; /* must use New here to match call to */
d2a0f284 266 Newx(chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
2e7ed132 267 sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
cac9b346
NC
268 }
269 uproot_SV(sv);
270 return sv;
271}
272
645c22ef
DM
273/* new_SV(): return a new, empty SV head */
274
eba0f806
DM
275#ifdef DEBUG_LEAKING_SCALARS
276/* provide a real function for a debugger to play with */
277STATIC SV*
d7a2c63c 278S_new_SV(pTHX_ const char *file, int line, const char *func)
eba0f806
DM
279{
280 SV* sv;
281
eba0f806
DM
282 if (PL_sv_root)
283 uproot_SV(sv);
284 else
cac9b346 285 sv = S_more_sv(aTHX);
eba0f806
DM
286 SvANY(sv) = 0;
287 SvREFCNT(sv) = 1;
288 SvFLAGS(sv) = 0;
fd0854ff 289 sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
e385c3bf
DM
290 sv->sv_debug_line = (U16) (PL_parser && PL_parser->copline != NOLINE
291 ? PL_parser->copline
292 : PL_curcop
f24aceb1
DM
293 ? CopLINE(PL_curcop)
294 : 0
e385c3bf 295 );
fd0854ff
DM
296 sv->sv_debug_inpad = 0;
297 sv->sv_debug_cloned = 0;
fd0854ff 298 sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL;
d7a2c63c
MHM
299
300 sv->sv_debug_serial = PL_sv_serial++;
301
302 MEM_LOG_NEW_SV(sv, file, line, func);
303 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) new_SV (from %s:%d [%s])\n",
304 PTR2UV(sv), (long)sv->sv_debug_serial, file, line, func));
305
eba0f806
DM
306 return sv;
307}
d7a2c63c 308# define new_SV(p) (p)=S_new_SV(aTHX_ __FILE__, __LINE__, FUNCTION__)
eba0f806
DM
309
310#else
311# define new_SV(p) \
053fc874 312 STMT_START { \
053fc874
GS
313 if (PL_sv_root) \
314 uproot_SV(p); \
315 else \
cac9b346 316 (p) = S_more_sv(aTHX); \
053fc874
GS
317 SvANY(p) = 0; \
318 SvREFCNT(p) = 1; \
319 SvFLAGS(p) = 0; \
d7a2c63c 320 MEM_LOG_NEW_SV(p, __FILE__, __LINE__, FUNCTION__); \
053fc874 321 } STMT_END
eba0f806 322#endif
463ee0b2 323
645c22ef
DM
324
325/* del_SV(): return an empty SV head to the free list */
326
a0d0e21e 327#ifdef DEBUGGING
4561caa4 328
053fc874
GS
329#define del_SV(p) \
330 STMT_START { \
aea4f609 331 if (DEBUG_D_TEST) \
053fc874
GS
332 del_sv(p); \
333 else \
334 plant_SV(p); \
053fc874 335 } STMT_END
a0d0e21e 336
76e3520e 337STATIC void
cea2e8a9 338S_del_sv(pTHX_ SV *p)
463ee0b2 339{
97aff369 340 dVAR;
7918f24d
NC
341
342 PERL_ARGS_ASSERT_DEL_SV;
343
aea4f609 344 if (DEBUG_D_TEST) {
4633a7c4 345 SV* sva;
a3b680e6 346 bool ok = 0;
daba3364 347 for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
53c1dcc0
AL
348 const SV * const sv = sva + 1;
349 const SV * const svend = &sva[SvREFCNT(sva)];
c0ff570e 350 if (p >= sv && p < svend) {
a0d0e21e 351 ok = 1;
c0ff570e
NC
352 break;
353 }
a0d0e21e
LW
354 }
355 if (!ok) {
0453d815 356 if (ckWARN_d(WARN_INTERNAL))
9014280d 357 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
472d47bc
SB
358 "Attempt to free non-arena SV: 0x%"UVxf
359 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
a0d0e21e
LW
360 return;
361 }
362 }
4561caa4 363 plant_SV(p);
463ee0b2 364}
a0d0e21e 365
4561caa4
CS
366#else /* ! DEBUGGING */
367
368#define del_SV(p) plant_SV(p)
369
370#endif /* DEBUGGING */
463ee0b2 371
645c22ef
DM
372
373/*
ccfc67b7
JH
374=head1 SV Manipulation Functions
375
645c22ef
DM
376=for apidoc sv_add_arena
377
378Given a chunk of memory, link it to the head of the list of arenas,
379and split it into a list of free SVs.
380
381=cut
382*/
383
d2bd4e7f
NC
384static void
385S_sv_add_arena(pTHX_ char *const ptr, const U32 size, const U32 flags)
463ee0b2 386{
97aff369 387 dVAR;
daba3364 388 SV *const sva = MUTABLE_SV(ptr);
463ee0b2
LW
389 register SV* sv;
390 register SV* svend;
4633a7c4 391
7918f24d
NC
392 PERL_ARGS_ASSERT_SV_ADD_ARENA;
393
4633a7c4 394 /* The first SV in an arena isn't an SV. */
3280af22 395 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
4633a7c4
LW
396 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
397 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
398
3280af22
NIS
399 PL_sv_arenaroot = sva;
400 PL_sv_root = sva + 1;
4633a7c4
LW
401
402 svend = &sva[SvREFCNT(sva) - 1];
403 sv = sva + 1;
463ee0b2 404 while (sv < svend) {
3eef1deb 405 SvARENA_CHAIN_SET(sv, (sv + 1));
03e36789 406#ifdef DEBUGGING
978b032e 407 SvREFCNT(sv) = 0;
03e36789 408#endif
4b69cbe3 409 /* Must always set typemask because it's always checked in on cleanup
03e36789 410 when the arenas are walked looking for objects. */
8990e307 411 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
412 sv++;
413 }
3eef1deb 414 SvARENA_CHAIN_SET(sv, 0);
03e36789
NC
415#ifdef DEBUGGING
416 SvREFCNT(sv) = 0;
417#endif
4633a7c4
LW
418 SvFLAGS(sv) = SVTYPEMASK;
419}
420
055972dc
DM
421/* visit(): call the named function for each non-free SV in the arenas
422 * whose flags field matches the flags/mask args. */
645c22ef 423
5226ed68 424STATIC I32
de37a194 425S_visit(pTHX_ SVFUNC_t f, const U32 flags, const U32 mask)
8990e307 426{
97aff369 427 dVAR;
4633a7c4 428 SV* sva;
5226ed68 429 I32 visited = 0;
8990e307 430
7918f24d
NC
431 PERL_ARGS_ASSERT_VISIT;
432
daba3364 433 for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
53c1dcc0 434 register const SV * const svend = &sva[SvREFCNT(sva)];
a3b680e6 435 register SV* sv;
4561caa4 436 for (sv = sva + 1; sv < svend; ++sv) {
055972dc
DM
437 if (SvTYPE(sv) != SVTYPEMASK
438 && (sv->sv_flags & mask) == flags
439 && SvREFCNT(sv))
440 {
acfe0abc 441 (FCALL)(aTHX_ sv);
5226ed68
JH
442 ++visited;
443 }
8990e307
LW
444 }
445 }
5226ed68 446 return visited;
8990e307
LW
447}
448
758a08c3
JH
449#ifdef DEBUGGING
450
645c22ef
DM
451/* called by sv_report_used() for each live SV */
452
453static void
5fa45a31 454do_report_used(pTHX_ SV *const sv)
645c22ef
DM
455{
456 if (SvTYPE(sv) != SVTYPEMASK) {
457 PerlIO_printf(Perl_debug_log, "****\n");
458 sv_dump(sv);
459 }
460}
758a08c3 461#endif
645c22ef
DM
462
463/*
464=for apidoc sv_report_used
465
466Dump the contents of all SVs not yet freed. (Debugging aid).
467
468=cut
469*/
470
8990e307 471void
864dbfa3 472Perl_sv_report_used(pTHX)
4561caa4 473{
ff270d3a 474#ifdef DEBUGGING
055972dc 475 visit(do_report_used, 0, 0);
96a5add6
AL
476#else
477 PERL_UNUSED_CONTEXT;
ff270d3a 478#endif
4561caa4
CS
479}
480
645c22ef
DM
481/* called by sv_clean_objs() for each live SV */
482
483static void
de37a194 484do_clean_objs(pTHX_ SV *const ref)
645c22ef 485{
97aff369 486 dVAR;
ea724faa
NC
487 assert (SvROK(ref));
488 {
823a54a3
AL
489 SV * const target = SvRV(ref);
490 if (SvOBJECT(target)) {
491 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(ref)));
492 if (SvWEAKREF(ref)) {
493 sv_del_backref(target, ref);
494 SvWEAKREF_off(ref);
495 SvRV_set(ref, NULL);
496 } else {
497 SvROK_off(ref);
498 SvRV_set(ref, NULL);
499 SvREFCNT_dec(target);
500 }
645c22ef
DM
501 }
502 }
503
504 /* XXX Might want to check arrays, etc. */
505}
506
507/* called by sv_clean_objs() for each live SV */
508
509#ifndef DISABLE_DESTRUCTOR_KLUDGE
510static void
f30de749 511do_clean_named_objs(pTHX_ SV *const sv)
645c22ef 512{
97aff369 513 dVAR;
ea724faa 514 assert(SvTYPE(sv) == SVt_PVGV);
d011219a
NC
515 assert(isGV_with_GP(sv));
516 if (GvGP(sv)) {
c69033f2
NC
517 if ((
518#ifdef PERL_DONT_CREATE_GVSV
519 GvSV(sv) &&
520#endif
521 SvOBJECT(GvSV(sv))) ||
645c22ef
DM
522 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
523 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
9c12f1e5
RGS
524 /* In certain rare cases GvIOp(sv) can be NULL, which would make SvOBJECT(GvIO(sv)) dereference NULL. */
525 (GvIO(sv) ? (SvFLAGS(GvIOp(sv)) & SVs_OBJECT) : 0) ||
645c22ef
DM
526 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
527 {
528 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
ec5f3c78 529 SvFLAGS(sv) |= SVf_BREAK;
645c22ef
DM
530 SvREFCNT_dec(sv);
531 }
532 }
533}
534#endif
535
536/*
537=for apidoc sv_clean_objs
538
539Attempt to destroy all objects not yet freed
540
541=cut
542*/
543
4561caa4 544void
864dbfa3 545Perl_sv_clean_objs(pTHX)
4561caa4 546{
97aff369 547 dVAR;
3280af22 548 PL_in_clean_objs = TRUE;
055972dc 549 visit(do_clean_objs, SVf_ROK, SVf_ROK);
4561caa4 550#ifndef DISABLE_DESTRUCTOR_KLUDGE
2d0f3c12 551 /* some barnacles may yet remain, clinging to typeglobs */
d011219a 552 visit(do_clean_named_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
4561caa4 553#endif
3280af22 554 PL_in_clean_objs = FALSE;
4561caa4
CS
555}
556
645c22ef
DM
557/* called by sv_clean_all() for each live SV */
558
559static void
de37a194 560do_clean_all(pTHX_ SV *const sv)
645c22ef 561{
97aff369 562 dVAR;
daba3364 563 if (sv == (const SV *) PL_fdpid || sv == (const SV *)PL_strtab) {
cddfcddc 564 /* don't clean pid table and strtab */
d17ea597 565 return;
cddfcddc 566 }
645c22ef
DM
567 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
568 SvFLAGS(sv) |= SVf_BREAK;
569 SvREFCNT_dec(sv);
570}
571
572/*
573=for apidoc sv_clean_all
574
575Decrement the refcnt of each remaining SV, possibly triggering a
576cleanup. This function may have to be called multiple times to free
ff276b08 577SVs which are in complex self-referential hierarchies.
645c22ef
DM
578
579=cut
580*/
581
5226ed68 582I32
864dbfa3 583Perl_sv_clean_all(pTHX)
8990e307 584{
97aff369 585 dVAR;
5226ed68 586 I32 cleaned;
3280af22 587 PL_in_clean_all = TRUE;
055972dc 588 cleaned = visit(do_clean_all, 0,0);
3280af22 589 PL_in_clean_all = FALSE;
5226ed68 590 return cleaned;
8990e307 591}
463ee0b2 592
5e258f8c
JC
593/*
594 ARENASETS: a meta-arena implementation which separates arena-info
595 into struct arena_set, which contains an array of struct
596 arena_descs, each holding info for a single arena. By separating
597 the meta-info from the arena, we recover the 1st slot, formerly
598 borrowed for list management. The arena_set is about the size of an
39244528 599 arena, avoiding the needless malloc overhead of a naive linked-list.
5e258f8c
JC
600
601 The cost is 1 arena-set malloc per ~320 arena-mallocs, + the unused
602 memory in the last arena-set (1/2 on average). In trade, we get
603 back the 1st slot in each arena (ie 1.7% of a CV-arena, less for
d2a0f284 604 smaller types). The recovery of the wasted space allows use of
e15dad31
JC
605 small arenas for large, rare body types, by changing array* fields
606 in body_details_by_type[] below.
5e258f8c 607*/
5e258f8c 608struct arena_desc {
398c677b
NC
609 char *arena; /* the raw storage, allocated aligned */
610 size_t size; /* its size ~4k typ */
0a848332 611 U32 misc; /* type, and in future other things. */
5e258f8c
JC
612};
613
e6148039
NC
614struct arena_set;
615
616/* Get the maximum number of elements in set[] such that struct arena_set
e15dad31 617 will fit within PERL_ARENA_SIZE, which is probably just under 4K, and
e6148039
NC
618 therefore likely to be 1 aligned memory page. */
619
620#define ARENAS_PER_SET ((PERL_ARENA_SIZE - sizeof(struct arena_set*) \
621 - 2 * sizeof(int)) / sizeof (struct arena_desc))
5e258f8c
JC
622
623struct arena_set {
624 struct arena_set* next;
0a848332
NC
625 unsigned int set_size; /* ie ARENAS_PER_SET */
626 unsigned int curr; /* index of next available arena-desc */
5e258f8c
JC
627 struct arena_desc set[ARENAS_PER_SET];
628};
629
645c22ef
DM
630/*
631=for apidoc sv_free_arenas
632
633Deallocate the memory used by all arenas. Note that all the individual SV
634heads and bodies within the arenas must already have been freed.
635
636=cut
637*/
4633a7c4 638void
864dbfa3 639Perl_sv_free_arenas(pTHX)
4633a7c4 640{
97aff369 641 dVAR;
4633a7c4
LW
642 SV* sva;
643 SV* svanext;
0a848332 644 unsigned int i;
4633a7c4
LW
645
646 /* Free arenas here, but be careful about fake ones. (We assume
647 contiguity of the fake ones with the corresponding real ones.) */
648
3280af22 649 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
daba3364 650 svanext = MUTABLE_SV(SvANY(sva));
4633a7c4 651 while (svanext && SvFAKE(svanext))
daba3364 652 svanext = MUTABLE_SV(SvANY(svanext));
4633a7c4
LW
653
654 if (!SvFAKE(sva))
1df70142 655 Safefree(sva);
4633a7c4 656 }
93e68bfb 657
5e258f8c 658 {
0a848332
NC
659 struct arena_set *aroot = (struct arena_set*) PL_body_arenas;
660
661 while (aroot) {
662 struct arena_set *current = aroot;
663 i = aroot->curr;
664 while (i--) {
5e258f8c
JC
665 assert(aroot->set[i].arena);
666 Safefree(aroot->set[i].arena);
667 }
0a848332
NC
668 aroot = aroot->next;
669 Safefree(current);
5e258f8c
JC
670 }
671 }
dc8220bf 672 PL_body_arenas = 0;
fdda85ca 673
0a848332
NC
674 i = PERL_ARENA_ROOTS_SIZE;
675 while (i--)
93e68bfb 676 PL_body_roots[i] = 0;
93e68bfb 677
43c5f42d 678 Safefree(PL_nice_chunk);
bd61b366 679 PL_nice_chunk = NULL;
3280af22
NIS
680 PL_nice_chunk_size = 0;
681 PL_sv_arenaroot = 0;
682 PL_sv_root = 0;
4633a7c4
LW
683}
684
bd81e77b
NC
685/*
686 Here are mid-level routines that manage the allocation of bodies out
687 of the various arenas. There are 5 kinds of arenas:
29489e7c 688
bd81e77b
NC
689 1. SV-head arenas, which are discussed and handled above
690 2. regular body arenas
691 3. arenas for reduced-size bodies
692 4. Hash-Entry arenas
693 5. pte arenas (thread related)
29489e7c 694
bd81e77b
NC
695 Arena types 2 & 3 are chained by body-type off an array of
696 arena-root pointers, which is indexed by svtype. Some of the
697 larger/less used body types are malloced singly, since a large
698 unused block of them is wasteful. Also, several svtypes dont have
699 bodies; the data fits into the sv-head itself. The arena-root
700 pointer thus has a few unused root-pointers (which may be hijacked
701 later for arena types 4,5)
29489e7c 702
bd81e77b
NC
703 3 differs from 2 as an optimization; some body types have several
704 unused fields in the front of the structure (which are kept in-place
705 for consistency). These bodies can be allocated in smaller chunks,
706 because the leading fields arent accessed. Pointers to such bodies
707 are decremented to point at the unused 'ghost' memory, knowing that
708 the pointers are used with offsets to the real memory.
29489e7c 709
bd81e77b
NC
710 HE, HEK arenas are managed separately, with separate code, but may
711 be merge-able later..
712
713 PTE arenas are not sv-bodies, but they share these mid-level
714 mechanics, so are considered here. The new mid-level mechanics rely
715 on the sv_type of the body being allocated, so we just reserve one
716 of the unused body-slots for PTEs, then use it in those (2) PTE
717 contexts below (line ~10k)
718*/
719
bd26d9a3 720/* get_arena(size): this creates custom-sized arenas
5e258f8c
JC
721 TBD: export properly for hv.c: S_more_he().
722*/
723void*
de37a194 724Perl_get_arena(pTHX_ const size_t arena_size, const U32 misc)
5e258f8c 725{
7a89be66 726 dVAR;
5e258f8c 727 struct arena_desc* adesc;
39244528 728 struct arena_set *aroot = (struct arena_set*) PL_body_arenas;
0a848332 729 unsigned int curr;
5e258f8c 730
476a1e16
JC
731 /* shouldnt need this
732 if (!arena_size) arena_size = PERL_ARENA_SIZE;
733 */
5e258f8c
JC
734
735 /* may need new arena-set to hold new arena */
39244528
NC
736 if (!aroot || aroot->curr >= aroot->set_size) {
737 struct arena_set *newroot;
5e258f8c
JC
738 Newxz(newroot, 1, struct arena_set);
739 newroot->set_size = ARENAS_PER_SET;
39244528
NC
740 newroot->next = aroot;
741 aroot = newroot;
742 PL_body_arenas = (void *) newroot;
52944de8 743 DEBUG_m(PerlIO_printf(Perl_debug_log, "new arenaset %p\n", (void*)aroot));
5e258f8c
JC
744 }
745
746 /* ok, now have arena-set with at least 1 empty/available arena-desc */
39244528
NC
747 curr = aroot->curr++;
748 adesc = &(aroot->set[curr]);
5e258f8c
JC
749 assert(!adesc->arena);
750
89086707 751 Newx(adesc->arena, arena_size, char);
5e258f8c 752 adesc->size = arena_size;
0a848332 753 adesc->misc = misc;
d67b3c53
JH
754 DEBUG_m(PerlIO_printf(Perl_debug_log, "arena %d added: %p size %"UVuf"\n",
755 curr, (void*)adesc->arena, (UV)arena_size));
5e258f8c
JC
756
757 return adesc->arena;
5e258f8c
JC
758}
759
53c1dcc0 760
bd81e77b 761/* return a thing to the free list */
29489e7c 762
bd81e77b
NC
763#define del_body(thing, root) \
764 STMT_START { \
00b6aa41 765 void ** const thing_copy = (void **)thing;\
bd81e77b
NC
766 *thing_copy = *root; \
767 *root = (void*)thing_copy; \
bd81e77b 768 } STMT_END
29489e7c 769
bd81e77b 770/*
d2a0f284
JC
771
772=head1 SV-Body Allocation
773
774Allocation of SV-bodies is similar to SV-heads, differing as follows;
775the allocation mechanism is used for many body types, so is somewhat
776more complicated, it uses arena-sets, and has no need for still-live
777SV detection.
778
779At the outermost level, (new|del)_X*V macros return bodies of the
780appropriate type. These macros call either (new|del)_body_type or
781(new|del)_body_allocated macro pairs, depending on specifics of the
782type. Most body types use the former pair, the latter pair is used to
783allocate body types with "ghost fields".
784
785"ghost fields" are fields that are unused in certain types, and
69ba284b 786consequently don't need to actually exist. They are declared because
d2a0f284
JC
787they're part of a "base type", which allows use of functions as
788methods. The simplest examples are AVs and HVs, 2 aggregate types
789which don't use the fields which support SCALAR semantics.
790
69ba284b 791For these types, the arenas are carved up into appropriately sized
d2a0f284
JC
792chunks, we thus avoid wasted memory for those unaccessed members.
793When bodies are allocated, we adjust the pointer back in memory by the
69ba284b 794size of the part not allocated, so it's as if we allocated the full
d2a0f284
JC
795structure. (But things will all go boom if you write to the part that
796is "not there", because you'll be overwriting the last members of the
797preceding structure in memory.)
798
69ba284b
NC
799We calculate the correction using the STRUCT_OFFSET macro on the first
800member present. If the allocated structure is smaller (no initial NV
801actually allocated) then the net effect is to subtract the size of the NV
802from the pointer, to return a new pointer as if an initial NV were actually
803allocated. (We were using structures named *_allocated for this, but
804this turned out to be a subtle bug, because a structure without an NV
805could have a lower alignment constraint, but the compiler is allowed to
806optimised accesses based on the alignment constraint of the actual pointer
807to the full structure, for example, using a single 64 bit load instruction
808because it "knows" that two adjacent 32 bit members will be 8-byte aligned.)
d2a0f284
JC
809
810This is the same trick as was used for NV and IV bodies. Ironically it
811doesn't need to be used for NV bodies any more, because NV is now at
812the start of the structure. IV bodies don't need it either, because
813they are no longer allocated.
814
815In turn, the new_body_* allocators call S_new_body(), which invokes
816new_body_inline macro, which takes a lock, and takes a body off the
817linked list at PL_body_roots[sv_type], calling S_more_bodies() if
818necessary to refresh an empty list. Then the lock is released, and
819the body is returned.
820
821S_more_bodies calls get_arena(), and carves it up into an array of N
822bodies, which it strings into a linked list. It looks up arena-size
823and body-size from the body_details table described below, thus
824supporting the multiple body-types.
825
826If PURIFY is defined, or PERL_ARENA_SIZE=0, arenas are not used, and
827the (new|del)_X*V macros are mapped directly to malloc/free.
828
829*/
830
831/*
832
833For each sv-type, struct body_details bodies_by_type[] carries
834parameters which control these aspects of SV handling:
835
836Arena_size determines whether arenas are used for this body type, and if
837so, how big they are. PURIFY or PERL_ARENA_SIZE=0 set this field to
838zero, forcing individual mallocs and frees.
839
840Body_size determines how big a body is, and therefore how many fit into
841each arena. Offset carries the body-pointer adjustment needed for
69ba284b 842"ghost fields", and is used in *_allocated macros.
d2a0f284
JC
843
844But its main purpose is to parameterize info needed in
845Perl_sv_upgrade(). The info here dramatically simplifies the function
69ba284b 846vs the implementation in 5.8.8, making it table-driven. All fields
d2a0f284
JC
847are used for this, except for arena_size.
848
849For the sv-types that have no bodies, arenas are not used, so those
850PL_body_roots[sv_type] are unused, and can be overloaded. In
851something of a special case, SVt_NULL is borrowed for HE arenas;
c6f8b1d0 852PL_body_roots[HE_SVSLOT=SVt_NULL] is filled by S_more_he, but the
d2a0f284 853bodies_by_type[SVt_NULL] slot is not used, as the table is not
c6f8b1d0 854available in hv.c.
d2a0f284 855
c6f8b1d0
JC
856PTEs also use arenas, but are never seen in Perl_sv_upgrade. Nonetheless,
857they get their own slot in bodies_by_type[PTE_SVSLOT =SVt_IV], so they can
858just use the same allocation semantics. At first, PTEs were also
859overloaded to a non-body sv-type, but this yielded hard-to-find malloc
860bugs, so was simplified by claiming a new slot. This choice has no
861consequence at this time.
d2a0f284 862
29489e7c
DM
863*/
864
bd81e77b 865struct body_details {
0fb58b32 866 U8 body_size; /* Size to allocate */
10666ae3 867 U8 copy; /* Size of structure to copy (may be shorter) */
0fb58b32 868 U8 offset;
10666ae3
NC
869 unsigned int type : 4; /* We have space for a sanity check. */
870 unsigned int cant_upgrade : 1; /* Cannot upgrade this type */
871 unsigned int zero_nv : 1; /* zero the NV when upgrading from this */
872 unsigned int arena : 1; /* Allocated from an arena */
873 size_t arena_size; /* Size of arena to allocate */
bd81e77b 874};
29489e7c 875
bd81e77b
NC
876#define HADNV FALSE
877#define NONV TRUE
29489e7c 878
d2a0f284 879
bd81e77b
NC
880#ifdef PURIFY
881/* With -DPURFIY we allocate everything directly, and don't use arenas.
882 This seems a rather elegant way to simplify some of the code below. */
883#define HASARENA FALSE
884#else
885#define HASARENA TRUE
886#endif
887#define NOARENA FALSE
29489e7c 888
d2a0f284
JC
889/* Size the arenas to exactly fit a given number of bodies. A count
890 of 0 fits the max number bodies into a PERL_ARENA_SIZE.block,
891 simplifying the default. If count > 0, the arena is sized to fit
892 only that many bodies, allowing arenas to be used for large, rare
893 bodies (XPVFM, XPVIO) without undue waste. The arena size is
894 limited by PERL_ARENA_SIZE, so we can safely oversize the
895 declarations.
896 */
95db5f15
MB
897#define FIT_ARENA0(body_size) \
898 ((size_t)(PERL_ARENA_SIZE / body_size) * body_size)
899#define FIT_ARENAn(count,body_size) \
900 ( count * body_size <= PERL_ARENA_SIZE) \
901 ? count * body_size \
902 : FIT_ARENA0 (body_size)
903#define FIT_ARENA(count,body_size) \
904 count \
905 ? FIT_ARENAn (count, body_size) \
906 : FIT_ARENA0 (body_size)
d2a0f284 907
bd81e77b
NC
908/* Calculate the length to copy. Specifically work out the length less any
909 final padding the compiler needed to add. See the comment in sv_upgrade
910 for why copying the padding proved to be a bug. */
29489e7c 911
bd81e77b
NC
912#define copy_length(type, last_member) \
913 STRUCT_OFFSET(type, last_member) \
daba3364 914 + sizeof (((type*)SvANY((const SV *)0))->last_member)
29489e7c 915
bd81e77b 916static const struct body_details bodies_by_type[] = {
10666ae3
NC
917 { sizeof(HE), 0, 0, SVt_NULL,
918 FALSE, NONV, NOARENA, FIT_ARENA(0, sizeof(HE)) },
d2a0f284 919
1cb9cd50 920 /* The bind placeholder pretends to be an RV for now.
c6f8b1d0 921 Also it's marked as "can't upgrade" to stop anyone using it before it's
1cb9cd50
NC
922 implemented. */
923 { 0, 0, 0, SVt_BIND, TRUE, NONV, NOARENA, 0 },
924
d2a0f284
JC
925 /* IVs are in the head, so the allocation size is 0.
926 However, the slot is overloaded for PTEs. */
927 { sizeof(struct ptr_tbl_ent), /* This is used for PTEs. */
928 sizeof(IV), /* This is used to copy out the IV body. */
10666ae3 929 STRUCT_OFFSET(XPVIV, xiv_iv), SVt_IV, FALSE, NONV,
d2a0f284
JC
930 NOARENA /* IVS don't need an arena */,
931 /* But PTEs need to know the size of their arena */
932 FIT_ARENA(0, sizeof(struct ptr_tbl_ent))
933 },
934
bd81e77b 935 /* 8 bytes on most ILP32 with IEEE doubles */
10666ae3 936 { sizeof(NV), sizeof(NV), 0, SVt_NV, FALSE, HADNV, HASARENA,
d2a0f284
JC
937 FIT_ARENA(0, sizeof(NV)) },
938
bd81e77b 939 /* 8 bytes on most ILP32 with IEEE doubles */
69ba284b
NC
940 { sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur),
941 copy_length(XPV, xpv_len) - STRUCT_OFFSET(XPV, xpv_cur),
942 + STRUCT_OFFSET(XPV, xpv_cur),
943 SVt_PV, FALSE, NONV, HASARENA,
944 FIT_ARENA(0, sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur)) },
d2a0f284 945
bd81e77b 946 /* 12 */
69ba284b
NC
947 { sizeof(XPVIV) - STRUCT_OFFSET(XPV, xpv_cur),
948 copy_length(XPVIV, xiv_u) - STRUCT_OFFSET(XPV, xpv_cur),
949 + STRUCT_OFFSET(XPVIV, xpv_cur),
950 SVt_PVIV, FALSE, NONV, HASARENA,
951 FIT_ARENA(0, sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur)) },
d2a0f284 952
bd81e77b 953 /* 20 */
10666ae3 954 { sizeof(XPVNV), copy_length(XPVNV, xiv_u), 0, SVt_PVNV, FALSE, HADNV,
d2a0f284
JC
955 HASARENA, FIT_ARENA(0, sizeof(XPVNV)) },
956
bd81e77b 957 /* 28 */
10666ae3 958 { sizeof(XPVMG), copy_length(XPVMG, xmg_stash), 0, SVt_PVMG, FALSE, HADNV,
d2a0f284 959 HASARENA, FIT_ARENA(0, sizeof(XPVMG)) },
4df7f6af 960
288b8c02 961 /* something big */
b6f60916
NC
962 { sizeof(regexp) - STRUCT_OFFSET(regexp, xpv_cur),
963 sizeof(regexp) - STRUCT_OFFSET(regexp, xpv_cur),
964 + STRUCT_OFFSET(regexp, xpv_cur),
08e44740 965 SVt_REGEXP, FALSE, NONV, HASARENA,
b6f60916 966 FIT_ARENA(0, sizeof(regexp) - STRUCT_OFFSET(regexp, xpv_cur))
5c35adbb 967 },
4df7f6af 968
bd81e77b 969 /* 48 */
10666ae3 970 { sizeof(XPVGV), sizeof(XPVGV), 0, SVt_PVGV, TRUE, HADNV,
d2a0f284
JC
971 HASARENA, FIT_ARENA(0, sizeof(XPVGV)) },
972
bd81e77b 973 /* 64 */
10666ae3 974 { sizeof(XPVLV), sizeof(XPVLV), 0, SVt_PVLV, TRUE, HADNV,
d2a0f284
JC
975 HASARENA, FIT_ARENA(0, sizeof(XPVLV)) },
976
69ba284b
NC
977 { sizeof(XPVAV) - STRUCT_OFFSET(XPVAV, xav_fill),
978 copy_length(XPVAV, xmg_stash) - STRUCT_OFFSET(XPVAV, xav_fill),
979 + STRUCT_OFFSET(XPVAV, xav_fill),
980 SVt_PVAV, TRUE, NONV, HASARENA,
981 FIT_ARENA(0, sizeof(XPVAV) - STRUCT_OFFSET(XPVAV, xav_fill)) },
d2a0f284 982
69ba284b
NC
983 { sizeof(XPVHV) - STRUCT_OFFSET(XPVHV, xhv_fill),
984 copy_length(XPVHV, xmg_stash) - STRUCT_OFFSET(XPVHV, xhv_fill),
985 + STRUCT_OFFSET(XPVHV, xhv_fill),
986 SVt_PVHV, TRUE, NONV, HASARENA,
987 FIT_ARENA(0, sizeof(XPVHV) - STRUCT_OFFSET(XPVHV, xhv_fill)) },
d2a0f284 988
c84c4652 989 /* 56 */
69ba284b
NC
990 { sizeof(XPVCV) - STRUCT_OFFSET(XPVCV, xpv_cur),
991 sizeof(XPVCV) - STRUCT_OFFSET(XPVCV, xpv_cur),
992 + STRUCT_OFFSET(XPVCV, xpv_cur),
993 SVt_PVCV, TRUE, NONV, HASARENA,
994 FIT_ARENA(0, sizeof(XPVCV) - STRUCT_OFFSET(XPVCV, xpv_cur)) },
995
996 { sizeof(XPVFM) - STRUCT_OFFSET(XPVFM, xpv_cur),
997 sizeof(XPVFM) - STRUCT_OFFSET(XPVFM, xpv_cur),
998 + STRUCT_OFFSET(XPVFM, xpv_cur),
999 SVt_PVFM, TRUE, NONV, NOARENA,
1000 FIT_ARENA(20, sizeof(XPVFM) - STRUCT_OFFSET(XPVFM, xpv_cur)) },
d2a0f284
JC
1001
1002 /* XPVIO is 84 bytes, fits 48x */
b6f60916
NC
1003 { sizeof(XPVIO) - STRUCT_OFFSET(XPVIO, xpv_cur),
1004 sizeof(XPVIO) - STRUCT_OFFSET(XPVIO, xpv_cur),
1005 + STRUCT_OFFSET(XPVIO, xpv_cur),
1006 SVt_PVIO, TRUE, NONV, HASARENA,
1007 FIT_ARENA(24, sizeof(XPVIO) - STRUCT_OFFSET(XPVIO, xpv_cur)) },
bd81e77b 1008};
29489e7c 1009
d2a0f284
JC
1010#define new_body_type(sv_type) \
1011 (void *)((char *)S_new_body(aTHX_ sv_type))
29489e7c 1012
bd81e77b
NC
1013#define del_body_type(p, sv_type) \
1014 del_body(p, &PL_body_roots[sv_type])
29489e7c 1015
29489e7c 1016
bd81e77b 1017#define new_body_allocated(sv_type) \
d2a0f284 1018 (void *)((char *)S_new_body(aTHX_ sv_type) \
bd81e77b 1019 - bodies_by_type[sv_type].offset)
29489e7c 1020
bd81e77b
NC
1021#define del_body_allocated(p, sv_type) \
1022 del_body(p + bodies_by_type[sv_type].offset, &PL_body_roots[sv_type])
29489e7c 1023
29489e7c 1024
bd81e77b
NC
1025#define my_safemalloc(s) (void*)safemalloc(s)
1026#define my_safecalloc(s) (void*)safecalloc(s, 1)
1027#define my_safefree(p) safefree((char*)p)
29489e7c 1028
bd81e77b 1029#ifdef PURIFY
29489e7c 1030
bd81e77b
NC
1031#define new_XNV() my_safemalloc(sizeof(XPVNV))
1032#define del_XNV(p) my_safefree(p)
29489e7c 1033
bd81e77b
NC
1034#define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1035#define del_XPVNV(p) my_safefree(p)
29489e7c 1036
bd81e77b
NC
1037#define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1038#define del_XPVAV(p) my_safefree(p)
29489e7c 1039
bd81e77b
NC
1040#define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1041#define del_XPVHV(p) my_safefree(p)
29489e7c 1042
bd81e77b
NC
1043#define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1044#define del_XPVMG(p) my_safefree(p)
29489e7c 1045
bd81e77b
NC
1046#define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1047#define del_XPVGV(p) my_safefree(p)
29489e7c 1048
bd81e77b 1049#else /* !PURIFY */
29489e7c 1050
bd81e77b
NC
1051#define new_XNV() new_body_type(SVt_NV)
1052#define del_XNV(p) del_body_type(p, SVt_NV)
29489e7c 1053
bd81e77b
NC
1054#define new_XPVNV() new_body_type(SVt_PVNV)
1055#define del_XPVNV(p) del_body_type(p, SVt_PVNV)
29489e7c 1056
bd81e77b
NC
1057#define new_XPVAV() new_body_allocated(SVt_PVAV)
1058#define del_XPVAV(p) del_body_allocated(p, SVt_PVAV)
645c22ef 1059
bd81e77b
NC
1060#define new_XPVHV() new_body_allocated(SVt_PVHV)
1061#define del_XPVHV(p) del_body_allocated(p, SVt_PVHV)
645c22ef 1062
bd81e77b
NC
1063#define new_XPVMG() new_body_type(SVt_PVMG)
1064#define del_XPVMG(p) del_body_type(p, SVt_PVMG)
645c22ef 1065
bd81e77b
NC
1066#define new_XPVGV() new_body_type(SVt_PVGV)
1067#define del_XPVGV(p) del_body_type(p, SVt_PVGV)
1d7c1841 1068
bd81e77b 1069#endif /* PURIFY */
93e68bfb 1070
bd81e77b 1071/* no arena for you! */
93e68bfb 1072
bd81e77b 1073#define new_NOARENA(details) \
d2a0f284 1074 my_safemalloc((details)->body_size + (details)->offset)
bd81e77b 1075#define new_NOARENAZ(details) \
d2a0f284
JC
1076 my_safecalloc((details)->body_size + (details)->offset)
1077
1078STATIC void *
de37a194 1079S_more_bodies (pTHX_ const svtype sv_type)
d2a0f284
JC
1080{
1081 dVAR;
1082 void ** const root = &PL_body_roots[sv_type];
96a5add6 1083 const struct body_details * const bdp = &bodies_by_type[sv_type];
d2a0f284
JC
1084 const size_t body_size = bdp->body_size;
1085 char *start;
1086 const char *end;
d8fca402 1087 const size_t arena_size = Perl_malloc_good_size(bdp->arena_size);
0b2d3faa 1088#if defined(DEBUGGING) && !defined(PERL_GLOBAL_STRUCT_PRIVATE)
23e9d66c
NC
1089 static bool done_sanity_check;
1090
0b2d3faa
JH
1091 /* PERL_GLOBAL_STRUCT_PRIVATE cannot coexist with global
1092 * variables like done_sanity_check. */
10666ae3 1093 if (!done_sanity_check) {
ea471437 1094 unsigned int i = SVt_LAST;
10666ae3
NC
1095
1096 done_sanity_check = TRUE;
1097
1098 while (i--)
1099 assert (bodies_by_type[i].type == i);
1100 }
1101#endif
1102
23e9d66c
NC
1103 assert(bdp->arena_size);
1104
d8fca402 1105 start = (char*) Perl_get_arena(aTHX_ arena_size, sv_type);
d2a0f284 1106
d8fca402 1107 end = start + arena_size - 2 * body_size;
d2a0f284 1108
d2a0f284 1109 /* computed count doesnt reflect the 1st slot reservation */
d8fca402
NC
1110#if defined(MYMALLOC) || defined(HAS_MALLOC_GOOD_SIZE)
1111 DEBUG_m(PerlIO_printf(Perl_debug_log,
1112 "arena %p end %p arena-size %d (from %d) type %d "
1113 "size %d ct %d\n",
1114 (void*)start, (void*)end, (int)arena_size,
1115 (int)bdp->arena_size, sv_type, (int)body_size,
1116 (int)arena_size / (int)body_size));
1117#else
d2a0f284
JC
1118 DEBUG_m(PerlIO_printf(Perl_debug_log,
1119 "arena %p end %p arena-size %d type %d size %d ct %d\n",
6c9570dc 1120 (void*)start, (void*)end,
0e84aef4
JH
1121 (int)bdp->arena_size, sv_type, (int)body_size,
1122 (int)bdp->arena_size / (int)body_size));
d8fca402 1123#endif
d2a0f284
JC
1124 *root = (void *)start;
1125
d8fca402 1126 while (start <= end) {
d2a0f284
JC
1127 char * const next = start + body_size;
1128 *(void**) start = (void *)next;
1129 start = next;
1130 }
1131 *(void **)start = 0;
1132
1133 return *root;
1134}
1135
1136/* grab a new thing from the free list, allocating more if necessary.
1137 The inline version is used for speed in hot routines, and the
1138 function using it serves the rest (unless PURIFY).
1139*/
1140#define new_body_inline(xpv, sv_type) \
1141 STMT_START { \
1142 void ** const r3wt = &PL_body_roots[sv_type]; \
11b79775
DD
1143 xpv = (PTR_TBL_ENT_t*) (*((void **)(r3wt)) \
1144 ? *((void **)(r3wt)) : more_bodies(sv_type)); \
d2a0f284 1145 *(r3wt) = *(void**)(xpv); \
d2a0f284
JC
1146 } STMT_END
1147
1148#ifndef PURIFY
1149
1150STATIC void *
de37a194 1151S_new_body(pTHX_ const svtype sv_type)
d2a0f284
JC
1152{
1153 dVAR;
1154 void *xpv;
1155 new_body_inline(xpv, sv_type);
1156 return xpv;
1157}
1158
1159#endif
93e68bfb 1160
238b27b3
NC
1161static const struct body_details fake_rv =
1162 { 0, 0, 0, SVt_IV, FALSE, NONV, NOARENA, 0 };
1163
bd81e77b
NC
1164/*
1165=for apidoc sv_upgrade
93e68bfb 1166
bd81e77b
NC
1167Upgrade an SV to a more complex form. Generally adds a new body type to the
1168SV, then copies across as much information as possible from the old body.
1169You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
93e68bfb 1170
bd81e77b 1171=cut
93e68bfb 1172*/
93e68bfb 1173
bd81e77b 1174void
aad570aa 1175Perl_sv_upgrade(pTHX_ register SV *const sv, svtype new_type)
cac9b346 1176{
97aff369 1177 dVAR;
bd81e77b
NC
1178 void* old_body;
1179 void* new_body;
42d0e0b7 1180 const svtype old_type = SvTYPE(sv);
d2a0f284 1181 const struct body_details *new_type_details;
238b27b3 1182 const struct body_details *old_type_details
bd81e77b 1183 = bodies_by_type + old_type;
4df7f6af 1184 SV *referant = NULL;
cac9b346 1185
7918f24d
NC
1186 PERL_ARGS_ASSERT_SV_UPGRADE;
1187
bd81e77b
NC
1188 if (new_type != SVt_PV && SvIsCOW(sv)) {
1189 sv_force_normal_flags(sv, 0);
1190 }
cac9b346 1191
bd81e77b
NC
1192 if (old_type == new_type)
1193 return;
cac9b346 1194
bd81e77b 1195 old_body = SvANY(sv);
de042e1d 1196
bd81e77b
NC
1197 /* Copying structures onto other structures that have been neatly zeroed
1198 has a subtle gotcha. Consider XPVMG
cac9b346 1199
bd81e77b
NC
1200 +------+------+------+------+------+-------+-------+
1201 | NV | CUR | LEN | IV | MAGIC | STASH |
1202 +------+------+------+------+------+-------+-------+
1203 0 4 8 12 16 20 24 28
645c22ef 1204
bd81e77b
NC
1205 where NVs are aligned to 8 bytes, so that sizeof that structure is
1206 actually 32 bytes long, with 4 bytes of padding at the end:
08742458 1207
bd81e77b
NC
1208 +------+------+------+------+------+-------+-------+------+
1209 | NV | CUR | LEN | IV | MAGIC | STASH | ??? |
1210 +------+------+------+------+------+-------+-------+------+
1211 0 4 8 12 16 20 24 28 32
08742458 1212
bd81e77b 1213 so what happens if you allocate memory for this structure:
30f9da9e 1214
bd81e77b
NC
1215 +------+------+------+------+------+-------+-------+------+------+...
1216 | NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME |
1217 +------+------+------+------+------+-------+-------+------+------+...
1218 0 4 8 12 16 20 24 28 32 36
bfc44f79 1219
bd81e77b
NC
1220 zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
1221 expect, because you copy the area marked ??? onto GP. Now, ??? may have
1222 started out as zero once, but it's quite possible that it isn't. So now,
1223 rather than a nicely zeroed GP, you have it pointing somewhere random.
1224 Bugs ensue.
bfc44f79 1225
bd81e77b
NC
1226 (In fact, GP ends up pointing at a previous GP structure, because the
1227 principle cause of the padding in XPVMG getting garbage is a copy of
6c9e42f7
NC
1228 sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob. Right now
1229 this happens to be moot because XPVGV has been re-ordered, with GP
1230 no longer after STASH)
30f9da9e 1231
bd81e77b
NC
1232 So we are careful and work out the size of used parts of all the
1233 structures. */
bfc44f79 1234
bd81e77b
NC
1235 switch (old_type) {
1236 case SVt_NULL:
1237 break;
1238 case SVt_IV:
4df7f6af
NC
1239 if (SvROK(sv)) {
1240 referant = SvRV(sv);
238b27b3
NC
1241 old_type_details = &fake_rv;
1242 if (new_type == SVt_NV)
1243 new_type = SVt_PVNV;
4df7f6af
NC
1244 } else {
1245 if (new_type < SVt_PVIV) {
1246 new_type = (new_type == SVt_NV)
1247 ? SVt_PVNV : SVt_PVIV;
1248 }
bd81e77b
NC
1249 }
1250 break;
1251 case SVt_NV:
1252 if (new_type < SVt_PVNV) {
1253 new_type = SVt_PVNV;
bd81e77b
NC
1254 }
1255 break;
bd81e77b
NC
1256 case SVt_PV:
1257 assert(new_type > SVt_PV);
1258 assert(SVt_IV < SVt_PV);
1259 assert(SVt_NV < SVt_PV);
1260 break;
1261 case SVt_PVIV:
1262 break;
1263 case SVt_PVNV:
1264 break;
1265 case SVt_PVMG:
1266 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1267 there's no way that it can be safely upgraded, because perl.c
1268 expects to Safefree(SvANY(PL_mess_sv)) */
1269 assert(sv != PL_mess_sv);
1270 /* This flag bit is used to mean other things in other scalar types.
1271 Given that it only has meaning inside the pad, it shouldn't be set
1272 on anything that can get upgraded. */
00b1698f 1273 assert(!SvPAD_TYPED(sv));
bd81e77b
NC
1274 break;
1275 default:
1276 if (old_type_details->cant_upgrade)
c81225bc
NC
1277 Perl_croak(aTHX_ "Can't upgrade %s (%" UVuf ") to %" UVuf,
1278 sv_reftype(sv, 0), (UV) old_type, (UV) new_type);
bd81e77b 1279 }
3376de98
NC
1280
1281 if (old_type > new_type)
1282 Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
1283 (int)old_type, (int)new_type);
1284
2fa1109b 1285 new_type_details = bodies_by_type + new_type;
645c22ef 1286
bd81e77b
NC
1287 SvFLAGS(sv) &= ~SVTYPEMASK;
1288 SvFLAGS(sv) |= new_type;
932e9ff9 1289
ab4416c0
NC
1290 /* This can't happen, as SVt_NULL is <= all values of new_type, so one of
1291 the return statements above will have triggered. */
1292 assert (new_type != SVt_NULL);
bd81e77b 1293 switch (new_type) {
bd81e77b
NC
1294 case SVt_IV:
1295 assert(old_type == SVt_NULL);
1296 SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
1297 SvIV_set(sv, 0);
1298 return;
1299 case SVt_NV:
1300 assert(old_type == SVt_NULL);
1301 SvANY(sv) = new_XNV();
1302 SvNV_set(sv, 0);
1303 return;
bd81e77b 1304 case SVt_PVHV:
bd81e77b 1305 case SVt_PVAV:
d2a0f284 1306 assert(new_type_details->body_size);
c1ae03ae
NC
1307
1308#ifndef PURIFY
1309 assert(new_type_details->arena);
d2a0f284 1310 assert(new_type_details->arena_size);
c1ae03ae 1311 /* This points to the start of the allocated area. */
d2a0f284
JC
1312 new_body_inline(new_body, new_type);
1313 Zero(new_body, new_type_details->body_size, char);
c1ae03ae
NC
1314 new_body = ((char *)new_body) - new_type_details->offset;
1315#else
1316 /* We always allocated the full length item with PURIFY. To do this
1317 we fake things so that arena is false for all 16 types.. */
1318 new_body = new_NOARENAZ(new_type_details);
1319#endif
1320 SvANY(sv) = new_body;
1321 if (new_type == SVt_PVAV) {
1322 AvMAX(sv) = -1;
1323 AvFILLp(sv) = -1;
1324 AvREAL_only(sv);
64484faa 1325 if (old_type_details->body_size) {
ac572bf4
NC
1326 AvALLOC(sv) = 0;
1327 } else {
1328 /* It will have been zeroed when the new body was allocated.
1329 Lets not write to it, in case it confuses a write-back
1330 cache. */
1331 }
78ac7dd9
NC
1332 } else {
1333 assert(!SvOK(sv));
1334 SvOK_off(sv);
1335#ifndef NODEFAULT_SHAREKEYS
1336 HvSHAREKEYS_on(sv); /* key-sharing on by default */
1337#endif
1338 HvMAX(sv) = 7; /* (start with 8 buckets) */
64484faa 1339 if (old_type_details->body_size) {
78ac7dd9
NC
1340 HvFILL(sv) = 0;
1341 } else {
1342 /* It will have been zeroed when the new body was allocated.
1343 Lets not write to it, in case it confuses a write-back
1344 cache. */
1345 }
c1ae03ae 1346 }
aeb18a1e 1347
bd81e77b
NC
1348 /* SVt_NULL isn't the only thing upgraded to AV or HV.
1349 The target created by newSVrv also is, and it can have magic.
1350 However, it never has SvPVX set.
1351 */
4df7f6af
NC
1352 if (old_type == SVt_IV) {
1353 assert(!SvROK(sv));
1354 } else if (old_type >= SVt_PV) {
bd81e77b
NC
1355 assert(SvPVX_const(sv) == 0);
1356 }
aeb18a1e 1357
bd81e77b 1358 if (old_type >= SVt_PVMG) {
e736a858 1359 SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_u.xmg_magic);
bd81e77b 1360 SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
797c7171
NC
1361 } else {
1362 sv->sv_u.svu_array = NULL; /* or svu_hash */
bd81e77b
NC
1363 }
1364 break;
93e68bfb 1365
93e68bfb 1366
bd81e77b
NC
1367 case SVt_PVIV:
1368 /* XXX Is this still needed? Was it ever needed? Surely as there is
1369 no route from NV to PVIV, NOK can never be true */
1370 assert(!SvNOKp(sv));
1371 assert(!SvNOK(sv));
1372 case SVt_PVIO:
1373 case SVt_PVFM:
bd81e77b
NC
1374 case SVt_PVGV:
1375 case SVt_PVCV:
1376 case SVt_PVLV:
5c35adbb 1377 case SVt_REGEXP:
bd81e77b
NC
1378 case SVt_PVMG:
1379 case SVt_PVNV:
1380 case SVt_PV:
93e68bfb 1381
d2a0f284 1382 assert(new_type_details->body_size);
bd81e77b
NC
1383 /* We always allocated the full length item with PURIFY. To do this
1384 we fake things so that arena is false for all 16 types.. */
1385 if(new_type_details->arena) {
1386 /* This points to the start of the allocated area. */
d2a0f284
JC
1387 new_body_inline(new_body, new_type);
1388 Zero(new_body, new_type_details->body_size, char);
bd81e77b
NC
1389 new_body = ((char *)new_body) - new_type_details->offset;
1390 } else {
1391 new_body = new_NOARENAZ(new_type_details);
1392 }
1393 SvANY(sv) = new_body;
5e2fc214 1394
bd81e77b 1395 if (old_type_details->copy) {
f9ba3d20
NC
1396 /* There is now the potential for an upgrade from something without
1397 an offset (PVNV or PVMG) to something with one (PVCV, PVFM) */
1398 int offset = old_type_details->offset;
1399 int length = old_type_details->copy;
1400
1401 if (new_type_details->offset > old_type_details->offset) {
d4c19fe8 1402 const int difference
f9ba3d20
NC
1403 = new_type_details->offset - old_type_details->offset;
1404 offset += difference;
1405 length -= difference;
1406 }
1407 assert (length >= 0);
1408
1409 Copy((char *)old_body + offset, (char *)new_body + offset, length,
1410 char);
bd81e77b
NC
1411 }
1412
1413#ifndef NV_ZERO_IS_ALLBITS_ZERO
f2524eef 1414 /* If NV 0.0 is stores as all bits 0 then Zero() already creates a
e5ce394c
NC
1415 * correct 0.0 for us. Otherwise, if the old body didn't have an
1416 * NV slot, but the new one does, then we need to initialise the
1417 * freshly created NV slot with whatever the correct bit pattern is
1418 * for 0.0 */
e22a937e
NC
1419 if (old_type_details->zero_nv && !new_type_details->zero_nv
1420 && !isGV_with_GP(sv))
bd81e77b 1421 SvNV_set(sv, 0);
82048762 1422#endif
5e2fc214 1423
bd81e77b 1424 if (new_type == SVt_PVIO)
f2524eef 1425 IoPAGE_LEN(sv) = 60;
4df7f6af
NC
1426 if (old_type < SVt_PV) {
1427 /* referant will be NULL unless the old type was SVt_IV emulating
1428 SVt_RV */
1429 sv->sv_u.svu_rv = referant;
1430 }
bd81e77b
NC
1431 break;
1432 default:
afd78fd5
JH
1433 Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu",
1434 (unsigned long)new_type);
bd81e77b 1435 }
73171d91 1436
d2a0f284
JC
1437 if (old_type_details->arena) {
1438 /* If there was an old body, then we need to free it.
1439 Note that there is an assumption that all bodies of types that
1440 can be upgraded came from arenas. Only the more complex non-
1441 upgradable types are allowed to be directly malloc()ed. */
bd81e77b
NC
1442#ifdef PURIFY
1443 my_safefree(old_body);
1444#else
1445 del_body((void*)((char*)old_body + old_type_details->offset),
1446 &PL_body_roots[old_type]);
1447#endif
1448 }
1449}
73171d91 1450
bd81e77b
NC
1451/*
1452=for apidoc sv_backoff
73171d91 1453
bd81e77b
NC
1454Remove any string offset. You should normally use the C<SvOOK_off> macro
1455wrapper instead.
73171d91 1456
bd81e77b 1457=cut
73171d91
NC
1458*/
1459
bd81e77b 1460int
aad570aa 1461Perl_sv_backoff(pTHX_ register SV *const sv)
bd81e77b 1462{
69240efd 1463 STRLEN delta;
7a4bba22 1464 const char * const s = SvPVX_const(sv);
7918f24d
NC
1465
1466 PERL_ARGS_ASSERT_SV_BACKOFF;
96a5add6 1467 PERL_UNUSED_CONTEXT;
7918f24d 1468
bd81e77b
NC
1469 assert(SvOOK(sv));
1470 assert(SvTYPE(sv) != SVt_PVHV);
1471 assert(SvTYPE(sv) != SVt_PVAV);
7a4bba22 1472
69240efd
NC
1473 SvOOK_offset(sv, delta);
1474
7a4bba22
NC
1475 SvLEN_set(sv, SvLEN(sv) + delta);
1476 SvPV_set(sv, SvPVX(sv) - delta);
1477 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
bd81e77b
NC
1478 SvFLAGS(sv) &= ~SVf_OOK;
1479 return 0;
1480}
73171d91 1481
bd81e77b
NC
1482/*
1483=for apidoc sv_grow
73171d91 1484
bd81e77b
NC
1485Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1486upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1487Use the C<SvGROW> wrapper instead.
93e68bfb 1488
bd81e77b
NC
1489=cut
1490*/
93e68bfb 1491
bd81e77b 1492char *
aad570aa 1493Perl_sv_grow(pTHX_ register SV *const sv, register STRLEN newlen)
bd81e77b
NC
1494{
1495 register char *s;
93e68bfb 1496
7918f24d
NC
1497 PERL_ARGS_ASSERT_SV_GROW;
1498
5db06880
NC
1499 if (PL_madskills && newlen >= 0x100000) {
1500 PerlIO_printf(Perl_debug_log,
1501 "Allocation too large: %"UVxf"\n", (UV)newlen);
1502 }
bd81e77b
NC
1503#ifdef HAS_64K_LIMIT
1504 if (newlen >= 0x10000) {
1505 PerlIO_printf(Perl_debug_log,
1506 "Allocation too large: %"UVxf"\n", (UV)newlen);
1507 my_exit(1);
1508 }
1509#endif /* HAS_64K_LIMIT */
1510 if (SvROK(sv))
1511 sv_unref(sv);
1512 if (SvTYPE(sv) < SVt_PV) {
1513 sv_upgrade(sv, SVt_PV);
1514 s = SvPVX_mutable(sv);
1515 }
1516 else if (SvOOK(sv)) { /* pv is offset? */
1517 sv_backoff(sv);
1518 s = SvPVX_mutable(sv);
1519 if (newlen > SvLEN(sv))
1520 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1521#ifdef HAS_64K_LIMIT
1522 if (newlen >= 0x10000)
1523 newlen = 0xFFFF;
1524#endif
1525 }
1526 else
1527 s = SvPVX_mutable(sv);
aeb18a1e 1528
bd81e77b 1529 if (newlen > SvLEN(sv)) { /* need more room? */
aedff202 1530#ifndef Perl_safesysmalloc_size
bd81e77b 1531 newlen = PERL_STRLEN_ROUNDUP(newlen);
bd81e77b 1532#endif
98653f18 1533 if (SvLEN(sv) && s) {
10edeb5d 1534 s = (char*)saferealloc(s, newlen);
bd81e77b
NC
1535 }
1536 else {
10edeb5d 1537 s = (char*)safemalloc(newlen);
bd81e77b
NC
1538 if (SvPVX_const(sv) && SvCUR(sv)) {
1539 Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
1540 }
1541 }
1542 SvPV_set(sv, s);
ca7c1a29 1543#ifdef Perl_safesysmalloc_size
98653f18
NC
1544 /* Do this here, do it once, do it right, and then we will never get
1545 called back into sv_grow() unless there really is some growing
1546 needed. */
ca7c1a29 1547 SvLEN_set(sv, Perl_safesysmalloc_size(s));
98653f18 1548#else
bd81e77b 1549 SvLEN_set(sv, newlen);
98653f18 1550#endif
bd81e77b
NC
1551 }
1552 return s;
1553}
aeb18a1e 1554
bd81e77b
NC
1555/*
1556=for apidoc sv_setiv
932e9ff9 1557
bd81e77b
NC
1558Copies an integer into the given SV, upgrading first if necessary.
1559Does not handle 'set' magic. See also C<sv_setiv_mg>.
463ee0b2 1560
bd81e77b
NC
1561=cut
1562*/
463ee0b2 1563
bd81e77b 1564void
aad570aa 1565Perl_sv_setiv(pTHX_ register SV *const sv, const IV i)
bd81e77b 1566{
97aff369 1567 dVAR;
7918f24d
NC
1568
1569 PERL_ARGS_ASSERT_SV_SETIV;
1570
bd81e77b
NC
1571 SV_CHECK_THINKFIRST_COW_DROP(sv);
1572 switch (SvTYPE(sv)) {
1573 case SVt_NULL:
bd81e77b 1574 case SVt_NV:
3376de98 1575 sv_upgrade(sv, SVt_IV);
bd81e77b 1576 break;
bd81e77b
NC
1577 case SVt_PV:
1578 sv_upgrade(sv, SVt_PVIV);
1579 break;
463ee0b2 1580
bd81e77b 1581 case SVt_PVGV:
6e592b3a
BM
1582 if (!isGV_with_GP(sv))
1583 break;
bd81e77b
NC
1584 case SVt_PVAV:
1585 case SVt_PVHV:
1586 case SVt_PVCV:
1587 case SVt_PVFM:
1588 case SVt_PVIO:
1589 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1590 OP_DESC(PL_op));
42d0e0b7 1591 default: NOOP;
bd81e77b
NC
1592 }
1593 (void)SvIOK_only(sv); /* validate number */
1594 SvIV_set(sv, i);
1595 SvTAINT(sv);
1596}
932e9ff9 1597
bd81e77b
NC
1598/*
1599=for apidoc sv_setiv_mg
d33b2eba 1600
bd81e77b 1601Like C<sv_setiv>, but also handles 'set' magic.
1c846c1f 1602
bd81e77b
NC
1603=cut
1604*/
d33b2eba 1605
bd81e77b 1606void
aad570aa 1607Perl_sv_setiv_mg(pTHX_ register SV *const sv, const IV i)
bd81e77b 1608{
7918f24d
NC
1609 PERL_ARGS_ASSERT_SV_SETIV_MG;
1610
bd81e77b
NC
1611 sv_setiv(sv,i);
1612 SvSETMAGIC(sv);
1613}
727879eb 1614
bd81e77b
NC
1615/*
1616=for apidoc sv_setuv
d33b2eba 1617
bd81e77b
NC
1618Copies an unsigned integer into the given SV, upgrading first if necessary.
1619Does not handle 'set' magic. See also C<sv_setuv_mg>.
9b94d1dd 1620
bd81e77b
NC
1621=cut
1622*/
d33b2eba 1623
bd81e77b 1624void
aad570aa 1625Perl_sv_setuv(pTHX_ register SV *const sv, const UV u)
bd81e77b 1626{
7918f24d
NC
1627 PERL_ARGS_ASSERT_SV_SETUV;
1628
bd81e77b
NC
1629 /* With these two if statements:
1630 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d33b2eba 1631
bd81e77b
NC
1632 without
1633 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1c846c1f 1634
bd81e77b
NC
1635 If you wish to remove them, please benchmark to see what the effect is
1636 */
1637 if (u <= (UV)IV_MAX) {
1638 sv_setiv(sv, (IV)u);
1639 return;
1640 }
1641 sv_setiv(sv, 0);
1642 SvIsUV_on(sv);
1643 SvUV_set(sv, u);
1644}
d33b2eba 1645
bd81e77b
NC
1646/*
1647=for apidoc sv_setuv_mg
727879eb 1648
bd81e77b 1649Like C<sv_setuv>, but also handles 'set' magic.
9b94d1dd 1650
bd81e77b
NC
1651=cut
1652*/
5e2fc214 1653
bd81e77b 1654void
aad570aa 1655Perl_sv_setuv_mg(pTHX_ register SV *const sv, const UV u)
bd81e77b 1656{
7918f24d
NC
1657 PERL_ARGS_ASSERT_SV_SETUV_MG;
1658
bd81e77b
NC
1659 sv_setuv(sv,u);
1660 SvSETMAGIC(sv);
1661}
5e2fc214 1662
954c1994 1663/*
bd81e77b 1664=for apidoc sv_setnv
954c1994 1665
bd81e77b
NC
1666Copies a double into the given SV, upgrading first if necessary.
1667Does not handle 'set' magic. See also C<sv_setnv_mg>.
954c1994
GS
1668
1669=cut
1670*/
1671
63f97190 1672void
aad570aa 1673Perl_sv_setnv(pTHX_ register SV *const sv, const NV num)
79072805 1674{
97aff369 1675 dVAR;
7918f24d
NC
1676
1677 PERL_ARGS_ASSERT_SV_SETNV;
1678
bd81e77b
NC
1679 SV_CHECK_THINKFIRST_COW_DROP(sv);
1680 switch (SvTYPE(sv)) {
79072805 1681 case SVt_NULL:
79072805 1682 case SVt_IV:
bd81e77b 1683 sv_upgrade(sv, SVt_NV);
79072805
LW
1684 break;
1685 case SVt_PV:
79072805 1686 case SVt_PVIV:
bd81e77b 1687 sv_upgrade(sv, SVt_PVNV);
79072805 1688 break;
bd4b1eb5 1689
bd4b1eb5 1690 case SVt_PVGV:
6e592b3a
BM
1691 if (!isGV_with_GP(sv))
1692 break;
bd81e77b
NC
1693 case SVt_PVAV:
1694 case SVt_PVHV:
79072805 1695 case SVt_PVCV:
bd81e77b
NC
1696 case SVt_PVFM:
1697 case SVt_PVIO:
1698 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1699 OP_NAME(PL_op));
42d0e0b7 1700 default: NOOP;
2068cd4d 1701 }
bd81e77b
NC
1702 SvNV_set(sv, num);
1703 (void)SvNOK_only(sv); /* validate number */
1704 SvTAINT(sv);
79072805
LW
1705}
1706
645c22ef 1707/*
bd81e77b 1708=for apidoc sv_setnv_mg
645c22ef 1709
bd81e77b 1710Like C<sv_setnv>, but also handles 'set' magic.
645c22ef
DM
1711
1712=cut
1713*/
1714
bd81e77b 1715void
aad570aa 1716Perl_sv_setnv_mg(pTHX_ register SV *const sv, const NV num)
79072805 1717{
7918f24d
NC
1718 PERL_ARGS_ASSERT_SV_SETNV_MG;
1719
bd81e77b
NC
1720 sv_setnv(sv,num);
1721 SvSETMAGIC(sv);
79072805
LW
1722}
1723
bd81e77b
NC
1724/* Print an "isn't numeric" warning, using a cleaned-up,
1725 * printable version of the offending string
1726 */
954c1994 1727
bd81e77b 1728STATIC void
aad570aa 1729S_not_a_number(pTHX_ SV *const sv)
79072805 1730{
97aff369 1731 dVAR;
bd81e77b
NC
1732 SV *dsv;
1733 char tmpbuf[64];
1734 const char *pv;
94463019 1735
7918f24d
NC
1736 PERL_ARGS_ASSERT_NOT_A_NUMBER;
1737
94463019 1738 if (DO_UTF8(sv)) {
84bafc02 1739 dsv = newSVpvs_flags("", SVs_TEMP);
94463019
JH
1740 pv = sv_uni_display(dsv, sv, 10, 0);
1741 } else {
1742 char *d = tmpbuf;
551405c4 1743 const char * const limit = tmpbuf + sizeof(tmpbuf) - 8;
94463019
JH
1744 /* each *s can expand to 4 chars + "...\0",
1745 i.e. need room for 8 chars */
ecdeb87c 1746
00b6aa41
AL
1747 const char *s = SvPVX_const(sv);
1748 const char * const end = s + SvCUR(sv);
1749 for ( ; s < end && d < limit; s++ ) {
94463019
JH
1750 int ch = *s & 0xFF;
1751 if (ch & 128 && !isPRINT_LC(ch)) {
1752 *d++ = 'M';
1753 *d++ = '-';
1754 ch &= 127;
1755 }
1756 if (ch == '\n') {
1757 *d++ = '\\';
1758 *d++ = 'n';
1759 }
1760 else if (ch == '\r') {
1761 *d++ = '\\';
1762 *d++ = 'r';
1763 }
1764 else if (ch == '\f') {
1765 *d++ = '\\';
1766 *d++ = 'f';
1767 }
1768 else if (ch == '\\') {
1769 *d++ = '\\';
1770 *d++ = '\\';
1771 }
1772 else if (ch == '\0') {
1773 *d++ = '\\';
1774 *d++ = '0';
1775 }
1776 else if (isPRINT_LC(ch))
1777 *d++ = ch;
1778 else {
1779 *d++ = '^';
1780 *d++ = toCTRL(ch);
1781 }
1782 }
1783 if (s < end) {
1784 *d++ = '.';
1785 *d++ = '.';
1786 *d++ = '.';
1787 }
1788 *d = '\0';
1789 pv = tmpbuf;
a0d0e21e 1790 }
a0d0e21e 1791
533c011a 1792 if (PL_op)
9014280d 1793 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019
JH
1794 "Argument \"%s\" isn't numeric in %s", pv,
1795 OP_DESC(PL_op));
a0d0e21e 1796 else
9014280d 1797 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019 1798 "Argument \"%s\" isn't numeric", pv);
a0d0e21e
LW
1799}
1800
c2988b20
NC
1801/*
1802=for apidoc looks_like_number
1803
645c22ef
DM
1804Test if the content of an SV looks like a number (or is a number).
1805C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1806non-numeric warning), even if your atof() doesn't grok them.
c2988b20
NC
1807
1808=cut
1809*/
1810
1811I32
aad570aa 1812Perl_looks_like_number(pTHX_ SV *const sv)
c2988b20 1813{
a3b680e6 1814 register const char *sbegin;
c2988b20
NC
1815 STRLEN len;
1816
7918f24d
NC
1817 PERL_ARGS_ASSERT_LOOKS_LIKE_NUMBER;
1818
c2988b20 1819 if (SvPOK(sv)) {
3f7c398e 1820 sbegin = SvPVX_const(sv);
c2988b20
NC
1821 len = SvCUR(sv);
1822 }
1823 else if (SvPOKp(sv))
83003860 1824 sbegin = SvPV_const(sv, len);
c2988b20 1825 else
e0ab1c0e 1826 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
c2988b20
NC
1827 return grok_number(sbegin, len, NULL);
1828}
25da4f38 1829
19f6321d
NC
1830STATIC bool
1831S_glob_2number(pTHX_ GV * const gv)
180488f8
NC
1832{
1833 const U32 wasfake = SvFLAGS(gv) & SVf_FAKE;
1834 SV *const buffer = sv_newmortal();
1835
7918f24d
NC
1836 PERL_ARGS_ASSERT_GLOB_2NUMBER;
1837
180488f8
NC
1838 /* FAKE globs can get coerced, so need to turn this off temporarily if it
1839 is on. */
1840 SvFAKE_off(gv);
1841 gv_efullname3(buffer, gv, "*");
1842 SvFLAGS(gv) |= wasfake;
1843
675c862f
AL
1844 /* We know that all GVs stringify to something that is not-a-number,
1845 so no need to test that. */
1846 if (ckWARN(WARN_NUMERIC))
1847 not_a_number(buffer);
1848 /* We just want something true to return, so that S_sv_2iuv_common
1849 can tail call us and return true. */
19f6321d 1850 return TRUE;
675c862f
AL
1851}
1852
25da4f38
IZ
1853/* Actually, ISO C leaves conversion of UV to IV undefined, but
1854 until proven guilty, assume that things are not that bad... */
1855
645c22ef
DM
1856/*
1857 NV_PRESERVES_UV:
1858
1859 As 64 bit platforms often have an NV that doesn't preserve all bits of
28e5dec8
JH
1860 an IV (an assumption perl has been based on to date) it becomes necessary
1861 to remove the assumption that the NV always carries enough precision to
1862 recreate the IV whenever needed, and that the NV is the canonical form.
1863 Instead, IV/UV and NV need to be given equal rights. So as to not lose
645c22ef 1864 precision as a side effect of conversion (which would lead to insanity
28e5dec8
JH
1865 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1866 1) to distinguish between IV/UV/NV slots that have cached a valid
1867 conversion where precision was lost and IV/UV/NV slots that have a
1868 valid conversion which has lost no precision
645c22ef 1869 2) to ensure that if a numeric conversion to one form is requested that
28e5dec8
JH
1870 would lose precision, the precise conversion (or differently
1871 imprecise conversion) is also performed and cached, to prevent
1872 requests for different numeric formats on the same SV causing
1873 lossy conversion chains. (lossless conversion chains are perfectly
1874 acceptable (still))
1875
1876
1877 flags are used:
1878 SvIOKp is true if the IV slot contains a valid value
1879 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1880 SvNOKp is true if the NV slot contains a valid value
1881 SvNOK is true only if the NV value is accurate
1882
1883 so
645c22ef 1884 while converting from PV to NV, check to see if converting that NV to an
28e5dec8
JH
1885 IV(or UV) would lose accuracy over a direct conversion from PV to
1886 IV(or UV). If it would, cache both conversions, return NV, but mark
1887 SV as IOK NOKp (ie not NOK).
1888
645c22ef 1889 While converting from PV to IV, check to see if converting that IV to an
28e5dec8
JH
1890 NV would lose accuracy over a direct conversion from PV to NV. If it
1891 would, cache both conversions, flag similarly.
1892
1893 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1894 correctly because if IV & NV were set NV *always* overruled.
645c22ef
DM
1895 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1896 changes - now IV and NV together means that the two are interchangeable:
28e5dec8 1897 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
d460ef45 1898
645c22ef
DM
1899 The benefit of this is that operations such as pp_add know that if
1900 SvIOK is true for both left and right operands, then integer addition
1901 can be used instead of floating point (for cases where the result won't
1902 overflow). Before, floating point was always used, which could lead to
28e5dec8
JH
1903 loss of precision compared with integer addition.
1904
1905 * making IV and NV equal status should make maths accurate on 64 bit
1906 platforms
1907 * may speed up maths somewhat if pp_add and friends start to use
645c22ef 1908 integers when possible instead of fp. (Hopefully the overhead in
28e5dec8
JH
1909 looking for SvIOK and checking for overflow will not outweigh the
1910 fp to integer speedup)
1911 * will slow down integer operations (callers of SvIV) on "inaccurate"
1912 values, as the change from SvIOK to SvIOKp will cause a call into
1913 sv_2iv each time rather than a macro access direct to the IV slot
1914 * should speed up number->string conversion on integers as IV is
645c22ef 1915 favoured when IV and NV are equally accurate
28e5dec8
JH
1916
1917 ####################################################################
645c22ef
DM
1918 You had better be using SvIOK_notUV if you want an IV for arithmetic:
1919 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1920 On the other hand, SvUOK is true iff UV.
28e5dec8
JH
1921 ####################################################################
1922
645c22ef 1923 Your mileage will vary depending your CPU's relative fp to integer
28e5dec8
JH
1924 performance ratio.
1925*/
1926
1927#ifndef NV_PRESERVES_UV
645c22ef
DM
1928# define IS_NUMBER_UNDERFLOW_IV 1
1929# define IS_NUMBER_UNDERFLOW_UV 2
1930# define IS_NUMBER_IV_AND_UV 2
1931# define IS_NUMBER_OVERFLOW_IV 4
1932# define IS_NUMBER_OVERFLOW_UV 5
1933
1934/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
28e5dec8
JH
1935
1936/* For sv_2nv these three cases are "SvNOK and don't bother casting" */
1937STATIC int
5de3775c 1938S_sv_2iuv_non_preserve(pTHX_ register SV *const sv
47031da6
NC
1939# ifdef DEBUGGING
1940 , I32 numtype
1941# endif
1942 )
28e5dec8 1943{
97aff369 1944 dVAR;
7918f24d
NC
1945
1946 PERL_ARGS_ASSERT_SV_2IUV_NON_PRESERVE;
1947
3f7c398e 1948 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));
28e5dec8
JH
1949 if (SvNVX(sv) < (NV)IV_MIN) {
1950 (void)SvIOKp_on(sv);
1951 (void)SvNOK_on(sv);
45977657 1952 SvIV_set(sv, IV_MIN);
28e5dec8
JH
1953 return IS_NUMBER_UNDERFLOW_IV;
1954 }
1955 if (SvNVX(sv) > (NV)UV_MAX) {
1956 (void)SvIOKp_on(sv);
1957 (void)SvNOK_on(sv);
1958 SvIsUV_on(sv);
607fa7f2 1959 SvUV_set(sv, UV_MAX);
28e5dec8
JH
1960 return IS_NUMBER_OVERFLOW_UV;
1961 }
c2988b20
NC
1962 (void)SvIOKp_on(sv);
1963 (void)SvNOK_on(sv);
1964 /* Can't use strtol etc to convert this string. (See truth table in
1965 sv_2iv */
1966 if (SvNVX(sv) <= (UV)IV_MAX) {
45977657 1967 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
1968 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
1969 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
1970 } else {
1971 /* Integer is imprecise. NOK, IOKp */
1972 }
1973 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
1974 }
1975 SvIsUV_on(sv);
607fa7f2 1976 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
1977 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
1978 if (SvUVX(sv) == UV_MAX) {
1979 /* As we know that NVs don't preserve UVs, UV_MAX cannot
1980 possibly be preserved by NV. Hence, it must be overflow.
1981 NOK, IOKp */
1982 return IS_NUMBER_OVERFLOW_UV;
1983 }
1984 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
1985 } else {
1986 /* Integer is imprecise. NOK, IOKp */
28e5dec8 1987 }
c2988b20 1988 return IS_NUMBER_OVERFLOW_IV;
28e5dec8 1989}
645c22ef
DM
1990#endif /* !NV_PRESERVES_UV*/
1991
af359546 1992STATIC bool
7918f24d
NC
1993S_sv_2iuv_common(pTHX_ SV *const sv)
1994{
97aff369 1995 dVAR;
7918f24d
NC
1996
1997 PERL_ARGS_ASSERT_SV_2IUV_COMMON;
1998
af359546 1999 if (SvNOKp(sv)) {
28e5dec8
JH
2000 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2001 * without also getting a cached IV/UV from it at the same time
2002 * (ie PV->NV conversion should detect loss of accuracy and cache
af359546
NC
2003 * IV or UV at same time to avoid this. */
2004 /* IV-over-UV optimisation - choose to cache IV if possible */
25da4f38
IZ
2005
2006 if (SvTYPE(sv) == SVt_NV)
2007 sv_upgrade(sv, SVt_PVNV);
2008
28e5dec8
JH
2009 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2010 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2011 certainly cast into the IV range at IV_MAX, whereas the correct
2012 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2013 cases go to UV */
cab190d4
JD
2014#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2015 if (Perl_isnan(SvNVX(sv))) {
2016 SvUV_set(sv, 0);
2017 SvIsUV_on(sv);
fdbe6d7c 2018 return FALSE;
cab190d4 2019 }
cab190d4 2020#endif
28e5dec8 2021 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2022 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2023 if (SvNVX(sv) == (NV) SvIVX(sv)
2024#ifndef NV_PRESERVES_UV
2025 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2026 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2027 /* Don't flag it as "accurately an integer" if the number
2028 came from a (by definition imprecise) NV operation, and
2029 we're outside the range of NV integer precision */
2030#endif
2031 ) {
a43d94f2
NC
2032 if (SvNOK(sv))
2033 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2034 else {
2035 /* scalar has trailing garbage, eg "42a" */
2036 }
28e5dec8 2037 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2038 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2039 PTR2UV(sv),
2040 SvNVX(sv),
2041 SvIVX(sv)));
2042
2043 } else {
2044 /* IV not precise. No need to convert from PV, as NV
2045 conversion would already have cached IV if it detected
2046 that PV->IV would be better than PV->NV->IV
2047 flags already correct - don't set public IOK. */
2048 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2049 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2050 PTR2UV(sv),
2051 SvNVX(sv),
2052 SvIVX(sv)));
2053 }
2054 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2055 but the cast (NV)IV_MIN rounds to a the value less (more
2056 negative) than IV_MIN which happens to be equal to SvNVX ??
2057 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2058 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2059 (NV)UVX == NVX are both true, but the values differ. :-(
2060 Hopefully for 2s complement IV_MIN is something like
2061 0x8000000000000000 which will be exact. NWC */
d460ef45 2062 }
25da4f38 2063 else {
607fa7f2 2064 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2065 if (
2066 (SvNVX(sv) == (NV) SvUVX(sv))
2067#ifndef NV_PRESERVES_UV
2068 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2069 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2070 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2071 /* Don't flag it as "accurately an integer" if the number
2072 came from a (by definition imprecise) NV operation, and
2073 we're outside the range of NV integer precision */
2074#endif
a43d94f2 2075 && SvNOK(sv)
28e5dec8
JH
2076 )
2077 SvIOK_on(sv);
25da4f38 2078 SvIsUV_on(sv);
1c846c1f 2079 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 2080 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 2081 PTR2UV(sv),
57def98f
JH
2082 SvUVX(sv),
2083 SvUVX(sv)));
25da4f38 2084 }
748a9306
LW
2085 }
2086 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2087 UV value;
504618e9 2088 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
af359546 2089 /* We want to avoid a possible problem when we cache an IV/ a UV which
25da4f38 2090 may be later translated to an NV, and the resulting NV is not
c2988b20
NC
2091 the same as the direct translation of the initial string
2092 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2093 be careful to ensure that the value with the .456 is around if the
2094 NV value is requested in the future).
1c846c1f 2095
af359546 2096 This means that if we cache such an IV/a UV, we need to cache the
25da4f38 2097 NV as well. Moreover, we trade speed for space, and do not
28e5dec8 2098 cache the NV if we are sure it's not needed.
25da4f38 2099 */
16b7a9a4 2100
c2988b20
NC
2101 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2102 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2103 == IS_NUMBER_IN_UV) {
5e045b90 2104 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8
JH
2105 if (SvTYPE(sv) < SVt_PVIV)
2106 sv_upgrade(sv, SVt_PVIV);
f7bbb42a 2107 (void)SvIOK_on(sv);
c2988b20
NC
2108 } else if (SvTYPE(sv) < SVt_PVNV)
2109 sv_upgrade(sv, SVt_PVNV);
28e5dec8 2110
f2524eef 2111 /* If NVs preserve UVs then we only use the UV value if we know that
c2988b20
NC
2112 we aren't going to call atof() below. If NVs don't preserve UVs
2113 then the value returned may have more precision than atof() will
2114 return, even though value isn't perfectly accurate. */
2115 if ((numtype & (IS_NUMBER_IN_UV
2116#ifdef NV_PRESERVES_UV
2117 | IS_NUMBER_NOT_INT
2118#endif
2119 )) == IS_NUMBER_IN_UV) {
2120 /* This won't turn off the public IOK flag if it was set above */
2121 (void)SvIOKp_on(sv);
2122
2123 if (!(numtype & IS_NUMBER_NEG)) {
2124 /* positive */;
2125 if (value <= (UV)IV_MAX) {
45977657 2126 SvIV_set(sv, (IV)value);
c2988b20 2127 } else {
af359546 2128 /* it didn't overflow, and it was positive. */
607fa7f2 2129 SvUV_set(sv, value);
c2988b20
NC
2130 SvIsUV_on(sv);
2131 }
2132 } else {
2133 /* 2s complement assumption */
2134 if (value <= (UV)IV_MIN) {
45977657 2135 SvIV_set(sv, -(IV)value);
c2988b20
NC
2136 } else {
2137 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2138 I'm assuming it will be rare. */
c2988b20
NC
2139 if (SvTYPE(sv) < SVt_PVNV)
2140 sv_upgrade(sv, SVt_PVNV);
2141 SvNOK_on(sv);
2142 SvIOK_off(sv);
2143 SvIOKp_on(sv);
9d6ce603 2144 SvNV_set(sv, -(NV)value);
45977657 2145 SvIV_set(sv, IV_MIN);
c2988b20
NC
2146 }
2147 }
2148 }
2149 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2150 will be in the previous block to set the IV slot, and the next
2151 block to set the NV slot. So no else here. */
2152
2153 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2154 != IS_NUMBER_IN_UV) {
2155 /* It wasn't an (integer that doesn't overflow the UV). */
3f7c398e 2156 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8 2157
c2988b20
NC
2158 if (! numtype && ckWARN(WARN_NUMERIC))
2159 not_a_number(sv);
28e5dec8 2160
65202027 2161#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2162 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2163 PTR2UV(sv), SvNVX(sv)));
65202027 2164#else
1779d84d 2165 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
c2988b20 2166 PTR2UV(sv), SvNVX(sv)));
65202027 2167#endif
28e5dec8 2168
28e5dec8 2169#ifdef NV_PRESERVES_UV
af359546
NC
2170 (void)SvIOKp_on(sv);
2171 (void)SvNOK_on(sv);
2172 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2173 SvIV_set(sv, I_V(SvNVX(sv)));
2174 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2175 SvIOK_on(sv);
2176 } else {
6f207bd3 2177 NOOP; /* Integer is imprecise. NOK, IOKp */
af359546
NC
2178 }
2179 /* UV will not work better than IV */
2180 } else {
2181 if (SvNVX(sv) > (NV)UV_MAX) {
2182 SvIsUV_on(sv);
2183 /* Integer is inaccurate. NOK, IOKp, is UV */
2184 SvUV_set(sv, UV_MAX);
af359546
NC
2185 } else {
2186 SvUV_set(sv, U_V(SvNVX(sv)));
2187 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2188 NV preservse UV so can do correct comparison. */
2189 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2190 SvIOK_on(sv);
af359546 2191 } else {
6f207bd3 2192 NOOP; /* Integer is imprecise. NOK, IOKp, is UV */
af359546
NC
2193 }
2194 }
4b0c9573 2195 SvIsUV_on(sv);
af359546 2196 }
28e5dec8 2197#else /* NV_PRESERVES_UV */
c2988b20
NC
2198 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2199 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
af359546 2200 /* The IV/UV slot will have been set from value returned by
c2988b20
NC
2201 grok_number above. The NV slot has just been set using
2202 Atof. */
560b0c46 2203 SvNOK_on(sv);
c2988b20
NC
2204 assert (SvIOKp(sv));
2205 } else {
2206 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2207 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2208 /* Small enough to preserve all bits. */
2209 (void)SvIOKp_on(sv);
2210 SvNOK_on(sv);
45977657 2211 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2212 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2213 SvIOK_on(sv);
2214 /* Assumption: first non-preserved integer is < IV_MAX,
2215 this NV is in the preserved range, therefore: */
2216 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2217 < (UV)IV_MAX)) {
32fdb065 2218 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);
c2988b20
NC
2219 }
2220 } else {
2221 /* IN_UV NOT_INT
2222 0 0 already failed to read UV.
2223 0 1 already failed to read UV.
2224 1 0 you won't get here in this case. IV/UV
2225 slot set, public IOK, Atof() unneeded.
2226 1 1 already read UV.
2227 so there's no point in sv_2iuv_non_preserve() attempting
2228 to use atol, strtol, strtoul etc. */
47031da6 2229# ifdef DEBUGGING
40a17c4c 2230 sv_2iuv_non_preserve (sv, numtype);
47031da6
NC
2231# else
2232 sv_2iuv_non_preserve (sv);
2233# endif
c2988b20
NC
2234 }
2235 }
28e5dec8 2236#endif /* NV_PRESERVES_UV */
a43d94f2
NC
2237 /* It might be more code efficient to go through the entire logic above
2238 and conditionally set with SvIOKp_on() rather than SvIOK(), but it
2239 gets complex and potentially buggy, so more programmer efficient
2240 to do it this way, by turning off the public flags: */
2241 if (!numtype)
2242 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
25da4f38 2243 }
af359546
NC
2244 }
2245 else {
675c862f 2246 if (isGV_with_GP(sv))
159b6efe 2247 return glob_2number(MUTABLE_GV(sv));
180488f8 2248
af359546
NC
2249 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2250 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2251 report_uninit(sv);
2252 }
25da4f38
IZ
2253 if (SvTYPE(sv) < SVt_IV)
2254 /* Typically the caller expects that sv_any is not NULL now. */
2255 sv_upgrade(sv, SVt_IV);
af359546
NC
2256 /* Return 0 from the caller. */
2257 return TRUE;
2258 }
2259 return FALSE;
2260}
2261
2262/*
2263=for apidoc sv_2iv_flags
2264
2265Return the integer value of an SV, doing any necessary string
2266conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2267Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2268
2269=cut
2270*/
2271
2272IV
5de3775c 2273Perl_sv_2iv_flags(pTHX_ register SV *const sv, const I32 flags)
af359546 2274{
97aff369 2275 dVAR;
af359546 2276 if (!sv)
a0d0e21e 2277 return 0;
cecf5685
NC
2278 if (SvGMAGICAL(sv) || (SvTYPE(sv) == SVt_PVGV && SvVALID(sv))) {
2279 /* FBMs use the same flag bit as SVf_IVisUV, so must let them
50caf62e
NC
2280 cache IVs just in case. In practice it seems that they never
2281 actually anywhere accessible by user Perl code, let alone get used
2282 in anything other than a string context. */
af359546
NC
2283 if (flags & SV_GMAGIC)
2284 mg_get(sv);
2285 if (SvIOKp(sv))
2286 return SvIVX(sv);
2287 if (SvNOKp(sv)) {
2288 return I_V(SvNVX(sv));
2289 }
71c558c3
NC
2290 if (SvPOKp(sv) && SvLEN(sv)) {
2291 UV value;
2292 const int numtype
2293 = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2294
2295 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2296 == IS_NUMBER_IN_UV) {
2297 /* It's definitely an integer */
2298 if (numtype & IS_NUMBER_NEG) {
2299 if (value < (UV)IV_MIN)
2300 return -(IV)value;
2301 } else {
2302 if (value < (UV)IV_MAX)
2303 return (IV)value;
2304 }
2305 }
2306 if (!numtype) {
2307 if (ckWARN(WARN_NUMERIC))
2308 not_a_number(sv);
2309 }
2310 return I_V(Atof(SvPVX_const(sv)));
2311 }
1c7ff15e
NC
2312 if (SvROK(sv)) {
2313 goto return_rok;
af359546 2314 }
1c7ff15e
NC
2315 assert(SvTYPE(sv) >= SVt_PVMG);
2316 /* This falls through to the report_uninit inside S_sv_2iuv_common. */
4cb1ec55 2317 } else if (SvTHINKFIRST(sv)) {
af359546 2318 if (SvROK(sv)) {
1c7ff15e 2319 return_rok:
af359546
NC
2320 if (SvAMAGIC(sv)) {
2321 SV * const tmpstr=AMG_CALLun(sv,numer);
2322 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2323 return SvIV(tmpstr);
2324 }
2325 }
2326 return PTR2IV(SvRV(sv));
2327 }
2328 if (SvIsCOW(sv)) {
2329 sv_force_normal_flags(sv, 0);
2330 }
2331 if (SvREADONLY(sv) && !SvOK(sv)) {
2332 if (ckWARN(WARN_UNINITIALIZED))
2333 report_uninit(sv);
2334 return 0;
2335 }
2336 }
2337 if (!SvIOKp(sv)) {
2338 if (S_sv_2iuv_common(aTHX_ sv))
2339 return 0;
79072805 2340 }
1d7c1841
GS
2341 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2342 PTR2UV(sv),SvIVX(sv)));
25da4f38 2343 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
2344}
2345
645c22ef 2346/*
891f9566 2347=for apidoc sv_2uv_flags
645c22ef
DM
2348
2349Return the unsigned integer value of an SV, doing any necessary string
891f9566
YST
2350conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2351Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
645c22ef
DM
2352
2353=cut
2354*/
2355
ff68c719 2356UV
5de3775c 2357Perl_sv_2uv_flags(pTHX_ register SV *const sv, const I32 flags)
ff68c719 2358{
97aff369 2359 dVAR;
ff68c719 2360 if (!sv)
2361 return 0;
cecf5685
NC
2362 if (SvGMAGICAL(sv) || (SvTYPE(sv) == SVt_PVGV && SvVALID(sv))) {
2363 /* FBMs use the same flag bit as SVf_IVisUV, so must let them
50caf62e 2364 cache IVs just in case. */
891f9566
YST
2365 if (flags & SV_GMAGIC)
2366 mg_get(sv);
ff68c719 2367 if (SvIOKp(sv))
2368 return SvUVX(sv);
2369 if (SvNOKp(sv))
2370 return U_V(SvNVX(sv));
71c558c3
NC
2371 if (SvPOKp(sv) && SvLEN(sv)) {
2372 UV value;
2373 const int numtype
2374 = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2375
2376 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2377 == IS_NUMBER_IN_UV) {
2378 /* It's definitely an integer */
2379 if (!(numtype & IS_NUMBER_NEG))
2380 return value;
2381 }
2382 if (!numtype) {
2383 if (ckWARN(WARN_NUMERIC))
2384 not_a_number(sv);
2385 }
2386 return U_V(Atof(SvPVX_const(sv)));
2387 }
1c7ff15e
NC
2388 if (SvROK(sv)) {
2389 goto return_rok;
3fe9a6f1 2390 }
1c7ff15e
NC
2391 assert(SvTYPE(sv) >= SVt_PVMG);
2392 /* This falls through to the report_uninit inside S_sv_2iuv_common. */
4cb1ec55 2393 } else if (SvTHINKFIRST(sv)) {
ff68c719 2394 if (SvROK(sv)) {
1c7ff15e 2395 return_rok:
deb46114
NC
2396 if (SvAMAGIC(sv)) {
2397 SV *const tmpstr = AMG_CALLun(sv,numer);
2398 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2399 return SvUV(tmpstr);
2400 }
2401 }
2402 return PTR2UV(SvRV(sv));
ff68c719 2403 }
765f542d
NC
2404 if (SvIsCOW(sv)) {
2405 sv_force_normal_flags(sv, 0);
8a818333 2406 }
0336b60e 2407 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2408 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2409 report_uninit(sv);
ff68c719 2410 return 0;
2411 }
2412 }
af359546
NC
2413 if (!SvIOKp(sv)) {
2414 if (S_sv_2iuv_common(aTHX_ sv))
2415 return 0;
ff68c719 2416 }
25da4f38 2417
1d7c1841
GS
2418 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2419 PTR2UV(sv),SvUVX(sv)));
25da4f38 2420 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719 2421}
2422
645c22ef
DM
2423/*
2424=for apidoc sv_2nv
2425
2426Return the num value of an SV, doing any necessary string or integer
2427conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
2428macros.
2429
2430=cut
2431*/
2432
65202027 2433NV
5de3775c 2434Perl_sv_2nv(pTHX_ register SV *const sv)
79072805 2435{
97aff369 2436 dVAR;
79072805
LW
2437 if (!sv)
2438 return 0.0;
cecf5685
NC
2439 if (SvGMAGICAL(sv) || (SvTYPE(sv) == SVt_PVGV && SvVALID(sv))) {
2440 /* FBMs use the same flag bit as SVf_IVisUV, so must let them
50caf62e 2441 cache IVs just in case. */
463ee0b2
LW
2442 mg_get(sv);
2443 if (SvNOKp(sv))
2444 return SvNVX(sv);
0aa395f8 2445 if ((SvPOKp(sv) && SvLEN(sv)) && !SvIOKp(sv)) {
041457d9 2446 if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) &&
504618e9 2447 !grok_number(SvPVX_const(sv), SvCUR(sv), NULL))
a0d0e21e 2448 not_a_number(sv);
3f7c398e 2449 return Atof(SvPVX_const(sv));
a0d0e21e 2450 }
25da4f38 2451 if (SvIOKp(sv)) {
1c846c1f 2452 if (SvIsUV(sv))
65202027 2453 return (NV)SvUVX(sv);
25da4f38 2454 else
65202027 2455 return (NV)SvIVX(sv);
47a72cb8
NC
2456 }
2457 if (SvROK(sv)) {
2458 goto return_rok;
2459 }
2460 assert(SvTYPE(sv) >= SVt_PVMG);
2461 /* This falls through to the report_uninit near the end of the
2462 function. */
2463 } else if (SvTHINKFIRST(sv)) {
a0d0e21e 2464 if (SvROK(sv)) {
47a72cb8 2465 return_rok:
deb46114
NC
2466 if (SvAMAGIC(sv)) {
2467 SV *const tmpstr = AMG_CALLun(sv,numer);
2468 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2469 return SvNV(tmpstr);
2470 }
2471 }
2472 return PTR2NV(SvRV(sv));
a0d0e21e 2473 }
765f542d
NC
2474 if (SvIsCOW(sv)) {
2475 sv_force_normal_flags(sv, 0);
8a818333 2476 }
0336b60e 2477 if (SvREADONLY(sv) && !SvOK(sv)) {
599cee73 2478 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2479 report_uninit(sv);
ed6116ce
LW
2480 return 0.0;
2481 }
79072805
LW
2482 }
2483 if (SvTYPE(sv) < SVt_NV) {
7e25a7e9
NC
2484 /* The logic to use SVt_PVNV if necessary is in sv_upgrade. */
2485 sv_upgrade(sv, SVt_NV);
906f284f 2486#ifdef USE_LONG_DOUBLE
097ee67d 2487 DEBUG_c({
f93f4e46 2488 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2489 PerlIO_printf(Perl_debug_log,
2490 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2491 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2492 RESTORE_NUMERIC_LOCAL();
2493 });
65202027 2494#else
572bbb43 2495 DEBUG_c({
f93f4e46 2496 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2497 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
1d7c1841 2498 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2499 RESTORE_NUMERIC_LOCAL();
2500 });
572bbb43 2501#endif
79072805
LW
2502 }
2503 else if (SvTYPE(sv) < SVt_PVNV)
2504 sv_upgrade(sv, SVt_PVNV);
59d8ce62
NC
2505 if (SvNOKp(sv)) {
2506 return SvNVX(sv);
61604483 2507 }
59d8ce62 2508 if (SvIOKp(sv)) {
9d6ce603 2509 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
28e5dec8 2510#ifdef NV_PRESERVES_UV
a43d94f2
NC
2511 if (SvIOK(sv))
2512 SvNOK_on(sv);
2513 else
2514 SvNOKp_on(sv);
28e5dec8
JH
2515#else
2516 /* Only set the public NV OK flag if this NV preserves the IV */
2517 /* Check it's not 0xFFFFFFFFFFFFFFFF */
a43d94f2
NC
2518 if (SvIOK(sv) &&
2519 SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
28e5dec8
JH
2520 : (SvIVX(sv) == I_V(SvNVX(sv))))
2521 SvNOK_on(sv);
2522 else
2523 SvNOKp_on(sv);
2524#endif
93a17b20 2525 }
748a9306 2526 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2527 UV value;
3f7c398e 2528 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
041457d9 2529 if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC))
a0d0e21e 2530 not_a_number(sv);
28e5dec8 2531#ifdef NV_PRESERVES_UV
c2988b20
NC
2532 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2533 == IS_NUMBER_IN_UV) {
5e045b90 2534 /* It's definitely an integer */
9d6ce603 2535 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
c2988b20 2536 } else
3f7c398e 2537 SvNV_set(sv, Atof(SvPVX_const(sv)));
a43d94f2
NC
2538 if (numtype)
2539 SvNOK_on(sv);
2540 else
2541 SvNOKp_on(sv);
28e5dec8 2542#else
3f7c398e 2543 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
2544 /* Only set the public NV OK flag if this NV preserves the value in
2545 the PV at least as well as an IV/UV would.
2546 Not sure how to do this 100% reliably. */
2547 /* if that shift count is out of range then Configure's test is
2548 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2549 UV_BITS */
2550 if (((UV)1 << NV_PRESERVES_UV_BITS) >
c2988b20 2551 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
28e5dec8 2552 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
c2988b20
NC
2553 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2554 /* Can't use strtol etc to convert this string, so don't try.
2555 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2556 SvNOK_on(sv);
2557 } else {
2558 /* value has been set. It may not be precise. */
2559 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
2560 /* 2s complement assumption for (UV)IV_MIN */
2561 SvNOK_on(sv); /* Integer is too negative. */
2562 } else {
2563 SvNOKp_on(sv);
2564 SvIOKp_on(sv);
6fa402ec 2565
c2988b20 2566 if (numtype & IS_NUMBER_NEG) {
45977657 2567 SvIV_set(sv, -(IV)value);
c2988b20 2568 } else if (value <= (UV)IV_MAX) {
45977657 2569 SvIV_set(sv, (IV)value);
c2988b20 2570 } else {
607fa7f2 2571 SvUV_set(sv, value);
c2988b20
NC
2572 SvIsUV_on(sv);
2573 }
2574
2575 if (numtype & IS_NUMBER_NOT_INT) {
2576 /* I believe that even if the original PV had decimals,
2577 they are lost beyond the limit of the FP precision.
2578 However, neither is canonical, so both only get p
2579 flags. NWC, 2000/11/25 */
2580 /* Both already have p flags, so do nothing */
2581 } else {
66a1b24b 2582 const NV nv = SvNVX(sv);
c2988b20
NC
2583 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2584 if (SvIVX(sv) == I_V(nv)) {
2585 SvNOK_on(sv);
c2988b20 2586 } else {
c2988b20
NC
2587 /* It had no "." so it must be integer. */
2588 }
00b6aa41 2589 SvIOK_on(sv);
c2988b20
NC
2590 } else {
2591 /* between IV_MAX and NV(UV_MAX).
2592 Could be slightly > UV_MAX */
6fa402ec 2593
c2988b20
NC
2594 if (numtype & IS_NUMBER_NOT_INT) {
2595 /* UV and NV both imprecise. */
2596 } else {
66a1b24b 2597 const UV nv_as_uv = U_V(nv);
c2988b20
NC
2598
2599 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2600 SvNOK_on(sv);
c2988b20 2601 }
00b6aa41 2602 SvIOK_on(sv);
c2988b20
NC
2603 }
2604 }
2605 }
2606 }
2607 }
a43d94f2
NC
2608 /* It might be more code efficient to go through the entire logic above
2609 and conditionally set with SvNOKp_on() rather than SvNOK(), but it
2610 gets complex and potentially buggy, so more programmer efficient
2611 to do it this way, by turning off the public flags: */
2612 if (!numtype)
2613 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
28e5dec8 2614#endif /* NV_PRESERVES_UV */
93a17b20 2615 }
79072805 2616 else {
f7877b28 2617 if (isGV_with_GP(sv)) {
159b6efe 2618 glob_2number(MUTABLE_GV(sv));
180488f8
NC
2619 return 0.0;
2620 }
2621
041457d9 2622 if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED))
29489e7c 2623 report_uninit(sv);
7e25a7e9
NC
2624 assert (SvTYPE(sv) >= SVt_NV);
2625 /* Typically the caller expects that sv_any is not NULL now. */
2626 /* XXX Ilya implies that this is a bug in callers that assume this
2627 and ideally should be fixed. */
a0d0e21e 2628 return 0.0;
79072805 2629 }
572bbb43 2630#if defined(USE_LONG_DOUBLE)
097ee67d 2631 DEBUG_c({
f93f4e46 2632 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2633 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2634 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2635 RESTORE_NUMERIC_LOCAL();
2636 });
65202027 2637#else
572bbb43 2638 DEBUG_c({
f93f4e46 2639 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2640 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
1d7c1841 2641 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2642 RESTORE_NUMERIC_LOCAL();
2643 });
572bbb43 2644#endif
463ee0b2 2645 return SvNVX(sv);
79072805
LW
2646}
2647
800401ee
JH
2648/*
2649=for apidoc sv_2num
2650
2651Return an SV with the numeric value of the source SV, doing any necessary
a196a5fa
JH
2652reference or overload conversion. You must use the C<SvNUM(sv)> macro to
2653access this function.
800401ee
JH
2654
2655=cut
2656*/
2657
2658SV *
5de3775c 2659Perl_sv_2num(pTHX_ register SV *const sv)
800401ee 2660{
7918f24d
NC
2661 PERL_ARGS_ASSERT_SV_2NUM;
2662
b9ee0594
RGS
2663 if (!SvROK(sv))
2664 return sv;
800401ee
JH
2665 if (SvAMAGIC(sv)) {
2666 SV * const tmpsv = AMG_CALLun(sv,numer);
2667 if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
2668 return sv_2num(tmpsv);
2669 }
2670 return sv_2mortal(newSVuv(PTR2UV(SvRV(sv))));
2671}
2672
645c22ef
DM
2673/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2674 * UV as a string towards the end of buf, and return pointers to start and
2675 * end of it.
2676 *
2677 * We assume that buf is at least TYPE_CHARS(UV) long.
2678 */
2679
864dbfa3 2680static char *
5de3775c 2681S_uiv_2buf(char *const buf, const IV iv, UV uv, const int is_uv, char **const peob)
25da4f38 2682{
25da4f38 2683 char *ptr = buf + TYPE_CHARS(UV);
823a54a3 2684 char * const ebuf = ptr;
25da4f38 2685 int sign;
25da4f38 2686
7918f24d
NC
2687 PERL_ARGS_ASSERT_UIV_2BUF;
2688
25da4f38
IZ
2689 if (is_uv)
2690 sign = 0;
2691 else if (iv >= 0) {
2692 uv = iv;
2693 sign = 0;
2694 } else {
2695 uv = -iv;
2696 sign = 1;
2697 }
2698 do {
eb160463 2699 *--ptr = '0' + (char)(uv % 10);
25da4f38
IZ
2700 } while (uv /= 10);
2701 if (sign)
2702 *--ptr = '-';
2703 *peob = ebuf;
2704 return ptr;
2705}
2706
645c22ef
DM
2707/*
2708=for apidoc sv_2pv_flags
2709
ff276b08 2710Returns a pointer to the string value of an SV, and sets *lp to its length.
645c22ef
DM
2711If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
2712if necessary.
2713Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
2714usually end up here too.
2715
2716=cut
2717*/
2718
8d6d96c1 2719char *
5de3775c 2720Perl_sv_2pv_flags(pTHX_ register SV *const sv, STRLEN *const lp, const I32 flags)
8d6d96c1 2721{
97aff369 2722 dVAR;
79072805 2723 register char *s;
79072805 2724
463ee0b2 2725 if (!sv) {
cdb061a3
NC
2726 if (lp)
2727 *lp = 0;
73d840c0 2728 return (char *)"";
463ee0b2 2729 }
8990e307 2730 if (SvGMAGICAL(sv)) {
8d6d96c1
HS
2731 if (flags & SV_GMAGIC)
2732 mg_get(sv);
463ee0b2 2733 if (SvPOKp(sv)) {
cdb061a3
NC
2734 if (lp)
2735 *lp = SvCUR(sv);
10516c54
NC
2736 if (flags & SV_MUTABLE_RETURN)
2737 return SvPVX_mutable(sv);
4d84ee25
NC
2738 if (flags & SV_CONST_RETURN)
2739 return (char *)SvPVX_const(sv);
463ee0b2
LW
2740 return SvPVX(sv);
2741 }
75dfc8ec
NC
2742 if (SvIOKp(sv) || SvNOKp(sv)) {
2743 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
75dfc8ec
NC
2744 STRLEN len;
2745
2746 if (SvIOKp(sv)) {
e80fed9d 2747 len = SvIsUV(sv)
d9fad198
JH
2748 ? my_snprintf(tbuf, sizeof(tbuf), "%"UVuf, (UV)SvUVX(sv))
2749 : my_snprintf(tbuf, sizeof(tbuf), "%"IVdf, (IV)SvIVX(sv));
75dfc8ec 2750 } else {
e8ada2d0
NC
2751 Gconvert(SvNVX(sv), NV_DIG, 0, tbuf);
2752 len = strlen(tbuf);
75dfc8ec 2753 }
b5b886f0
NC
2754 assert(!SvROK(sv));
2755 {
75dfc8ec
NC
2756 dVAR;
2757
2758#ifdef FIXNEGATIVEZERO
e8ada2d0
NC
2759 if (len == 2 && tbuf[0] == '-' && tbuf[1] == '0') {
2760 tbuf[0] = '0';
2761 tbuf[1] = 0;
75dfc8ec
NC
2762 len = 1;
2763 }
2764#endif
2765 SvUPGRADE(sv, SVt_PV);
2766 if (lp)
2767 *lp = len;
2768 s = SvGROW_mutable(sv, len + 1);
2769 SvCUR_set(sv, len);
2770 SvPOKp_on(sv);
10edeb5d 2771 return (char*)memcpy(s, tbuf, len + 1);
75dfc8ec 2772 }
463ee0b2 2773 }
1c7ff15e
NC
2774 if (SvROK(sv)) {
2775 goto return_rok;
2776 }
2777 assert(SvTYPE(sv) >= SVt_PVMG);
2778 /* This falls through to the report_uninit near the end of the
2779 function. */
2780 } else if (SvTHINKFIRST(sv)) {
ed6116ce 2781 if (SvROK(sv)) {
1c7ff15e 2782 return_rok:
deb46114
NC
2783 if (SvAMAGIC(sv)) {
2784 SV *const tmpstr = AMG_CALLun(sv,string);
2785 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2786 /* Unwrap this: */
2787 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr);
2788 */
2789
2790 char *pv;
2791 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
2792 if (flags & SV_CONST_RETURN) {
2793 pv = (char *) SvPVX_const(tmpstr);
2794 } else {
2795 pv = (flags & SV_MUTABLE_RETURN)
2796 ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
2797 }
2798 if (lp)
2799 *lp = SvCUR(tmpstr);
50adf7d2 2800 } else {
deb46114 2801 pv = sv_2pv_flags(tmpstr, lp, flags);
50adf7d2 2802 }
deb46114
NC
2803 if (SvUTF8(tmpstr))
2804 SvUTF8_on(sv);
2805 else
2806 SvUTF8_off(sv);
2807 return pv;
50adf7d2 2808 }
deb46114
NC
2809 }
2810 {
fafee734
NC
2811 STRLEN len;
2812 char *retval;
2813 char *buffer;
d2c6dc5e 2814 SV *const referent = SvRV(sv);
d8eae41e
NC
2815
2816 if (!referent) {
fafee734
NC
2817 len = 7;
2818 retval = buffer = savepvn("NULLREF", len);
5c35adbb 2819 } else if (SvTYPE(referent) == SVt_REGEXP) {
d2c6dc5e 2820 REGEXP * const re = (REGEXP *)MUTABLE_PTR(referent);
67d2d14d
AB
2821 I32 seen_evals = 0;
2822
2823 assert(re);
2824
2825 /* If the regex is UTF-8 we want the containing scalar to
2826 have an UTF-8 flag too */
2827 if (RX_UTF8(re))
2828 SvUTF8_on(sv);
2829 else
2830 SvUTF8_off(sv);
2831
2832 if ((seen_evals = RX_SEEN_EVALS(re)))
2833 PL_reginterp_cnt += seen_evals;
2834
2835 if (lp)
2836 *lp = RX_WRAPLEN(re);
2837
2838 return RX_WRAPPED(re);
d8eae41e
NC
2839 } else {
2840 const char *const typestr = sv_reftype(referent, 0);
fafee734
NC
2841 const STRLEN typelen = strlen(typestr);
2842 UV addr = PTR2UV(referent);
2843 const char *stashname = NULL;
2844 STRLEN stashnamelen = 0; /* hush, gcc */
2845 const char *buffer_end;
d8eae41e 2846
d8eae41e 2847 if (SvOBJECT(referent)) {
fafee734
NC
2848 const HEK *const name = HvNAME_HEK(SvSTASH(referent));
2849
2850 if (name) {
2851 stashname = HEK_KEY(name);
2852 stashnamelen = HEK_LEN(name);
2853
2854 if (HEK_UTF8(name)) {
2855 SvUTF8_on(sv);
2856 } else {
2857 SvUTF8_off(sv);
2858 }
2859 } else {
2860 stashname = "__ANON__";
2861 stashnamelen = 8;
2862 }
2863 len = stashnamelen + 1 /* = */ + typelen + 3 /* (0x */
2864 + 2 * sizeof(UV) + 2 /* )\0 */;
2865 } else {
2866 len = typelen + 3 /* (0x */
2867 + 2 * sizeof(UV) + 2 /* )\0 */;
d8eae41e 2868 }
fafee734
NC
2869
2870 Newx(buffer, len, char);
2871 buffer_end = retval = buffer + len;
2872
2873 /* Working backwards */
2874 *--retval = '\0';
2875 *--retval = ')';
2876 do {
2877 *--retval = PL_hexdigit[addr & 15];
2878 } while (addr >>= 4);
2879 *--retval = 'x';
2880 *--retval = '0';
2881 *--retval = '(';
2882
2883 retval -= typelen;
2884 memcpy(retval, typestr, typelen);
2885
2886 if (stashname) {
2887 *--retval = '=';
2888 retval -= stashnamelen;
2889 memcpy(retval, stashname, stashnamelen);
2890 }
2891 /* retval may not neccesarily have reached the start of the
2892 buffer here. */
2893 assert (retval >= buffer);
2894
2895 len = buffer_end - retval - 1; /* -1 for that \0 */
c080367d 2896 }
042dae7a 2897 if (lp)
fafee734
NC
2898 *lp = len;
2899 SAVEFREEPV(buffer);
2900 return retval;
463ee0b2 2901 }
79072805 2902 }
0336b60e 2903 if (SvREADONLY(sv) && !SvOK(sv)) {
cdb061a3
NC
2904 if (lp)
2905 *lp = 0;
9f621bb0
NC
2906 if (flags & SV_UNDEF_RETURNS_NULL)
2907 return NULL;
2908 if (ckWARN(WARN_UNINITIALIZED))
2909 report_uninit(sv);
73d840c0 2910 return (char *)"";
79072805 2911 }
79072805 2912 }
28e5dec8
JH
2913 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
2914 /* I'm assuming that if both IV and NV are equally valid then
2915 converting the IV is going to be more efficient */
e1ec3a88 2916 const U32 isUIOK = SvIsUV(sv);
28e5dec8
JH
2917 char buf[TYPE_CHARS(UV)];
2918 char *ebuf, *ptr;
97a130b8 2919 STRLEN len;
28e5dec8
JH
2920
2921 if (SvTYPE(sv) < SVt_PVIV)
2922 sv_upgrade(sv, SVt_PVIV);
4ea1d550 2923 ptr = uiv_2buf(buf, SvIVX(sv), SvUVX(sv), isUIOK, &ebuf);
97a130b8 2924 len = ebuf - ptr;
5902b6a9 2925 /* inlined from sv_setpvn */
97a130b8
NC
2926 s = SvGROW_mutable(sv, len + 1);
2927 Move(ptr, s, len, char);
2928 s += len;
28e5dec8 2929 *s = '\0';
28e5dec8
JH
2930 }
2931 else if (SvNOKp(sv)) {
4ee39169 2932 dSAVE_ERRNO;
79072805
LW
2933 if (SvTYPE(sv) < SVt_PVNV)
2934 sv_upgrade(sv, SVt_PVNV);
1c846c1f 2935 /* The +20 is pure guesswork. Configure test needed. --jhi */
5902b6a9 2936 s = SvGROW_mutable(sv, NV_DIG + 20);
c81271c3 2937 /* some Xenix systems wipe out errno here */
79072805 2938#ifdef apollo
463ee0b2 2939 if (SvNVX(sv) == 0.0)
d1307786 2940 my_strlcpy(s, "0", SvLEN(sv));
79072805
LW
2941 else
2942#endif /*apollo*/
bbce6d69 2943 {
2d4389e4 2944 Gconvert(SvNVX(sv), NV_DIG, 0, s);
bbce6d69 2945 }
4ee39169 2946 RESTORE_ERRNO;
a0d0e21e 2947#ifdef FIXNEGATIVEZERO
20773dcd
NC
2948 if (*s == '-' && s[1] == '0' && !s[2]) {
2949 s[0] = '0';
2950 s[1] = 0;
2951 }
a0d0e21e 2952#endif
79072805
LW
2953 while (*s) s++;
2954#ifdef hcx
2955 if (s[-1] == '.')
46fc3d4c 2956 *--s = '\0';
79072805
LW
2957#endif
2958 }
79072805 2959 else {
8d1c3e26
NC
2960 if (isGV_with_GP(sv)) {
2961 GV *const gv = MUTABLE_GV(sv);
2962 const U32 wasfake = SvFLAGS(gv) & SVf_FAKE;
2963 SV *const buffer = sv_newmortal();
2964
2965 /* FAKE globs can get coerced, so need to turn this off temporarily
2966 if it is on. */
2967 SvFAKE_off(gv);
2968 gv_efullname3(buffer, gv, "*");
2969 SvFLAGS(gv) |= wasfake;
2970
2971 assert(SvPOK(buffer));
2972 if (lp) {
2973 *lp = SvCUR(buffer);
2974 }
2975 return SvPVX(buffer);
2976 }
180488f8 2977
cdb061a3 2978 if (lp)
00b6aa41 2979 *lp = 0;
9f621bb0
NC
2980 if (flags & SV_UNDEF_RETURNS_NULL)
2981 return NULL;
2982 if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED))
2983 report_uninit(sv);
25da4f38
IZ
2984 if (SvTYPE(sv) < SVt_PV)
2985 /* Typically the caller expects that sv_any is not NULL now. */
2986 sv_upgrade(sv, SVt_PV);
73d840c0 2987 return (char *)"";
79072805 2988 }
cdb061a3 2989 {
823a54a3 2990 const STRLEN len = s - SvPVX_const(sv);
cdb061a3
NC
2991 if (lp)
2992 *lp = len;
2993 SvCUR_set(sv, len);
2994 }
79072805 2995 SvPOK_on(sv);
1d7c1841 2996 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3f7c398e 2997 PTR2UV(sv),SvPVX_const(sv)));
4d84ee25
NC
2998 if (flags & SV_CONST_RETURN)
2999 return (char *)SvPVX_const(sv);
10516c54
NC
3000 if (flags & SV_MUTABLE_RETURN)
3001 return SvPVX_mutable(sv);
463ee0b2
LW
3002 return SvPVX(sv);
3003}
3004
645c22ef 3005/*
6050d10e
JP
3006=for apidoc sv_copypv
3007
3008Copies a stringified representation of the source SV into the
3009destination SV. Automatically performs any necessary mg_get and
54f0641b 3010coercion of numeric values into strings. Guaranteed to preserve
2575c402 3011UTF8 flag even from overloaded objects. Similar in nature to
54f0641b
NIS
3012sv_2pv[_flags] but operates directly on an SV instead of just the
3013string. Mostly uses sv_2pv_flags to do its work, except when that
6050d10e
JP
3014would lose the UTF-8'ness of the PV.
3015
3016=cut
3017*/
3018
3019void
5de3775c 3020Perl_sv_copypv(pTHX_ SV *const dsv, register SV *const ssv)
6050d10e 3021{
446eaa42 3022 STRLEN len;
53c1dcc0 3023 const char * const s = SvPV_const(ssv,len);
7918f24d
NC
3024
3025 PERL_ARGS_ASSERT_SV_COPYPV;
3026
cb50f42d 3027 sv_setpvn(dsv,s,len);
446eaa42 3028 if (SvUTF8(ssv))
cb50f42d 3029 SvUTF8_on(dsv);
446eaa42 3030 else
cb50f42d 3031 SvUTF8_off(dsv);
6050d10e
JP
3032}
3033
3034/*
645c22ef
DM
3035=for apidoc sv_2pvbyte
3036
3037Return a pointer to the byte-encoded representation of the SV, and set *lp
1e54db1a 3038to its length. May cause the SV to be downgraded from UTF-8 as a
645c22ef
DM
3039side-effect.
3040
3041Usually accessed via the C<SvPVbyte> macro.
3042
3043=cut
3044*/
3045
7340a771 3046char *
5de3775c 3047Perl_sv_2pvbyte(pTHX_ register SV *const sv, STRLEN *const lp)
7340a771 3048{
7918f24d
NC
3049 PERL_ARGS_ASSERT_SV_2PVBYTE;
3050
0875d2fe 3051 sv_utf8_downgrade(sv,0);
97972285 3052 return lp ? SvPV(sv,*lp) : SvPV_nolen(sv);
7340a771
GS
3053}
3054
645c22ef 3055/*
035cbb0e
RGS
3056=for apidoc sv_2pvutf8
3057
3058Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3059to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
3060
3061Usually accessed via the C<SvPVutf8> macro.
3062
3063=cut
3064*/
645c22ef 3065
7340a771 3066char *
7bc54cea 3067Perl_sv_2pvutf8(pTHX_ register SV *const sv, STRLEN *const lp)
7340a771 3068{
7918f24d
NC
3069 PERL_ARGS_ASSERT_SV_2PVUTF8;
3070
035cbb0e
RGS
3071 sv_utf8_upgrade(sv);
3072 return lp ? SvPV(sv,*lp) : SvPV_nolen(sv);
7340a771 3073}
1c846c1f 3074
7ee2227d 3075
645c22ef
DM
3076/*
3077=for apidoc sv_2bool
3078
3079This function is only called on magical items, and is only used by
8cf8f3d1 3080sv_true() or its macro equivalent.
645c22ef
DM
3081
3082=cut
3083*/
3084
463ee0b2 3085bool
7bc54cea 3086Perl_sv_2bool(pTHX_ register SV *const sv)
463ee0b2 3087{
97aff369 3088 dVAR;
7918f24d
NC
3089
3090 PERL_ARGS_ASSERT_SV_2BOOL;
3091
5b295bef 3092 SvGETMAGIC(sv);
463ee0b2 3093
a0d0e21e
LW
3094 if (!SvOK(sv))
3095 return 0;
3096 if (SvROK(sv)) {
fabdb6c0
AL
3097 if (SvAMAGIC(sv)) {
3098 SV * const tmpsv = AMG_CALLun(sv,bool_);
3099 if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
3100 return (bool)SvTRUE(tmpsv);
3101 }
3102 return SvRV(sv) != 0;
a0d0e21e 3103 }
463ee0b2 3104 if (SvPOKp(sv)) {
53c1dcc0
AL
3105 register XPV* const Xpvtmp = (XPV*)SvANY(sv);
3106 if (Xpvtmp &&
339049b0 3107 (*sv->sv_u.svu_pv > '0' ||
11343788 3108 Xpvtmp->xpv_cur > 1 ||
339049b0 3109 (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0')))
463ee0b2
LW
3110 return 1;
3111 else
3112 return 0;
3113 }
3114 else {
3115 if (SvIOKp(sv))
3116 return SvIVX(sv) != 0;
3117 else {
3118 if (SvNOKp(sv))
3119 return SvNVX(sv) != 0.0;
180488f8 3120 else {
f7877b28 3121 if (isGV_with_GP(sv))
180488f8
NC
3122 return TRUE;
3123 else
3124 return FALSE;
3125 }
463ee0b2
LW
3126 }
3127 }
79072805
LW
3128}
3129
c461cf8f
JH
3130/*
3131=for apidoc sv_utf8_upgrade
3132
78ea37eb 3133Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3134Forces the SV to string form if it is not already.
2bbc8d55 3135Will C<mg_get> on C<sv> if appropriate.
4411f3b6 3136Always sets the SvUTF8 flag to avoid future validity checks even
2bbc8d55
SP
3137if the whole string is the same in UTF-8 as not.
3138Returns the number of bytes in the converted string
c461cf8f 3139
13a6c0e0
JH
3140This is not as a general purpose byte encoding to Unicode interface:
3141use the Encode extension for that.
3142
fe749c9a
KW
3143=for apidoc sv_utf8_upgrade_nomg
3144
3145Like sv_utf8_upgrade, but doesn't do magic on C<sv>
3146
8d6d96c1
HS
3147=for apidoc sv_utf8_upgrade_flags
3148
78ea37eb 3149Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3150Forces the SV to string form if it is not already.
8d6d96c1 3151Always sets the SvUTF8 flag to avoid future validity checks even
2bbc8d55
SP
3152if all the bytes are invariant in UTF-8. If C<flags> has C<SV_GMAGIC> bit set,
3153will C<mg_get> on C<sv> if appropriate, else not.
3154Returns the number of bytes in the converted string
3155C<sv_utf8_upgrade> and
8d6d96c1
HS
3156C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3157
13a6c0e0
JH
3158This is not as a general purpose byte encoding to Unicode interface:
3159use the Encode extension for that.
3160
8d6d96c1 3161=cut
b3ab6785
KW
3162
3163The grow version is currently not externally documented. It adds a parameter,
3164extra, which is the number of unused bytes the string of 'sv' is guaranteed to
3165have free after it upon return. This allows the caller to reserve extra space
3166that it intends to fill, to avoid extra grows.
3167
3168Also externally undocumented for the moment is the flag SV_FORCE_UTF8_UPGRADE,
3169which can be used to tell this function to not first check to see if there are
3170any characters that are different in UTF-8 (variant characters) which would
3171force it to allocate a new string to sv, but to assume there are. Typically
3172this flag is used by a routine that has already parsed the string to find that
3173there are such characters, and passes this information on so that the work
3174doesn't have to be repeated.
3175
3176(One might think that the calling routine could pass in the position of the
3177first such variant, so it wouldn't have to be found again. But that is not the
3178case, because typically when the caller is likely to use this flag, it won't be
3179calling this routine unless it finds something that won't fit into a byte.
3180Otherwise it tries to not upgrade and just use bytes. But some things that
3181do fit into a byte are variants in utf8, and the caller may not have been
3182keeping track of these.)
3183
3184If the routine itself changes the string, it adds a trailing NUL. Such a NUL
3185isn't guaranteed due to having other routines do the work in some input cases,
3186or if the input is already flagged as being in utf8.
3187
3188The speed of this could perhaps be improved for many cases if someone wanted to
3189write a fast function that counts the number of variant characters in a string,
3190especially if it could return the position of the first one.
3191
8d6d96c1
HS
3192*/
3193
3194STRLEN
b3ab6785 3195Perl_sv_utf8_upgrade_flags_grow(pTHX_ register SV *const sv, const I32 flags, STRLEN extra)
8d6d96c1 3196{
97aff369 3197 dVAR;
7918f24d 3198
b3ab6785 3199 PERL_ARGS_ASSERT_SV_UTF8_UPGRADE_FLAGS_GROW;
7918f24d 3200
808c356f
RGS
3201 if (sv == &PL_sv_undef)
3202 return 0;
e0e62c2a
NIS
3203 if (!SvPOK(sv)) {
3204 STRLEN len = 0;
d52b7888
NC
3205 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3206 (void) sv_2pv_flags(sv,&len, flags);
b3ab6785
KW
3207 if (SvUTF8(sv)) {
3208 if (extra) SvGROW(sv, SvCUR(sv) + extra);
d52b7888 3209 return len;
b3ab6785 3210 }
d52b7888
NC
3211 } else {
3212 (void) SvPV_force(sv,len);
3213 }
e0e62c2a 3214 }
4411f3b6 3215
f5cee72b 3216 if (SvUTF8(sv)) {
b3ab6785 3217 if (extra) SvGROW(sv, SvCUR(sv) + extra);
5fec3b1d 3218 return SvCUR(sv);
f5cee72b 3219 }
5fec3b1d 3220
765f542d
NC
3221 if (SvIsCOW(sv)) {
3222 sv_force_normal_flags(sv, 0);
db42d148
NIS
3223 }
3224
b3ab6785 3225 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING)) {
799ef3cb 3226 sv_recode_to_utf8(sv, PL_encoding);
b3ab6785
KW
3227 if (extra) SvGROW(sv, SvCUR(sv) + extra);
3228 return SvCUR(sv);
3229 }
3230
3231 if (SvCUR(sv) > 0) { /* Assume Latin-1/EBCDIC */
c4e7c712 3232 /* This function could be much more efficient if we
2bbc8d55 3233 * had a FLAG in SVs to signal if there are any variant
c4e7c712 3234 * chars in the PV. Given that there isn't such a flag
b3ab6785
KW
3235 * make the loop as fast as possible (although there are certainly ways
3236 * to speed this up, eg. through vectorization) */
3237 U8 * s = (U8 *) SvPVX_const(sv);
3238 U8 * e = (U8 *) SvEND(sv);
3239 U8 *t = s;
3240 STRLEN two_byte_count = 0;
c4e7c712 3241
b3ab6785
KW
3242 if (flags & SV_FORCE_UTF8_UPGRADE) goto must_be_utf8;
3243
3244 /* See if really will need to convert to utf8. We mustn't rely on our
3245 * incoming SV being well formed and having a trailing '\0', as certain
3246 * code in pp_formline can send us partially built SVs. */
3247
c4e7c712 3248 while (t < e) {
53c1dcc0 3249 const U8 ch = *t++;
b3ab6785
KW
3250 if (NATIVE_IS_INVARIANT(ch)) continue;
3251
3252 t--; /* t already incremented; re-point to first variant */
3253 two_byte_count = 1;
3254 goto must_be_utf8;
c4e7c712 3255 }
b3ab6785
KW
3256
3257 /* utf8 conversion not needed because all are invariants. Mark as
3258 * UTF-8 even if no variant - saves scanning loop */
c4e7c712 3259 SvUTF8_on(sv);
b3ab6785
KW
3260 return SvCUR(sv);
3261
3262must_be_utf8:
3263
3264 /* Here, the string should be converted to utf8, either because of an
3265 * input flag (two_byte_count = 0), or because a character that
3266 * requires 2 bytes was found (two_byte_count = 1). t points either to
3267 * the beginning of the string (if we didn't examine anything), or to
3268 * the first variant. In either case, everything from s to t - 1 will
3269 * occupy only 1 byte each on output.
3270 *
3271 * There are two main ways to convert. One is to create a new string
3272 * and go through the input starting from the beginning, appending each
3273 * converted value onto the new string as we go along. It's probably
3274 * best to allocate enough space in the string for the worst possible
3275 * case rather than possibly running out of space and having to
3276 * reallocate and then copy what we've done so far. Since everything
3277 * from s to t - 1 is invariant, the destination can be initialized
3278 * with these using a fast memory copy
3279 *
3280 * The other way is to figure out exactly how big the string should be
3281 * by parsing the entire input. Then you don't have to make it big
3282 * enough to handle the worst possible case, and more importantly, if
3283 * the string you already have is large enough, you don't have to
3284 * allocate a new string, you can copy the last character in the input
3285 * string to the final position(s) that will be occupied by the
3286 * converted string and go backwards, stopping at t, since everything
3287 * before that is invariant.
3288 *
3289 * There are advantages and disadvantages to each method.
3290 *
3291 * In the first method, we can allocate a new string, do the memory
3292 * copy from the s to t - 1, and then proceed through the rest of the
3293 * string byte-by-byte.
3294 *
3295 * In the second method, we proceed through the rest of the input
3296 * string just calculating how big the converted string will be. Then
3297 * there are two cases:
3298 * 1) if the string has enough extra space to handle the converted
3299 * value. We go backwards through the string, converting until we
3300 * get to the position we are at now, and then stop. If this
3301 * position is far enough along in the string, this method is
3302 * faster than the other method. If the memory copy were the same
3303 * speed as the byte-by-byte loop, that position would be about
3304 * half-way, as at the half-way mark, parsing to the end and back
3305 * is one complete string's parse, the same amount as starting
3306 * over and going all the way through. Actually, it would be
3307 * somewhat less than half-way, as it's faster to just count bytes
3308 * than to also copy, and we don't have the overhead of allocating
3309 * a new string, changing the scalar to use it, and freeing the
3310 * existing one. But if the memory copy is fast, the break-even
3311 * point is somewhere after half way. The counting loop could be
3312 * sped up by vectorization, etc, to move the break-even point
3313 * further towards the beginning.
3314 * 2) if the string doesn't have enough space to handle the converted
3315 * value. A new string will have to be allocated, and one might
3316 * as well, given that, start from the beginning doing the first
3317 * method. We've spent extra time parsing the string and in
3318 * exchange all we've gotten is that we know precisely how big to
3319 * make the new one. Perl is more optimized for time than space,
3320 * so this case is a loser.
3321 * So what I've decided to do is not use the 2nd method unless it is
3322 * guaranteed that a new string won't have to be allocated, assuming
3323 * the worst case. I also decided not to put any more conditions on it
3324 * than this, for now. It seems likely that, since the worst case is
3325 * twice as big as the unknown portion of the string (plus 1), we won't
3326 * be guaranteed enough space, causing us to go to the first method,
3327 * unless the string is short, or the first variant character is near
3328 * the end of it. In either of these cases, it seems best to use the
3329 * 2nd method. The only circumstance I can think of where this would
3330 * be really slower is if the string had once had much more data in it
3331 * than it does now, but there is still a substantial amount in it */
3332
3333 {
3334 STRLEN invariant_head = t - s;
3335 STRLEN size = invariant_head + (e - t) * 2 + 1 + extra;
3336 if (SvLEN(sv) < size) {
3337
3338 /* Here, have decided to allocate a new string */
3339
3340 U8 *dst;
3341 U8 *d;
3342
3343 Newx(dst, size, U8);
3344
3345 /* If no known invariants at the beginning of the input string,
3346 * set so starts from there. Otherwise, can use memory copy to
3347 * get up to where we are now, and then start from here */
3348
3349 if (invariant_head <= 0) {
3350 d = dst;
3351 } else {
3352 Copy(s, dst, invariant_head, char);
3353 d = dst + invariant_head;
3354 }
3355
3356 while (t < e) {
3357 const UV uv = NATIVE8_TO_UNI(*t++);
3358 if (UNI_IS_INVARIANT(uv))
3359 *d++ = (U8)UNI_TO_NATIVE(uv);
3360 else {
3361 *d++ = (U8)UTF8_EIGHT_BIT_HI(uv);
3362 *d++ = (U8)UTF8_EIGHT_BIT_LO(uv);
3363 }
3364 }
3365 *d = '\0';
3366 SvPV_free(sv); /* No longer using pre-existing string */
3367 SvPV_set(sv, (char*)dst);
3368 SvCUR_set(sv, d - dst);
3369 SvLEN_set(sv, size);
3370 } else {
3371
3372 /* Here, have decided to get the exact size of the string.
3373 * Currently this happens only when we know that there is
3374 * guaranteed enough space to fit the converted string, so
3375 * don't have to worry about growing. If two_byte_count is 0,
3376 * then t points to the first byte of the string which hasn't
3377 * been examined yet. Otherwise two_byte_count is 1, and t
3378 * points to the first byte in the string that will expand to
3379 * two. Depending on this, start examining at t or 1 after t.
3380 * */
3381
3382 U8 *d = t + two_byte_count;
3383
3384
3385 /* Count up the remaining bytes that expand to two */
3386
3387 while (d < e) {
3388 const U8 chr = *d++;
3389 if (! NATIVE_IS_INVARIANT(chr)) two_byte_count++;
3390 }
3391
3392 /* The string will expand by just the number of bytes that
3393 * occupy two positions. But we are one afterwards because of
3394 * the increment just above. This is the place to put the
3395 * trailing NUL, and to set the length before we decrement */
3396
3397 d += two_byte_count;
3398 SvCUR_set(sv, d - s);
3399 *d-- = '\0';
3400
3401
3402 /* Having decremented d, it points to the position to put the
3403 * very last byte of the expanded string. Go backwards through
3404 * the string, copying and expanding as we go, stopping when we
3405 * get to the part that is invariant the rest of the way down */
3406
3407 e--;
3408 while (e >= t) {
3409 const U8 ch = NATIVE8_TO_UNI(*e--);
3410 if (UNI_IS_INVARIANT(ch)) {
3411 *d-- = UNI_TO_NATIVE(ch);
3412 } else {
3413 *d-- = (U8)UTF8_EIGHT_BIT_LO(ch);
3414 *d-- = (U8)UTF8_EIGHT_BIT_HI(ch);
3415 }
3416 }
3417 }
3418 }
560a288e 3419 }
b3ab6785
KW
3420
3421 /* Mark as UTF-8 even if no variant - saves scanning loop */
3422 SvUTF8_on(sv);
4411f3b6 3423 return SvCUR(sv);
560a288e
GS
3424}
3425
c461cf8f
JH
3426/*
3427=for apidoc sv_utf8_downgrade
3428
78ea37eb 3429Attempts to convert the PV of an SV from characters to bytes.
2bbc8d55
SP
3430If the PV contains a character that cannot fit
3431in a byte, this conversion will fail;
78ea37eb 3432in this case, either returns false or, if C<fail_ok> is not
c461cf8f
JH
3433true, croaks.
3434
13a6c0e0
JH
3435This is not as a general purpose Unicode to byte encoding interface:
3436use the Encode extension for that.
3437
c461cf8f
JH
3438=cut
3439*/
3440
560a288e 3441bool
7bc54cea 3442Perl_sv_utf8_downgrade(pTHX_ register SV *const sv, const bool fail_ok)
560a288e 3443{
97aff369 3444 dVAR;
7918f24d
NC
3445
3446 PERL_ARGS_ASSERT_SV_UTF8_DOWNGRADE;
3447
78ea37eb 3448 if (SvPOKp(sv) && SvUTF8(sv)) {
fa301091 3449 if (SvCUR(sv)) {
03cfe0ae 3450 U8 *s;
652088fc 3451 STRLEN len;
fa301091 3452
765f542d
NC
3453 if (SvIsCOW(sv)) {
3454 sv_force_normal_flags(sv, 0);
3455 }
03cfe0ae
NIS
3456 s = (U8 *) SvPV(sv, len);
3457 if (!utf8_to_bytes(s, &len)) {
fa301091
JH
3458 if (fail_ok)
3459 return FALSE;
3460 else {
3461 if (PL_op)
3462 Perl_croak(aTHX_ "Wide character in %s",
53e06cf0 3463 OP_DESC(PL_op));
fa301091
JH
3464 else
3465 Perl_croak(aTHX_ "Wide character");
3466 }
4b3603a4 3467 }
b162af07 3468 SvCUR_set(sv, len);
67e989fb 3469 }
560a288e 3470 }
ffebcc3e 3471 SvUTF8_off(sv);
560a288e
GS
3472 return TRUE;
3473}
3474
c461cf8f
JH
3475/*
3476=for apidoc sv_utf8_encode
3477
78ea37eb
TS
3478Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3479flag off so that it looks like octets again.
c461cf8f
JH
3480
3481=cut
3482*/
3483
560a288e 3484void
7bc54cea 3485Perl_sv_utf8_encode(pTHX_ register SV *const sv)
560a288e 3486{
7918f24d
NC
3487 PERL_ARGS_ASSERT_SV_UTF8_ENCODE;
3488
4c94c214
NC
3489 if (SvIsCOW(sv)) {
3490 sv_force_normal_flags(sv, 0);
3491 }
3492 if (SvREADONLY(sv)) {
f1f66076 3493 Perl_croak(aTHX_ "%s", PL_no_modify);
4c94c214 3494 }
a5f5288a 3495 (void) sv_utf8_upgrade(sv);
560a288e
GS
3496 SvUTF8_off(sv);
3497}
3498
4411f3b6
NIS
3499/*
3500=for apidoc sv_utf8_decode
3501
78ea37eb
TS
3502If the PV of the SV is an octet sequence in UTF-8
3503and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3504so that it looks like a character. If the PV contains only single-byte
3505characters, the C<SvUTF8> flag stays being off.
3506Scans PV for validity and returns false if the PV is invalid UTF-8.
4411f3b6
NIS
3507
3508=cut
3509*/
3510
560a288e 3511bool
7bc54cea 3512Perl_sv_utf8_decode(pTHX_ register SV *const sv)
560a288e 3513{
7918f24d
NC
3514 PERL_ARGS_ASSERT_SV_UTF8_DECODE;
3515
78ea37eb 3516 if (SvPOKp(sv)) {
93524f2b
NC
3517 const U8 *c;
3518 const U8 *e;
9cbac4c7 3519
645c22ef
DM
3520 /* The octets may have got themselves encoded - get them back as
3521 * bytes
3522 */
3523 if (!sv_utf8_downgrade(sv, TRUE))
560a288e
GS
3524 return FALSE;
3525
3526 /* it is actually just a matter of turning the utf8 flag on, but
3527 * we want to make sure everything inside is valid utf8 first.
3528 */
93524f2b 3529 c = (const U8 *) SvPVX_const(sv);
63cd0674 3530 if (!is_utf8_string(c, SvCUR(sv)+1))
67e989fb 3531 return FALSE;
93524f2b 3532 e = (const U8 *) SvEND(sv);
511c2ff0 3533 while (c < e) {
b64e5050 3534 const U8 ch = *c++;
c4d5f83a 3535 if (!UTF8_IS_INVARIANT(ch)) {
67e989fb
JH
3536 SvUTF8_on(sv);
3537 break;
3538 }
560a288e 3539 }
560a288e
GS
3540 }
3541 return TRUE;
3542}
3543
954c1994
GS
3544/*
3545=for apidoc sv_setsv
3546
645c22ef
DM
3547Copies the contents of the source SV C<ssv> into the destination SV
3548C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3549function if the source SV needs to be reused. Does not handle 'set' magic.
3550Loosely speaking, it performs a copy-by-value, obliterating any previous
3551content of the destination.
3552
3553You probably want to use one of the assortment of wrappers, such as
3554C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3555C<SvSetMagicSV_nosteal>.
3556
8d6d96c1
HS
3557=for apidoc sv_setsv_flags
3558
645c22ef
DM
3559Copies the contents of the source SV C<ssv> into the destination SV
3560C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3561function if the source SV needs to be reused. Does not handle 'set' magic.
3562Loosely speaking, it performs a copy-by-value, obliterating any previous
3563content of the destination.
3564If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
5fcdf167
NC
3565C<ssv> if appropriate, else not. If the C<flags> parameter has the
3566C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
3567and C<sv_setsv_nomg> are implemented in terms of this function.
645c22ef
DM
3568
3569You probably want to use one of the assortment of wrappers, such as
3570C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3571C<SvSetMagicSV_nosteal>.
3572
3573This is the primary function for copying scalars, and most other
3574copy-ish functions and macros use this underneath.
8d6d96c1
HS
3575
3576=cut
3577*/
3578
5d0301b7 3579static void
7bc54cea 3580S_glob_assign_glob(pTHX_ SV *const dstr, SV *const sstr, const int dtype)
5d0301b7 3581{
70cd14a1 3582 I32 mro_changes = 0; /* 1 = method, 2 = isa */
dd69841b 3583
7918f24d
NC
3584 PERL_ARGS_ASSERT_GLOB_ASSIGN_GLOB;
3585
5d0301b7
NC
3586 if (dtype != SVt_PVGV) {
3587 const char * const name = GvNAME(sstr);
3588 const STRLEN len = GvNAMELEN(sstr);
0d092c36 3589 {
f7877b28
NC
3590 if (dtype >= SVt_PV) {
3591 SvPV_free(dstr);
3592 SvPV_set(dstr, 0);
3593 SvLEN_set(dstr, 0);
3594 SvCUR_set(dstr, 0);
3595 }
0d092c36 3596 SvUPGRADE(dstr, SVt_PVGV);
dedf8e73 3597 (void)SvOK_off(dstr);
2e5b91de
NC
3598 /* FIXME - why are we doing this, then turning it off and on again
3599 below? */
3600 isGV_with_GP_on(dstr);
f7877b28 3601 }
5d0301b7
NC
3602 GvSTASH(dstr) = GvSTASH(sstr);
3603 if (GvSTASH(dstr))
daba3364 3604 Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dstr)), dstr);
159b6efe 3605 gv_name_set(MUTABLE_GV(dstr), name, len, GV_ADD);
5d0301b7
NC
3606 SvFAKE_on(dstr); /* can coerce to non-glob */
3607 }
3608
159b6efe 3609 if(GvGP(MUTABLE_GV(sstr))) {
dd69841b
BB
3610 /* If source has method cache entry, clear it */
3611 if(GvCVGEN(sstr)) {
3612 SvREFCNT_dec(GvCV(sstr));
3613 GvCV(sstr) = NULL;
3614 GvCVGEN(sstr) = 0;
3615 }
3616 /* If source has a real method, then a method is
3617 going to change */
159b6efe 3618 else if(GvCV((const GV *)sstr)) {
70cd14a1 3619 mro_changes = 1;
dd69841b
BB
3620 }
3621 }
3622
3623 /* If dest already had a real method, that's a change as well */
159b6efe 3624 if(!mro_changes && GvGP(MUTABLE_GV(dstr)) && GvCVu((const GV *)dstr)) {
70cd14a1 3625 mro_changes = 1;
dd69841b
BB
3626 }
3627
159b6efe 3628 if(strEQ(GvNAME((const GV *)dstr),"ISA"))
70cd14a1
CB
3629 mro_changes = 2;
3630
159b6efe 3631 gp_free(MUTABLE_GV(dstr));
2e5b91de 3632 isGV_with_GP_off(dstr);
5d0301b7 3633 (void)SvOK_off(dstr);
2e5b91de 3634 isGV_with_GP_on(dstr);
dedf8e73 3635 GvINTRO_off(dstr); /* one-shot flag */
5d0301b7
NC
3636 GvGP(dstr) = gp_ref(GvGP(sstr));
3637 if (SvTAINTED(sstr))
3638 SvTAINT(dstr);
3639 if (GvIMPORTED(dstr) != GVf_IMPORTED
3640 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3641 {
3642 GvIMPORTED_on(dstr);
3643 }
3644 GvMULTI_on(dstr);
70cd14a1
CB
3645 if(mro_changes == 2) mro_isa_changed_in(GvSTASH(dstr));
3646 else if(mro_changes) mro_method_changed_in(GvSTASH(dstr));
5d0301b7
NC
3647 return;
3648}
3649
b8473700 3650static void
7bc54cea 3651S_glob_assign_ref(pTHX_ SV *const dstr, SV *const sstr)
7918f24d 3652{
b8473700
NC
3653 SV * const sref = SvREFCNT_inc(SvRV(sstr));
3654 SV *dref = NULL;
3655 const int intro = GvINTRO(dstr);
2440974c 3656 SV **location;
3386d083 3657 U8 import_flag = 0;
27242d61
NC
3658 const U32 stype = SvTYPE(sref);
3659
7918f24d 3660 PERL_ARGS_ASSERT_GLOB_ASSIGN_REF;
b8473700 3661
b8473700
NC
3662 if (intro) {
3663 GvINTRO_off(dstr); /* one-shot flag */
3664 GvLINE(dstr) = CopLINE(PL_curcop);
159b6efe 3665 GvEGV(dstr) = MUTABLE_GV(dstr);
b8473700
NC
3666 }
3667 GvMULTI_on(dstr);
27242d61 3668 switch (stype) {
b8473700 3669 case SVt_PVCV:
27242d61
NC
3670 location = (SV **) &GvCV(dstr);
3671 import_flag = GVf_IMPORTED_CV;
3672 goto common;
3673 case SVt_PVHV:
3674 location = (SV **) &GvHV(dstr);
3675 import_flag = GVf_IMPORTED_HV;
3676 goto common;
3677 case SVt_PVAV:
3678 location = (SV **) &GvAV(dstr);
3679 import_flag = GVf_IMPORTED_AV;
3680 goto common;
3681 case SVt_PVIO:
3682 location = (SV **) &GvIOp(dstr);
3683 goto common;
3684 case SVt_PVFM:
3685 location = (SV **) &GvFORM(dstr);
ef595a33 3686 goto common;
27242d61
NC
3687 default:
3688 location = &GvSV(dstr);
3689 import_flag = GVf_IMPORTED_SV;
3690 common:
b8473700 3691 if (intro) {
27242d61 3692 if (stype == SVt_PVCV) {
ea726b52 3693 /*if (GvCVGEN(dstr) && (GvCV(dstr) != (const CV *)sref || GvCVGEN(dstr))) {*/
5f2fca8a 3694 if (GvCVGEN(dstr)) {
27242d61
NC
3695 SvREFCNT_dec(GvCV(dstr));
3696 GvCV(dstr) = NULL;
3697 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
27242d61 3698 }
b8473700 3699 }
27242d61 3700 SAVEGENERICSV(*location);
b8473700
NC
3701 }
3702 else
27242d61 3703 dref = *location;
5f2fca8a 3704 if (stype == SVt_PVCV && (*location != sref || GvCVGEN(dstr))) {
ea726b52 3705 CV* const cv = MUTABLE_CV(*location);
b8473700 3706 if (cv) {
159b6efe 3707 if (!GvCVGEN((const GV *)dstr) &&
b8473700
NC
3708 (CvROOT(cv) || CvXSUB(cv)))
3709 {
3710 /* Redefining a sub - warning is mandatory if
3711 it was a const and its value changed. */
ea726b52 3712 if (CvCONST(cv) && CvCONST((const CV *)sref)
126f53f3
NC
3713 && cv_const_sv(cv)
3714 == cv_const_sv((const CV *)sref)) {
6f207bd3 3715 NOOP;
b8473700
NC
3716 /* They are 2 constant subroutines generated from
3717 the same constant. This probably means that
3718 they are really the "same" proxy subroutine
3719 instantiated in 2 places. Most likely this is
3720 when a constant is exported twice. Don't warn.
3721 */
3722 }
3723 else if (ckWARN(WARN_REDEFINE)
3724 || (CvCONST(cv)
ea726b52 3725 && (!CvCONST((const CV *)sref)
b8473700 3726 || sv_cmp(cv_const_sv(cv),
126f53f3
NC
3727 cv_const_sv((const CV *)
3728 sref))))) {
b8473700 3729 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
10edeb5d
JH
3730 (const char *)
3731 (CvCONST(cv)
3732 ? "Constant subroutine %s::%s redefined"
3733 : "Subroutine %s::%s redefined"),
159b6efe
NC
3734 HvNAME_get(GvSTASH((const GV *)dstr)),
3735 GvENAME(MUTABLE_GV(dstr)));
b8473700
NC
3736 }
3737 }
3738 if (!intro)
159b6efe 3739 cv_ckproto_len(cv, (const GV *)dstr,
cbf82dd0
NC
3740 SvPOK(sref) ? SvPVX_const(sref) : NULL,
3741 SvPOK(sref) ? SvCUR(sref) : 0);
b8473700 3742 }
b8473700
NC
3743 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3744 GvASSUMECV_on(dstr);
dd69841b 3745 if(GvSTASH(dstr)) mro_method_changed_in(GvSTASH(dstr)); /* sub foo { 1 } sub bar { 2 } *bar = \&foo */
b8473700 3746 }
2440974c 3747 *location = sref;
3386d083
NC
3748 if (import_flag && !(GvFLAGS(dstr) & import_flag)
3749 && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) {
3750 GvFLAGS(dstr) |= import_flag;
b8473700
NC
3751 }
3752 break;
3753 }
b37c2d43 3754 SvREFCNT_dec(dref);
b8473700
NC
3755 if (SvTAINTED(sstr))
3756 SvTAINT(dstr);
3757 return;
3758}
3759
8d6d96c1 3760void
7bc54cea 3761Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV* sstr, const I32 flags)
8d6d96c1 3762{
97aff369 3763 dVAR;
8990e307
LW
3764 register U32 sflags;
3765 register int dtype;
42d0e0b7 3766 register svtype stype;
463ee0b2 3767
7918f24d
NC
3768 PERL_ARGS_ASSERT_SV_SETSV_FLAGS;
3769
79072805
LW
3770 if (sstr == dstr)
3771 return;
29f4f0ab
NC
3772
3773 if (SvIS_FREED(dstr)) {
3774 Perl_croak(aTHX_ "panic: attempt to copy value %" SVf
be2597df 3775 " to a freed scalar %p", SVfARG(sstr), (void *)dstr);
29f4f0ab 3776 }
765f542d 3777 SV_CHECK_THINKFIRST_COW_DROP(dstr);
79072805 3778 if (!sstr)
3280af22 3779 sstr = &PL_sv_undef;
29f4f0ab 3780 if (SvIS_FREED(sstr)) {
6c9570dc
MHM
3781 Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p to %p",
3782 (void*)sstr, (void*)dstr);
29f4f0ab 3783 }
8990e307
LW
3784 stype = SvTYPE(sstr);
3785 dtype = SvTYPE(dstr);
79072805 3786
52944de8 3787 (void)SvAMAGIC_off(dstr);
7a5fa8a2 3788 if ( SvVOK(dstr) )
ece467f9
JP
3789 {
3790 /* need to nuke the magic */
3791 mg_free(dstr);
ece467f9 3792 }
9e7bc3e8 3793
463ee0b2 3794 /* There's a lot of redundancy below but we're going for speed here */
79072805 3795
8990e307 3796 switch (stype) {
79072805 3797 case SVt_NULL:
aece5585 3798 undef_sstr:
20408e3c
GS
3799 if (dtype != SVt_PVGV) {
3800 (void)SvOK_off(dstr);
3801 return;
3802 }
3803 break;
463ee0b2 3804 case SVt_IV:
aece5585
GA
3805 if (SvIOK(sstr)) {
3806 switch (dtype) {
3807 case SVt_NULL:
8990e307 3808 sv_upgrade(dstr, SVt_IV);
aece5585
GA
3809 break;
3810 case SVt_NV:
aece5585 3811 case SVt_PV:
a0d0e21e 3812 sv_upgrade(dstr, SVt_PVIV);
aece5585 3813 break;
010be86b
NC
3814 case SVt_PVGV:
3815 goto end_of_first_switch;
aece5585
GA
3816 }
3817 (void)SvIOK_only(dstr);
45977657 3818 SvIV_set(dstr, SvIVX(sstr));
25da4f38
IZ
3819 if (SvIsUV(sstr))
3820 SvIsUV_on(dstr);
37c25af0
NC
3821 /* SvTAINTED can only be true if the SV has taint magic, which in
3822 turn means that the SV type is PVMG (or greater). This is the
3823 case statement for SVt_IV, so this cannot be true (whatever gcov
3824 may say). */
3825 assert(!SvTAINTED(sstr));
aece5585 3826 return;
8990e307 3827 }
4df7f6af
NC
3828 if (!SvROK(sstr))
3829 goto undef_sstr;
3830 if (dtype < SVt_PV && dtype != SVt_IV)
3831 sv_upgrade(dstr, SVt_IV);
3832 break;
aece5585 3833
463ee0b2 3834 case SVt_NV:
aece5585
GA
3835 if (SvNOK(sstr)) {
3836 switch (dtype) {
3837 case SVt_NULL:
3838 case SVt_IV:
8990e307 3839 sv_upgrade(dstr, SVt_NV);
aece5585 3840 break;
aece5585
GA
3841 case SVt_PV:
3842 case SVt_PVIV:
a0d0e21e 3843 sv_upgrade(dstr, SVt_PVNV);
aece5585 3844 break;
010be86b
NC
3845 case SVt_PVGV:
3846 goto end_of_first_switch;
aece5585 3847 }
9d6ce603 3848 SvNV_set(dstr, SvNVX(sstr));
aece5585 3849 (void)SvNOK_only(dstr);
37c25af0
NC
3850 /* SvTAINTED can only be true if the SV has taint magic, which in
3851 turn means that the SV type is PVMG (or greater). This is the
3852 case statement for SVt_NV, so this cannot be true (whatever gcov
3853 may say). */
3854 assert(!SvTAINTED(sstr));
aece5585 3855 return;
8990e307 3856 }
aece5585
GA
3857 goto undef_sstr;
3858
fc36a67e 3859 case SVt_PVFM:
f8c7b90f 3860#ifdef PERL_OLD_COPY_ON_WRITE
d89fc664
NC
3861 if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) {
3862 if (dtype < SVt_PVIV)
3863 sv_upgrade(dstr, SVt_PVIV);
3864 break;
3865 }
3866 /* Fall through */
3867#endif
fd44068c 3868 case SVt_REGEXP:
d89fc664 3869 case SVt_PV:
8990e307 3870 if (dtype < SVt_PV)
463ee0b2 3871 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
3872 break;
3873 case SVt_PVIV:
8990e307 3874 if (dtype < SVt_PVIV)
463ee0b2 3875 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
3876 break;
3877 case SVt_PVNV:
8990e307 3878 if (dtype < SVt_PVNV)
463ee0b2 3879 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 3880 break;
489f7bfe 3881 default:
a3b680e6
AL
3882 {
3883 const char * const type = sv_reftype(sstr,0);
533c011a 3884 if (PL_op)
a3b680e6 3885 Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_NAME(PL_op));
4633a7c4 3886 else
a3b680e6
AL
3887 Perl_croak(aTHX_ "Bizarre copy of %s", type);
3888 }
4633a7c4
LW
3889 break;
3890
cecf5685 3891 /* case SVt_BIND: */
39cb70dc 3892 case SVt_PVLV:
79072805 3893 case SVt_PVGV:
cecf5685 3894 if (isGV_with_GP(sstr) && dtype <= SVt_PVGV) {
d4c19fe8 3895 glob_assign_glob(dstr, sstr, dtype);
b8c701c1 3896 return;
79072805 3897 }
cecf5685 3898 /* SvVALID means that this PVGV is playing at being an FBM. */
5f66b61c 3899 /*FALLTHROUGH*/
79072805 3900
489f7bfe 3901 case SVt_PVMG:
8d6d96c1 3902 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
973f89ab 3903 mg_get(sstr);
1d9c78c6 3904 if (SvTYPE(sstr) != stype) {
973f89ab 3905 stype = SvTYPE(sstr);
cecf5685 3906 if (isGV_with_GP(sstr) && stype == SVt_PVGV && dtype <= SVt_PVGV) {
d4c19fe8 3907 glob_assign_glob(dstr, sstr, dtype);
b8c701c1
NC
3908 return;
3909 }
973f89ab
CS
3910 }
3911 }
ded42b9f 3912 if (stype == SVt_PVLV)
862a34c6 3913 SvUPGRADE(dstr, SVt_PVNV);
ded42b9f 3914 else
42d0e0b7 3915 SvUPGRADE(dstr, (svtype)stype);
79072805 3916 }
010be86b 3917 end_of_first_switch:
79072805 3918
ff920335
NC
3919 /* dstr may have been upgraded. */
3920 dtype = SvTYPE(dstr);
8990e307
LW
3921 sflags = SvFLAGS(sstr);
3922
ba2fdce6 3923 if (dtype == SVt_PVCV || dtype == SVt_PVFM) {
85324b4d
NC
3924 /* Assigning to a subroutine sets the prototype. */
3925 if (SvOK(sstr)) {
3926 STRLEN len;
3927 const char *const ptr = SvPV_const(sstr, len);
3928
3929 SvGROW(dstr, len + 1);
3930 Copy(ptr, SvPVX(dstr), len + 1, char);
3931 SvCUR_set(dstr, len);
fcddd32e 3932 SvPOK_only(dstr);
ba2fdce6 3933 SvFLAGS(dstr) |= sflags & SVf_UTF8;
85324b4d
NC
3934 } else {
3935 SvOK_off(dstr);
3936 }
ba2fdce6
NC
3937 } else if (dtype == SVt_PVAV || dtype == SVt_PVHV) {
3938 const char * const type = sv_reftype(dstr,0);
3939 if (PL_op)
3940 Perl_croak(aTHX_ "Cannot copy to %s in %s", type, OP_NAME(PL_op));
3941 else
3942 Perl_croak(aTHX_ "Cannot copy to %s", type);
85324b4d 3943 } else if (sflags & SVf_ROK) {
cecf5685 3944 if (isGV_with_GP(dstr) && dtype == SVt_PVGV
785bee4f 3945 && SvTYPE(SvRV(sstr)) == SVt_PVGV && isGV_with_GP(SvRV(sstr))) {
acaa9288
NC
3946 sstr = SvRV(sstr);
3947 if (sstr == dstr) {
3948 if (GvIMPORTED(dstr) != GVf_IMPORTED
3949 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3950 {
3951 GvIMPORTED_on(dstr);
3952 }
3953 GvMULTI_on(dstr);
3954 return;
3955 }
785bee4f
NC
3956 glob_assign_glob(dstr, sstr, dtype);
3957 return;
acaa9288
NC
3958 }
3959
8990e307 3960 if (dtype >= SVt_PV) {
fdc5b023 3961 if (dtype == SVt_PVGV && isGV_with_GP(dstr)) {
d4c19fe8 3962 glob_assign_ref(dstr, sstr);
b8c701c1
NC
3963 return;
3964 }
3f7c398e 3965 if (SvPVX_const(dstr)) {
8bd4d4c5 3966 SvPV_free(dstr);
b162af07
SP
3967 SvLEN_set(dstr, 0);
3968 SvCUR_set(dstr, 0);
a0d0e21e 3969 }
8990e307 3970 }
a0d0e21e 3971 (void)SvOK_off(dstr);
b162af07 3972 SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
96d4b0ee 3973 SvFLAGS(dstr) |= sflags & SVf_ROK;
dfd48732
NC
3974 assert(!(sflags & SVp_NOK));
3975 assert(!(sflags & SVp_IOK));
3976 assert(!(sflags & SVf_NOK));
3977 assert(!(sflags & SVf_IOK));
ed6116ce 3978 }
cecf5685 3979 else if (dtype == SVt_PVGV && isGV_with_GP(dstr)) {
c0c44674
NC
3980 if (!(sflags & SVf_OK)) {
3981 if (ckWARN(WARN_MISC))
3982 Perl_warner(aTHX_ packWARN(WARN_MISC),
3983 "Undefined value assigned to typeglob");
3984 }
3985 else {
3986 GV *gv = gv_fetchsv(sstr, GV_ADD, SVt_PVGV);
daba3364 3987 if (dstr != (const SV *)gv) {
c0c44674 3988 if (GvGP(dstr))
159b6efe 3989 gp_free(MUTABLE_GV(dstr));
c0c44674
NC
3990 GvGP(dstr) = gp_ref(GvGP(gv));
3991 }
3992 }
3993 }
8990e307 3994 else if (sflags & SVp_POK) {
765f542d 3995 bool isSwipe = 0;
79072805
LW
3996
3997 /*
3998 * Check to see if we can just swipe the string. If so, it's a
3999 * possible small lose on short strings, but a big win on long ones.
3f7c398e
SP
4000 * It might even be a win on short strings if SvPVX_const(dstr)
4001 * has to be allocated and SvPVX_const(sstr) has to be freed.
34482cd6
NC
4002 * Likewise if we can set up COW rather than doing an actual copy, we
4003 * drop to the else clause, as the swipe code and the COW setup code
4004 * have much in common.
79072805
LW
4005 */
4006
120fac95
NC
4007 /* Whichever path we take through the next code, we want this true,
4008 and doing it now facilitates the COW check. */
4009 (void)SvPOK_only(dstr);
4010
765f542d 4011 if (
34482cd6
NC
4012 /* If we're already COW then this clause is not true, and if COW
4013 is allowed then we drop down to the else and make dest COW
4014 with us. If caller hasn't said that we're allowed to COW
4015 shared hash keys then we don't do the COW setup, even if the
4016 source scalar is a shared hash key scalar. */
4017 (((flags & SV_COW_SHARED_HASH_KEYS)
4018 ? (sflags & (SVf_FAKE|SVf_READONLY)) != (SVf_FAKE|SVf_READONLY)
4019 : 1 /* If making a COW copy is forbidden then the behaviour we
4020 desire is as if the source SV isn't actually already
4021 COW, even if it is. So we act as if the source flags
4022 are not COW, rather than actually testing them. */
4023 )
f8c7b90f 4024#ifndef PERL_OLD_COPY_ON_WRITE
34482cd6
NC
4025 /* The change that added SV_COW_SHARED_HASH_KEYS makes the logic
4026 when PERL_OLD_COPY_ON_WRITE is defined a little wrong.
4027 Conceptually PERL_OLD_COPY_ON_WRITE being defined should
4028 override SV_COW_SHARED_HASH_KEYS, because it means "always COW"
4029 but in turn, it's somewhat dead code, never expected to go
4030 live, but more kept as a placeholder on how to do it better
4031 in a newer implementation. */
4032 /* If we are COW and dstr is a suitable target then we drop down
4033 into the else and make dest a COW of us. */
b8f9541a
NC
4034 || (SvFLAGS(dstr) & CAN_COW_MASK) != CAN_COW_FLAGS
4035#endif
4036 )
765f542d 4037 &&
765f542d
NC
4038 !(isSwipe =
4039 (sflags & SVs_TEMP) && /* slated for free anyway? */
4040 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
5fcdf167
NC
4041 (!(flags & SV_NOSTEAL)) &&
4042 /* and we're allowed to steal temps */
765f542d
NC
4043 SvREFCNT(sstr) == 1 && /* and no other references to it? */
4044 SvLEN(sstr) && /* and really is a string */
645c22ef 4045 /* and won't be needed again, potentially */
765f542d 4046 !(PL_op && PL_op->op_type == OP_AASSIGN))
f8c7b90f 4047#ifdef PERL_OLD_COPY_ON_WRITE
cb23d5b1
NC
4048 && ((flags & SV_COW_SHARED_HASH_KEYS)
4049 ? (!((sflags & CAN_COW_MASK) == CAN_COW_FLAGS
4050 && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS
4b1c7d9e 4051 && SvTYPE(sstr) >= SVt_PVIV && SvTYPE(sstr) != SVt_PVFM))
cb23d5b1 4052 : 1)
765f542d
NC
4053#endif
4054 ) {
4055 /* Failed the swipe test, and it's not a shared hash key either.
4056 Have to copy the string. */
4057 STRLEN len = SvCUR(sstr);
4058 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
3f7c398e 4059 Move(SvPVX_const(sstr),SvPVX(dstr),len,char);
765f542d
NC
4060 SvCUR_set(dstr, len);
4061 *SvEND(dstr) = '\0';
765f542d 4062 } else {
f8c7b90f 4063 /* If PERL_OLD_COPY_ON_WRITE is not defined, then isSwipe will always
765f542d 4064 be true in here. */
765f542d
NC
4065 /* Either it's a shared hash key, or it's suitable for
4066 copy-on-write or we can swipe the string. */
46187eeb 4067 if (DEBUG_C_TEST) {
ed252734 4068 PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
e419cbc5
NC
4069 sv_dump(sstr);
4070 sv_dump(dstr);
46187eeb 4071 }
f8c7b90f 4072#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 4073 if (!isSwipe) {
765f542d
NC
4074 if ((sflags & (SVf_FAKE | SVf_READONLY))
4075 != (SVf_FAKE | SVf_READONLY)) {
4076 SvREADONLY_on(sstr);
4077 SvFAKE_on(sstr);
4078 /* Make the source SV into a loop of 1.
4079 (about to become 2) */
a29f6d03 4080 SV_COW_NEXT_SV_SET(sstr, sstr);
765f542d
NC
4081 }
4082 }
4083#endif
4084 /* Initial code is common. */
94010e71
NC
4085 if (SvPVX_const(dstr)) { /* we know that dtype >= SVt_PV */
4086 SvPV_free(dstr);
79072805 4087 }
765f542d 4088
765f542d
NC
4089 if (!isSwipe) {
4090 /* making another shared SV. */
4091 STRLEN cur = SvCUR(sstr);
4092 STRLEN len = SvLEN(sstr);
f8c7b90f 4093#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 4094 if (len) {
b8f9541a 4095 assert (SvTYPE(dstr) >= SVt_PVIV);
765f542d
NC
4096 /* SvIsCOW_normal */
4097 /* splice us in between source and next-after-source. */
a29f6d03
NC
4098 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4099 SV_COW_NEXT_SV_SET(sstr, dstr);
940132f3 4100 SvPV_set(dstr, SvPVX_mutable(sstr));
a604c751
NC
4101 } else
4102#endif
4103 {
765f542d 4104 /* SvIsCOW_shared_hash */
46187eeb
NC
4105 DEBUG_C(PerlIO_printf(Perl_debug_log,
4106 "Copy on write: Sharing hash\n"));
b8f9541a 4107
bdd68bc3 4108 assert (SvTYPE(dstr) >= SVt_PV);
765f542d 4109 SvPV_set(dstr,
d1db91c6 4110 HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)))));
bdd68bc3 4111 }
87a1ef3d
SP
4112 SvLEN_set(dstr, len);
4113 SvCUR_set(dstr, cur);
765f542d
NC
4114 SvREADONLY_on(dstr);
4115 SvFAKE_on(dstr);
765f542d
NC
4116 }
4117 else
765f542d 4118 { /* Passes the swipe test. */
78d1e721 4119 SvPV_set(dstr, SvPVX_mutable(sstr));
765f542d
NC
4120 SvLEN_set(dstr, SvLEN(sstr));
4121 SvCUR_set(dstr, SvCUR(sstr));
4122
4123 SvTEMP_off(dstr);
4124 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
6136c704 4125 SvPV_set(sstr, NULL);
765f542d
NC
4126 SvLEN_set(sstr, 0);
4127 SvCUR_set(sstr, 0);
4128 SvTEMP_off(sstr);
4129 }
4130 }
8990e307 4131 if (sflags & SVp_NOK) {
9d6ce603 4132 SvNV_set(dstr, SvNVX(sstr));
79072805 4133 }
8990e307 4134 if (sflags & SVp_IOK) {
23525414
NC
4135 SvIV_set(dstr, SvIVX(sstr));
4136 /* Must do this otherwise some other overloaded use of 0x80000000
4137 gets confused. I guess SVpbm_VALID */
2b1c7e3e 4138 if (sflags & SVf_IVisUV)
25da4f38 4139 SvIsUV_on(dstr);
79072805 4140 }
96d4b0ee 4141 SvFLAGS(dstr) |= sflags & (SVf_IOK|SVp_IOK|SVf_NOK|SVp_NOK|SVf_UTF8);
4f2da183 4142 {
b0a11fe1 4143 const MAGIC * const smg = SvVSTRING_mg(sstr);
4f2da183
NC
4144 if (smg) {
4145 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4146 smg->mg_ptr, smg->mg_len);
4147 SvRMAGICAL_on(dstr);
4148 }
7a5fa8a2 4149 }
79072805 4150 }
5d581361 4151 else if (sflags & (SVp_IOK|SVp_NOK)) {
c2468cc7 4152 (void)SvOK_off(dstr);
96d4b0ee 4153 SvFLAGS(dstr) |= sflags & (SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_NOK|SVp_NOK);
5d581361
NC
4154 if (sflags & SVp_IOK) {
4155 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
4156 SvIV_set(dstr, SvIVX(sstr));
4157 }
3332b3c1 4158 if (sflags & SVp_NOK) {
9d6ce603 4159 SvNV_set(dstr, SvNVX(sstr));
3332b3c1
JH
4160 }
4161 }
79072805 4162 else {
f7877b28 4163 if (isGV_with_GP(sstr)) {
180488f8
NC
4164 /* This stringification rule for globs is spread in 3 places.
4165 This feels bad. FIXME. */
4166 const U32 wasfake = sflags & SVf_FAKE;
4167
4168 /* FAKE globs can get coerced, so need to turn this off
4169 temporarily if it is on. */
4170 SvFAKE_off(sstr);
159b6efe 4171 gv_efullname3(dstr, MUTABLE_GV(sstr), "*");
180488f8
NC
4172 SvFLAGS(sstr) |= wasfake;
4173 }
20408e3c
GS
4174 else
4175 (void)SvOK_off(dstr);
a0d0e21e 4176 }
27c9684d
AP
4177 if (SvTAINTED(sstr))
4178 SvTAINT(dstr);
79072805
LW
4179}
4180
954c1994
GS
4181/*
4182=for apidoc sv_setsv_mg
4183
4184Like C<sv_setsv>, but also handles 'set' magic.
4185
4186=cut
4187*/
4188
79072805 4189void
7bc54cea 4190Perl_sv_setsv_mg(pTHX_ SV *const dstr, register SV *const sstr)
ef50df4b 4191{
7918f24d
NC
4192 PERL_ARGS_ASSERT_SV_SETSV_MG;
4193
ef50df4b
GS
4194 sv_setsv(dstr,sstr);
4195 SvSETMAGIC(dstr);
4196}
4197
f8c7b90f 4198#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
4199SV *
4200Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr)
4201{
4202 STRLEN cur = SvCUR(sstr);
4203 STRLEN len = SvLEN(sstr);
4204 register char *new_pv;
4205
7918f24d
NC
4206 PERL_ARGS_ASSERT_SV_SETSV_COW;
4207
ed252734
NC
4208 if (DEBUG_C_TEST) {
4209 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
6c9570dc 4210 (void*)sstr, (void*)dstr);
ed252734
NC
4211 sv_dump(sstr);
4212 if (dstr)
4213 sv_dump(dstr);
4214 }
4215
4216 if (dstr) {
4217 if (SvTHINKFIRST(dstr))
4218 sv_force_normal_flags(dstr, SV_COW_DROP_PV);
3f7c398e
SP
4219 else if (SvPVX_const(dstr))
4220 Safefree(SvPVX_const(dstr));
ed252734
NC
4221 }
4222 else
4223 new_SV(dstr);
862a34c6 4224 SvUPGRADE(dstr, SVt_PVIV);
ed252734
NC
4225
4226 assert (SvPOK(sstr));
4227 assert (SvPOKp(sstr));
4228 assert (!SvIOK(sstr));
4229 assert (!SvIOKp(sstr));
4230 assert (!SvNOK(sstr));
4231 assert (!SvNOKp(sstr));
4232
4233 if (SvIsCOW(sstr)) {
4234
4235 if (SvLEN(sstr) == 0) {
4236 /* source is a COW shared hash key. */
ed252734
NC
4237 DEBUG_C(PerlIO_printf(Perl_debug_log,
4238 "Fast copy on write: Sharing hash\n"));
d1db91c6 4239 new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr))));
ed252734
NC
4240 goto common_exit;
4241 }
4242 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4243 } else {
4244 assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS);
862a34c6 4245 SvUPGRADE(sstr, SVt_PVIV);
ed252734
NC
4246 SvREADONLY_on(sstr);
4247 SvFAKE_on(sstr);
4248 DEBUG_C(PerlIO_printf(Perl_debug_log,
4249 "Fast copy on write: Converting sstr to COW\n"));
4250 SV_COW_NEXT_SV_SET(dstr, sstr);
4251 }
4252 SV_COW_NEXT_SV_SET(sstr, dstr);
940132f3 4253 new_pv = SvPVX_mutable(sstr);
ed252734
NC
4254
4255 common_exit:
4256 SvPV_set(dstr, new_pv);
4257 SvFLAGS(dstr) = (SVt_PVIV|SVf_POK|SVp_POK|SVf_FAKE|SVf_READONLY);
4258 if (SvUTF8(sstr))
4259 SvUTF8_on(dstr);
87a1ef3d
SP
4260 SvLEN_set(dstr, len);
4261 SvCUR_set(dstr, cur);
ed252734
NC
4262 if (DEBUG_C_TEST) {
4263 sv_dump(dstr);
4264 }
4265 return dstr;
4266}
4267#endif
4268
954c1994
GS
4269/*
4270=for apidoc sv_setpvn
4271
4272Copies a string into an SV. The C<len> parameter indicates the number of
9e09f5f2
MHM
4273bytes to be copied. If the C<ptr> argument is NULL the SV will become
4274undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>.
954c1994
GS
4275
4276=cut
4277*/
4278
ef50df4b 4279void
2e000ff2 4280Perl_sv_setpvn(pTHX_ register SV *const sv, register const char *const ptr, register const STRLEN len)
79072805 4281{
97aff369 4282 dVAR;
c6f8c383 4283 register char *dptr;
22c522df 4284
7918f24d
NC
4285 PERL_ARGS_ASSERT_SV_SETPVN;
4286
765f542d 4287 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2 4288 if (!ptr) {
a0d0e21e 4289 (void)SvOK_off(sv);
463ee0b2
LW
4290 return;
4291 }
22c522df
JH
4292 else {
4293 /* len is STRLEN which is unsigned, need to copy to signed */
a3b680e6 4294 const IV iv = len;
9c5ffd7c
JH
4295 if (iv < 0)
4296 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
22c522df 4297 }
862a34c6 4298 SvUPGRADE(sv, SVt_PV);
c6f8c383 4299
5902b6a9 4300 dptr = SvGROW(sv, len + 1);
c6f8c383
GA
4301 Move(ptr,dptr,len,char);
4302 dptr[len] = '\0';
79072805 4303 SvCUR_set(sv, len);
1aa99e6b 4304 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4305 SvTAINT(sv);
79072805
LW
4306}
4307
954c1994
GS
4308/*
4309=for apidoc sv_setpvn_mg
4310
4311Like C<sv_setpvn>, but also handles 'set' magic.
4312
4313=cut
4314*/
4315
79072805 4316void
2e000ff2 4317Perl_sv_setpvn_mg(pTHX_ register SV *const sv, register const char *const ptr, register const STRLEN len)
ef50df4b 4318{
7918f24d
NC
4319 PERL_ARGS_ASSERT_SV_SETPVN_MG;
4320
ef50df4b
GS
4321 sv_setpvn(sv,ptr,len);
4322 SvSETMAGIC(sv);
4323}
4324
954c1994
GS
4325/*
4326=for apidoc sv_setpv
4327
4328Copies a string into an SV. The string must be null-terminated. Does not
4329handle 'set' magic. See C<sv_setpv_mg>.
4330
4331=cut
4332*/
4333
ef50df4b 4334void
2e000ff2 4335Perl_sv_setpv(pTHX_ register SV *const sv, register const char *const ptr)
79072805 4336{
97aff369 4337 dVAR;
79072805
LW
4338 register STRLEN len;
4339
7918f24d
NC
4340 PERL_ARGS_ASSERT_SV_SETPV;
4341
765f542d 4342 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2 4343 if (!ptr) {
a0d0e21e 4344 (void)SvOK_off(sv);
463ee0b2
LW
4345 return;
4346 }
79072805 4347 len = strlen(ptr);
862a34c6 4348 SvUPGRADE(sv, SVt_PV);
c6f8c383 4349
79072805 4350 SvGROW(sv, len + 1);
463ee0b2 4351 Move(ptr,SvPVX(sv),len+1,char);
79072805 4352 SvCUR_set(sv, len);
1aa99e6b 4353 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2
LW
4354 SvTAINT(sv);
4355}
4356
954c1994
GS
4357/*
4358=for apidoc sv_setpv_mg
4359
4360Like C<sv_setpv>, but also handles 'set' magic.
4361
4362=cut
4363*/
4364
463ee0b2 4365void
2e000ff2 4366Perl_sv_setpv_mg(pTHX_ register SV *const sv, register const char *const ptr)
ef50df4b 4367{
7918f24d
NC
4368 PERL_ARGS_ASSERT_SV_SETPV_MG;
4369
ef50df4b
GS
4370 sv_setpv(sv,ptr);
4371 SvSETMAGIC(sv);
4372}
4373
954c1994 4374/*
47518d95 4375=for apidoc sv_usepvn_flags
954c1994 4376
794a0d33
JH
4377Tells an SV to use C<ptr> to find its string value. Normally the
4378string is stored inside the SV but sv_usepvn allows the SV to use an
4379outside string. The C<ptr> should point to memory that was allocated
c1c21316
NC
4380by C<malloc>. The string length, C<len>, must be supplied. By default
4381this function will realloc (i.e. move) the memory pointed to by C<ptr>,
794a0d33
JH
4382so that pointer should not be freed or used by the programmer after
4383giving it to sv_usepvn, and neither should any pointers from "behind"
c1c21316
NC
4384that pointer (e.g. ptr + 1) be used.
4385
4386If C<flags> & SV_SMAGIC is true, will call SvSETMAGIC. If C<flags> &
4387SV_HAS_TRAILING_NUL is true, then C<ptr[len]> must be NUL, and the realloc
cbf82dd0 4388will be skipped. (i.e. the buffer is actually at least 1 byte longer than
c1c21316 4389C<len>, and already meets the requirements for storing in C<SvPVX>)
954c1994
GS
4390
4391=cut
4392*/
4393
ef50df4b 4394void
2e000ff2 4395Perl_sv_usepvn_flags(pTHX_ SV *const sv, char *ptr, const STRLEN len, const U32 flags)
463ee0b2 4396{
97aff369 4397 dVAR;
1936d2a7 4398 STRLEN allocate;
7918f24d
NC
4399
4400 PERL_ARGS_ASSERT_SV_USEPVN_FLAGS;
4401
765f542d 4402 SV_CHECK_THINKFIRST_COW_DROP(sv);
862a34c6 4403 SvUPGRADE(sv, SVt_PV);
463ee0b2 4404 if (!ptr) {
a0d0e21e 4405 (void)SvOK_off(sv);
47518d95
NC
4406 if (flags & SV_SMAGIC)
4407 SvSETMAGIC(sv);
463ee0b2
LW
4408 return;
4409 }
3f7c398e 4410 if (SvPVX_const(sv))
8bd4d4c5 4411 SvPV_free(sv);
1936d2a7 4412
0b7042f9 4413#ifdef DEBUGGING
2e90b4cd
NC
4414 if (flags & SV_HAS_TRAILING_NUL)
4415 assert(ptr[len] == '\0');
0b7042f9 4416#endif
2e90b4cd 4417
c1c21316 4418 allocate = (flags & SV_HAS_TRAILING_NUL)
5d487c26 4419 ? len + 1 :
ca7c1a29 4420#ifdef Perl_safesysmalloc_size
5d487c26
NC
4421 len + 1;
4422#else
4423 PERL_STRLEN_ROUNDUP(len + 1);
4424#endif
cbf82dd0
NC
4425 if (flags & SV_HAS_TRAILING_NUL) {
4426 /* It's long enough - do nothing.
4427 Specfically Perl_newCONSTSUB is relying on this. */
4428 } else {
69d25b4f 4429#ifdef DEBUGGING
69d25b4f 4430 /* Force a move to shake out bugs in callers. */
10edeb5d 4431 char *new_ptr = (char*)safemalloc(allocate);
69d25b4f
NC
4432 Copy(ptr, new_ptr, len, char);
4433 PoisonFree(ptr,len,char);
4434 Safefree(ptr);
4435 ptr = new_ptr;
69d25b4f 4436#else
10edeb5d 4437 ptr = (char*) saferealloc (ptr, allocate);
69d25b4f 4438#endif
cbf82dd0 4439 }
ca7c1a29
NC
4440#ifdef Perl_safesysmalloc_size
4441 SvLEN_set(sv, Perl_safesysmalloc_size(ptr));
5d487c26 4442#else
1936d2a7 4443 SvLEN_set(sv, allocate);
5d487c26
NC
4444#endif
4445 SvCUR_set(sv, len);
4446 SvPV_set(sv, ptr);
c1c21316 4447 if (!(flags & SV_HAS_TRAILING_NUL)) {
97a130b8 4448 ptr[len] = '\0';
c1c21316 4449 }
1aa99e6b 4450 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4451 SvTAINT(sv);
47518d95
NC
4452 if (flags & SV_SMAGIC)
4453 SvSETMAGIC(sv);
ef50df4b
GS
4454}
4455
f8c7b90f 4456#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
4457/* Need to do this *after* making the SV normal, as we need the buffer
4458 pointer to remain valid until after we've copied it. If we let go too early,
4459 another thread could invalidate it by unsharing last of the same hash key
4460 (which it can do by means other than releasing copy-on-write Svs)
4461 or by changing the other copy-on-write SVs in the loop. */
4462STATIC void
5302ffd4 4463S_sv_release_COW(pTHX_ register SV *sv, const char *pvx, SV *after)
765f542d 4464{
7918f24d
NC
4465 PERL_ARGS_ASSERT_SV_RELEASE_COW;
4466
5302ffd4 4467 { /* this SV was SvIsCOW_normal(sv) */
765f542d 4468 /* we need to find the SV pointing to us. */
cf5629ad 4469 SV *current = SV_COW_NEXT_SV(after);
7a5fa8a2 4470
765f542d
NC
4471 if (current == sv) {
4472 /* The SV we point to points back to us (there were only two of us
4473 in the loop.)
4474 Hence other SV is no longer copy on write either. */
4475 SvFAKE_off(after);
4476 SvREADONLY_off(after);
4477 } else {
4478 /* We need to follow the pointers around the loop. */
4479 SV *next;
4480 while ((next = SV_COW_NEXT_SV(current)) != sv) {
4481 assert (next);
4482 current = next;
4483 /* don't loop forever if the structure is bust, and we have
4484 a pointer into a closed loop. */
4485 assert (current != after);
3f7c398e 4486 assert (SvPVX_const(current) == pvx);
765f542d
NC
4487 }
4488 /* Make the SV before us point to the SV after us. */
a29f6d03 4489 SV_COW_NEXT_SV_SET(current, after);
765f542d 4490 }
765f542d
NC
4491 }
4492}
765f542d 4493#endif
645c22ef
DM
4494/*
4495=for apidoc sv_force_normal_flags
4496
4497Undo various types of fakery on an SV: if the PV is a shared string, make
4498a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
765f542d
NC
4499an xpvmg; if we're a copy-on-write scalar, this is the on-write time when
4500we do the copy, and is also used locally. If C<SV_COW_DROP_PV> is set
4501then a copy-on-write scalar drops its PV buffer (if any) and becomes
4502SvPOK_off rather than making a copy. (Used where this scalar is about to be
d3050d9d 4503set to some other value.) In addition, the C<flags> parameter gets passed to
765f542d
NC
4504C<sv_unref_flags()> when unrefing. C<sv_force_normal> calls this function
4505with flags set to 0.
645c22ef
DM
4506
4507=cut
4508*/
4509
6fc92669 4510void
2e000ff2 4511Perl_sv_force_normal_flags(pTHX_ register SV *const sv, const U32 flags)
0f15f207 4512{
97aff369 4513 dVAR;
7918f24d
NC
4514
4515 PERL_ARGS_ASSERT_SV_FORCE_NORMAL_FLAGS;
4516
f8c7b90f 4517#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 4518 if (SvREADONLY(sv)) {
765f542d 4519 if (SvFAKE(sv)) {
b64e5050 4520 const char * const pvx = SvPVX_const(sv);
a28509cc
AL
4521 const STRLEN len = SvLEN(sv);
4522 const STRLEN cur = SvCUR(sv);
5302ffd4
NC
4523 /* next COW sv in the loop. If len is 0 then this is a shared-hash
4524 key scalar, so we mustn't attempt to call SV_COW_NEXT_SV(), as
4525 we'll fail an assertion. */
4526 SV * const next = len ? SV_COW_NEXT_SV(sv) : 0;
4527
46187eeb
NC
4528 if (DEBUG_C_TEST) {
4529 PerlIO_printf(Perl_debug_log,
4530 "Copy on write: Force normal %ld\n",
4531 (long) flags);
e419cbc5 4532 sv_dump(sv);
46187eeb 4533 }
765f542d
NC
4534 SvFAKE_off(sv);
4535 SvREADONLY_off(sv);
9f653bb5 4536 /* This SV doesn't own the buffer, so need to Newx() a new one: */
6136c704 4537 SvPV_set(sv, NULL);
87a1ef3d 4538 SvLEN_set(sv, 0);
765f542d
NC
4539 if (flags & SV_COW_DROP_PV) {
4540 /* OK, so we don't need to copy our buffer. */
4541 SvPOK_off(sv);
4542 } else {
4543 SvGROW(sv, cur + 1);
4544 Move(pvx,SvPVX(sv),cur,char);
87a1ef3d 4545 SvCUR_set(sv, cur);
765f542d
NC
4546 *SvEND(sv) = '\0';
4547 }
5302ffd4
NC
4548 if (len) {
4549 sv_release_COW(sv, pvx, next);
4550 } else {
4551 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
4552 }
46187eeb 4553 if (DEBUG_C_TEST) {
e419cbc5 4554 sv_dump(sv);
46187eeb 4555 }
765f542d 4556 }
923e4eb5 4557 else if (IN_PERL_RUNTIME)
f1f66076 4558 Perl_croak(aTHX_ "%s", PL_no_modify);
765f542d
NC
4559 }
4560#else
2213622d 4561 if (SvREADONLY(sv)) {
1c846c1f 4562 if (SvFAKE(sv)) {
b64e5050 4563 const char * const pvx = SvPVX_const(sv);
66a1b24b 4564 const STRLEN len = SvCUR(sv);
10bcdfd6
NC
4565 SvFAKE_off(sv);
4566 SvREADONLY_off(sv);
bd61b366 4567 SvPV_set(sv, NULL);
66a1b24b 4568 SvLEN_set(sv, 0);
1c846c1f 4569 SvGROW(sv, len + 1);
706aa1c9 4570 Move(pvx,SvPVX(sv),len,char);
1c846c1f 4571 *SvEND(sv) = '\0';
bdd68bc3 4572 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
1c846c1f 4573 }
923e4eb5 4574 else if (IN_PERL_RUNTIME)
f1f66076 4575 Perl_croak(aTHX_ "%s", PL_no_modify);
0f15f207 4576 }
765f542d 4577#endif
2213622d 4578 if (SvROK(sv))
840a7b70 4579 sv_unref_flags(sv, flags);
6fc92669
GS
4580 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
4581 sv_unglob(sv);
0f15f207 4582}
1c846c1f 4583
645c22ef 4584/*
954c1994
GS
4585=for apidoc sv_chop
4586
1c846c1f 4587Efficient removal of characters from the beginning of the string buffer.
954c1994
GS
4588SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
4589the string buffer. The C<ptr> becomes the first character of the adjusted
645c22ef 4590string. Uses the "OOK hack".
3f7c398e 4591Beware: after this function returns, C<ptr> and SvPVX_const(sv) may no longer
31869a79 4592refer to the same chunk of data.
954c1994
GS
4593
4594=cut
4595*/
4596
79072805 4597void
2e000ff2 4598Perl_sv_chop(pTHX_ register SV *const sv, register const char *const ptr)
79072805 4599{
69240efd
NC
4600 STRLEN delta;
4601 STRLEN old_delta;
7a4bba22
NC
4602 U8 *p;
4603#ifdef DEBUGGING
4604 const U8 *real_start;
4605#endif
6c65d5f9 4606 STRLEN max_delta;
7a4bba22 4607
7918f24d
NC
4608 PERL_ARGS_ASSERT_SV_CHOP;
4609
a0d0e21e 4610 if (!ptr || !SvPOKp(sv))
79072805 4611 return;
3f7c398e 4612 delta = ptr - SvPVX_const(sv);
15895f8a
NC
4613 if (!delta) {
4614 /* Nothing to do. */
4615 return;
4616 }
6c65d5f9
NC
4617 /* SvPVX(sv) may move in SV_CHECK_THINKFIRST(sv), but after this line,
4618 nothing uses the value of ptr any more. */
837cb3ba 4619 max_delta = SvLEN(sv) ? SvLEN(sv) : SvCUR(sv);
6c65d5f9
NC
4620 if (ptr <= SvPVX_const(sv))
4621 Perl_croak(aTHX_ "panic: sv_chop ptr=%p, start=%p, end=%p",
4622 ptr, SvPVX_const(sv), SvPVX_const(sv) + max_delta);
2213622d 4623 SV_CHECK_THINKFIRST(sv);
6c65d5f9
NC
4624 if (delta > max_delta)
4625 Perl_croak(aTHX_ "panic: sv_chop ptr=%p (was %p), start=%p, end=%p",
4626 SvPVX_const(sv) + delta, ptr, SvPVX_const(sv),
4627 SvPVX_const(sv) + max_delta);
79072805
LW
4628
4629 if (!SvOOK(sv)) {
50483b2c 4630 if (!SvLEN(sv)) { /* make copy of shared string */
3f7c398e 4631 const char *pvx = SvPVX_const(sv);
a28509cc 4632 const STRLEN len = SvCUR(sv);
50483b2c 4633 SvGROW(sv, len + 1);
706aa1c9 4634 Move(pvx,SvPVX(sv),len,char);
50483b2c
JD
4635 *SvEND(sv) = '\0';
4636 }
7a5fa8a2 4637 SvFLAGS(sv) |= SVf_OOK;
7a4bba22
NC
4638 old_delta = 0;
4639 } else {
69240efd 4640 SvOOK_offset(sv, old_delta);
79072805 4641 }
b162af07
SP
4642 SvLEN_set(sv, SvLEN(sv) - delta);
4643 SvCUR_set(sv, SvCUR(sv) - delta);
f880fe2f 4644 SvPV_set(sv, SvPVX(sv) + delta);
7a4bba22
NC
4645
4646 p = (U8 *)SvPVX_const(sv);
4647
4648 delta += old_delta;
4649
50af2e61 4650#ifdef DEBUGGING
7a4bba22
NC
4651 real_start = p - delta;
4652#endif
4653
69240efd
NC
4654 assert(delta);
4655 if (delta < 0x100) {
7a4bba22
NC
4656 *--p = (U8) delta;
4657 } else {
69240efd
NC
4658 *--p = 0;
4659 p -= sizeof(STRLEN);
4660 Copy((U8*)&delta, p, sizeof(STRLEN), U8);
7a4bba22
NC
4661 }
4662
4663#ifdef DEBUGGING
4664 /* Fill the preceding buffer with sentinals to verify that no-one is
4665 using it. */
4666 while (p > real_start) {
4667 --p;
4668 *p = (U8)PTR2UV(p);
50af2e61
NC
4669 }
4670#endif
79072805
LW
4671}
4672
954c1994
GS
4673/*
4674=for apidoc sv_catpvn
4675
4676Concatenates the string onto the end of the string which is in the SV. The
1e54db1a
JH
4677C<len> indicates number of bytes to copy. If the SV has the UTF-8
4678status set, then the bytes appended should be valid UTF-8.
d5ce4a7c 4679Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>.
954c1994 4680
8d6d96c1
HS
4681=for apidoc sv_catpvn_flags
4682
4683Concatenates the string onto the end of the string which is in the SV. The
1e54db1a
JH
4684C<len> indicates number of bytes to copy. If the SV has the UTF-8
4685status set, then the bytes appended should be valid UTF-8.
8d6d96c1
HS
4686If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if
4687appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented
4688in terms of this function.
4689
4690=cut
4691*/
4692
4693void
2e000ff2 4694Perl_sv_catpvn_flags(pTHX_ register SV *const dsv, register const char *sstr, register const STRLEN slen, const I32 flags)
8d6d96c1 4695{
97aff369 4696 dVAR;
8d6d96c1 4697 STRLEN dlen;
fabdb6c0 4698 const char * const dstr = SvPV_force_flags(dsv, dlen, flags);
8d6d96c1 4699
7918f24d
NC
4700 PERL_ARGS_ASSERT_SV_CATPVN_FLAGS;
4701
8d6d96c1
HS
4702 SvGROW(dsv, dlen + slen + 1);
4703 if (sstr == dstr)
3f7c398e 4704 sstr = SvPVX_const(dsv);
8d6d96c1 4705 Move(sstr, SvPVX(dsv) + dlen, slen, char);
b162af07 4706 SvCUR_set(dsv, SvCUR(dsv) + slen);
8d6d96c1
HS
4707 *SvEND(dsv) = '\0';
4708 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
4709 SvTAINT(dsv);
bddd5118
NC
4710 if (flags & SV_SMAGIC)
4711 SvSETMAGIC(dsv);
79072805
LW
4712}
4713
954c1994 4714/*
954c1994
GS
4715=for apidoc sv_catsv
4716
13e8c8e3
JH
4717Concatenates the string from SV C<ssv> onto the end of the string in
4718SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
4719not 'set' magic. See C<sv_catsv_mg>.
954c1994 4720
8d6d96c1
HS
4721=for apidoc sv_catsv_flags
4722
4723Concatenates the string from SV C<ssv> onto the end of the string in
4724SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC>
4725bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv>
4726and C<sv_catsv_nomg> are implemented in terms of this function.
4727
4728=cut */
4729
ef50df4b 4730void
2e000ff2 4731Perl_sv_catsv_flags(pTHX_ SV *const dsv, register SV *const ssv, const I32 flags)
79072805 4732{
97aff369 4733 dVAR;
7918f24d
NC
4734
4735 PERL_ARGS_ASSERT_SV_CATSV_FLAGS;
4736
4737 if (ssv) {
00b6aa41
AL
4738 STRLEN slen;
4739 const char *spv = SvPV_const(ssv, slen);
4740 if (spv) {
bddd5118
NC
4741 /* sutf8 and dutf8 were type bool, but under USE_ITHREADS,
4742 gcc version 2.95.2 20000220 (Debian GNU/Linux) for
4743 Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously
4744 get dutf8 = 0x20000000, (i.e. SVf_UTF8) even though
4745 dsv->sv_flags doesn't have that bit set.
4fd84b44 4746 Andy Dougherty 12 Oct 2001
bddd5118
NC
4747 */
4748 const I32 sutf8 = DO_UTF8(ssv);
4749 I32 dutf8;
13e8c8e3 4750
bddd5118
NC
4751 if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC))
4752 mg_get(dsv);
4753 dutf8 = DO_UTF8(dsv);
8d6d96c1 4754
bddd5118
NC
4755 if (dutf8 != sutf8) {
4756 if (dutf8) {
4757 /* Not modifying source SV, so taking a temporary copy. */
59cd0e26 4758 SV* const csv = newSVpvn_flags(spv, slen, SVs_TEMP);
13e8c8e3 4759
bddd5118
NC
4760 sv_utf8_upgrade(csv);
4761 spv = SvPV_const(csv, slen);
4762 }
4763 else
7bf79863
KW
4764 /* Leave enough space for the cat that's about to happen */
4765 sv_utf8_upgrade_flags_grow(dsv, 0, slen);
13e8c8e3 4766 }
bddd5118 4767 sv_catpvn_nomg(dsv, spv, slen);
e84ff256 4768 }
560a288e 4769 }
bddd5118
NC
4770 if (flags & SV_SMAGIC)
4771 SvSETMAGIC(dsv);
79072805
LW
4772}
4773
954c1994 4774/*
954c1994
GS
4775=for apidoc sv_catpv
4776
4777Concatenates the string onto the end of the string which is in the SV.
1e54db1a
JH
4778If the SV has the UTF-8 status set, then the bytes appended should be
4779valid UTF-8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
954c1994 4780
d5ce4a7c 4781=cut */
954c1994 4782
ef50df4b 4783void
2b021c53 4784Perl_sv_catpv(pTHX_ register SV *const sv, register const char *ptr)
79072805 4785{
97aff369 4786 dVAR;
79072805 4787 register STRLEN len;
463ee0b2 4788 STRLEN tlen;
748a9306 4789 char *junk;
79072805 4790
7918f24d
NC
4791 PERL_ARGS_ASSERT_SV_CATPV;
4792
0c981600 4793 if (!ptr)
79072805 4794 return;
748a9306 4795 junk = SvPV_force(sv, tlen);
0c981600 4796 len = strlen(ptr);
463ee0b2 4797 SvGROW(sv, tlen + len + 1);
0c981600 4798 if (ptr == junk)
3f7c398e 4799 ptr = SvPVX_const(sv);
0c981600 4800 Move(ptr,SvPVX(sv)+tlen,len+1,char);
b162af07 4801 SvCUR_set(sv, SvCUR(sv) + len);
d41ff1b8 4802 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4803 SvTAINT(sv);
79072805
LW
4804}
4805
954c1994
GS
4806/*
4807=for apidoc sv_catpv_mg
4808
4809Like C<sv_catpv>, but also handles 'set' magic.
4810
4811=cut
4812*/
4813
ef50df4b 4814void
2b021c53 4815Perl_sv_catpv_mg(pTHX_ register SV *const sv, register const char *const ptr)
ef50df4b 4816{
7918f24d
NC
4817 PERL_ARGS_ASSERT_SV_CATPV_MG;
4818
0c981600 4819 sv_catpv(sv,ptr);
ef50df4b
GS
4820 SvSETMAGIC(sv);
4821}
4822
645c22ef
DM
4823/*
4824=for apidoc newSV
4825
561b68a9
SH
4826Creates a new SV. A non-zero C<len> parameter indicates the number of
4827bytes of preallocated string space the SV should have. An extra byte for a
4828trailing NUL is also reserved. (SvPOK is not set for the SV even if string
4829space is allocated.) The reference count for the new SV is set to 1.
4830
4831In 5.9.3, newSV() replaces the older NEWSV() API, and drops the first
4832parameter, I<x>, a debug aid which allowed callers to identify themselves.
4833This aid has been superseded by a new build option, PERL_MEM_LOG (see
4834L<perlhack/PERL_MEM_LOG>). The older API is still there for use in XS
4835modules supporting older perls.
645c22ef
DM
4836
4837=cut
4838*/
4839
79072805 4840SV *
2b021c53 4841Perl_newSV(pTHX_ const STRLEN len)
79072805 4842{
97aff369 4843 dVAR;
79072805 4844 register SV *sv;
1c846c1f 4845
4561caa4 4846 new_SV(sv);
79072805
LW
4847 if (len) {
4848 sv_upgrade(sv, SVt_PV);
4849 SvGROW(sv, len + 1);
4850 }
4851 return sv;
4852}
954c1994 4853/*
92110913 4854=for apidoc sv_magicext
954c1994 4855
68795e93 4856Adds magic to an SV, upgrading it if necessary. Applies the
2d8d5d5a 4857supplied vtable and returns a pointer to the magic added.
92110913 4858
2d8d5d5a
SH
4859Note that C<sv_magicext> will allow things that C<sv_magic> will not.
4860In particular, you can add magic to SvREADONLY SVs, and add more than
4861one instance of the same 'how'.
645c22ef 4862
2d8d5d5a
SH
4863If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
4864stored, if C<namlen> is zero then C<name> is stored as-is and - as another
4865special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
4866to contain an C<SV*> and is stored as-is with its REFCNT incremented.
92110913 4867
2d8d5d5a 4868(This is now used as a subroutine by C<sv_magic>.)
954c1994
GS
4869
4870=cut
4871*/
92110913 4872MAGIC *
2b021c53
SS
4873Perl_sv_magicext(pTHX_ SV *const sv, SV *const obj, const int how,
4874 const MGVTBL *const vtable, const char *const name, const I32 namlen)
79072805 4875{
97aff369 4876 dVAR;
79072805 4877 MAGIC* mg;
68795e93 4878
7918f24d
NC
4879 PERL_ARGS_ASSERT_SV_MAGICEXT;
4880
7a7f3e45 4881 SvUPGRADE(sv, SVt_PVMG);
a02a5408 4882 Newxz(mg, 1, MAGIC);
79072805 4883 mg->mg_moremagic = SvMAGIC(sv);
b162af07 4884 SvMAGIC_set(sv, mg);
75f9d97a 4885
05f95b08
SB
4886 /* Sometimes a magic contains a reference loop, where the sv and
4887 object refer to each other. To prevent a reference loop that
4888 would prevent such objects being freed, we look for such loops
4889 and if we find one we avoid incrementing the object refcount.
87f0b213
JH
4890
4891 Note we cannot do this to avoid self-tie loops as intervening RV must
b5ccf5f2 4892 have its REFCNT incremented to keep it in existence.
87f0b213
JH
4893
4894 */
14befaf4
DM
4895 if (!obj || obj == sv ||
4896 how == PERL_MAGIC_arylen ||
8d2f4536 4897 how == PERL_MAGIC_symtab ||
75f9d97a 4898 (SvTYPE(obj) == SVt_PVGV &&
4c4652b6
NC
4899 (GvSV(obj) == sv || GvHV(obj) == (const HV *)sv
4900 || GvAV(obj) == (const AV *)sv || GvCV(obj) == (const CV *)sv
4901 || GvIOp(obj) == (const IO *)sv || GvFORM(obj) == (const CV *)sv)))
75f9d97a 4902 {
8990e307 4903 mg->mg_obj = obj;
75f9d97a 4904 }
85e6fe83 4905 else {
b37c2d43 4906 mg->mg_obj = SvREFCNT_inc_simple(obj);
85e6fe83
LW
4907 mg->mg_flags |= MGf_REFCOUNTED;
4908 }
b5ccf5f2
YST
4909
4910 /* Normal self-ties simply pass a null object, and instead of
4911 using mg_obj directly, use the SvTIED_obj macro to produce a
4912 new RV as needed. For glob "self-ties", we are tieing the PVIO
4913 with an RV obj pointing to the glob containing the PVIO. In
4914 this case, to avoid a reference loop, we need to weaken the
4915 reference.
4916 */
4917
4918 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
a45c7426 4919 obj && SvROK(obj) && GvIO(SvRV(obj)) == (const IO *)sv)
b5ccf5f2
YST
4920 {
4921 sv_rvweaken(obj);
4922 }
4923
79072805 4924 mg->mg_type = how;
565764a8 4925 mg->mg_len = namlen;
9cbac4c7 4926 if (name) {
92110913 4927 if (namlen > 0)
1edc1566 4928 mg->mg_ptr = savepvn(name, namlen);
daba3364
NC
4929 else if (namlen == HEf_SVKEY) {
4930 /* Yes, this is casting away const. This is only for the case of
4931 HEf_SVKEY. I think we need to document this abberation of the
4932 constness of the API, rather than making name non-const, as
4933 that change propagating outwards a long way. */
4934 mg->mg_ptr = (char*)SvREFCNT_inc_simple_NN((SV *)name);
4935 } else
92110913 4936 mg->mg_ptr = (char *) name;
9cbac4c7 4937 }
53d44271 4938 mg->mg_virtual = (MGVTBL *) vtable;
68795e93 4939
92110913
NIS
4940 mg_magical(sv);
4941 if (SvGMAGICAL(sv))
4942 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
4943 return mg;
4944}
4945
4946/*
4947=for apidoc sv_magic
1c846c1f 4948
92110913
NIS
4949Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary,
4950then adds a new magic item of type C<how> to the head of the magic list.
4951
2d8d5d5a
SH
4952See C<sv_magicext> (which C<sv_magic> now calls) for a description of the
4953handling of the C<name> and C<namlen> arguments.
4954
4509d3fb
SB
4955You need to use C<sv_magicext> to add magic to SvREADONLY SVs and also
4956to add more than one instance of the same 'how'.
4957
92110913
NIS
4958=cut
4959*/
4960
4961void
2b021c53
SS
4962Perl_sv_magic(pTHX_ register SV *const sv, SV *const obj, const int how,
4963 const char *const name, const I32 namlen)
68795e93 4964{
97aff369 4965 dVAR;
53d44271 4966 const MGVTBL *vtable;
92110913 4967 MAGIC* mg;
92110913 4968
7918f24d
NC
4969 PERL_ARGS_ASSERT_SV_MAGIC;
4970
f8c7b90f 4971#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
4972 if (SvIsCOW(sv))
4973 sv_force_normal_flags(sv, 0);
4974#endif
92110913 4975 if (SvREADONLY(sv)) {
d8084ca5
DM
4976 if (
4977 /* its okay to attach magic to shared strings; the subsequent
4978 * upgrade to PVMG will unshare the string */
4979 !(SvFAKE(sv) && SvTYPE(sv) < SVt_PVMG)
4980
4981 && IN_PERL_RUNTIME
92110913
NIS
4982 && how != PERL_MAGIC_regex_global
4983 && how != PERL_MAGIC_bm
4984 && how != PERL_MAGIC_fm
4985 && how != PERL_MAGIC_sv
e6469971 4986 && how != PERL_MAGIC_backref
92110913
NIS
4987 )
4988 {
f1f66076 4989 Perl_croak(aTHX_ "%s", PL_no_modify);
92110913
NIS
4990 }
4991 }
4992 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
4993 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
68795e93
NIS
4994 /* sv_magic() refuses to add a magic of the same 'how' as an
4995 existing one
92110913 4996 */
2a509ed3 4997 if (how == PERL_MAGIC_taint) {
92110913 4998 mg->mg_len |= 1;
2a509ed3
NC
4999 /* Any scalar which already had taint magic on which someone
5000 (erroneously?) did SvIOK_on() or similar will now be
5001 incorrectly sporting public "OK" flags. */
5002 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
5003 }
92110913
NIS
5004 return;
5005 }
5006 }
68795e93 5007
79072805 5008 switch (how) {
14befaf4 5009 case PERL_MAGIC_sv:
92110913 5010 vtable = &PL_vtbl_sv;
79072805 5011 break;
14befaf4 5012 case PERL_MAGIC_overload:
92110913 5013 vtable = &PL_vtbl_amagic;
a0d0e21e 5014 break;
14befaf4 5015 case PERL_MAGIC_overload_elem:
92110913 5016 vtable = &PL_vtbl_amagicelem;
a0d0e21e 5017 break;
14befaf4 5018 case PERL_MAGIC_overload_table:
92110913 5019 vtable = &PL_vtbl_ovrld;
a0d0e21e 5020 break;
14befaf4 5021 case PERL_MAGIC_bm:
92110913 5022 vtable = &PL_vtbl_bm;
79072805 5023 break;
14befaf4 5024 case PERL_MAGIC_regdata:
92110913 5025 vtable = &PL_vtbl_regdata;
6cef1e77 5026 break;
14befaf4 5027 case PERL_MAGIC_regdatum:
92110913 5028 vtable = &PL_vtbl_regdatum;
6cef1e77 5029 break;
14befaf4 5030 case PERL_MAGIC_env:
92110913 5031 vtable = &PL_vtbl_env;
79072805 5032 break;
14befaf4 5033 case PERL_MAGIC_fm:
92110913 5034 vtable = &PL_vtbl_fm;
55497cff 5035 break;
14befaf4 5036 case PERL_MAGIC_envelem:
92110913 5037 vtable = &PL_vtbl_envelem;
79072805 5038 break;
14befaf4 5039 case PERL_MAGIC_regex_global:
92110913 5040 vtable = &PL_vtbl_mglob;
93a17b20 5041 break;
14befaf4 5042 case PERL_MAGIC_isa:
92110913 5043 vtable = &PL_vtbl_isa;
463ee0b2 5044 break;
14befaf4 5045 case PERL_MAGIC_isaelem:
92110913 5046 vtable = &PL_vtbl_isaelem;
463ee0b2 5047 break;
14befaf4 5048 case PERL_MAGIC_nkeys:
92110913 5049 vtable = &PL_vtbl_nkeys;
16660edb 5050 break;
14befaf4 5051 case PERL_MAGIC_dbfile:
aec46f14 5052 vtable = NULL;
93a17b20 5053 break;
14befaf4 5054 case PERL_MAGIC_dbline:
92110913 5055 vtable = &PL_vtbl_dbline;
79072805 5056 break;
36477c24 5057#ifdef USE_LOCALE_COLLATE
14befaf4 5058 case PERL_MAGIC_collxfrm:
92110913 5059 vtable = &PL_vtbl_collxfrm;
bbce6d69 5060 break;
36477c24 5061#endif /* USE_LOCALE_COLLATE */
14befaf4 5062 case PERL_MAGIC_tied:
92110913 5063 vtable = &PL_vtbl_pack;
463ee0b2 5064 break;
14befaf4
DM
5065 case PERL_MAGIC_tiedelem:
5066 case PERL_MAGIC_tiedscalar:
92110913 5067 vtable = &PL_vtbl_packelem;
463ee0b2 5068 break;
14befaf4 5069 case PERL_MAGIC_qr:
92110913 5070 vtable = &PL_vtbl_regexp;
c277df42 5071 break;
b3ca2e83
NC
5072 case PERL_MAGIC_hints:
5073 /* As this vtable is all NULL, we can reuse it. */
14befaf4 5074 case PERL_MAGIC_sig:
92110913 5075 vtable = &PL_vtbl_sig;
79072805 5076 break;
14befaf4 5077 case PERL_MAGIC_sigelem:
92110913 5078 vtable = &PL_vtbl_sigelem;
79072805 5079 break;
14befaf4 5080 case PERL_MAGIC_taint:
92110913 5081 vtable = &PL_vtbl_taint;
463ee0b2 5082 break;
14befaf4 5083 case PERL_MAGIC_uvar:
92110913 5084 vtable = &PL_vtbl_uvar;
79072805 5085 break;
14befaf4 5086 case PERL_MAGIC_vec:
92110913 5087 vtable = &PL_vtbl_vec;
79072805 5088 break;
a3874608 5089 case PERL_MAGIC_arylen_p:
bfcb3514 5090 case PERL_MAGIC_rhash:
8d2f4536 5091 case PERL_MAGIC_symtab:
ece467f9 5092 case PERL_MAGIC_vstring:
aec46f14 5093 vtable = NULL;
ece467f9 5094 break;
7e8c5dac
HS
5095 case PERL_MAGIC_utf8:
5096 vtable = &PL_vtbl_utf8;
5097 break;
14befaf4 5098 case PERL_MAGIC_substr:
92110913 5099 vtable = &PL_vtbl_substr;
79072805 5100 break;
14befaf4 5101 case PERL_MAGIC_defelem:
92110913 5102 vtable = &PL_vtbl_defelem;
5f05dabc 5103 break;
14befaf4 5104 case PERL_MAGIC_arylen:
92110913 5105 vtable = &PL_vtbl_arylen;
79072805 5106 break;
14befaf4 5107 case PERL_MAGIC_pos:
92110913 5108 vtable = &PL_vtbl_pos;
a0d0e21e 5109 break;
14befaf4 5110 case PERL_MAGIC_backref:
92110913 5111 vtable = &PL_vtbl_backref;
810b8aa5 5112 break;
b3ca2e83
NC
5113 case PERL_MAGIC_hintselem:
5114 vtable = &PL_vtbl_hintselem;
5115 break;
14befaf4
DM
5116 case PERL_MAGIC_ext:
5117 /* Reserved for use by extensions not perl internals. */
4633a7c4
LW
5118 /* Useful for attaching extension internal data to perl vars. */
5119 /* Note that multiple extensions may clash if magical scalars */
5120 /* etc holding private data from one are passed to another. */
aec46f14 5121 vtable = NULL;
a0d0e21e 5122 break;
79072805 5123 default:
14befaf4 5124 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
463ee0b2 5125 }
68795e93 5126
92110913 5127 /* Rest of work is done else where */
aec46f14 5128 mg = sv_magicext(sv,obj,how,vtable,name,namlen);
68795e93 5129
92110913
NIS
5130 switch (how) {
5131 case PERL_MAGIC_taint:
5132 mg->mg_len = 1;
5133 break;
5134 case PERL_MAGIC_ext:
5135 case PERL_MAGIC_dbfile:
5136 SvRMAGICAL_on(sv);
5137 break;
5138 }
463ee0b2
LW
5139}
5140
c461cf8f
JH
5141/*
5142=for apidoc sv_unmagic
5143
645c22ef 5144Removes all magic of type C<type> from an SV.
c461cf8f
JH
5145
5146=cut
5147*/
5148
463ee0b2 5149int
2b021c53 5150Perl_sv_unmagic(pTHX_ SV *const sv, const int type)
463ee0b2
LW
5151{
5152 MAGIC* mg;
5153 MAGIC** mgp;
7918f24d
NC
5154
5155 PERL_ARGS_ASSERT_SV_UNMAGIC;
5156
91bba347 5157 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
463ee0b2 5158 return 0;
064cf529 5159 mgp = &(((XPVMG*) SvANY(sv))->xmg_u.xmg_magic);
463ee0b2
LW
5160 for (mg = *mgp; mg; mg = *mgp) {
5161 if (mg->mg_type == type) {
e1ec3a88 5162 const MGVTBL* const vtbl = mg->mg_virtual;
463ee0b2 5163 *mgp = mg->mg_moremagic;
1d7c1841 5164 if (vtbl && vtbl->svt_free)
fc0dc3b3 5165 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
14befaf4 5166 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
92110913 5167 if (mg->mg_len > 0)
1edc1566 5168 Safefree(mg->mg_ptr);
565764a8 5169 else if (mg->mg_len == HEf_SVKEY)
daba3364 5170 SvREFCNT_dec(MUTABLE_SV(mg->mg_ptr));
d2923cdd 5171 else if (mg->mg_type == PERL_MAGIC_utf8)
7e8c5dac 5172 Safefree(mg->mg_ptr);
9cbac4c7 5173 }
a0d0e21e
LW
5174 if (mg->mg_flags & MGf_REFCOUNTED)
5175 SvREFCNT_dec(mg->mg_obj);
463ee0b2
LW
5176 Safefree(mg);
5177 }
5178 else
5179 mgp = &mg->mg_moremagic;
79072805 5180 }
91bba347 5181 if (!SvMAGIC(sv)) {
463ee0b2 5182 SvMAGICAL_off(sv);
c268c2a6 5183 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
86f55936 5184 SvMAGIC_set(sv, NULL);
463ee0b2
LW
5185 }
5186
5187 return 0;
79072805
LW
5188}
5189
c461cf8f
JH
5190/*
5191=for apidoc sv_rvweaken
5192
645c22ef
DM
5193Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
5194referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
5195push a back-reference to this RV onto the array of backreferences
1e73acc8
AS
5196associated with that magic. If the RV is magical, set magic will be
5197called after the RV is cleared.
c461cf8f
JH
5198
5199=cut
5200*/
5201
810b8aa5 5202SV *
2b021c53 5203Perl_sv_rvweaken(pTHX_ SV *const sv)
810b8aa5
GS
5204{
5205 SV *tsv;
7918f24d
NC
5206
5207 PERL_ARGS_ASSERT_SV_RVWEAKEN;
5208
810b8aa5
GS
5209 if (!SvOK(sv)) /* let undefs pass */
5210 return sv;
5211 if (!SvROK(sv))
cea2e8a9 5212 Perl_croak(aTHX_ "Can't weaken a nonreference");
810b8aa5 5213 else if (SvWEAKREF(sv)) {
810b8aa5 5214 if (ckWARN(WARN_MISC))
9014280d 5215 Perl_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
810b8aa5
GS
5216 return sv;
5217 }
5218 tsv = SvRV(sv);
e15faf7d 5219 Perl_sv_add_backref(aTHX_ tsv, sv);
810b8aa5 5220 SvWEAKREF_on(sv);
1c846c1f 5221 SvREFCNT_dec(tsv);
810b8aa5
GS
5222 return sv;
5223}
5224
645c22ef
DM
5225/* Give tsv backref magic if it hasn't already got it, then push a
5226 * back-reference to sv onto the array associated with the backref magic.
5227 */
5228
fd996479
DM
5229/* A discussion about the backreferences array and its refcount:
5230 *
5231 * The AV holding the backreferences is pointed to either as the mg_obj of
5232 * PERL_MAGIC_backref, or in the specific case of a HV that has the hv_aux
5233 * structure, from the xhv_backreferences field. (A HV without hv_aux will
5234 * have the standard magic instead.) The array is created with a refcount
5235 * of 2. This means that if during global destruction the array gets
5236 * picked on first to have its refcount decremented by the random zapper,
5237 * it won't actually be freed, meaning it's still theere for when its
5238 * parent gets freed.
5239 * When the parent SV is freed, in the case of magic, the magic is freed,
5240 * Perl_magic_killbackrefs is called which decrements one refcount, then
5241 * mg_obj is freed which kills the second count.
5242 * In the vase of a HV being freed, one ref is removed by
5243 * Perl_hv_kill_backrefs, the other by Perl_sv_kill_backrefs, which it
5244 * calls.
5245 */
5246
e15faf7d 5247void
2b021c53 5248Perl_sv_add_backref(pTHX_ SV *const tsv, SV *const sv)
810b8aa5 5249{
97aff369 5250 dVAR;
810b8aa5 5251 AV *av;
86f55936 5252
7918f24d
NC
5253 PERL_ARGS_ASSERT_SV_ADD_BACKREF;
5254
86f55936 5255 if (SvTYPE(tsv) == SVt_PVHV) {
85fbaab2 5256 AV **const avp = Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv));
86f55936
NC
5257
5258 av = *avp;
5259 if (!av) {
5260 /* There is no AV in the offical place - try a fixup. */
5261 MAGIC *const mg = mg_find(tsv, PERL_MAGIC_backref);
5262
5263 if (mg) {
5264 /* Aha. They've got it stowed in magic. Bring it back. */
502c6561 5265 av = MUTABLE_AV(mg->mg_obj);
86f55936
NC
5266 /* Stop mg_free decreasing the refernce count. */
5267 mg->mg_obj = NULL;
5268 /* Stop mg_free even calling the destructor, given that
5269 there's no AV to free up. */
5270 mg->mg_virtual = 0;
5271 sv_unmagic(tsv, PERL_MAGIC_backref);
5272 } else {
5273 av = newAV();
5274 AvREAL_off(av);
fd996479 5275 SvREFCNT_inc_simple_void(av); /* see discussion above */
86f55936
NC
5276 }
5277 *avp = av;
5278 }
5279 } else {
5280 const MAGIC *const mg
5281 = SvMAGICAL(tsv) ? mg_find(tsv, PERL_MAGIC_backref) : NULL;
5282 if (mg)
502c6561 5283 av = MUTABLE_AV(mg->mg_obj);
86f55936
NC
5284 else {
5285 av = newAV();
5286 AvREAL_off(av);
daba3364 5287 sv_magic(tsv, MUTABLE_SV(av), PERL_MAGIC_backref, NULL, 0);
fd996479 5288 /* av now has a refcnt of 2; see discussion above */
86f55936 5289 }
810b8aa5 5290 }
d91d49e8 5291 if (AvFILLp(av) >= AvMAX(av)) {
d91d49e8
MM
5292 av_extend(av, AvFILLp(av)+1);
5293 }
5294 AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
810b8aa5
GS
5295}
5296
645c22ef
DM
5297/* delete a back-reference to ourselves from the backref magic associated
5298 * with the SV we point to.
5299 */
5300
1c846c1f 5301STATIC void
2b021c53 5302S_sv_del_backref(pTHX_ SV *const tsv, SV *const sv)
810b8aa5 5303{
97aff369 5304 dVAR;
86f55936 5305 AV *av = NULL;
810b8aa5
GS
5306 SV **svp;
5307 I32 i;
86f55936 5308
7918f24d
NC
5309 PERL_ARGS_ASSERT_SV_DEL_BACKREF;
5310
86f55936 5311 if (SvTYPE(tsv) == SVt_PVHV && SvOOK(tsv)) {
85fbaab2 5312 av = *Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv));
5b285ea4
NC
5313 /* We mustn't attempt to "fix up" the hash here by moving the
5314 backreference array back to the hv_aux structure, as that is stored
5315 in the main HvARRAY(), and hfreentries assumes that no-one
5316 reallocates HvARRAY() while it is running. */
86f55936
NC
5317 }
5318 if (!av) {
5319 const MAGIC *const mg
5320 = SvMAGICAL(tsv) ? mg_find(tsv, PERL_MAGIC_backref) : NULL;
5321 if (mg)
502c6561 5322 av = MUTABLE_AV(mg->mg_obj);
86f55936 5323 }
41fae7a1
DM
5324
5325 if (!av)
cea2e8a9 5326 Perl_croak(aTHX_ "panic: del_backref");
86f55936 5327
41fae7a1 5328 assert(!SvIS_FREED(av));
86f55936 5329
810b8aa5 5330 svp = AvARRAY(av);
6a76db8b
NC
5331 /* We shouldn't be in here more than once, but for paranoia reasons lets
5332 not assume this. */
5333 for (i = AvFILLp(av); i >= 0; i--) {
5334 if (svp[i] == sv) {
5335 const SSize_t fill = AvFILLp(av);
5336 if (i != fill) {
5337 /* We weren't the last entry.
5338 An unordered list has this property that you can take the
5339 last element off the end to fill the hole, and it's still
5340 an unordered list :-)
5341 */
5342 svp[i] = svp[fill];
5343 }
a0714e2c 5344 svp[fill] = NULL;
6a76db8b
NC
5345 AvFILLp(av) = fill - 1;
5346 }
5347 }
810b8aa5
GS
5348}
5349
86f55936 5350int
2b021c53 5351Perl_sv_kill_backrefs(pTHX_ SV *const sv, AV *const av)
86f55936
NC
5352{
5353 SV **svp = AvARRAY(av);
5354
7918f24d 5355 PERL_ARGS_ASSERT_SV_KILL_BACKREFS;
86f55936
NC
5356 PERL_UNUSED_ARG(sv);
5357
41fae7a1
DM
5358 assert(!svp || !SvIS_FREED(av));
5359 if (svp) {
86f55936
NC
5360 SV *const *const last = svp + AvFILLp(av);
5361
5362 while (svp <= last) {
5363 if (*svp) {
5364 SV *const referrer = *svp;
5365 if (SvWEAKREF(referrer)) {
5366 /* XXX Should we check that it hasn't changed? */
5367 SvRV_set(referrer, 0);
5368 SvOK_off(referrer);
5369 SvWEAKREF_off(referrer);
1e73acc8 5370 SvSETMAGIC(referrer);
86f55936
NC
5371 } else if (SvTYPE(referrer) == SVt_PVGV ||
5372 SvTYPE(referrer) == SVt_PVLV) {
5373 /* You lookin' at me? */
5374 assert(GvSTASH(referrer));
1d193675 5375 assert(GvSTASH(referrer) == (const HV *)sv);
86f55936
NC
5376 GvSTASH(referrer) = 0;
5377 } else {
5378 Perl_croak(aTHX_
5379 "panic: magic_killbackrefs (flags=%"UVxf")",
5380 (UV)SvFLAGS(referrer));
5381 }
5382
a0714e2c 5383 *svp = NULL;
86f55936
NC
5384 }
5385 svp++;
5386 }
5387 }
5388 SvREFCNT_dec(av); /* remove extra count added by sv_add_backref() */
5389 return 0;
5390}
5391
954c1994
GS
5392/*
5393=for apidoc sv_insert
5394
5395Inserts a string at the specified offset/length within the SV. Similar to
c0dd94a0 5396the Perl substr() function. Handles get magic.
954c1994 5397
c0dd94a0
VP
5398=for apidoc sv_insert_flags
5399
5400Same as C<sv_insert>, but the extra C<flags> are passed the C<SvPV_force_flags> that applies to C<bigstr>.
5401
5402=cut
5403*/
5404
5405void
5406Perl_sv_insert_flags(pTHX_ SV *const bigstr, const STRLEN offset, const STRLEN len, const char *const little, const STRLEN littlelen, const U32 flags)
5407{
97aff369 5408 dVAR;
79072805
LW
5409 register char *big;
5410 register char *mid;
5411 register char *midend;
5412 register char *bigend;
5413 register I32 i;
6ff81951 5414 STRLEN curlen;
1c846c1f 5415
27aecdc6 5416 PERL_ARGS_ASSERT_SV_INSERT_FLAGS;
79072805 5417
8990e307 5418 if (!bigstr)
cea2e8a9 5419 Perl_croak(aTHX_ "Can't modify non-existent substring");
c0dd94a0 5420 SvPV_force_flags(bigstr, curlen, flags);
60fa28ff 5421 (void)SvPOK_only_UTF8(bigstr);
6ff81951
GS
5422 if (offset + len > curlen) {
5423 SvGROW(bigstr, offset+len+1);
93524f2b 5424 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
6ff81951
GS
5425 SvCUR_set(bigstr, offset+len);
5426 }
79072805 5427
69b47968 5428 SvTAINT(bigstr);
79072805
LW
5429 i = littlelen - len;
5430 if (i > 0) { /* string might grow */
a0d0e21e 5431 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
79072805
LW
5432 mid = big + offset + len;
5433 midend = bigend = big + SvCUR(bigstr);
5434 bigend += i;
5435 *bigend = '\0';
5436 while (midend > mid) /* shove everything down */
5437 *--bigend = *--midend;
5438 Move(little,big+offset,littlelen,char);
b162af07 5439 SvCUR_set(bigstr, SvCUR(bigstr) + i);
79072805
LW
5440 SvSETMAGIC(bigstr);
5441 return;
5442 }
5443 else if (i == 0) {
463ee0b2 5444 Move(little,SvPVX(bigstr)+offset,len,char);
79072805
LW
5445 SvSETMAGIC(bigstr);
5446 return;
5447 }
5448
463ee0b2 5449 big = SvPVX(bigstr);
79072805
LW
5450 mid = big + offset;
5451 midend = mid + len;
5452 bigend = big + SvCUR(bigstr);
5453
5454 if (midend > bigend)
cea2e8a9 5455 Perl_croak(aTHX_ "panic: sv_insert");
79072805
LW
5456
5457 if (mid - big > bigend - midend) { /* faster to shorten from end */
5458 if (littlelen) {
5459 Move(little, mid, littlelen,char);
5460 mid += littlelen;
5461 }
5462 i = bigend - midend;
5463 if (i > 0) {
5464 Move(midend, mid, i,char);
5465 mid += i;
5466 }
5467 *mid = '\0';
5468 SvCUR_set(bigstr, mid - big);
5469 }
155aba94 5470 else if ((i = mid - big)) { /* faster from front */
79072805
LW
5471 midend -= littlelen;
5472 mid = midend;
0d3c21b0 5473 Move(big, midend - i, i, char);
79072805 5474 sv_chop(bigstr,midend-i);
79072805
LW
5475 if (littlelen)
5476 Move(little, mid, littlelen,char);
5477 }
5478 else if (littlelen) {
5479 midend -= littlelen;
5480 sv_chop(bigstr,midend);
5481 Move(little,midend,littlelen,char);
5482 }
5483 else {
5484 sv_chop(bigstr,midend);
5485 }
5486 SvSETMAGIC(bigstr);
5487}
5488
c461cf8f
JH
5489/*
5490=for apidoc sv_replace
5491
5492Make the first argument a copy of the second, then delete the original.
645c22ef
DM
5493The target SV physically takes over ownership of the body of the source SV
5494and inherits its flags; however, the target keeps any magic it owns,
5495and any magic in the source is discarded.
ff276b08 5496Note that this is a rather specialist SV copying operation; most of the
645c22ef 5497time you'll want to use C<sv_setsv> or one of its many macro front-ends.
c461cf8f
JH
5498
5499=cut
5500*/
79072805
LW
5501
5502void
af828c01 5503Perl_sv_replace(pTHX_ register SV *const sv, register SV *const nsv)
79072805 5504{
97aff369 5505 dVAR;
a3b680e6 5506 const U32 refcnt = SvREFCNT(sv);
7918f24d
NC
5507
5508 PERL_ARGS_ASSERT_SV_REPLACE;
5509
765f542d 5510 SV_CHECK_THINKFIRST_COW_DROP(sv);
30e5c352 5511 if (SvREFCNT(nsv) != 1) {
fe13d51d
JM
5512 Perl_croak(aTHX_ "panic: reference miscount on nsv in sv_replace()"
5513 " (%" UVuf " != 1)", (UV) SvREFCNT(nsv));
30e5c352 5514 }
93a17b20 5515 if (SvMAGICAL(sv)) {
a0d0e21e
LW
5516 if (SvMAGICAL(nsv))
5517 mg_free(nsv);
5518 else
5519 sv_upgrade(nsv, SVt_PVMG);
b162af07 5520 SvMAGIC_set(nsv, SvMAGIC(sv));
a0d0e21e 5521 SvFLAGS(nsv) |= SvMAGICAL(sv);
93a17b20 5522 SvMAGICAL_off(sv);
b162af07 5523 SvMAGIC_set(sv, NULL);
93a17b20 5524 }
79072805
LW
5525 SvREFCNT(sv) = 0;
5526 sv_clear(sv);
477f5d66 5527 assert(!SvREFCNT(sv));
fd0854ff
DM
5528#ifdef DEBUG_LEAKING_SCALARS
5529 sv->sv_flags = nsv->sv_flags;
5530 sv->sv_any = nsv->sv_any;
5531 sv->sv_refcnt = nsv->sv_refcnt;
f34d0642 5532 sv->sv_u = nsv->sv_u;
fd0854ff 5533#else
79072805 5534 StructCopy(nsv,sv,SV);
fd0854ff 5535#endif
4df7f6af 5536 if(SvTYPE(sv) == SVt_IV) {
7b2c381c 5537 SvANY(sv)
339049b0 5538 = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
7b2c381c
NC
5539 }
5540
fd0854ff 5541
f8c7b90f 5542#ifdef PERL_OLD_COPY_ON_WRITE
d3d0e6f1
NC
5543 if (SvIsCOW_normal(nsv)) {
5544 /* We need to follow the pointers around the loop to make the
5545 previous SV point to sv, rather than nsv. */
5546 SV *next;
5547 SV *current = nsv;
5548 while ((next = SV_COW_NEXT_SV(current)) != nsv) {
5549 assert(next);
5550 current = next;
3f7c398e 5551 assert(SvPVX_const(current) == SvPVX_const(nsv));
d3d0e6f1
NC
5552 }
5553 /* Make the SV before us point to the SV after us. */
5554 if (DEBUG_C_TEST) {
5555 PerlIO_printf(Perl_debug_log, "previous is\n");
5556 sv_dump(current);
a29f6d03
NC
5557 PerlIO_printf(Perl_debug_log,
5558 "move it from 0x%"UVxf" to 0x%"UVxf"\n",
d3d0e6f1
NC
5559 (UV) SV_COW_NEXT_SV(current), (UV) sv);
5560 }
a29f6d03 5561 SV_COW_NEXT_SV_SET(current, sv);
d3d0e6f1
NC
5562 }
5563#endif
79072805 5564 SvREFCNT(sv) = refcnt;
1edc1566 5565 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
39cf41c2 5566 SvREFCNT(nsv) = 0;
463ee0b2 5567 del_SV(nsv);
79072805
LW
5568}
5569
c461cf8f
JH
5570/*
5571=for apidoc sv_clear
5572
645c22ef
DM
5573Clear an SV: call any destructors, free up any memory used by the body,
5574and free the body itself. The SV's head is I<not> freed, although
5575its type is set to all 1's so that it won't inadvertently be assumed
5576to be live during global destruction etc.
5577This function should only be called when REFCNT is zero. Most of the time
5578you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
5579instead.
c461cf8f
JH
5580
5581=cut
5582*/
5583
79072805 5584void
af828c01 5585Perl_sv_clear(pTHX_ register SV *const sv)
79072805 5586{
27da23d5 5587 dVAR;
82bb6deb 5588 const U32 type = SvTYPE(sv);
8edfc514
NC
5589 const struct body_details *const sv_type_details
5590 = bodies_by_type + type;
dd69841b 5591 HV *stash;
82bb6deb 5592
7918f24d 5593 PERL_ARGS_ASSERT_SV_CLEAR;
79072805 5594 assert(SvREFCNT(sv) == 0);
ceb531cd 5595 assert(SvTYPE(sv) != SVTYPEMASK);
79072805 5596
d2a0f284
JC
5597 if (type <= SVt_IV) {
5598 /* See the comment in sv.h about the collusion between this early
5599 return and the overloading of the NULL and IV slots in the size
5600 table. */
4df7f6af
NC
5601 if (SvROK(sv)) {
5602 SV * const target = SvRV(sv);
5603 if (SvWEAKREF(sv))
5604 sv_del_backref(target, sv);
5605 else
5606 SvREFCNT_dec(target);
5607 }
5608 SvFLAGS(sv) &= SVf_BREAK;
5609 SvFLAGS(sv) |= SVTYPEMASK;
82bb6deb 5610 return;
d2a0f284 5611 }
82bb6deb 5612
ed6116ce 5613 if (SvOBJECT(sv)) {
eba16661
JH
5614 if (PL_defstash && /* Still have a symbol table? */
5615 SvDESTROYABLE(sv))
5616 {
39644a26 5617 dSP;
893645bd 5618 HV* stash;
d460ef45 5619 do {
b464bac0 5620 CV* destructor;
4e8e7886 5621 stash = SvSTASH(sv);
32251b26 5622 destructor = StashHANDLER(stash,DESTROY);
fbb3ee5a 5623 if (destructor
99ab892b
NC
5624 /* A constant subroutine can have no side effects, so
5625 don't bother calling it. */
5626 && !CvCONST(destructor)
fbb3ee5a
RGS
5627 /* Don't bother calling an empty destructor */
5628 && (CvISXSUB(destructor)
5629 || CvSTART(destructor)->op_next->op_type != OP_LEAVESUB))
5630 {
1b6737cc 5631 SV* const tmpref = newRV(sv);
5cc433a6 5632 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
4e8e7886 5633 ENTER;
e788e7d3 5634 PUSHSTACKi(PERLSI_DESTROY);
4e8e7886
GS
5635 EXTEND(SP, 2);
5636 PUSHMARK(SP);
5cc433a6 5637 PUSHs(tmpref);
4e8e7886 5638 PUTBACK;
daba3364 5639 call_sv(MUTABLE_SV(destructor), G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
7a5fa8a2
NIS
5640
5641
d3acc0f7 5642 POPSTACK;
3095d977 5643 SPAGAIN;
4e8e7886 5644 LEAVE;
5cc433a6
AB
5645 if(SvREFCNT(tmpref) < 2) {
5646 /* tmpref is not kept alive! */
5647 SvREFCNT(sv)--;
b162af07 5648 SvRV_set(tmpref, NULL);
5cc433a6
AB
5649 SvROK_off(tmpref);
5650 }
5651 SvREFCNT_dec(tmpref);
4e8e7886
GS
5652 }
5653 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
8ebc5c01 5654
6f44e0a4
JP
5655
5656 if (SvREFCNT(sv)) {
5657 if (PL_in_clean_objs)
cea2e8a9 5658 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
bfcb3514 5659 HvNAME_get(stash));
6f44e0a4
JP
5660 /* DESTROY gave object new lease on life */
5661 return;
5662 }
a0d0e21e 5663 }
4e8e7886 5664
a0d0e21e 5665 if (SvOBJECT(sv)) {
4e8e7886 5666 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
a0d0e21e 5667 SvOBJECT_off(sv); /* Curse the object. */
82bb6deb 5668 if (type != SVt_PVIO)
3280af22 5669 --PL_sv_objcount; /* XXX Might want something more general */
a0d0e21e 5670 }
463ee0b2 5671 }
82bb6deb 5672 if (type >= SVt_PVMG) {
cecf5685 5673 if (type == SVt_PVMG && SvPAD_OUR(sv)) {
73d95100 5674 SvREFCNT_dec(SvOURSTASH(sv));
e736a858 5675 } else if (SvMAGIC(sv))
524189f1 5676 mg_free(sv);
00b1698f 5677 if (type == SVt_PVMG && SvPAD_TYPED(sv))
524189f1
JH
5678 SvREFCNT_dec(SvSTASH(sv));
5679 }
82bb6deb 5680 switch (type) {
cecf5685 5681 /* case SVt_BIND: */
8990e307 5682 case SVt_PVIO:
df0bd2f4
GS
5683 if (IoIFP(sv) &&
5684 IoIFP(sv) != PerlIO_stdin() &&
5f05dabc 5685 IoIFP(sv) != PerlIO_stdout() &&
5686 IoIFP(sv) != PerlIO_stderr())
93578b34 5687 {
a45c7426 5688 io_close(MUTABLE_IO(sv), FALSE);
93578b34 5689 }
1d7c1841 5690 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
1236053a 5691 PerlDir_close(IoDIRP(sv));
1d7c1841 5692 IoDIRP(sv) = (DIR*)NULL;
8990e307
LW
5693 Safefree(IoTOP_NAME(sv));
5694 Safefree(IoFMT_NAME(sv));
5695 Safefree(IoBOTTOM_NAME(sv));
82bb6deb 5696 goto freescalar;
5c35adbb 5697 case SVt_REGEXP:
288b8c02 5698 /* FIXME for plugins */
d2f13c59 5699 pregfree2((REGEXP*) sv);
5c35adbb 5700 goto freescalar;
79072805 5701 case SVt_PVCV:
748a9306 5702 case SVt_PVFM:
ea726b52 5703 cv_undef(MUTABLE_CV(sv));
a0d0e21e 5704 goto freescalar;
79072805 5705 case SVt_PVHV:
1d193675 5706 if (PL_last_swash_hv == (const HV *)sv) {
e7fab884
NC
5707 PL_last_swash_hv = NULL;
5708 }
85fbaab2
NC
5709 Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
5710 hv_undef(MUTABLE_HV(sv));
a0d0e21e 5711 break;
79072805 5712 case SVt_PVAV:
502c6561 5713 if (PL_comppad == MUTABLE_AV(sv)) {
3f90d085
DM
5714 PL_comppad = NULL;
5715 PL_curpad = NULL;
5716 }
502c6561 5717 av_undef(MUTABLE_AV(sv));
a0d0e21e 5718 break;
02270b4e 5719 case SVt_PVLV:
dd28f7bb
DM
5720 if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
5721 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
5722 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
5723 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
5724 }
5725 else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */
5726 SvREFCNT_dec(LvTARG(sv));
a0d0e21e 5727 case SVt_PVGV:
cecf5685 5728 if (isGV_with_GP(sv)) {
159b6efe
NC
5729 if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
5730 && HvNAME_get(stash))
dd69841b 5731 mro_method_changed_in(stash);
159b6efe 5732 gp_free(MUTABLE_GV(sv));
cecf5685
NC
5733 if (GvNAME_HEK(sv))
5734 unshare_hek(GvNAME_HEK(sv));
dd69841b
BB
5735 /* If we're in a stash, we don't own a reference to it. However it does
5736 have a back reference to us, which needs to be cleared. */
5737 if (!SvVALID(sv) && (stash = GvSTASH(sv)))
daba3364 5738 sv_del_backref(MUTABLE_SV(stash), sv);
cecf5685 5739 }
8571fe2f
NC
5740 /* FIXME. There are probably more unreferenced pointers to SVs in the
5741 interpreter struct that we should check and tidy in a similar
5742 fashion to this: */
159b6efe 5743 if ((const GV *)sv == PL_last_in_gv)
8571fe2f 5744 PL_last_in_gv = NULL;
79072805 5745 case SVt_PVMG:
79072805
LW
5746 case SVt_PVNV:
5747 case SVt_PVIV:
7a4bba22 5748 case SVt_PV:
a0d0e21e 5749 freescalar:
5228ca4e
NC
5750 /* Don't bother with SvOOK_off(sv); as we're only going to free it. */
5751 if (SvOOK(sv)) {
69240efd
NC
5752 STRLEN offset;
5753 SvOOK_offset(sv, offset);
5754 SvPV_set(sv, SvPVX_mutable(sv) - offset);
5228ca4e
NC
5755 /* Don't even bother with turning off the OOK flag. */
5756 }
810b8aa5 5757 if (SvROK(sv)) {
b37c2d43 5758 SV * const target = SvRV(sv);
810b8aa5 5759 if (SvWEAKREF(sv))
e15faf7d 5760 sv_del_backref(target, sv);
810b8aa5 5761 else
e15faf7d 5762 SvREFCNT_dec(target);
810b8aa5 5763 }
f8c7b90f 5764#ifdef PERL_OLD_COPY_ON_WRITE
3f7c398e 5765 else if (SvPVX_const(sv)) {
765f542d 5766 if (SvIsCOW(sv)) {
46187eeb
NC
5767 if (DEBUG_C_TEST) {
5768 PerlIO_printf(Perl_debug_log, "Copy on write: clear\n");
e419cbc5 5769 sv_dump(sv);
46187eeb 5770 }
5302ffd4
NC
5771 if (SvLEN(sv)) {
5772 sv_release_COW(sv, SvPVX_const(sv), SV_COW_NEXT_SV(sv));
5773 } else {
5774 unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
5775 }
5776
765f542d
NC
5777 SvFAKE_off(sv);
5778 } else if (SvLEN(sv)) {
3f7c398e 5779 Safefree(SvPVX_const(sv));
765f542d
NC
5780 }
5781 }
5782#else
3f7c398e 5783 else if (SvPVX_const(sv) && SvLEN(sv))
94010e71 5784 Safefree(SvPVX_mutable(sv));
3f7c398e 5785 else if (SvPVX_const(sv) && SvREADONLY(sv) && SvFAKE(sv)) {
bdd68bc3 5786 unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
1c846c1f
NIS
5787 SvFAKE_off(sv);
5788 }
765f542d 5789#endif
79072805
LW
5790 break;
5791 case SVt_NV:
79072805
LW
5792 break;
5793 }
5794
893645bd
NC
5795 SvFLAGS(sv) &= SVf_BREAK;
5796 SvFLAGS(sv) |= SVTYPEMASK;
5797
8edfc514 5798 if (sv_type_details->arena) {
b9502f15 5799 del_body(((char *)SvANY(sv) + sv_type_details->offset),
8edfc514
NC
5800 &PL_body_roots[type]);
5801 }
d2a0f284 5802 else if (sv_type_details->body_size) {
8edfc514
NC
5803 my_safefree(SvANY(sv));
5804 }
79072805
LW
5805}
5806
645c22ef
DM
5807/*
5808=for apidoc sv_newref
5809
5810Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
5811instead.
5812
5813=cut
5814*/
5815
79072805 5816SV *
af828c01 5817Perl_sv_newref(pTHX_ SV *const sv)
79072805 5818{
96a5add6 5819 PERL_UNUSED_CONTEXT;
463ee0b2 5820 if (sv)
4db098f4 5821 (SvREFCNT(sv))++;
79072805
LW
5822 return sv;
5823}
5824
c461cf8f
JH
5825/*
5826=for apidoc sv_free
5827
645c22ef
DM
5828Decrement an SV's reference count, and if it drops to zero, call
5829C<sv_clear> to invoke destructors and free up any memory used by
5830the body; finally, deallocate the SV's head itself.
5831Normally called via a wrapper macro C<SvREFCNT_dec>.
c461cf8f
JH
5832
5833=cut
5834*/
5835
79072805 5836void
af828c01 5837Perl_sv_free(pTHX_ SV *const sv)
79072805 5838{
27da23d5 5839 dVAR;
79072805
LW
5840 if (!sv)
5841 return;
a0d0e21e
LW
5842 if (SvREFCNT(sv) == 0) {
5843 if (SvFLAGS(sv) & SVf_BREAK)
645c22ef
DM
5844 /* this SV's refcnt has been artificially decremented to
5845 * trigger cleanup */
a0d0e21e 5846 return;
3280af22 5847 if (PL_in_clean_all) /* All is fair */
1edc1566 5848 return;
d689ffdd
JP
5849 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
5850 /* make sure SvREFCNT(sv)==0 happens very seldom */
5851 SvREFCNT(sv) = (~(U32)0)/2;
5852 return;
5853 }
41e4abd8 5854 if (ckWARN_d(WARN_INTERNAL)) {
41e4abd8
NC
5855#ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
5856 Perl_dump_sv_child(aTHX_ sv);
e4c5322d
DM
5857#else
5858 #ifdef DEBUG_LEAKING_SCALARS
bfd95973 5859 sv_dump(sv);
e4c5322d 5860 #endif
bfd95973
NC
5861#ifdef DEBUG_LEAKING_SCALARS_ABORT
5862 if (PL_warnhook == PERL_WARNHOOK_FATAL
5863 || ckDEAD(packWARN(WARN_INTERNAL))) {
5864 /* Don't let Perl_warner cause us to escape our fate: */
5865 abort();
5866 }
5867#endif
5868 /* This may not return: */
5869 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
5870 "Attempt to free unreferenced scalar: SV 0x%"UVxf
5871 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
41e4abd8
NC
5872#endif
5873 }
77abb4c6
NC
5874#ifdef DEBUG_LEAKING_SCALARS_ABORT
5875 abort();
5876#endif
79072805
LW
5877 return;
5878 }
4db098f4 5879 if (--(SvREFCNT(sv)) > 0)
8990e307 5880 return;
8c4d3c90
NC
5881 Perl_sv_free2(aTHX_ sv);
5882}
5883
5884void
af828c01 5885Perl_sv_free2(pTHX_ SV *const sv)
8c4d3c90 5886{
27da23d5 5887 dVAR;
7918f24d
NC
5888
5889 PERL_ARGS_ASSERT_SV_FREE2;
5890
463ee0b2
LW
5891#ifdef DEBUGGING
5892 if (SvTEMP(sv)) {
0453d815 5893 if (ckWARN_d(WARN_DEBUGGING))
9014280d 5894 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
472d47bc
SB
5895 "Attempt to free temp prematurely: SV 0x%"UVxf
5896 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
79072805 5897 return;
79072805 5898 }
463ee0b2 5899#endif
d689ffdd
JP
5900 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
5901 /* make sure SvREFCNT(sv)==0 happens very seldom */
5902 SvREFCNT(sv) = (~(U32)0)/2;
5903 return;
5904 }
79072805 5905 sv_clear(sv);
477f5d66
CS
5906 if (! SvREFCNT(sv))
5907 del_SV(sv);
79072805
LW
5908}
5909
954c1994
GS
5910/*
5911=for apidoc sv_len
5912
645c22ef
DM
5913Returns the length of the string in the SV. Handles magic and type
5914coercion. See also C<SvCUR>, which gives raw access to the xpv_cur slot.
954c1994
GS
5915
5916=cut
5917*/
5918
79072805 5919STRLEN
af828c01 5920Perl_sv_len(pTHX_ register SV *const sv)
79072805 5921{
463ee0b2 5922 STRLEN len;
79072805
LW
5923
5924 if (!sv)
5925 return 0;
5926
8990e307 5927 if (SvGMAGICAL(sv))
565764a8 5928 len = mg_length(sv);
8990e307 5929 else
4d84ee25 5930 (void)SvPV_const(sv, len);
463ee0b2 5931 return len;
79072805
LW
5932}
5933
c461cf8f
JH
5934/*
5935=for apidoc sv_len_utf8
5936
5937Returns the number of characters in the string in an SV, counting wide
1e54db1a 5938UTF-8 bytes as a single character. Handles magic and type coercion.
c461cf8f
JH
5939
5940=cut
5941*/
5942
7e8c5dac 5943/*
c05a5c57 5944 * The length is cached in PERL_MAGIC_utf8, in the mg_len field. Also the
9564a3bd
NC
5945 * mg_ptr is used, by sv_pos_u2b() and sv_pos_b2u() - see the comments below.
5946 * (Note that the mg_len is not the length of the mg_ptr field.
5947 * This allows the cache to store the character length of the string without
5948 * needing to malloc() extra storage to attach to the mg_ptr.)
7a5fa8a2 5949 *
7e8c5dac
HS
5950 */
5951
a0ed51b3 5952STRLEN
af828c01 5953Perl_sv_len_utf8(pTHX_ register SV *const sv)
a0ed51b3 5954{
a0ed51b3
LW
5955 if (!sv)
5956 return 0;
5957
a0ed51b3 5958 if (SvGMAGICAL(sv))
b76347f2 5959 return mg_length(sv);
a0ed51b3 5960 else
b76347f2 5961 {
26346457 5962 STRLEN len;
e62f0680 5963 const U8 *s = (U8*)SvPV_const(sv, len);
7e8c5dac 5964
26346457
NC
5965 if (PL_utf8cache) {
5966 STRLEN ulen;
fe5bfecd 5967 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
26346457
NC
5968
5969 if (mg && mg->mg_len != -1) {
5970 ulen = mg->mg_len;
5971 if (PL_utf8cache < 0) {
5972 const STRLEN real = Perl_utf8_length(aTHX_ s, s + len);
5973 if (real != ulen) {
5974 /* Need to turn the assertions off otherwise we may
5975 recurse infinitely while printing error messages.
5976 */
5977 SAVEI8(PL_utf8cache);
5978 PL_utf8cache = 0;
f5992bc4
RB
5979 Perl_croak(aTHX_ "panic: sv_len_utf8 cache %"UVuf
5980 " real %"UVuf" for %"SVf,
be2597df 5981 (UV) ulen, (UV) real, SVfARG(sv));
26346457
NC
5982 }
5983 }
5984 }
5985 else {
5986 ulen = Perl_utf8_length(aTHX_ s, s + len);
5987 if (!SvREADONLY(sv)) {
5988 if (!mg) {
5989 mg = sv_magicext(sv, 0, PERL_MAGIC_utf8,
5990 &PL_vtbl_utf8, 0, 0);
5991 }
cb9e20bb 5992 assert(mg);
26346457 5993 mg->mg_len = ulen;
cb9e20bb 5994 }
cb9e20bb 5995 }
26346457 5996 return ulen;
7e8c5dac 5997 }
26346457 5998 return Perl_utf8_length(aTHX_ s, s + len);
7e8c5dac
HS
5999 }
6000}
6001
9564a3bd
NC
6002/* Walk forwards to find the byte corresponding to the passed in UTF-8
6003 offset. */
bdf30dd6 6004static STRLEN
721e86b6 6005S_sv_pos_u2b_forwards(const U8 *const start, const U8 *const send,
bdf30dd6
NC
6006 STRLEN uoffset)
6007{
6008 const U8 *s = start;
6009
7918f24d
NC
6010 PERL_ARGS_ASSERT_SV_POS_U2B_FORWARDS;
6011
bdf30dd6
NC
6012 while (s < send && uoffset--)
6013 s += UTF8SKIP(s);
6014 if (s > send) {
6015 /* This is the existing behaviour. Possibly it should be a croak, as
6016 it's actually a bounds error */
6017 s = send;
6018 }
6019 return s - start;
6020}
6021
9564a3bd
NC
6022/* Given the length of the string in both bytes and UTF-8 characters, decide
6023 whether to walk forwards or backwards to find the byte corresponding to
6024 the passed in UTF-8 offset. */
c336ad0b 6025static STRLEN
721e86b6 6026S_sv_pos_u2b_midway(const U8 *const start, const U8 *send,
af828c01 6027 const STRLEN uoffset, const STRLEN uend)
c336ad0b
NC
6028{
6029 STRLEN backw = uend - uoffset;
7918f24d
NC
6030
6031 PERL_ARGS_ASSERT_SV_POS_U2B_MIDWAY;
6032
c336ad0b 6033 if (uoffset < 2 * backw) {
25a8a4ef 6034 /* The assumption is that going forwards is twice the speed of going
c336ad0b
NC
6035 forward (that's where the 2 * backw comes from).
6036 (The real figure of course depends on the UTF-8 data.) */
721e86b6 6037 return sv_pos_u2b_forwards(start, send, uoffset);
c336ad0b
NC
6038 }
6039
6040 while (backw--) {
6041 send--;
6042 while (UTF8_IS_CONTINUATION(*send))
6043 send--;
6044 }
6045 return send - start;
6046}
6047
9564a3bd
NC
6048/* For the string representation of the given scalar, find the byte
6049 corresponding to the passed in UTF-8 offset. uoffset0 and boffset0
6050 give another position in the string, *before* the sought offset, which
6051 (which is always true, as 0, 0 is a valid pair of positions), which should
6052 help reduce the amount of linear searching.
6053 If *mgp is non-NULL, it should point to the UTF-8 cache magic, which
6054 will be used to reduce the amount of linear searching. The cache will be
6055 created if necessary, and the found value offered to it for update. */
28ccbf94 6056static STRLEN
af828c01
SS
6057S_sv_pos_u2b_cached(pTHX_ SV *const sv, MAGIC **const mgp, const U8 *const start,
6058 const U8 *const send, const STRLEN uoffset,
7918f24d
NC
6059 STRLEN uoffset0, STRLEN boffset0)
6060{
7087a21c 6061 STRLEN boffset = 0; /* Actually always set, but let's keep gcc happy. */
c336ad0b
NC
6062 bool found = FALSE;
6063
7918f24d
NC
6064 PERL_ARGS_ASSERT_SV_POS_U2B_CACHED;
6065
75c33c12
NC
6066 assert (uoffset >= uoffset0);
6067
c336ad0b 6068 if (SvMAGICAL(sv) && !SvREADONLY(sv) && PL_utf8cache
0905937d 6069 && (*mgp || (*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
d8b2e1f9
NC
6070 if ((*mgp)->mg_ptr) {
6071 STRLEN *cache = (STRLEN *) (*mgp)->mg_ptr;
6072 if (cache[0] == uoffset) {
6073 /* An exact match. */
6074 return cache[1];
6075 }
ab455f60
NC
6076 if (cache[2] == uoffset) {
6077 /* An exact match. */
6078 return cache[3];
6079 }
668af93f
NC
6080
6081 if (cache[0] < uoffset) {
d8b2e1f9
NC
6082 /* The cache already knows part of the way. */
6083 if (cache[0] > uoffset0) {
6084 /* The cache knows more than the passed in pair */
6085 uoffset0 = cache[0];
6086 boffset0 = cache[1];
6087 }
6088 if ((*mgp)->mg_len != -1) {
6089 /* And we know the end too. */
6090 boffset = boffset0
721e86b6 6091 + sv_pos_u2b_midway(start + boffset0, send,
d8b2e1f9
NC
6092 uoffset - uoffset0,
6093 (*mgp)->mg_len - uoffset0);
6094 } else {
6095 boffset = boffset0
721e86b6 6096 + sv_pos_u2b_forwards(start + boffset0,
d8b2e1f9
NC
6097 send, uoffset - uoffset0);
6098 }
dd7c5fd3
NC
6099 }
6100 else if (cache[2] < uoffset) {
6101 /* We're between the two cache entries. */
6102 if (cache[2] > uoffset0) {
6103 /* and the cache knows more than the passed in pair */
6104 uoffset0 = cache[2];
6105 boffset0 = cache[3];
6106 }
6107
668af93f 6108 boffset = boffset0
721e86b6 6109 + sv_pos_u2b_midway(start + boffset0,
668af93f
NC
6110 start + cache[1],
6111 uoffset - uoffset0,
6112 cache[0] - uoffset0);
dd7c5fd3
NC
6113 } else {
6114 boffset = boffset0
721e86b6 6115 + sv_pos_u2b_midway(start + boffset0,
dd7c5fd3
NC
6116 start + cache[3],
6117 uoffset - uoffset0,
6118 cache[2] - uoffset0);
d8b2e1f9 6119 }
668af93f 6120 found = TRUE;
d8b2e1f9
NC
6121 }
6122 else if ((*mgp)->mg_len != -1) {
75c33c12
NC
6123 /* If we can take advantage of a passed in offset, do so. */
6124 /* In fact, offset0 is either 0, or less than offset, so don't
6125 need to worry about the other possibility. */
6126 boffset = boffset0
721e86b6 6127 + sv_pos_u2b_midway(start + boffset0, send,
75c33c12
NC
6128 uoffset - uoffset0,
6129 (*mgp)->mg_len - uoffset0);
c336ad0b
NC
6130 found = TRUE;
6131 }
28ccbf94 6132 }
c336ad0b
NC
6133
6134 if (!found || PL_utf8cache < 0) {
75c33c12 6135 const STRLEN real_boffset
721e86b6 6136 = boffset0 + sv_pos_u2b_forwards(start + boffset0,
75c33c12
NC
6137 send, uoffset - uoffset0);
6138
c336ad0b
NC
6139 if (found && PL_utf8cache < 0) {
6140 if (real_boffset != boffset) {
6141 /* Need to turn the assertions off otherwise we may recurse
6142 infinitely while printing error messages. */
6143 SAVEI8(PL_utf8cache);
6144 PL_utf8cache = 0;
f5992bc4
RB
6145 Perl_croak(aTHX_ "panic: sv_pos_u2b_cache cache %"UVuf
6146 " real %"UVuf" for %"SVf,
be2597df 6147 (UV) boffset, (UV) real_boffset, SVfARG(sv));
c336ad0b
NC
6148 }
6149 }
6150 boffset = real_boffset;
28ccbf94 6151 }
0905937d 6152
efcbbafb
NC
6153 if (PL_utf8cache)
6154 utf8_mg_pos_cache_update(sv, mgp, boffset, uoffset, send - start);
28ccbf94
NC
6155 return boffset;
6156}
6157
9564a3bd
NC
6158
6159/*
6160=for apidoc sv_pos_u2b
6161
6162Converts the value pointed to by offsetp from a count of UTF-8 chars from
6163the start of the string, to a count of the equivalent number of bytes; if
6164lenp is non-zero, it does the same to lenp, but this time starting from
6165the offset, rather than from the start of the string. Handles magic and
6166type coercion.
6167
6168=cut
6169*/
6170
6171/*
6172 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
c05a5c57 6173 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
9564a3bd
NC
6174 * byte offsets. See also the comments of S_utf8_mg_pos_cache_update().
6175 *
6176 */
6177
a0ed51b3 6178void
af828c01 6179Perl_sv_pos_u2b(pTHX_ register SV *const sv, I32 *const offsetp, I32 *const lenp)
a0ed51b3 6180{
245d4a47 6181 const U8 *start;
a0ed51b3
LW
6182 STRLEN len;
6183
7918f24d
NC
6184 PERL_ARGS_ASSERT_SV_POS_U2B;
6185
a0ed51b3
LW
6186 if (!sv)
6187 return;
6188
245d4a47 6189 start = (U8*)SvPV_const(sv, len);
7e8c5dac 6190 if (len) {
bdf30dd6
NC
6191 STRLEN uoffset = (STRLEN) *offsetp;
6192 const U8 * const send = start + len;
0905937d 6193 MAGIC *mg = NULL;
721e86b6 6194 const STRLEN boffset = sv_pos_u2b_cached(sv, &mg, start, send,
28ccbf94 6195 uoffset, 0, 0);
bdf30dd6
NC
6196
6197 *offsetp = (I32) boffset;
6198
6199 if (lenp) {
28ccbf94 6200 /* Convert the relative offset to absolute. */
721e86b6
AL
6201 const STRLEN uoffset2 = uoffset + (STRLEN) *lenp;
6202 const STRLEN boffset2
6203 = sv_pos_u2b_cached(sv, &mg, start, send, uoffset2,
28ccbf94 6204 uoffset, boffset) - boffset;
bdf30dd6 6205
28ccbf94 6206 *lenp = boffset2;
bdf30dd6 6207 }
7e8c5dac
HS
6208 }
6209 else {
6210 *offsetp = 0;
6211 if (lenp)
6212 *lenp = 0;
a0ed51b3 6213 }
e23c8137 6214
a0ed51b3
LW
6215 return;
6216}
6217
9564a3bd
NC
6218/* Create and update the UTF8 magic offset cache, with the proffered utf8/
6219 byte length pairing. The (byte) length of the total SV is passed in too,
6220 as blen, because for some (more esoteric) SVs, the call to SvPV_const()
6221 may not have updated SvCUR, so we can't rely on reading it directly.
6222
6223 The proffered utf8/byte length pairing isn't used if the cache already has
6224 two pairs, and swapping either for the proffered pair would increase the
6225 RMS of the intervals between known byte offsets.
6226
6227 The cache itself consists of 4 STRLEN values
6228 0: larger UTF-8 offset
6229 1: corresponding byte offset
6230 2: smaller UTF-8 offset
6231 3: corresponding byte offset
6232
6233 Unused cache pairs have the value 0, 0.
6234 Keeping the cache "backwards" means that the invariant of
6235 cache[0] >= cache[2] is maintained even with empty slots, which means that
6236 the code that uses it doesn't need to worry if only 1 entry has actually
6237 been set to non-zero. It also makes the "position beyond the end of the
6238 cache" logic much simpler, as the first slot is always the one to start
6239 from.
645c22ef 6240*/
ec07b5e0 6241static void
ac1e9476
SS
6242S_utf8_mg_pos_cache_update(pTHX_ SV *const sv, MAGIC **const mgp, const STRLEN byte,
6243 const STRLEN utf8, const STRLEN blen)
ec07b5e0
NC
6244{
6245 STRLEN *cache;
7918f24d
NC
6246
6247 PERL_ARGS_ASSERT_UTF8_MG_POS_CACHE_UPDATE;
6248
ec07b5e0
NC
6249 if (SvREADONLY(sv))
6250 return;
6251
6252 if (!*mgp) {
6253 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0,
6254 0);
6255 (*mgp)->mg_len = -1;
6256 }
6257 assert(*mgp);
6258
6259 if (!(cache = (STRLEN *)(*mgp)->mg_ptr)) {
6260 Newxz(cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
6261 (*mgp)->mg_ptr = (char *) cache;
6262 }
6263 assert(cache);
6264
6265 if (PL_utf8cache < 0) {
ef816a78 6266 const U8 *start = (const U8 *) SvPVX_const(sv);
6448472a 6267 const STRLEN realutf8 = utf8_length(start, start + byte);
ec07b5e0
NC
6268
6269 if (realutf8 != utf8) {
6270 /* Need to turn the assertions off otherwise we may recurse
6271 infinitely while printing error messages. */
6272 SAVEI8(PL_utf8cache);
6273 PL_utf8cache = 0;
f5992bc4 6274 Perl_croak(aTHX_ "panic: utf8_mg_pos_cache_update cache %"UVuf
be2597df 6275 " real %"UVuf" for %"SVf, (UV) utf8, (UV) realutf8, SVfARG(sv));
ec07b5e0
NC
6276 }
6277 }
ab455f60
NC
6278
6279 /* Cache is held with the later position first, to simplify the code
6280 that deals with unbounded ends. */
6281
6282 ASSERT_UTF8_CACHE(cache);
6283 if (cache[1] == 0) {
6284 /* Cache is totally empty */
6285 cache[0] = utf8;
6286 cache[1] = byte;
6287 } else if (cache[3] == 0) {
6288 if (byte > cache[1]) {
6289 /* New one is larger, so goes first. */
6290 cache[2] = cache[0];
6291 cache[3] = cache[1];
6292 cache[0] = utf8;
6293 cache[1] = byte;
6294 } else {
6295 cache[2] = utf8;
6296 cache[3] = byte;
6297 }
6298 } else {
6299#define THREEWAY_SQUARE(a,b,c,d) \
6300 ((float)((d) - (c))) * ((float)((d) - (c))) \
6301 + ((float)((c) - (b))) * ((float)((c) - (b))) \
6302 + ((float)((b) - (a))) * ((float)((b) - (a)))
6303
6304 /* Cache has 2 slots in use, and we know three potential pairs.
6305 Keep the two that give the lowest RMS distance. Do the
6306 calcualation in bytes simply because we always know the byte
6307 length. squareroot has the same ordering as the positive value,
6308 so don't bother with the actual square root. */
6309 const float existing = THREEWAY_SQUARE(0, cache[3], cache[1], blen);
6310 if (byte > cache[1]) {
6311 /* New position is after the existing pair of pairs. */
6312 const float keep_earlier
6313 = THREEWAY_SQUARE(0, cache[3], byte, blen);
6314 const float keep_later
6315 = THREEWAY_SQUARE(0, cache[1], byte, blen);
6316
6317 if (keep_later < keep_earlier) {
6318 if (keep_later < existing) {
6319 cache[2] = cache[0];
6320 cache[3] = cache[1];
6321 cache[0] = utf8;
6322 cache[1] = byte;
6323 }
6324 }
6325 else {
6326 if (keep_earlier < existing) {
6327 cache[0] = utf8;
6328 cache[1] = byte;
6329 }
6330 }
6331 }
57d7fbf1
NC
6332 else if (byte > cache[3]) {
6333 /* New position is between the existing pair of pairs. */
6334 const float keep_earlier
6335 = THREEWAY_SQUARE(0, cache[3], byte, blen);
6336 const float keep_later
6337 = THREEWAY_SQUARE(0, byte, cache[1], blen);
6338
6339 if (keep_later < keep_earlier) {
6340 if (keep_later < existing) {
6341 cache[2] = utf8;
6342 cache[3] = byte;
6343 }
6344 }
6345 else {
6346 if (keep_earlier < existing) {
6347 cache[0] = utf8;
6348 cache[1] = byte;
6349 }
6350 }
6351 }
6352 else {
6353 /* New position is before the existing pair of pairs. */
6354 const float keep_earlier
6355 = THREEWAY_SQUARE(0, byte, cache[3], blen);
6356 const float keep_later
6357 = THREEWAY_SQUARE(0, byte, cache[1], blen);
6358
6359 if (keep_later < keep_earlier) {
6360 if (keep_later < existing) {
6361 cache[2] = utf8;
6362 cache[3] = byte;
6363 }
6364 }
6365 else {
6366 if (keep_earlier < existing) {
6367 cache[0] = cache[2];
6368 cache[1] = cache[3];
6369 cache[2] = utf8;
6370 cache[3] = byte;
6371 }
6372 }
6373 }
ab455f60 6374 }
0905937d 6375 ASSERT_UTF8_CACHE(cache);
ec07b5e0
NC
6376}
6377
ec07b5e0 6378/* We already know all of the way, now we may be able to walk back. The same
25a8a4ef
NC
6379 assumption is made as in S_sv_pos_u2b_midway(), namely that walking
6380 backward is half the speed of walking forward. */
ec07b5e0 6381static STRLEN
ac1e9476
SS
6382S_sv_pos_b2u_midway(pTHX_ const U8 *const s, const U8 *const target,
6383 const U8 *end, STRLEN endu)
ec07b5e0
NC
6384{
6385 const STRLEN forw = target - s;
6386 STRLEN backw = end - target;
6387
7918f24d
NC
6388 PERL_ARGS_ASSERT_SV_POS_B2U_MIDWAY;
6389
ec07b5e0 6390 if (forw < 2 * backw) {
6448472a 6391 return utf8_length(s, target);
ec07b5e0
NC
6392 }
6393
6394 while (end > target) {
6395 end--;
6396 while (UTF8_IS_CONTINUATION(*end)) {
6397 end--;
6398 }
6399 endu--;
6400 }
6401 return endu;
6402}
6403
9564a3bd
NC
6404/*
6405=for apidoc sv_pos_b2u
6406
6407Converts the value pointed to by offsetp from a count of bytes from the
6408start of the string, to a count of the equivalent number of UTF-8 chars.
6409Handles magic and type coercion.
6410
6411=cut
6412*/
6413
6414/*
6415 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
c05a5c57 6416 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
9564a3bd
NC
6417 * byte offsets.
6418 *
6419 */
a0ed51b3 6420void
ac1e9476 6421Perl_sv_pos_b2u(pTHX_ register SV *const sv, I32 *const offsetp)
a0ed51b3 6422{
83003860 6423 const U8* s;
ec07b5e0 6424 const STRLEN byte = *offsetp;
7087a21c 6425 STRLEN len = 0; /* Actually always set, but let's keep gcc happy. */
ab455f60 6426 STRLEN blen;
ec07b5e0
NC
6427 MAGIC* mg = NULL;
6428 const U8* send;
a922f900 6429 bool found = FALSE;
a0ed51b3 6430
7918f24d
NC
6431 PERL_ARGS_ASSERT_SV_POS_B2U;
6432
a0ed51b3
LW
6433 if (!sv)
6434 return;
6435
ab455f60 6436 s = (const U8*)SvPV_const(sv, blen);
7e8c5dac 6437
ab455f60 6438 if (blen < byte)
ec07b5e0 6439 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset");
7e8c5dac 6440
ec07b5e0 6441 send = s + byte;
a67d7df9 6442
ffca234a
NC
6443 if (SvMAGICAL(sv) && !SvREADONLY(sv) && PL_utf8cache
6444 && (mg = mg_find(sv, PERL_MAGIC_utf8))) {
6445 if (mg->mg_ptr) {
d4c19fe8 6446 STRLEN * const cache = (STRLEN *) mg->mg_ptr;
b9f984a5 6447 if (cache[1] == byte) {
ec07b5e0
NC
6448 /* An exact match. */
6449 *offsetp = cache[0];
ec07b5e0 6450 return;
7e8c5dac 6451 }
ab455f60
NC
6452 if (cache[3] == byte) {
6453 /* An exact match. */
6454 *offsetp = cache[2];
6455 return;
6456 }
668af93f
NC
6457
6458 if (cache[1] < byte) {
ec07b5e0 6459 /* We already know part of the way. */
b9f984a5
NC
6460 if (mg->mg_len != -1) {
6461 /* Actually, we know the end too. */
6462 len = cache[0]
6463 + S_sv_pos_b2u_midway(aTHX_ s + cache[1], send,
ab455f60 6464 s + blen, mg->mg_len - cache[0]);
b9f984a5 6465 } else {
6448472a 6466 len = cache[0] + utf8_length(s + cache[1], send);
b9f984a5 6467 }
7e8c5dac 6468 }
9f985e4c
NC
6469 else if (cache[3] < byte) {
6470 /* We're between the two cached pairs, so we do the calculation
6471 offset by the byte/utf-8 positions for the earlier pair,
6472 then add the utf-8 characters from the string start to
6473 there. */
6474 len = S_sv_pos_b2u_midway(aTHX_ s + cache[3], send,
6475 s + cache[1], cache[0] - cache[2])
6476 + cache[2];
6477
6478 }
6479 else { /* cache[3] > byte */
6480 len = S_sv_pos_b2u_midway(aTHX_ s, send, s + cache[3],
6481 cache[2]);
7e8c5dac 6482
7e8c5dac 6483 }
ec07b5e0 6484 ASSERT_UTF8_CACHE(cache);
a922f900 6485 found = TRUE;
ffca234a 6486 } else if (mg->mg_len != -1) {
ab455f60 6487 len = S_sv_pos_b2u_midway(aTHX_ s, send, s + blen, mg->mg_len);
a922f900 6488 found = TRUE;
7e8c5dac 6489 }
a0ed51b3 6490 }
a922f900 6491 if (!found || PL_utf8cache < 0) {
6448472a 6492 const STRLEN real_len = utf8_length(s, send);
a922f900
NC
6493
6494 if (found && PL_utf8cache < 0) {
6495 if (len != real_len) {
6496 /* Need to turn the assertions off otherwise we may recurse
6497 infinitely while printing error messages. */
6498 SAVEI8(PL_utf8cache);
6499 PL_utf8cache = 0;
f5992bc4
RB
6500 Perl_croak(aTHX_ "panic: sv_pos_b2u cache %"UVuf
6501 " real %"UVuf" for %"SVf,
be2597df 6502 (UV) len, (UV) real_len, SVfARG(sv));
a922f900
NC
6503 }
6504 }
6505 len = real_len;
ec07b5e0
NC
6506 }
6507 *offsetp = len;
6508
efcbbafb
NC
6509 if (PL_utf8cache)
6510 utf8_mg_pos_cache_update(sv, &mg, byte, len, blen);
a0ed51b3
LW
6511}
6512
954c1994
GS
6513/*
6514=for apidoc sv_eq
6515
6516Returns a boolean indicating whether the strings in the two SVs are
645c22ef
DM
6517identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will
6518coerce its args to strings if necessary.
954c1994
GS
6519
6520=cut
6521*/
6522
79072805 6523I32
e01b9e88 6524Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2)
79072805 6525{
97aff369 6526 dVAR;
e1ec3a88 6527 const char *pv1;
463ee0b2 6528 STRLEN cur1;
e1ec3a88 6529 const char *pv2;
463ee0b2 6530 STRLEN cur2;
e01b9e88 6531 I32 eq = 0;
bd61b366 6532 char *tpv = NULL;
a0714e2c 6533 SV* svrecode = NULL;
79072805 6534
e01b9e88 6535 if (!sv1) {
79072805
LW
6536 pv1 = "";
6537 cur1 = 0;
6538 }
ced497e2
YST
6539 else {
6540 /* if pv1 and pv2 are the same, second SvPV_const call may
6541 * invalidate pv1, so we may need to make a copy */
6542 if (sv1 == sv2 && (SvTHINKFIRST(sv1) || SvGMAGICAL(sv1))) {
6543 pv1 = SvPV_const(sv1, cur1);
59cd0e26 6544 sv1 = newSVpvn_flags(pv1, cur1, SVs_TEMP | SvUTF8(sv2));
ced497e2 6545 }
4d84ee25 6546 pv1 = SvPV_const(sv1, cur1);
ced497e2 6547 }
79072805 6548
e01b9e88
SC
6549 if (!sv2){
6550 pv2 = "";
6551 cur2 = 0;
92d29cee 6552 }
e01b9e88 6553 else
4d84ee25 6554 pv2 = SvPV_const(sv2, cur2);
79072805 6555
cf48d248 6556 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
799ef3cb
JH
6557 /* Differing utf8ness.
6558 * Do not UTF8size the comparands as a side-effect. */
6559 if (PL_encoding) {
6560 if (SvUTF8(sv1)) {
553e1bcc
AT
6561 svrecode = newSVpvn(pv2, cur2);
6562 sv_recode_to_utf8(svrecode, PL_encoding);
93524f2b 6563 pv2 = SvPV_const(svrecode, cur2);
799ef3cb
JH
6564 }
6565 else {
553e1bcc
AT
6566 svrecode = newSVpvn(pv1, cur1);
6567 sv_recode_to_utf8(svrecode, PL_encoding);
93524f2b 6568 pv1 = SvPV_const(svrecode, cur1);
799ef3cb
JH
6569 }
6570 /* Now both are in UTF-8. */
0a1bd7ac
DM
6571 if (cur1 != cur2) {
6572 SvREFCNT_dec(svrecode);
799ef3cb 6573 return FALSE;
0a1bd7ac 6574 }
799ef3cb
JH
6575 }
6576 else {
6577 bool is_utf8 = TRUE;
6578
6579 if (SvUTF8(sv1)) {
6580 /* sv1 is the UTF-8 one,
6581 * if is equal it must be downgrade-able */
9d4ba2ae 6582 char * const pv = (char*)bytes_from_utf8((const U8*)pv1,
799ef3cb
JH
6583 &cur1, &is_utf8);
6584 if (pv != pv1)
553e1bcc 6585 pv1 = tpv = pv;
799ef3cb
JH
6586 }
6587 else {
6588 /* sv2 is the UTF-8 one,
6589 * if is equal it must be downgrade-able */
9d4ba2ae 6590 char * const pv = (char *)bytes_from_utf8((const U8*)pv2,
799ef3cb
JH
6591 &cur2, &is_utf8);
6592 if (pv != pv2)
553e1bcc 6593 pv2 = tpv = pv;
799ef3cb
JH
6594 }
6595 if (is_utf8) {
6596 /* Downgrade not possible - cannot be eq */
bf694877 6597 assert (tpv == 0);
799ef3cb
JH
6598 return FALSE;
6599 }
6600 }
cf48d248
JH
6601 }
6602
6603 if (cur1 == cur2)
765f542d 6604 eq = (pv1 == pv2) || memEQ(pv1, pv2, cur1);
e01b9e88 6605
b37c2d43 6606 SvREFCNT_dec(svrecode);
553e1bcc
AT
6607 if (tpv)
6608 Safefree(tpv);
cf48d248 6609
e01b9e88 6610 return eq;
79072805
LW
6611}
6612
954c1994
GS
6613/*
6614=for apidoc sv_cmp
6615
6616Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
6617string in C<sv1> is less than, equal to, or greater than the string in
645c22ef
DM
6618C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will
6619coerce its args to strings if necessary. See also C<sv_cmp_locale>.
954c1994
GS
6620
6621=cut
6622*/
6623
79072805 6624I32
ac1e9476 6625Perl_sv_cmp(pTHX_ register SV *const sv1, register SV *const sv2)
79072805 6626{
97aff369 6627 dVAR;
560a288e 6628 STRLEN cur1, cur2;
e1ec3a88 6629 const char *pv1, *pv2;
bd61b366 6630 char *tpv = NULL;
cf48d248 6631 I32 cmp;
a0714e2c 6632 SV *svrecode = NULL;
560a288e 6633
e01b9e88
SC
6634 if (!sv1) {
6635 pv1 = "";
560a288e
GS
6636 cur1 = 0;
6637 }
e01b9e88 6638 else
4d84ee25 6639 pv1 = SvPV_const(sv1, cur1);
560a288e 6640
553e1bcc 6641 if (!sv2) {
e01b9e88 6642 pv2 = "";
560a288e
GS
6643 cur2 = 0;
6644 }
e01b9e88 6645 else
4d84ee25 6646 pv2 = SvPV_const(sv2, cur2);
79072805 6647
cf48d248 6648 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
799ef3cb
JH
6649 /* Differing utf8ness.
6650 * Do not UTF8size the comparands as a side-effect. */
cf48d248 6651 if (SvUTF8(sv1)) {
799ef3cb 6652 if (PL_encoding) {
553e1bcc
AT
6653 svrecode = newSVpvn(pv2, cur2);
6654 sv_recode_to_utf8(svrecode, PL_encoding);
93524f2b 6655 pv2 = SvPV_const(svrecode, cur2);
799ef3cb
JH
6656 }
6657 else {
e1ec3a88 6658 pv2 = tpv = (char*)bytes_to_utf8((const U8*)pv2, &cur2);
799ef3cb 6659 }
cf48d248
JH
6660 }
6661 else {
799ef3cb 6662 if (PL_encoding) {
553e1bcc
AT
6663 svrecode = newSVpvn(pv1, cur1);
6664 sv_recode_to_utf8(svrecode, PL_encoding);
93524f2b 6665 pv1 = SvPV_const(svrecode, cur1);
799ef3cb
JH
6666 }
6667 else {
e1ec3a88 6668 pv1 = tpv = (char*)bytes_to_utf8((const U8*)pv1, &cur1);
799ef3cb 6669 }
cf48d248
JH
6670 }
6671 }
6672
e01b9e88 6673 if (!cur1) {
cf48d248 6674 cmp = cur2 ? -1 : 0;
e01b9e88 6675 } else if (!cur2) {
cf48d248
JH
6676 cmp = 1;
6677 } else {
e1ec3a88 6678 const I32 retval = memcmp((const void*)pv1, (const void*)pv2, cur1 < cur2 ? cur1 : cur2);
e01b9e88
SC
6679
6680 if (retval) {
cf48d248 6681 cmp = retval < 0 ? -1 : 1;
e01b9e88 6682 } else if (cur1 == cur2) {
cf48d248
JH
6683 cmp = 0;
6684 } else {
6685 cmp = cur1 < cur2 ? -1 : 1;
e01b9e88 6686 }
cf48d248 6687 }
16660edb 6688
b37c2d43 6689 SvREFCNT_dec(svrecode);
553e1bcc
AT
6690 if (tpv)
6691 Safefree(tpv);
cf48d248
JH
6692
6693 return cmp;
bbce6d69 6694}
16660edb 6695
c461cf8f
JH
6696/*
6697=for apidoc sv_cmp_locale
6698
645c22ef
DM
6699Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
6700'use bytes' aware, handles get magic, and will coerce its args to strings
d77cdebf 6701if necessary. See also C<sv_cmp>.
c461cf8f
JH
6702
6703=cut
6704*/
6705
bbce6d69 6706I32
ac1e9476 6707Perl_sv_cmp_locale(pTHX_ register SV *const sv1, register SV *const sv2)
bbce6d69 6708{
97aff369 6709 dVAR;
36477c24 6710#ifdef USE_LOCALE_COLLATE
16660edb 6711
bbce6d69 6712 char *pv1, *pv2;
6713 STRLEN len1, len2;
6714 I32 retval;
16660edb 6715
3280af22 6716 if (PL_collation_standard)
bbce6d69 6717 goto raw_compare;
16660edb 6718
bbce6d69 6719 len1 = 0;
8ac85365 6720 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
bbce6d69 6721 len2 = 0;
8ac85365 6722 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
16660edb 6723
bbce6d69 6724 if (!pv1 || !len1) {
6725 if (pv2 && len2)
6726 return -1;
6727 else
6728 goto raw_compare;
6729 }
6730 else {
6731 if (!pv2 || !len2)
6732 return 1;
6733 }
16660edb 6734
bbce6d69 6735 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
16660edb 6736
bbce6d69 6737 if (retval)
16660edb 6738 return retval < 0 ? -1 : 1;
6739
bbce6d69 6740 /*
6741 * When the result of collation is equality, that doesn't mean
6742 * that there are no differences -- some locales exclude some
6743 * characters from consideration. So to avoid false equalities,
6744 * we use the raw string as a tiebreaker.
6745 */
16660edb 6746
bbce6d69 6747 raw_compare:
5f66b61c 6748 /*FALLTHROUGH*/
16660edb 6749
36477c24 6750#endif /* USE_LOCALE_COLLATE */
16660edb 6751
bbce6d69 6752 return sv_cmp(sv1, sv2);
6753}
79072805 6754
645c22ef 6755
36477c24 6756#ifdef USE_LOCALE_COLLATE
645c22ef 6757
7a4c00b4 6758/*
645c22ef
DM
6759=for apidoc sv_collxfrm
6760
6761Add Collate Transform magic to an SV if it doesn't already have it.
6762
6763Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the
6764scalar data of the variable, but transformed to such a format that a normal
6765memory comparison can be used to compare the data according to the locale
6766settings.
6767
6768=cut
6769*/
6770
bbce6d69 6771char *
ac1e9476 6772Perl_sv_collxfrm(pTHX_ SV *const sv, STRLEN *const nxp)
bbce6d69 6773{
97aff369 6774 dVAR;
7a4c00b4 6775 MAGIC *mg;
16660edb 6776
7918f24d
NC
6777 PERL_ARGS_ASSERT_SV_COLLXFRM;
6778
14befaf4 6779 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
3280af22 6780 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
93524f2b
NC
6781 const char *s;
6782 char *xf;
bbce6d69 6783 STRLEN len, xlen;
6784
7a4c00b4 6785 if (mg)
6786 Safefree(mg->mg_ptr);
93524f2b 6787 s = SvPV_const(sv, len);
bbce6d69 6788 if ((xf = mem_collxfrm(s, len, &xlen))) {
7a4c00b4 6789 if (! mg) {
d83f0a82
NC
6790#ifdef PERL_OLD_COPY_ON_WRITE
6791 if (SvIsCOW(sv))
6792 sv_force_normal_flags(sv, 0);
6793#endif
6794 mg = sv_magicext(sv, 0, PERL_MAGIC_collxfrm, &PL_vtbl_collxfrm,
6795 0, 0);
7a4c00b4 6796 assert(mg);
bbce6d69 6797 }
7a4c00b4 6798 mg->mg_ptr = xf;
565764a8 6799 mg->mg_len = xlen;
7a4c00b4 6800 }
6801 else {
ff0cee69 6802 if (mg) {
6803 mg->mg_ptr = NULL;
565764a8 6804 mg->mg_len = -1;
ff0cee69 6805 }
bbce6d69 6806 }
6807 }
7a4c00b4 6808 if (mg && mg->mg_ptr) {
565764a8 6809 *nxp = mg->mg_len;
3280af22 6810 return mg->mg_ptr + sizeof(PL_collation_ix);
bbce6d69 6811 }
6812 else {
6813 *nxp = 0;
6814 return NULL;
16660edb 6815 }
79072805
LW
6816}
6817
36477c24 6818#endif /* USE_LOCALE_COLLATE */
bbce6d69 6819
c461cf8f
JH
6820/*
6821=for apidoc sv_gets
6822
6823Get a line from the filehandle and store it into the SV, optionally
6824appending to the currently-stored string.
6825
6826=cut
6827*/
6828
79072805 6829char *
ac1e9476 6830Perl_sv_gets(pTHX_ register SV *const sv, register PerlIO *const fp, I32 append)
79072805 6831{
97aff369 6832 dVAR;
e1ec3a88 6833 const char *rsptr;
c07a80fd 6834 STRLEN rslen;
6835 register STDCHAR rslast;
6836 register STDCHAR *bp;
6837 register I32 cnt;
9c5ffd7c 6838 I32 i = 0;
8bfdd7d9 6839 I32 rspara = 0;
c07a80fd 6840
7918f24d
NC
6841 PERL_ARGS_ASSERT_SV_GETS;
6842
bc44a8a2
NC
6843 if (SvTHINKFIRST(sv))
6844 sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
765f542d
NC
6845 /* XXX. If you make this PVIV, then copy on write can copy scalars read
6846 from <>.
6847 However, perlbench says it's slower, because the existing swipe code
6848 is faster than copy on write.
6849 Swings and roundabouts. */
862a34c6 6850 SvUPGRADE(sv, SVt_PV);
99491443 6851
ff68c719 6852 SvSCREAM_off(sv);
efd8b2ba
AE
6853
6854 if (append) {
6855 if (PerlIO_isutf8(fp)) {
6856 if (!SvUTF8(sv)) {
6857 sv_utf8_upgrade_nomg(sv);
6858 sv_pos_u2b(sv,&append,0);
6859 }
6860 } else if (SvUTF8(sv)) {
561b68a9 6861 SV * const tsv = newSV(0);
efd8b2ba
AE
6862 sv_gets(tsv, fp, 0);
6863 sv_utf8_upgrade_nomg(tsv);
6864 SvCUR_set(sv,append);
6865 sv_catsv(sv,tsv);
6866 sv_free(tsv);
6867 goto return_string_or_null;
6868 }
6869 }
6870
6871 SvPOK_only(sv);
6872 if (PerlIO_isutf8(fp))
6873 SvUTF8_on(sv);
c07a80fd 6874
923e4eb5 6875 if (IN_PERL_COMPILETIME) {
8bfdd7d9
HS
6876 /* we always read code in line mode */
6877 rsptr = "\n";
6878 rslen = 1;
6879 }
6880 else if (RsSNARF(PL_rs)) {
7a5fa8a2 6881 /* If it is a regular disk file use size from stat() as estimate
acbd132f
JH
6882 of amount we are going to read -- may result in mallocing
6883 more memory than we really need if the layers below reduce
6884 the size we read (e.g. CRLF or a gzip layer).
e468d35b 6885 */
e311fd51 6886 Stat_t st;
e468d35b 6887 if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode)) {
f54cb97a 6888 const Off_t offset = PerlIO_tell(fp);
58f1856e 6889 if (offset != (Off_t) -1 && st.st_size + append > offset) {
e468d35b
NIS
6890 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
6891 }
6892 }
c07a80fd 6893 rsptr = NULL;
6894 rslen = 0;
6895 }
3280af22 6896 else if (RsRECORD(PL_rs)) {
e311fd51 6897 I32 bytesread;
5b2b9c68 6898 char *buffer;
acbd132f 6899 U32 recsize;
048d9da8
CB
6900#ifdef VMS
6901 int fd;
6902#endif
5b2b9c68
HM
6903
6904 /* Grab the size of the record we're getting */
acbd132f 6905 recsize = SvUV(SvRV(PL_rs)); /* RsRECORD() guarantees > 0. */
e311fd51 6906 buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
5b2b9c68
HM
6907 /* Go yank in */
6908#ifdef VMS
6909 /* VMS wants read instead of fread, because fread doesn't respect */
6910 /* RMS record boundaries. This is not necessarily a good thing to be */
e468d35b
NIS
6911 /* doing, but we've got no other real choice - except avoid stdio
6912 as implementation - perhaps write a :vms layer ?
6913 */
048d9da8
CB
6914 fd = PerlIO_fileno(fp);
6915 if (fd == -1) { /* in-memory file from PerlIO::Scalar */
6916 bytesread = PerlIO_read(fp, buffer, recsize);
6917 }
6918 else {
6919 bytesread = PerlLIO_read(fd, buffer, recsize);
6920 }
5b2b9c68
HM
6921#else
6922 bytesread = PerlIO_read(fp, buffer, recsize);
6923#endif
27e6ca2d
AE
6924 if (bytesread < 0)
6925 bytesread = 0;
82f1394b 6926 SvCUR_set(sv, bytesread + append);
e670df4e 6927 buffer[bytesread] = '\0';
efd8b2ba 6928 goto return_string_or_null;
5b2b9c68 6929 }
3280af22 6930 else if (RsPARA(PL_rs)) {
c07a80fd 6931 rsptr = "\n\n";
6932 rslen = 2;
8bfdd7d9 6933 rspara = 1;
c07a80fd 6934 }
7d59b7e4
NIS
6935 else {
6936 /* Get $/ i.e. PL_rs into same encoding as stream wants */
6937 if (PerlIO_isutf8(fp)) {
6938 rsptr = SvPVutf8(PL_rs, rslen);
6939 }
6940 else {
6941 if (SvUTF8(PL_rs)) {
6942 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
6943 Perl_croak(aTHX_ "Wide character in $/");
6944 }
6945 }
93524f2b 6946 rsptr = SvPV_const(PL_rs, rslen);
7d59b7e4
NIS
6947 }
6948 }
6949
c07a80fd 6950 rslast = rslen ? rsptr[rslen - 1] : '\0';
6951
8bfdd7d9 6952 if (rspara) { /* have to do this both before and after */
79072805 6953 do { /* to make sure file boundaries work right */
760ac839 6954 if (PerlIO_eof(fp))
a0d0e21e 6955 return 0;
760ac839 6956 i = PerlIO_getc(fp);
79072805 6957 if (i != '\n') {
a0d0e21e
LW
6958 if (i == -1)
6959 return 0;
760ac839 6960 PerlIO_ungetc(fp,i);
79072805
LW
6961 break;
6962 }
6963 } while (i != EOF);
6964 }
c07a80fd 6965
760ac839
LW
6966 /* See if we know enough about I/O mechanism to cheat it ! */
6967
6968 /* This used to be #ifdef test - it is made run-time test for ease
1c846c1f 6969 of abstracting out stdio interface. One call should be cheap
760ac839
LW
6970 enough here - and may even be a macro allowing compile
6971 time optimization.
6972 */
6973
6974 if (PerlIO_fast_gets(fp)) {
6975
6976 /*
6977 * We're going to steal some values from the stdio struct
6978 * and put EVERYTHING in the innermost loop into registers.
6979 */
6980 register STDCHAR *ptr;
6981 STRLEN bpx;
6982 I32 shortbuffered;
6983
16660edb 6984#if defined(VMS) && defined(PERLIO_IS_STDIO)
6985 /* An ungetc()d char is handled separately from the regular
6986 * buffer, so we getc() it back out and stuff it in the buffer.
6987 */
6988 i = PerlIO_getc(fp);
6989 if (i == EOF) return 0;
6990 *(--((*fp)->_ptr)) = (unsigned char) i;
6991 (*fp)->_cnt++;
6992#endif
c07a80fd 6993
c2960299 6994 /* Here is some breathtakingly efficient cheating */
c07a80fd 6995
a20bf0c3 6996 cnt = PerlIO_get_cnt(fp); /* get count into register */
e468d35b 6997 /* make sure we have the room */
7a5fa8a2 6998 if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
e468d35b 6999 /* Not room for all of it
7a5fa8a2 7000 if we are looking for a separator and room for some
e468d35b
NIS
7001 */
7002 if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
7a5fa8a2 7003 /* just process what we have room for */
79072805
LW
7004 shortbuffered = cnt - SvLEN(sv) + append + 1;
7005 cnt -= shortbuffered;
7006 }
7007 else {
7008 shortbuffered = 0;
bbce6d69 7009 /* remember that cnt can be negative */
eb160463 7010 SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
79072805
LW
7011 }
7012 }
7a5fa8a2 7013 else
79072805 7014 shortbuffered = 0;
3f7c398e 7015 bp = (STDCHAR*)SvPVX_const(sv) + append; /* move these two too to registers */
a20bf0c3 7016 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
16660edb 7017 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7018 "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
16660edb 7019 DEBUG_P(PerlIO_printf(Perl_debug_log,
ba7abf9d 7020 "Screamer: entering: PerlIO * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 7021 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 7022 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
79072805
LW
7023 for (;;) {
7024 screamer:
93a17b20 7025 if (cnt > 0) {
c07a80fd 7026 if (rslen) {
760ac839
LW
7027 while (cnt > 0) { /* this | eat */
7028 cnt--;
c07a80fd 7029 if ((*bp++ = *ptr++) == rslast) /* really | dust */
7030 goto thats_all_folks; /* screams | sed :-) */
7031 }
7032 }
7033 else {
1c846c1f
NIS
7034 Copy(ptr, bp, cnt, char); /* this | eat */
7035 bp += cnt; /* screams | dust */
c07a80fd 7036 ptr += cnt; /* louder | sed :-) */
a5f75d66 7037 cnt = 0;
93a17b20 7038 }
79072805
LW
7039 }
7040
748a9306 7041 if (shortbuffered) { /* oh well, must extend */
79072805
LW
7042 cnt = shortbuffered;
7043 shortbuffered = 0;
3f7c398e 7044 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
79072805
LW
7045 SvCUR_set(sv, bpx);
7046 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
3f7c398e 7047 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
79072805
LW
7048 continue;
7049 }
7050
16660edb 7051 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841
GS
7052 "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n",
7053 PTR2UV(ptr),(long)cnt));
cc00df79 7054 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
ba7abf9d 7055#if 0
16660edb 7056 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7057 "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 7058 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 7059 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
ba7abf9d 7060#endif
1c846c1f 7061 /* This used to call 'filbuf' in stdio form, but as that behaves like
774d564b 7062 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
7063 another abstraction. */
760ac839 7064 i = PerlIO_getc(fp); /* get more characters */
ba7abf9d 7065#if 0
16660edb 7066 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7067 "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 7068 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 7069 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
ba7abf9d 7070#endif
a20bf0c3
JH
7071 cnt = PerlIO_get_cnt(fp);
7072 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
16660edb 7073 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7074 "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
79072805 7075
748a9306
LW
7076 if (i == EOF) /* all done for ever? */
7077 goto thats_really_all_folks;
7078
3f7c398e 7079 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
79072805
LW
7080 SvCUR_set(sv, bpx);
7081 SvGROW(sv, bpx + cnt + 2);
3f7c398e 7082 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
c07a80fd 7083
eb160463 7084 *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */
79072805 7085
c07a80fd 7086 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
79072805 7087 goto thats_all_folks;
79072805
LW
7088 }
7089
7090thats_all_folks:
3f7c398e 7091 if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX_const(sv)) < rslen) ||
36477c24 7092 memNE((char*)bp - rslen, rsptr, rslen))
760ac839 7093 goto screamer; /* go back to the fray */
79072805
LW
7094thats_really_all_folks:
7095 if (shortbuffered)
7096 cnt += shortbuffered;
16660edb 7097 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7098 "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
cc00df79 7099 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */
16660edb 7100 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7101 "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 7102 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 7103 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
79072805 7104 *bp = '\0';
3f7c398e 7105 SvCUR_set(sv, bp - (STDCHAR*)SvPVX_const(sv)); /* set length */
16660edb 7106 DEBUG_P(PerlIO_printf(Perl_debug_log,
fb73857a 7107 "Screamer: done, len=%ld, string=|%.*s|\n",
3f7c398e 7108 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX_const(sv)));
760ac839
LW
7109 }
7110 else
79072805 7111 {
6edd2cd5 7112 /*The big, slow, and stupid way. */
27da23d5 7113#ifdef USE_HEAP_INSTEAD_OF_STACK /* Even slower way. */
cbbf8932 7114 STDCHAR *buf = NULL;
a02a5408 7115 Newx(buf, 8192, STDCHAR);
6edd2cd5 7116 assert(buf);
4d2c4e07 7117#else
6edd2cd5 7118 STDCHAR buf[8192];
4d2c4e07 7119#endif
79072805 7120
760ac839 7121screamer2:
c07a80fd 7122 if (rslen) {
00b6aa41 7123 register const STDCHAR * const bpe = buf + sizeof(buf);
760ac839 7124 bp = buf;
eb160463 7125 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
760ac839
LW
7126 ; /* keep reading */
7127 cnt = bp - buf;
c07a80fd 7128 }
7129 else {
760ac839 7130 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
16660edb 7131 /* Accomodate broken VAXC compiler, which applies U8 cast to
7132 * both args of ?: operator, causing EOF to change into 255
7133 */
37be0adf 7134 if (cnt > 0)
cbe9e203
JH
7135 i = (U8)buf[cnt - 1];
7136 else
37be0adf 7137 i = EOF;
c07a80fd 7138 }
79072805 7139
cbe9e203
JH
7140 if (cnt < 0)
7141 cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
7142 if (append)
7143 sv_catpvn(sv, (char *) buf, cnt);
7144 else
7145 sv_setpvn(sv, (char *) buf, cnt);
c07a80fd 7146
7147 if (i != EOF && /* joy */
7148 (!rslen ||
7149 SvCUR(sv) < rslen ||
3f7c398e 7150 memNE(SvPVX_const(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
79072805
LW
7151 {
7152 append = -1;
63e4d877
CS
7153 /*
7154 * If we're reading from a TTY and we get a short read,
7155 * indicating that the user hit his EOF character, we need
7156 * to notice it now, because if we try to read from the TTY
7157 * again, the EOF condition will disappear.
7158 *
7159 * The comparison of cnt to sizeof(buf) is an optimization
7160 * that prevents unnecessary calls to feof().
7161 *
7162 * - jik 9/25/96
7163 */
bb7a0f54 7164 if (!(cnt < (I32)sizeof(buf) && PerlIO_eof(fp)))
63e4d877 7165 goto screamer2;
79072805 7166 }
6edd2cd5 7167
27da23d5 7168#ifdef USE_HEAP_INSTEAD_OF_STACK
6edd2cd5
JH
7169 Safefree(buf);
7170#endif
79072805
LW
7171 }
7172
8bfdd7d9 7173 if (rspara) { /* have to do this both before and after */
c07a80fd 7174 while (i != EOF) { /* to make sure file boundaries work right */
760ac839 7175 i = PerlIO_getc(fp);
79072805 7176 if (i != '\n') {
760ac839 7177 PerlIO_ungetc(fp,i);
79072805
LW
7178 break;
7179 }
7180 }
7181 }
c07a80fd 7182
efd8b2ba 7183return_string_or_null:
bd61b366 7184 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
79072805
LW
7185}
7186
954c1994
GS
7187/*
7188=for apidoc sv_inc
7189
645c22ef
DM
7190Auto-increment of the value in the SV, doing string to numeric conversion
7191if necessary. Handles 'get' magic.
954c1994
GS
7192
7193=cut
7194*/
7195
79072805 7196void
ac1e9476 7197Perl_sv_inc(pTHX_ register SV *const sv)
79072805 7198{
97aff369 7199 dVAR;
79072805 7200 register char *d;
463ee0b2 7201 int flags;
79072805
LW
7202
7203 if (!sv)
7204 return;
5b295bef 7205 SvGETMAGIC(sv);
ed6116ce 7206 if (SvTHINKFIRST(sv)) {
765f542d
NC
7207 if (SvIsCOW(sv))
7208 sv_force_normal_flags(sv, 0);
0f15f207 7209 if (SvREADONLY(sv)) {
923e4eb5 7210 if (IN_PERL_RUNTIME)
f1f66076 7211 Perl_croak(aTHX_ "%s", PL_no_modify);
0f15f207 7212 }
a0d0e21e 7213 if (SvROK(sv)) {
b5be31e9 7214 IV i;
9e7bc3e8
JD
7215 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
7216 return;
56431972 7217 i = PTR2IV(SvRV(sv));
b5be31e9
SM
7218 sv_unref(sv);
7219 sv_setiv(sv, i);
a0d0e21e 7220 }
ed6116ce 7221 }
8990e307 7222 flags = SvFLAGS(sv);
28e5dec8
JH
7223 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
7224 /* It's (privately or publicly) a float, but not tested as an
7225 integer, so test it to see. */
d460ef45 7226 (void) SvIV(sv);
28e5dec8
JH
7227 flags = SvFLAGS(sv);
7228 }
7229 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
7230 /* It's publicly an integer, or privately an integer-not-float */
59d8ce62 7231#ifdef PERL_PRESERVE_IVUV
28e5dec8 7232 oops_its_int:
59d8ce62 7233#endif
25da4f38
IZ
7234 if (SvIsUV(sv)) {
7235 if (SvUVX(sv) == UV_MAX)
a1e868e7 7236 sv_setnv(sv, UV_MAX_P1);
25da4f38
IZ
7237 else
7238 (void)SvIOK_only_UV(sv);
607fa7f2 7239 SvUV_set(sv, SvUVX(sv) + 1);
25da4f38
IZ
7240 } else {
7241 if (SvIVX(sv) == IV_MAX)
28e5dec8 7242 sv_setuv(sv, (UV)IV_MAX + 1);
25da4f38
IZ
7243 else {
7244 (void)SvIOK_only(sv);
45977657 7245 SvIV_set(sv, SvIVX(sv) + 1);
1c846c1f 7246 }
55497cff 7247 }
79072805
LW
7248 return;
7249 }
28e5dec8 7250 if (flags & SVp_NOK) {
b88df990 7251 const NV was = SvNVX(sv);
b68c599a
NC
7252 if (NV_OVERFLOWS_INTEGERS_AT &&
7253 was >= NV_OVERFLOWS_INTEGERS_AT && ckWARN(WARN_IMPRECISION)) {
b88df990
NC
7254 Perl_warner(aTHX_ packWARN(WARN_IMPRECISION),
7255 "Lost precision when incrementing %" NVff " by 1",
7256 was);
7257 }
28e5dec8 7258 (void)SvNOK_only(sv);
b68c599a 7259 SvNV_set(sv, was + 1.0);
28e5dec8
JH
7260 return;
7261 }
7262
3f7c398e 7263 if (!(flags & SVp_POK) || !*SvPVX_const(sv)) {
28e5dec8 7264 if ((flags & SVTYPEMASK) < SVt_PVIV)
f5282e15 7265 sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV ? SVt_PVIV : SVt_IV));
28e5dec8 7266 (void)SvIOK_only(sv);
45977657 7267 SvIV_set(sv, 1);
79072805
LW
7268 return;
7269 }
463ee0b2 7270 d = SvPVX(sv);
79072805
LW
7271 while (isALPHA(*d)) d++;
7272 while (isDIGIT(*d)) d++;
7273 if (*d) {
28e5dec8 7274#ifdef PERL_PRESERVE_IVUV
d1be9408 7275 /* Got to punt this as an integer if needs be, but we don't issue
28e5dec8
JH
7276 warnings. Probably ought to make the sv_iv_please() that does
7277 the conversion if possible, and silently. */
504618e9 7278 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
28e5dec8
JH
7279 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
7280 /* Need to try really hard to see if it's an integer.
7281 9.22337203685478e+18 is an integer.
7282 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
7283 so $a="9.22337203685478e+18"; $a+0; $a++
7284 needs to be the same as $a="9.22337203685478e+18"; $a++
7285 or we go insane. */
d460ef45 7286
28e5dec8
JH
7287 (void) sv_2iv(sv);
7288 if (SvIOK(sv))
7289 goto oops_its_int;
7290
7291 /* sv_2iv *should* have made this an NV */
7292 if (flags & SVp_NOK) {
7293 (void)SvNOK_only(sv);
9d6ce603 7294 SvNV_set(sv, SvNVX(sv) + 1.0);
28e5dec8
JH
7295 return;
7296 }
7297 /* I don't think we can get here. Maybe I should assert this
7298 And if we do get here I suspect that sv_setnv will croak. NWC
7299 Fall through. */
7300#if defined(USE_LONG_DOUBLE)
7301 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
3f7c398e 7302 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8 7303#else
1779d84d 7304 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
3f7c398e 7305 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8
JH
7306#endif
7307 }
7308#endif /* PERL_PRESERVE_IVUV */
3f7c398e 7309 sv_setnv(sv,Atof(SvPVX_const(sv)) + 1.0);
79072805
LW
7310 return;
7311 }
7312 d--;
3f7c398e 7313 while (d >= SvPVX_const(sv)) {
79072805
LW
7314 if (isDIGIT(*d)) {
7315 if (++*d <= '9')
7316 return;
7317 *(d--) = '0';
7318 }
7319 else {
9d116dd7
JH
7320#ifdef EBCDIC
7321 /* MKS: The original code here died if letters weren't consecutive.
7322 * at least it didn't have to worry about non-C locales. The
7323 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
1c846c1f 7324 * arranged in order (although not consecutively) and that only
9d116dd7
JH
7325 * [A-Za-z] are accepted by isALPHA in the C locale.
7326 */
7327 if (*d != 'z' && *d != 'Z') {
7328 do { ++*d; } while (!isALPHA(*d));
7329 return;
7330 }
7331 *(d--) -= 'z' - 'a';
7332#else
79072805
LW
7333 ++*d;
7334 if (isALPHA(*d))
7335 return;
7336 *(d--) -= 'z' - 'a' + 1;
9d116dd7 7337#endif
79072805
LW
7338 }
7339 }
7340 /* oh,oh, the number grew */
7341 SvGROW(sv, SvCUR(sv) + 2);
b162af07 7342 SvCUR_set(sv, SvCUR(sv) + 1);
3f7c398e 7343 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX_const(sv); d--)
79072805
LW
7344 *d = d[-1];
7345 if (isDIGIT(d[1]))
7346 *d = '1';
7347 else
7348 *d = d[1];
7349}
7350
954c1994
GS
7351/*
7352=for apidoc sv_dec
7353
645c22ef
DM
7354Auto-decrement of the value in the SV, doing string to numeric conversion
7355if necessary. Handles 'get' magic.
954c1994
GS
7356
7357=cut
7358*/
7359
79072805 7360void
ac1e9476 7361Perl_sv_dec(pTHX_ register SV *const sv)
79072805 7362{
97aff369 7363 dVAR;
463ee0b2
LW
7364 int flags;
7365
79072805
LW
7366 if (!sv)
7367 return;
5b295bef 7368 SvGETMAGIC(sv);
ed6116ce 7369 if (SvTHINKFIRST(sv)) {
765f542d
NC
7370 if (SvIsCOW(sv))
7371 sv_force_normal_flags(sv, 0);
0f15f207 7372 if (SvREADONLY(sv)) {
923e4eb5 7373 if (IN_PERL_RUNTIME)
f1f66076 7374 Perl_croak(aTHX_ "%s", PL_no_modify);
0f15f207 7375 }
a0d0e21e 7376 if (SvROK(sv)) {
b5be31e9 7377 IV i;
9e7bc3e8
JD
7378 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
7379 return;
56431972 7380 i = PTR2IV(SvRV(sv));
b5be31e9
SM
7381 sv_unref(sv);
7382 sv_setiv(sv, i);
a0d0e21e 7383 }
ed6116ce 7384 }
28e5dec8
JH
7385 /* Unlike sv_inc we don't have to worry about string-never-numbers
7386 and keeping them magic. But we mustn't warn on punting */
8990e307 7387 flags = SvFLAGS(sv);
28e5dec8
JH
7388 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
7389 /* It's publicly an integer, or privately an integer-not-float */
59d8ce62 7390#ifdef PERL_PRESERVE_IVUV
28e5dec8 7391 oops_its_int:
59d8ce62 7392#endif
25da4f38
IZ
7393 if (SvIsUV(sv)) {
7394 if (SvUVX(sv) == 0) {
7395 (void)SvIOK_only(sv);
45977657 7396 SvIV_set(sv, -1);
25da4f38
IZ
7397 }
7398 else {
7399 (void)SvIOK_only_UV(sv);
f4eee32f 7400 SvUV_set(sv, SvUVX(sv) - 1);
1c846c1f 7401 }
25da4f38 7402 } else {
b88df990
NC
7403 if (SvIVX(sv) == IV_MIN) {
7404 sv_setnv(sv, (NV)IV_MIN);
7405 goto oops_its_num;
7406 }
25da4f38
IZ
7407 else {
7408 (void)SvIOK_only(sv);
45977657 7409 SvIV_set(sv, SvIVX(sv) - 1);
1c846c1f 7410 }
55497cff 7411 }
7412 return;
7413 }
28e5dec8 7414 if (flags & SVp_NOK) {
b88df990
NC
7415 oops_its_num:
7416 {
7417 const NV was = SvNVX(sv);
b68c599a
NC
7418 if (NV_OVERFLOWS_INTEGERS_AT &&
7419 was <= -NV_OVERFLOWS_INTEGERS_AT && ckWARN(WARN_IMPRECISION)) {
b88df990
NC
7420 Perl_warner(aTHX_ packWARN(WARN_IMPRECISION),
7421 "Lost precision when decrementing %" NVff " by 1",
7422 was);
7423 }
7424 (void)SvNOK_only(sv);
b68c599a 7425 SvNV_set(sv, was - 1.0);
b88df990
NC
7426 return;
7427 }
28e5dec8 7428 }
8990e307 7429 if (!(flags & SVp_POK)) {
ef088171
NC
7430 if ((flags & SVTYPEMASK) < SVt_PVIV)
7431 sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV) ? SVt_PVIV : SVt_IV);
7432 SvIV_set(sv, -1);
7433 (void)SvIOK_only(sv);
79072805
LW
7434 return;
7435 }
28e5dec8
JH
7436#ifdef PERL_PRESERVE_IVUV
7437 {
504618e9 7438 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
28e5dec8
JH
7439 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
7440 /* Need to try really hard to see if it's an integer.
7441 9.22337203685478e+18 is an integer.
7442 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
7443 so $a="9.22337203685478e+18"; $a+0; $a--
7444 needs to be the same as $a="9.22337203685478e+18"; $a--
7445 or we go insane. */
d460ef45 7446
28e5dec8
JH
7447 (void) sv_2iv(sv);
7448 if (SvIOK(sv))
7449 goto oops_its_int;
7450
7451 /* sv_2iv *should* have made this an NV */
7452 if (flags & SVp_NOK) {
7453 (void)SvNOK_only(sv);
9d6ce603 7454 SvNV_set(sv, SvNVX(sv) - 1.0);
28e5dec8
JH
7455 return;
7456 }
7457 /* I don't think we can get here. Maybe I should assert this
7458 And if we do get here I suspect that sv_setnv will croak. NWC
7459 Fall through. */
7460#if defined(USE_LONG_DOUBLE)
7461 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
3f7c398e 7462 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8 7463#else
1779d84d 7464 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
3f7c398e 7465 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8
JH
7466#endif
7467 }
7468 }
7469#endif /* PERL_PRESERVE_IVUV */
3f7c398e 7470 sv_setnv(sv,Atof(SvPVX_const(sv)) - 1.0); /* punt */
79072805
LW
7471}
7472
954c1994
GS
7473/*
7474=for apidoc sv_mortalcopy
7475
645c22ef 7476Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
d4236ebc
DM
7477The new SV is marked as mortal. It will be destroyed "soon", either by an
7478explicit call to FREETMPS, or by an implicit call at places such as
7479statement boundaries. See also C<sv_newmortal> and C<sv_2mortal>.
954c1994
GS
7480
7481=cut
7482*/
7483
79072805
LW
7484/* Make a string that will exist for the duration of the expression
7485 * evaluation. Actually, it may have to last longer than that, but
7486 * hopefully we won't free it until it has been assigned to a
7487 * permanent location. */
7488
7489SV *
ac1e9476 7490Perl_sv_mortalcopy(pTHX_ SV *const oldstr)
79072805 7491{
97aff369 7492 dVAR;
463ee0b2 7493 register SV *sv;
b881518d 7494
4561caa4 7495 new_SV(sv);
79072805 7496 sv_setsv(sv,oldstr);
677b06e3
GS
7497 EXTEND_MORTAL(1);
7498 PL_tmps_stack[++PL_tmps_ix] = sv;
8990e307
LW
7499 SvTEMP_on(sv);
7500 return sv;
7501}
7502
954c1994
GS
7503/*
7504=for apidoc sv_newmortal
7505
645c22ef 7506Creates a new null SV which is mortal. The reference count of the SV is
d4236ebc
DM
7507set to 1. It will be destroyed "soon", either by an explicit call to
7508FREETMPS, or by an implicit call at places such as statement boundaries.
7509See also C<sv_mortalcopy> and C<sv_2mortal>.
954c1994
GS
7510
7511=cut
7512*/
7513
8990e307 7514SV *
864dbfa3 7515Perl_sv_newmortal(pTHX)
8990e307 7516{
97aff369 7517 dVAR;
8990e307
LW
7518 register SV *sv;
7519
4561caa4 7520 new_SV(sv);
8990e307 7521 SvFLAGS(sv) = SVs_TEMP;
677b06e3
GS
7522 EXTEND_MORTAL(1);
7523 PL_tmps_stack[++PL_tmps_ix] = sv;
79072805
LW
7524 return sv;
7525}
7526
59cd0e26
NC
7527
7528/*
7529=for apidoc newSVpvn_flags
7530
7531Creates a new SV and copies a string into it. The reference count for the
7532SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
7533string. You are responsible for ensuring that the source string is at least
7534C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
7535Currently the only flag bits accepted are C<SVf_UTF8> and C<SVs_TEMP>.
7536If C<SVs_TEMP> is set, then C<sv2mortal()> is called on the result before
7537returning. If C<SVf_UTF8> is set, then it will be set on the new SV.
7538C<newSVpvn_utf8()> is a convenience wrapper for this function, defined as
7539
7540 #define newSVpvn_utf8(s, len, u) \
7541 newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
7542
7543=cut
7544*/
7545
7546SV *
23f13727 7547Perl_newSVpvn_flags(pTHX_ const char *const s, const STRLEN len, const U32 flags)
59cd0e26
NC
7548{
7549 dVAR;
7550 register SV *sv;
7551
7552 /* All the flags we don't support must be zero.
7553 And we're new code so I'm going to assert this from the start. */
7554 assert(!(flags & ~(SVf_UTF8|SVs_TEMP)));
7555 new_SV(sv);
7556 sv_setpvn(sv,s,len);
7557 SvFLAGS(sv) |= (flags & SVf_UTF8);
7558 return (flags & SVs_TEMP) ? sv_2mortal(sv) : sv;
7559}
7560
954c1994
GS
7561/*
7562=for apidoc sv_2mortal
7563
d4236ebc
DM
7564Marks an existing SV as mortal. The SV will be destroyed "soon", either
7565by an explicit call to FREETMPS, or by an implicit call at places such as
37d2ac18
NC
7566statement boundaries. SvTEMP() is turned on which means that the SV's
7567string buffer can be "stolen" if this SV is copied. See also C<sv_newmortal>
7568and C<sv_mortalcopy>.
954c1994
GS
7569
7570=cut
7571*/
7572
79072805 7573SV *
23f13727 7574Perl_sv_2mortal(pTHX_ register SV *const sv)
79072805 7575{
27da23d5 7576 dVAR;
79072805 7577 if (!sv)
7a5b473e 7578 return NULL;
d689ffdd 7579 if (SvREADONLY(sv) && SvIMMORTAL(sv))
11162842 7580 return sv;
677b06e3
GS
7581 EXTEND_MORTAL(1);
7582 PL_tmps_stack[++PL_tmps_ix] = sv;
8990e307 7583 SvTEMP_on(sv);
79072805
LW
7584 return sv;
7585}
7586
954c1994
GS
7587/*
7588=for apidoc newSVpv
7589
7590Creates a new SV and copies a string into it. The reference count for the
7591SV is set to 1. If C<len> is zero, Perl will compute the length using
7592strlen(). For efficiency, consider using C<newSVpvn> instead.
7593
7594=cut
7595*/
7596
79072805 7597SV *
23f13727 7598Perl_newSVpv(pTHX_ const char *const s, const STRLEN len)
79072805 7599{
97aff369 7600 dVAR;
463ee0b2 7601 register SV *sv;
79072805 7602
4561caa4 7603 new_SV(sv);
ddfa59c7 7604 sv_setpvn(sv, s, len || s == NULL ? len : strlen(s));
79072805
LW
7605 return sv;
7606}
7607
954c1994
GS
7608/*
7609=for apidoc newSVpvn
7610
7611Creates a new SV and copies a string into it. The reference count for the
1c846c1f 7612SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
954c1994 7613string. You are responsible for ensuring that the source string is at least
9e09f5f2 7614C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
954c1994
GS
7615
7616=cut
7617*/
7618
9da1e3b5 7619SV *
23f13727 7620Perl_newSVpvn(pTHX_ const char *const s, const STRLEN len)
9da1e3b5 7621{
97aff369 7622 dVAR;
9da1e3b5
MUN
7623 register SV *sv;
7624
7625 new_SV(sv);
9da1e3b5
MUN
7626 sv_setpvn(sv,s,len);
7627 return sv;
7628}
7629
740cce10 7630/*
926f8064 7631=for apidoc newSVhek
bd08039b
NC
7632
7633Creates a new SV from the hash key structure. It will generate scalars that
5aaec2b4
NC
7634point to the shared string table where possible. Returns a new (undefined)
7635SV if the hek is NULL.
bd08039b
NC
7636
7637=cut
7638*/
7639
7640SV *
23f13727 7641Perl_newSVhek(pTHX_ const HEK *const hek)
bd08039b 7642{
97aff369 7643 dVAR;
5aaec2b4
NC
7644 if (!hek) {
7645 SV *sv;
7646
7647 new_SV(sv);
7648 return sv;
7649 }
7650
bd08039b
NC
7651 if (HEK_LEN(hek) == HEf_SVKEY) {
7652 return newSVsv(*(SV**)HEK_KEY(hek));
7653 } else {
7654 const int flags = HEK_FLAGS(hek);
7655 if (flags & HVhek_WASUTF8) {
7656 /* Trouble :-)
7657 Andreas would like keys he put in as utf8 to come back as utf8
7658 */
7659 STRLEN utf8_len = HEK_LEN(hek);
b64e5050
AL
7660 const U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
7661 SV * const sv = newSVpvn ((const char*)as_utf8, utf8_len);
bd08039b
NC
7662
7663 SvUTF8_on (sv);
7664 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
7665 return sv;
45e34800 7666 } else if (flags & (HVhek_REHASH|HVhek_UNSHARED)) {
bd08039b
NC
7667 /* We don't have a pointer to the hv, so we have to replicate the
7668 flag into every HEK. This hv is using custom a hasing
7669 algorithm. Hence we can't return a shared string scalar, as
7670 that would contain the (wrong) hash value, and might get passed
45e34800
NC
7671 into an hv routine with a regular hash.
7672 Similarly, a hash that isn't using shared hash keys has to have
7673 the flag in every key so that we know not to try to call
7674 share_hek_kek on it. */
bd08039b 7675
b64e5050 7676 SV * const sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
bd08039b
NC
7677 if (HEK_UTF8(hek))
7678 SvUTF8_on (sv);
7679 return sv;
7680 }
7681 /* This will be overwhelminly the most common case. */
409dfe77
NC
7682 {
7683 /* Inline most of newSVpvn_share(), because share_hek_hek() is far
7684 more efficient than sharepvn(). */
7685 SV *sv;
7686
7687 new_SV(sv);
7688 sv_upgrade(sv, SVt_PV);
7689 SvPV_set(sv, (char *)HEK_KEY(share_hek_hek(hek)));
7690 SvCUR_set(sv, HEK_LEN(hek));
7691 SvLEN_set(sv, 0);
7692 SvREADONLY_on(sv);
7693 SvFAKE_on(sv);
7694 SvPOK_on(sv);
7695 if (HEK_UTF8(hek))
7696 SvUTF8_on(sv);
7697 return sv;
7698 }
bd08039b
NC
7699 }
7700}
7701
1c846c1f
NIS
7702/*
7703=for apidoc newSVpvn_share
7704
3f7c398e 7705Creates a new SV with its SvPVX_const pointing to a shared string in the string
645c22ef 7706table. If the string does not already exist in the table, it is created
758fcfc1
VP
7707first. Turns on READONLY and FAKE. If the C<hash> parameter is non-zero, that
7708value is used; otherwise the hash is computed. The string's hash can be later
7709be retrieved from the SV with the C<SvSHARED_HASH()> macro. The idea here is
7710that as the string table is used for shared hash keys these strings will have
7711SvPVX_const == HeKEY and hash lookup will avoid string compare.
1c846c1f
NIS
7712
7713=cut
7714*/
7715
7716SV *
c3654f1a 7717Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
1c846c1f 7718{
97aff369 7719 dVAR;
1c846c1f 7720 register SV *sv;
c3654f1a 7721 bool is_utf8 = FALSE;
a51caccf
NC
7722 const char *const orig_src = src;
7723
c3654f1a 7724 if (len < 0) {
77caf834 7725 STRLEN tmplen = -len;
c3654f1a 7726 is_utf8 = TRUE;
75a54232 7727 /* See the note in hv.c:hv_fetch() --jhi */
e1ec3a88 7728 src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
75a54232
JH
7729 len = tmplen;
7730 }
1c846c1f 7731 if (!hash)
5afd6d42 7732 PERL_HASH(hash, src, len);
1c846c1f 7733 new_SV(sv);
f46ee248
NC
7734 /* The logic for this is inlined in S_mro_get_linear_isa_dfs(), so if it
7735 changes here, update it there too. */
bdd68bc3 7736 sv_upgrade(sv, SVt_PV);
f880fe2f 7737 SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
b162af07 7738 SvCUR_set(sv, len);
b162af07 7739 SvLEN_set(sv, 0);
1c846c1f
NIS
7740 SvREADONLY_on(sv);
7741 SvFAKE_on(sv);
7742 SvPOK_on(sv);
c3654f1a
IH
7743 if (is_utf8)
7744 SvUTF8_on(sv);
a51caccf
NC
7745 if (src != orig_src)
7746 Safefree(src);
1c846c1f
NIS
7747 return sv;
7748}
7749
645c22ef 7750
cea2e8a9 7751#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
7752
7753/* pTHX_ magic can't cope with varargs, so this is a no-context
7754 * version of the main function, (which may itself be aliased to us).
7755 * Don't access this version directly.
7756 */
7757
46fc3d4c 7758SV *
23f13727 7759Perl_newSVpvf_nocontext(const char *const pat, ...)
46fc3d4c 7760{
cea2e8a9 7761 dTHX;
46fc3d4c 7762 register SV *sv;
7763 va_list args;
7918f24d
NC
7764
7765 PERL_ARGS_ASSERT_NEWSVPVF_NOCONTEXT;
7766
46fc3d4c 7767 va_start(args, pat);
c5be433b 7768 sv = vnewSVpvf(pat, &args);
46fc3d4c 7769 va_end(args);
7770 return sv;
7771}
cea2e8a9 7772#endif
46fc3d4c 7773
954c1994
GS
7774/*
7775=for apidoc newSVpvf
7776
645c22ef 7777Creates a new SV and initializes it with the string formatted like
954c1994
GS
7778C<sprintf>.
7779
7780=cut
7781*/
7782
cea2e8a9 7783SV *
23f13727 7784Perl_newSVpvf(pTHX_ const char *const pat, ...)
cea2e8a9
GS
7785{
7786 register SV *sv;
7787 va_list args;
7918f24d
NC
7788
7789 PERL_ARGS_ASSERT_NEWSVPVF;
7790
cea2e8a9 7791 va_start(args, pat);
c5be433b 7792 sv = vnewSVpvf(pat, &args);
cea2e8a9
GS
7793 va_end(args);
7794 return sv;
7795}
46fc3d4c 7796
645c22ef
DM
7797/* backend for newSVpvf() and newSVpvf_nocontext() */
7798
79072805 7799SV *
23f13727 7800Perl_vnewSVpvf(pTHX_ const char *const pat, va_list *const args)
c5be433b 7801{
97aff369 7802 dVAR;
c5be433b 7803 register SV *sv;
7918f24d
NC
7804
7805 PERL_ARGS_ASSERT_VNEWSVPVF;
7806
c5be433b 7807 new_SV(sv);
4608196e 7808 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
c5be433b
GS
7809 return sv;
7810}
7811
954c1994
GS
7812/*
7813=for apidoc newSVnv
7814
7815Creates a new SV and copies a floating point value into it.
7816The reference count for the SV is set to 1.
7817
7818=cut
7819*/
7820
c5be433b 7821SV *
23f13727 7822Perl_newSVnv(pTHX_ const NV n)
79072805 7823{
97aff369 7824 dVAR;
463ee0b2 7825 register SV *sv;
79072805 7826
4561caa4 7827 new_SV(sv);
79072805
LW
7828 sv_setnv(sv,n);
7829 return sv;
7830}
7831
954c1994
GS
7832/*
7833=for apidoc newSViv
7834
7835Creates a new SV and copies an integer into it. The reference count for the
7836SV is set to 1.
7837
7838=cut
7839*/
7840
79072805 7841SV *
23f13727 7842Perl_newSViv(pTHX_ const IV i)
79072805 7843{
97aff369 7844 dVAR;
463ee0b2 7845 register SV *sv;
79072805 7846
4561caa4 7847 new_SV(sv);
79072805
LW
7848 sv_setiv(sv,i);
7849 return sv;
7850}
7851
954c1994 7852/*
1a3327fb
JH
7853=for apidoc newSVuv
7854
7855Creates a new SV and copies an unsigned integer into it.
7856The reference count for the SV is set to 1.
7857
7858=cut
7859*/
7860
7861SV *
23f13727 7862Perl_newSVuv(pTHX_ const UV u)
1a3327fb 7863{
97aff369 7864 dVAR;
1a3327fb
JH
7865 register SV *sv;
7866
7867 new_SV(sv);
7868 sv_setuv(sv,u);
7869 return sv;
7870}
7871
7872/*
b9f83d2f
NC
7873=for apidoc newSV_type
7874
c41f7ed2 7875Creates a new SV, of the type specified. The reference count for the new SV
b9f83d2f
NC
7876is set to 1.
7877
7878=cut
7879*/
7880
7881SV *
fe9845cc 7882Perl_newSV_type(pTHX_ const svtype type)
b9f83d2f
NC
7883{
7884 register SV *sv;
7885
7886 new_SV(sv);
7887 sv_upgrade(sv, type);
7888 return sv;
7889}
7890
7891/*
954c1994
GS
7892=for apidoc newRV_noinc
7893
7894Creates an RV wrapper for an SV. The reference count for the original
7895SV is B<not> incremented.
7896
7897=cut
7898*/
7899
2304df62 7900SV *
23f13727 7901Perl_newRV_noinc(pTHX_ SV *const tmpRef)
2304df62 7902{
97aff369 7903 dVAR;
4df7f6af 7904 register SV *sv = newSV_type(SVt_IV);
7918f24d
NC
7905
7906 PERL_ARGS_ASSERT_NEWRV_NOINC;
7907
76e3520e 7908 SvTEMP_off(tmpRef);
b162af07 7909 SvRV_set(sv, tmpRef);
2304df62 7910 SvROK_on(sv);
2304df62
AD
7911 return sv;
7912}
7913
ff276b08 7914/* newRV_inc is the official function name to use now.
645c22ef
DM
7915 * newRV_inc is in fact #defined to newRV in sv.h
7916 */
7917
5f05dabc 7918SV *
23f13727 7919Perl_newRV(pTHX_ SV *const sv)
5f05dabc 7920{
97aff369 7921 dVAR;
7918f24d
NC
7922
7923 PERL_ARGS_ASSERT_NEWRV;
7924
7f466ec7 7925 return newRV_noinc(SvREFCNT_inc_simple_NN(sv));
5f05dabc 7926}
5f05dabc 7927
954c1994
GS
7928/*
7929=for apidoc newSVsv
7930
7931Creates a new SV which is an exact duplicate of the original SV.
645c22ef 7932(Uses C<sv_setsv>).
954c1994
GS
7933
7934=cut
7935*/
7936
79072805 7937SV *
23f13727 7938Perl_newSVsv(pTHX_ register SV *const old)
79072805 7939{
97aff369 7940 dVAR;
463ee0b2 7941 register SV *sv;
79072805
LW
7942
7943 if (!old)
7a5b473e 7944 return NULL;
8990e307 7945 if (SvTYPE(old) == SVTYPEMASK) {
0453d815 7946 if (ckWARN_d(WARN_INTERNAL))
9014280d 7947 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
a0714e2c 7948 return NULL;
79072805 7949 }
4561caa4 7950 new_SV(sv);
e90aabeb
NC
7951 /* SV_GMAGIC is the default for sv_setv()
7952 SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games
7953 with SvTEMP_off and SvTEMP_on round a call to sv_setsv. */
7954 sv_setsv_flags(sv, old, SV_GMAGIC | SV_NOSTEAL);
463ee0b2 7955 return sv;
79072805
LW
7956}
7957
645c22ef
DM
7958/*
7959=for apidoc sv_reset
7960
7961Underlying implementation for the C<reset> Perl function.
7962Note that the perl-level function is vaguely deprecated.
7963
7964=cut
7965*/
7966
79072805 7967void
23f13727 7968Perl_sv_reset(pTHX_ register const char *s, HV *const stash)
79072805 7969{
27da23d5 7970 dVAR;
4802d5d7 7971 char todo[PERL_UCHAR_MAX+1];
79072805 7972
7918f24d
NC
7973 PERL_ARGS_ASSERT_SV_RESET;
7974
49d8d3a1
MB
7975 if (!stash)
7976 return;
7977
79072805 7978 if (!*s) { /* reset ?? searches */
daba3364 7979 MAGIC * const mg = mg_find((const SV *)stash, PERL_MAGIC_symtab);
8d2f4536 7980 if (mg) {
c2b1997a
NC
7981 const U32 count = mg->mg_len / sizeof(PMOP**);
7982 PMOP **pmp = (PMOP**) mg->mg_ptr;
7983 PMOP *const *const end = pmp + count;
7984
7985 while (pmp < end) {
c737faaf 7986#ifdef USE_ITHREADS
c2b1997a 7987 SvREADONLY_off(PL_regex_pad[(*pmp)->op_pmoffset]);
c737faaf 7988#else
c2b1997a 7989 (*pmp)->op_pmflags &= ~PMf_USED;
c737faaf 7990#endif
c2b1997a 7991 ++pmp;
8d2f4536 7992 }
79072805
LW
7993 }
7994 return;
7995 }
7996
7997 /* reset variables */
7998
7999 if (!HvARRAY(stash))
8000 return;
463ee0b2
LW
8001
8002 Zero(todo, 256, char);
79072805 8003 while (*s) {
b464bac0
AL
8004 I32 max;
8005 I32 i = (unsigned char)*s;
79072805
LW
8006 if (s[1] == '-') {
8007 s += 2;
8008 }
4802d5d7 8009 max = (unsigned char)*s++;
79072805 8010 for ( ; i <= max; i++) {
463ee0b2
LW
8011 todo[i] = 1;
8012 }
a0d0e21e 8013 for (i = 0; i <= (I32) HvMAX(stash); i++) {
b464bac0 8014 HE *entry;
79072805 8015 for (entry = HvARRAY(stash)[i];
9e35f4b3
GS
8016 entry;
8017 entry = HeNEXT(entry))
8018 {
b464bac0
AL
8019 register GV *gv;
8020 register SV *sv;
8021
1edc1566 8022 if (!todo[(U8)*HeKEY(entry)])
463ee0b2 8023 continue;
159b6efe 8024 gv = MUTABLE_GV(HeVAL(entry));
79072805 8025 sv = GvSV(gv);
e203899d
NC
8026 if (sv) {
8027 if (SvTHINKFIRST(sv)) {
8028 if (!SvREADONLY(sv) && SvROK(sv))
8029 sv_unref(sv);
8030 /* XXX Is this continue a bug? Why should THINKFIRST
8031 exempt us from resetting arrays and hashes? */
8032 continue;
8033 }
8034 SvOK_off(sv);
8035 if (SvTYPE(sv) >= SVt_PV) {
8036 SvCUR_set(sv, 0);
bd61b366 8037 if (SvPVX_const(sv) != NULL)
e203899d
NC
8038 *SvPVX(sv) = '\0';
8039 SvTAINT(sv);
8040 }
79072805
LW
8041 }
8042 if (GvAV(gv)) {
8043 av_clear(GvAV(gv));
8044 }
bfcb3514 8045 if (GvHV(gv) && !HvNAME_get(GvHV(gv))) {
b0269e46
AB
8046#if defined(VMS)
8047 Perl_die(aTHX_ "Can't reset %%ENV on this system");
8048#else /* ! VMS */
463ee0b2 8049 hv_clear(GvHV(gv));
b0269e46
AB
8050# if defined(USE_ENVIRON_ARRAY)
8051 if (gv == PL_envgv)
8052 my_clearenv();
8053# endif /* USE_ENVIRON_ARRAY */
8054#endif /* VMS */
79072805
LW
8055 }
8056 }
8057 }
8058 }
8059}
8060
645c22ef
DM
8061/*
8062=for apidoc sv_2io
8063
8064Using various gambits, try to get an IO from an SV: the IO slot if its a
8065GV; or the recursive result if we're an RV; or the IO slot of the symbol
8066named after the PV if we're a string.
8067
8068=cut
8069*/
8070
46fc3d4c 8071IO*
23f13727 8072Perl_sv_2io(pTHX_ SV *const sv)
46fc3d4c 8073{
8074 IO* io;
8075 GV* gv;
8076
7918f24d
NC
8077 PERL_ARGS_ASSERT_SV_2IO;
8078
46fc3d4c 8079 switch (SvTYPE(sv)) {
8080 case SVt_PVIO:
a45c7426 8081 io = MUTABLE_IO(sv);
46fc3d4c 8082 break;
8083 case SVt_PVGV:
6e592b3a 8084 if (isGV_with_GP(sv)) {
159b6efe 8085 gv = MUTABLE_GV(sv);
6e592b3a
BM
8086 io = GvIO(gv);
8087 if (!io)
8088 Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
8089 break;
8090 }
8091 /* FALL THROUGH */
46fc3d4c 8092 default:
8093 if (!SvOK(sv))
cea2e8a9 8094 Perl_croak(aTHX_ PL_no_usym, "filehandle");
46fc3d4c 8095 if (SvROK(sv))
8096 return sv_2io(SvRV(sv));
f776e3cd 8097 gv = gv_fetchsv(sv, 0, SVt_PVIO);
46fc3d4c 8098 if (gv)
8099 io = GvIO(gv);
8100 else
8101 io = 0;
8102 if (!io)
be2597df 8103 Perl_croak(aTHX_ "Bad filehandle: %"SVf, SVfARG(sv));
46fc3d4c 8104 break;
8105 }
8106 return io;
8107}
8108
645c22ef
DM
8109/*
8110=for apidoc sv_2cv
8111
8112Using various gambits, try to get a CV from an SV; in addition, try if
8113possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
f2c0649b 8114The flags in C<lref> are passed to sv_fetchsv.
645c22ef
DM
8115
8116=cut
8117*/
8118
79072805 8119CV *
23f13727 8120Perl_sv_2cv(pTHX_ SV *sv, HV **const st, GV **const gvp, const I32 lref)
79072805 8121{
27da23d5 8122 dVAR;
a0714e2c 8123 GV *gv = NULL;
601f1833 8124 CV *cv = NULL;
79072805 8125
7918f24d
NC
8126 PERL_ARGS_ASSERT_SV_2CV;
8127
85dec29a
NC
8128 if (!sv) {
8129 *st = NULL;
8130 *gvp = NULL;
8131 return NULL;
8132 }
79072805 8133 switch (SvTYPE(sv)) {
79072805
LW
8134 case SVt_PVCV:
8135 *st = CvSTASH(sv);
a0714e2c 8136 *gvp = NULL;
ea726b52 8137 return MUTABLE_CV(sv);
79072805
LW
8138 case SVt_PVHV:
8139 case SVt_PVAV:
ef58ba18 8140 *st = NULL;
a0714e2c 8141 *gvp = NULL;
601f1833 8142 return NULL;
8990e307 8143 case SVt_PVGV:
6e592b3a 8144 if (isGV_with_GP(sv)) {
159b6efe 8145 gv = MUTABLE_GV(sv);
6e592b3a
BM
8146 *gvp = gv;
8147 *st = GvESTASH(gv);
8148 goto fix_gv;
8149 }
8150 /* FALL THROUGH */
8990e307 8151
79072805 8152 default:
a0d0e21e 8153 if (SvROK(sv)) {
823a54a3 8154 SV * const *sp = &sv; /* Used in tryAMAGICunDEREF macro. */
c4f3bd1e 8155 SvGETMAGIC(sv);
f5284f61
IZ
8156 tryAMAGICunDEREF(to_cv);
8157
62f274bf
GS
8158 sv = SvRV(sv);
8159 if (SvTYPE(sv) == SVt_PVCV) {
ea726b52 8160 cv = MUTABLE_CV(sv);
a0714e2c 8161 *gvp = NULL;
62f274bf
GS
8162 *st = CvSTASH(cv);
8163 return cv;
8164 }
6e592b3a 8165 else if(isGV_with_GP(sv))
159b6efe 8166 gv = MUTABLE_GV(sv);
62f274bf 8167 else
cea2e8a9 8168 Perl_croak(aTHX_ "Not a subroutine reference");
a0d0e21e 8169 }
6e592b3a 8170 else if (isGV_with_GP(sv)) {
9d0f7ed7 8171 SvGETMAGIC(sv);
159b6efe 8172 gv = MUTABLE_GV(sv);
9d0f7ed7 8173 }
79072805 8174 else
9d0f7ed7 8175 gv = gv_fetchsv(sv, lref, SVt_PVCV); /* Calls get magic */
79072805 8176 *gvp = gv;
ef58ba18
NC
8177 if (!gv) {
8178 *st = NULL;
601f1833 8179 return NULL;
ef58ba18 8180 }
e26df76a 8181 /* Some flags to gv_fetchsv mean don't really create the GV */
6e592b3a 8182 if (!isGV_with_GP(gv)) {
e26df76a
NC
8183 *st = NULL;
8184 return NULL;
8185 }
79072805 8186 *st = GvESTASH(gv);
8990e307 8187 fix_gv:
8ebc5c01 8188 if (lref && !GvCVu(gv)) {
4633a7c4 8189 SV *tmpsv;
748a9306 8190 ENTER;
561b68a9 8191 tmpsv = newSV(0);
bd61b366 8192 gv_efullname3(tmpsv, gv, NULL);
f6ec51f7
GS
8193 /* XXX this is probably not what they think they're getting.
8194 * It has the same effect as "sub name;", i.e. just a forward
8195 * declaration! */
774d564b 8196 newSUB(start_subparse(FALSE, 0),
4633a7c4 8197 newSVOP(OP_CONST, 0, tmpsv),
5f66b61c 8198 NULL, NULL);
748a9306 8199 LEAVE;
8ebc5c01 8200 if (!GvCVu(gv))
35c1215d 8201 Perl_croak(aTHX_ "Unable to create sub named \"%"SVf"\"",
4052d21c 8202 SVfARG(SvOK(sv) ? sv : &PL_sv_no));
8990e307 8203 }
8ebc5c01 8204 return GvCVu(gv);
79072805
LW
8205 }
8206}
8207
c461cf8f
JH
8208/*
8209=for apidoc sv_true
8210
8211Returns true if the SV has a true value by Perl's rules.
645c22ef
DM
8212Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
8213instead use an in-line version.
c461cf8f
JH
8214
8215=cut
8216*/
8217
79072805 8218I32
23f13727 8219Perl_sv_true(pTHX_ register SV *const sv)
79072805 8220{
8990e307
LW
8221 if (!sv)
8222 return 0;
79072805 8223 if (SvPOK(sv)) {
823a54a3
AL
8224 register const XPV* const tXpv = (XPV*)SvANY(sv);
8225 if (tXpv &&
c2f1de04 8226 (tXpv->xpv_cur > 1 ||
339049b0 8227 (tXpv->xpv_cur && *sv->sv_u.svu_pv != '0')))
79072805
LW
8228 return 1;
8229 else
8230 return 0;
8231 }
8232 else {
8233 if (SvIOK(sv))
463ee0b2 8234 return SvIVX(sv) != 0;
79072805
LW
8235 else {
8236 if (SvNOK(sv))
463ee0b2 8237 return SvNVX(sv) != 0.0;
79072805 8238 else
463ee0b2 8239 return sv_2bool(sv);
79072805
LW
8240 }
8241 }
8242}
79072805 8243
645c22ef 8244/*
c461cf8f
JH
8245=for apidoc sv_pvn_force
8246
8247Get a sensible string out of the SV somehow.
645c22ef
DM
8248A private implementation of the C<SvPV_force> macro for compilers which
8249can't cope with complex macro expressions. Always use the macro instead.
c461cf8f 8250
8d6d96c1
HS
8251=for apidoc sv_pvn_force_flags
8252
8253Get a sensible string out of the SV somehow.
8254If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if
8255appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
8256implemented in terms of this function.
645c22ef
DM
8257You normally want to use the various wrapper macros instead: see
8258C<SvPV_force> and C<SvPV_force_nomg>
8d6d96c1
HS
8259
8260=cut
8261*/
8262
8263char *
12964ddd 8264Perl_sv_pvn_force_flags(pTHX_ SV *const sv, STRLEN *const lp, const I32 flags)
8d6d96c1 8265{
97aff369 8266 dVAR;
7918f24d
NC
8267
8268 PERL_ARGS_ASSERT_SV_PVN_FORCE_FLAGS;
8269
6fc92669 8270 if (SvTHINKFIRST(sv) && !SvROK(sv))
765f542d 8271 sv_force_normal_flags(sv, 0);
1c846c1f 8272
a0d0e21e 8273 if (SvPOK(sv)) {
13c5b33c
NC
8274 if (lp)
8275 *lp = SvCUR(sv);
a0d0e21e
LW
8276 }
8277 else {
a3b680e6 8278 char *s;
13c5b33c
NC
8279 STRLEN len;
8280
4d84ee25 8281 if (SvREADONLY(sv) && !(flags & SV_MUTABLE_RETURN)) {
b64e5050 8282 const char * const ref = sv_reftype(sv,0);
4d84ee25
NC
8283 if (PL_op)
8284 Perl_croak(aTHX_ "Can't coerce readonly %s to string in %s",
b64e5050 8285 ref, OP_NAME(PL_op));
4d84ee25 8286 else
b64e5050 8287 Perl_croak(aTHX_ "Can't coerce readonly %s to string", ref);
4d84ee25 8288 }
1f257c95
NC
8289 if ((SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM)
8290 || isGV_with_GP(sv))
cea2e8a9 8291 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
53e06cf0 8292 OP_NAME(PL_op));
b64e5050 8293 s = sv_2pv_flags(sv, &len, flags);
13c5b33c
NC
8294 if (lp)
8295 *lp = len;
8296
3f7c398e 8297 if (s != SvPVX_const(sv)) { /* Almost, but not quite, sv_setpvn() */
a0d0e21e
LW
8298 if (SvROK(sv))
8299 sv_unref(sv);
862a34c6 8300 SvUPGRADE(sv, SVt_PV); /* Never FALSE */
a0d0e21e 8301 SvGROW(sv, len + 1);
706aa1c9 8302 Move(s,SvPVX(sv),len,char);
a0d0e21e 8303 SvCUR_set(sv, len);
97a130b8 8304 SvPVX(sv)[len] = '\0';
a0d0e21e
LW
8305 }
8306 if (!SvPOK(sv)) {
8307 SvPOK_on(sv); /* validate pointer */
8308 SvTAINT(sv);
1d7c1841 8309 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3f7c398e 8310 PTR2UV(sv),SvPVX_const(sv)));
a0d0e21e
LW
8311 }
8312 }
4d84ee25 8313 return SvPVX_mutable(sv);
a0d0e21e
LW
8314}
8315
645c22ef 8316/*
645c22ef
DM
8317=for apidoc sv_pvbyten_force
8318
0feed65a 8319The backend for the C<SvPVbytex_force> macro. Always use the macro instead.
645c22ef
DM
8320
8321=cut
8322*/
8323
7340a771 8324char *
12964ddd 8325Perl_sv_pvbyten_force(pTHX_ SV *const sv, STRLEN *const lp)
7340a771 8326{
7918f24d
NC
8327 PERL_ARGS_ASSERT_SV_PVBYTEN_FORCE;
8328
46ec2f14 8329 sv_pvn_force(sv,lp);
ffebcc3e 8330 sv_utf8_downgrade(sv,0);
46ec2f14
TS
8331 *lp = SvCUR(sv);
8332 return SvPVX(sv);
7340a771
GS
8333}
8334
645c22ef 8335/*
c461cf8f
JH
8336=for apidoc sv_pvutf8n_force
8337
0feed65a 8338The backend for the C<SvPVutf8x_force> macro. Always use the macro instead.
c461cf8f
JH
8339
8340=cut
8341*/
8342
7340a771 8343char *
12964ddd 8344Perl_sv_pvutf8n_force(pTHX_ SV *const sv, STRLEN *const lp)
7340a771 8345{
7918f24d
NC
8346 PERL_ARGS_ASSERT_SV_PVUTF8N_FORCE;
8347
46ec2f14 8348 sv_pvn_force(sv,lp);
560a288e 8349 sv_utf8_upgrade(sv);
46ec2f14
TS
8350 *lp = SvCUR(sv);
8351 return SvPVX(sv);
7340a771
GS
8352}
8353
c461cf8f
JH
8354/*
8355=for apidoc sv_reftype
8356
8357Returns a string describing what the SV is a reference to.
8358
8359=cut
8360*/
8361
2b388283 8362const char *
12964ddd 8363Perl_sv_reftype(pTHX_ const SV *const sv, const int ob)
a0d0e21e 8364{
7918f24d
NC
8365 PERL_ARGS_ASSERT_SV_REFTYPE;
8366
07409e01
NC
8367 /* The fact that I don't need to downcast to char * everywhere, only in ?:
8368 inside return suggests a const propagation bug in g++. */
c86bf373 8369 if (ob && SvOBJECT(sv)) {
1b6737cc 8370 char * const name = HvNAME_get(SvSTASH(sv));
07409e01 8371 return name ? name : (char *) "__ANON__";
c86bf373 8372 }
a0d0e21e
LW
8373 else {
8374 switch (SvTYPE(sv)) {
8375 case SVt_NULL:
8376 case SVt_IV:
8377 case SVt_NV:
a0d0e21e
LW
8378 case SVt_PV:
8379 case SVt_PVIV:
8380 case SVt_PVNV:
8381 case SVt_PVMG:
1cb0ed9b 8382 if (SvVOK(sv))
439cb1c4 8383 return "VSTRING";
a0d0e21e
LW
8384 if (SvROK(sv))
8385 return "REF";
8386 else
8387 return "SCALAR";
1cb0ed9b 8388
07409e01 8389 case SVt_PVLV: return (char *) (SvROK(sv) ? "REF"
be65207d
DM
8390 /* tied lvalues should appear to be
8391 * scalars for backwards compatitbility */
8392 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
07409e01 8393 ? "SCALAR" : "LVALUE");
a0d0e21e
LW
8394 case SVt_PVAV: return "ARRAY";
8395 case SVt_PVHV: return "HASH";
8396 case SVt_PVCV: return "CODE";
6e592b3a
BM
8397 case SVt_PVGV: return (char *) (isGV_with_GP(sv)
8398 ? "GLOB" : "SCALAR");
1d2dff63 8399 case SVt_PVFM: return "FORMAT";
27f9d8f3 8400 case SVt_PVIO: return "IO";
cecf5685 8401 case SVt_BIND: return "BIND";
b7c9370f 8402 case SVt_REGEXP: return "REGEXP";
a0d0e21e
LW
8403 default: return "UNKNOWN";
8404 }
8405 }
8406}
8407
954c1994
GS
8408/*
8409=for apidoc sv_isobject
8410
8411Returns a boolean indicating whether the SV is an RV pointing to a blessed
8412object. If the SV is not an RV, or if the object is not blessed, then this
8413will return false.
8414
8415=cut
8416*/
8417
463ee0b2 8418int
864dbfa3 8419Perl_sv_isobject(pTHX_ SV *sv)
85e6fe83 8420{
68dc0745 8421 if (!sv)
8422 return 0;
5b295bef 8423 SvGETMAGIC(sv);
85e6fe83
LW
8424 if (!SvROK(sv))
8425 return 0;
daba3364 8426 sv = SvRV(sv);
85e6fe83
LW
8427 if (!SvOBJECT(sv))
8428 return 0;
8429 return 1;
8430}
8431
954c1994
GS
8432/*
8433=for apidoc sv_isa
8434
8435Returns a boolean indicating whether the SV is blessed into the specified
8436class. This does not check for subtypes; use C<sv_derived_from> to verify
8437an inheritance relationship.
8438
8439=cut
8440*/
8441
85e6fe83 8442int
12964ddd 8443Perl_sv_isa(pTHX_ SV *sv, const char *const name)
463ee0b2 8444{
bfcb3514 8445 const char *hvname;
7918f24d
NC
8446
8447 PERL_ARGS_ASSERT_SV_ISA;
8448
68dc0745 8449 if (!sv)
8450 return 0;
5b295bef 8451 SvGETMAGIC(sv);
ed6116ce 8452 if (!SvROK(sv))
463ee0b2 8453 return 0;
daba3364 8454 sv = SvRV(sv);
ed6116ce 8455 if (!SvOBJECT(sv))
463ee0b2 8456 return 0;
bfcb3514
NC
8457 hvname = HvNAME_get(SvSTASH(sv));
8458 if (!hvname)
e27ad1f2 8459 return 0;
463ee0b2 8460
bfcb3514 8461 return strEQ(hvname, name);
463ee0b2
LW
8462}
8463
954c1994
GS
8464/*
8465=for apidoc newSVrv
8466
8467Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then
8468it will be upgraded to one. If C<classname> is non-null then the new SV will
8469be blessed in the specified package. The new SV is returned and its
8470reference count is 1.
8471
8472=cut
8473*/
8474
463ee0b2 8475SV*
12964ddd 8476Perl_newSVrv(pTHX_ SV *const rv, const char *const classname)
463ee0b2 8477{
97aff369 8478 dVAR;
463ee0b2
LW
8479 SV *sv;
8480
7918f24d
NC
8481 PERL_ARGS_ASSERT_NEWSVRV;
8482
4561caa4 8483 new_SV(sv);
51cf62d8 8484
765f542d 8485 SV_CHECK_THINKFIRST_COW_DROP(rv);
52944de8 8486 (void)SvAMAGIC_off(rv);
51cf62d8 8487
0199fce9 8488 if (SvTYPE(rv) >= SVt_PVMG) {
a3b680e6 8489 const U32 refcnt = SvREFCNT(rv);
0199fce9
JD
8490 SvREFCNT(rv) = 0;
8491 sv_clear(rv);
8492 SvFLAGS(rv) = 0;
8493 SvREFCNT(rv) = refcnt;
0199fce9 8494
4df7f6af 8495 sv_upgrade(rv, SVt_IV);
dc5494d2
NC
8496 } else if (SvROK(rv)) {
8497 SvREFCNT_dec(SvRV(rv));
43230e26
NC
8498 } else {
8499 prepare_SV_for_RV(rv);
0199fce9 8500 }
51cf62d8 8501
0c34ef67 8502 SvOK_off(rv);
b162af07 8503 SvRV_set(rv, sv);
ed6116ce 8504 SvROK_on(rv);
463ee0b2 8505
a0d0e21e 8506 if (classname) {
da51bb9b 8507 HV* const stash = gv_stashpv(classname, GV_ADD);
a0d0e21e
LW
8508 (void)sv_bless(rv, stash);
8509 }
8510 return sv;
8511}
8512
954c1994
GS
8513/*
8514=for apidoc sv_setref_pv
8515
8516Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
8517argument will be upgraded to an RV. That RV will be modified to point to
8518the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed
8519into the SV. The C<classname> argument indicates the package for the
bd61b366 8520blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
d34c2299 8521will have a reference count of 1, and the RV will be returned.
954c1994
GS
8522
8523Do not use with other Perl types such as HV, AV, SV, CV, because those
8524objects will become corrupted by the pointer copy process.
8525
8526Note that C<sv_setref_pvn> copies the string while this copies the pointer.
8527
8528=cut
8529*/
8530
a0d0e21e 8531SV*
12964ddd 8532Perl_sv_setref_pv(pTHX_ SV *const rv, const char *const classname, void *const pv)
a0d0e21e 8533{
97aff369 8534 dVAR;
7918f24d
NC
8535
8536 PERL_ARGS_ASSERT_SV_SETREF_PV;
8537
189b2af5 8538 if (!pv) {
3280af22 8539 sv_setsv(rv, &PL_sv_undef);
189b2af5
GS
8540 SvSETMAGIC(rv);
8541 }
a0d0e21e 8542 else
56431972 8543 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
a0d0e21e
LW
8544 return rv;
8545}
8546
954c1994
GS
8547/*
8548=for apidoc sv_setref_iv
8549
8550Copies an integer into a new SV, optionally blessing the SV. The C<rv>
8551argument will be upgraded to an RV. That RV will be modified to point to
8552the new SV. The C<classname> argument indicates the package for the
bd61b366 8553blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
d34c2299 8554will have a reference count of 1, and the RV will be returned.
954c1994
GS
8555
8556=cut
8557*/
8558
a0d0e21e 8559SV*
12964ddd 8560Perl_sv_setref_iv(pTHX_ SV *const rv, const char *const classname, const IV iv)
a0d0e21e 8561{
7918f24d
NC
8562 PERL_ARGS_ASSERT_SV_SETREF_IV;
8563
a0d0e21e
LW
8564 sv_setiv(newSVrv(rv,classname), iv);
8565 return rv;
8566}
8567
954c1994 8568/*
e1c57cef
JH
8569=for apidoc sv_setref_uv
8570
8571Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
8572argument will be upgraded to an RV. That RV will be modified to point to
8573the new SV. The C<classname> argument indicates the package for the
bd61b366 8574blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
d34c2299 8575will have a reference count of 1, and the RV will be returned.
e1c57cef
JH
8576
8577=cut
8578*/
8579
8580SV*
12964ddd 8581Perl_sv_setref_uv(pTHX_ SV *const rv, const char *const classname, const UV uv)
e1c57cef 8582{
7918f24d
NC
8583 PERL_ARGS_ASSERT_SV_SETREF_UV;
8584
e1c57cef
JH
8585 sv_setuv(newSVrv(rv,classname), uv);
8586 return rv;
8587}
8588
8589/*
954c1994
GS
8590=for apidoc sv_setref_nv
8591
8592Copies a double into a new SV, optionally blessing the SV. The C<rv>
8593argument will be upgraded to an RV. That RV will be modified to point to
8594the new SV. The C<classname> argument indicates the package for the
bd61b366 8595blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
d34c2299 8596will have a reference count of 1, and the RV will be returned.
954c1994
GS
8597
8598=cut
8599*/
8600
a0d0e21e 8601SV*
12964ddd 8602Perl_sv_setref_nv(pTHX_ SV *const rv, const char *const classname, const NV nv)
a0d0e21e 8603{
7918f24d
NC
8604 PERL_ARGS_ASSERT_SV_SETREF_NV;
8605
a0d0e21e
LW
8606 sv_setnv(newSVrv(rv,classname), nv);
8607 return rv;
8608}
463ee0b2 8609
954c1994
GS
8610/*
8611=for apidoc sv_setref_pvn
8612
8613Copies a string into a new SV, optionally blessing the SV. The length of the
8614string must be specified with C<n>. The C<rv> argument will be upgraded to
8615an RV. That RV will be modified to point to the new SV. The C<classname>
8616argument indicates the package for the blessing. Set C<classname> to
bd61b366 8617C<NULL> to avoid the blessing. The new SV will have a reference count
d34c2299 8618of 1, and the RV will be returned.
954c1994
GS
8619
8620Note that C<sv_setref_pv> copies the pointer while this copies the string.
8621
8622=cut
8623*/
8624
a0d0e21e 8625SV*
12964ddd
SS
8626Perl_sv_setref_pvn(pTHX_ SV *const rv, const char *const classname,
8627 const char *const pv, const STRLEN n)
a0d0e21e 8628{
7918f24d
NC
8629 PERL_ARGS_ASSERT_SV_SETREF_PVN;
8630
a0d0e21e 8631 sv_setpvn(newSVrv(rv,classname), pv, n);
463ee0b2
LW
8632 return rv;
8633}
8634
954c1994
GS
8635/*
8636=for apidoc sv_bless
8637
8638Blesses an SV into a specified package. The SV must be an RV. The package
8639must be designated by its stash (see C<gv_stashpv()>). The reference count
8640of the SV is unaffected.
8641
8642=cut
8643*/
8644
a0d0e21e 8645SV*
12964ddd 8646Perl_sv_bless(pTHX_ SV *const sv, HV *const stash)
a0d0e21e 8647{
97aff369 8648 dVAR;
76e3520e 8649 SV *tmpRef;
7918f24d
NC
8650
8651 PERL_ARGS_ASSERT_SV_BLESS;
8652
a0d0e21e 8653 if (!SvROK(sv))
cea2e8a9 8654 Perl_croak(aTHX_ "Can't bless non-reference value");
76e3520e
GS
8655 tmpRef = SvRV(sv);
8656 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
e0744413
NC
8657 if (SvIsCOW(tmpRef))
8658 sv_force_normal_flags(tmpRef, 0);
76e3520e 8659 if (SvREADONLY(tmpRef))
f1f66076 8660 Perl_croak(aTHX_ "%s", PL_no_modify);
76e3520e
GS
8661 if (SvOBJECT(tmpRef)) {
8662 if (SvTYPE(tmpRef) != SVt_PVIO)
3280af22 8663 --PL_sv_objcount;
76e3520e 8664 SvREFCNT_dec(SvSTASH(tmpRef));
2e3febc6 8665 }
a0d0e21e 8666 }
76e3520e
GS
8667 SvOBJECT_on(tmpRef);
8668 if (SvTYPE(tmpRef) != SVt_PVIO)
3280af22 8669 ++PL_sv_objcount;
862a34c6 8670 SvUPGRADE(tmpRef, SVt_PVMG);
85fbaab2 8671 SvSTASH_set(tmpRef, MUTABLE_HV(SvREFCNT_inc_simple(stash)));
a0d0e21e 8672
2e3febc6
CS
8673 if (Gv_AMG(stash))
8674 SvAMAGIC_on(sv);
8675 else
52944de8 8676 (void)SvAMAGIC_off(sv);
a0d0e21e 8677
1edbfb88
AB
8678 if(SvSMAGICAL(tmpRef))
8679 if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
8680 mg_set(tmpRef);
8681
8682
ecdeb87c 8683
a0d0e21e
LW
8684 return sv;
8685}
8686
645c22ef 8687/* Downgrades a PVGV to a PVMG.
645c22ef
DM
8688 */
8689
76e3520e 8690STATIC void
89e38212 8691S_sv_unglob(pTHX_ SV *const sv)
a0d0e21e 8692{
97aff369 8693 dVAR;
850fabdf 8694 void *xpvmg;
dd69841b 8695 HV *stash;
b37c2d43 8696 SV * const temp = sv_newmortal();
850fabdf 8697
7918f24d
NC
8698 PERL_ARGS_ASSERT_SV_UNGLOB;
8699
a0d0e21e
LW
8700 assert(SvTYPE(sv) == SVt_PVGV);
8701 SvFAKE_off(sv);
159b6efe 8702 gv_efullname3(temp, MUTABLE_GV(sv), "*");
180488f8 8703
f7877b28 8704 if (GvGP(sv)) {
159b6efe
NC
8705 if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
8706 && HvNAME_get(stash))
dd69841b 8707 mro_method_changed_in(stash);
159b6efe 8708 gp_free(MUTABLE_GV(sv));
f7877b28 8709 }
e826b3c7 8710 if (GvSTASH(sv)) {
daba3364 8711 sv_del_backref(MUTABLE_SV(GvSTASH(sv)), sv);
5c284bb0 8712 GvSTASH(sv) = NULL;
e826b3c7 8713 }
a5f75d66 8714 GvMULTI_off(sv);
acda4c6a
NC
8715 if (GvNAME_HEK(sv)) {
8716 unshare_hek(GvNAME_HEK(sv));
8717 }
2e5b91de 8718 isGV_with_GP_off(sv);
850fabdf
GS
8719
8720 /* need to keep SvANY(sv) in the right arena */
8721 xpvmg = new_XPVMG();
8722 StructCopy(SvANY(sv), xpvmg, XPVMG);
8723 del_XPVGV(SvANY(sv));
8724 SvANY(sv) = xpvmg;
8725
a0d0e21e
LW
8726 SvFLAGS(sv) &= ~SVTYPEMASK;
8727 SvFLAGS(sv) |= SVt_PVMG;
180488f8
NC
8728
8729 /* Intentionally not calling any local SET magic, as this isn't so much a
8730 set operation as merely an internal storage change. */
8731 sv_setsv_flags(sv, temp, 0);
a0d0e21e
LW
8732}
8733
954c1994 8734/*
840a7b70 8735=for apidoc sv_unref_flags
954c1994
GS
8736
8737Unsets the RV status of the SV, and decrements the reference count of
8738whatever was being referenced by the RV. This can almost be thought of
840a7b70
IZ
8739as a reversal of C<newSVrv>. The C<cflags> argument can contain
8740C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
8741(otherwise the decrementing is conditional on the reference count being
8742different from one or the reference being a readonly SV).
7889fe52 8743See C<SvROK_off>.
954c1994
GS
8744
8745=cut
8746*/
8747
ed6116ce 8748void
89e38212 8749Perl_sv_unref_flags(pTHX_ SV *const ref, const U32 flags)
ed6116ce 8750{
b64e5050 8751 SV* const target = SvRV(ref);
810b8aa5 8752
7918f24d
NC
8753 PERL_ARGS_ASSERT_SV_UNREF_FLAGS;
8754
e15faf7d
NC
8755 if (SvWEAKREF(ref)) {
8756 sv_del_backref(target, ref);
8757 SvWEAKREF_off(ref);
8758 SvRV_set(ref, NULL);
810b8aa5
GS
8759 return;
8760 }
e15faf7d
NC
8761 SvRV_set(ref, NULL);
8762 SvROK_off(ref);
8763 /* You can't have a || SvREADONLY(target) here, as $a = $$a, where $a was
04ca4930 8764 assigned to as BEGIN {$a = \"Foo"} will fail. */
e15faf7d
NC
8765 if (SvREFCNT(target) != 1 || (flags & SV_IMMEDIATE_UNREF))
8766 SvREFCNT_dec(target);
840a7b70 8767 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
e15faf7d 8768 sv_2mortal(target); /* Schedule for freeing later */
ed6116ce 8769}
8990e307 8770
840a7b70 8771/*
645c22ef
DM
8772=for apidoc sv_untaint
8773
8774Untaint an SV. Use C<SvTAINTED_off> instead.
8775=cut
8776*/
8777
bbce6d69 8778void
89e38212 8779Perl_sv_untaint(pTHX_ SV *const sv)
bbce6d69 8780{
7918f24d
NC
8781 PERL_ARGS_ASSERT_SV_UNTAINT;
8782
13f57bf8 8783 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
b64e5050 8784 MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
36477c24 8785 if (mg)
565764a8 8786 mg->mg_len &= ~1;
36477c24 8787 }
bbce6d69 8788}
8789
645c22ef
DM
8790/*
8791=for apidoc sv_tainted
8792
8793Test an SV for taintedness. Use C<SvTAINTED> instead.
8794=cut
8795*/
8796
bbce6d69 8797bool
89e38212 8798Perl_sv_tainted(pTHX_ SV *const sv)
bbce6d69 8799{
7918f24d
NC
8800 PERL_ARGS_ASSERT_SV_TAINTED;
8801
13f57bf8 8802 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
823a54a3 8803 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
2ddb8a4f 8804 if (mg && (mg->mg_len & 1) )
36477c24 8805 return TRUE;
8806 }
8807 return FALSE;
bbce6d69 8808}
8809
09540bc3
JH
8810/*
8811=for apidoc sv_setpviv
8812
8813Copies an integer into the given SV, also updating its string value.
8814Does not handle 'set' magic. See C<sv_setpviv_mg>.
8815
8816=cut
8817*/
8818
8819void
89e38212 8820Perl_sv_setpviv(pTHX_ SV *const sv, const IV iv)
09540bc3
JH
8821{
8822 char buf[TYPE_CHARS(UV)];
8823 char *ebuf;
b64e5050 8824 char * const ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
09540bc3 8825
7918f24d
NC
8826 PERL_ARGS_ASSERT_SV_SETPVIV;
8827
09540bc3
JH
8828 sv_setpvn(sv, ptr, ebuf - ptr);
8829}
8830
8831/*
8832=for apidoc sv_setpviv_mg
8833
8834Like C<sv_setpviv>, but also handles 'set' magic.
8835
8836=cut
8837*/
8838
8839void
89e38212 8840Perl_sv_setpviv_mg(pTHX_ SV *const sv, const IV iv)
09540bc3 8841{
7918f24d
NC
8842 PERL_ARGS_ASSERT_SV_SETPVIV_MG;
8843
df7eb254 8844 sv_setpviv(sv, iv);
09540bc3
JH
8845 SvSETMAGIC(sv);
8846}
8847
cea2e8a9 8848#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
8849
8850/* pTHX_ magic can't cope with varargs, so this is a no-context
8851 * version of the main function, (which may itself be aliased to us).
8852 * Don't access this version directly.
8853 */
8854
cea2e8a9 8855void
89e38212 8856Perl_sv_setpvf_nocontext(SV *const sv, const char *const pat, ...)
cea2e8a9
GS
8857{
8858 dTHX;
8859 va_list args;
7918f24d
NC
8860
8861 PERL_ARGS_ASSERT_SV_SETPVF_NOCONTEXT;
8862
cea2e8a9 8863 va_start(args, pat);
c5be433b 8864 sv_vsetpvf(sv, pat, &args);
cea2e8a9
GS
8865 va_end(args);
8866}
8867
645c22ef
DM
8868/* pTHX_ magic can't cope with varargs, so this is a no-context
8869 * version of the main function, (which may itself be aliased to us).
8870 * Don't access this version directly.
8871 */
cea2e8a9
GS
8872
8873void
89e38212 8874Perl_sv_setpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
cea2e8a9
GS
8875{
8876 dTHX;
8877 va_list args;
7918f24d
NC
8878
8879 PERL_ARGS_ASSERT_SV_SETPVF_MG_NOCONTEXT;
8880
cea2e8a9 8881 va_start(args, pat);
c5be433b 8882 sv_vsetpvf_mg(sv, pat, &args);
cea2e8a9 8883 va_end(args);
cea2e8a9
GS
8884}
8885#endif
8886
954c1994
GS
8887/*
8888=for apidoc sv_setpvf
8889
bffc3d17
SH
8890Works like C<sv_catpvf> but copies the text into the SV instead of
8891appending it. Does not handle 'set' magic. See C<sv_setpvf_mg>.
954c1994
GS
8892
8893=cut
8894*/
8895
46fc3d4c 8896void
89e38212 8897Perl_sv_setpvf(pTHX_ SV *const sv, const char *const pat, ...)
46fc3d4c 8898{
8899 va_list args;
7918f24d
NC
8900
8901 PERL_ARGS_ASSERT_SV_SETPVF;
8902
46fc3d4c 8903 va_start(args, pat);
c5be433b 8904 sv_vsetpvf(sv, pat, &args);
46fc3d4c 8905 va_end(args);
8906}
8907
bffc3d17
SH
8908/*
8909=for apidoc sv_vsetpvf
8910
8911Works like C<sv_vcatpvf> but copies the text into the SV instead of
8912appending it. Does not handle 'set' magic. See C<sv_vsetpvf_mg>.
8913
8914Usually used via its frontend C<sv_setpvf>.
8915
8916=cut
8917*/
645c22ef 8918
c5be433b 8919void
89e38212 8920Perl_sv_vsetpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
c5be433b 8921{
7918f24d
NC
8922 PERL_ARGS_ASSERT_SV_VSETPVF;
8923
4608196e 8924 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
c5be433b 8925}
ef50df4b 8926
954c1994
GS
8927/*
8928=for apidoc sv_setpvf_mg
8929
8930Like C<sv_setpvf>, but also handles 'set' magic.
8931
8932=cut
8933*/
8934
ef50df4b 8935void
89e38212 8936Perl_sv_setpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
ef50df4b
GS
8937{
8938 va_list args;
7918f24d
NC
8939
8940 PERL_ARGS_ASSERT_SV_SETPVF_MG;
8941
ef50df4b 8942 va_start(args, pat);
c5be433b 8943 sv_vsetpvf_mg(sv, pat, &args);
ef50df4b 8944 va_end(args);
c5be433b
GS
8945}
8946
bffc3d17
SH
8947/*
8948=for apidoc sv_vsetpvf_mg
8949
8950Like C<sv_vsetpvf>, but also handles 'set' magic.
8951
8952Usually used via its frontend C<sv_setpvf_mg>.
8953
8954=cut
8955*/
645c22ef 8956
c5be433b 8957void
89e38212 8958Perl_sv_vsetpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
c5be433b 8959{
7918f24d
NC
8960 PERL_ARGS_ASSERT_SV_VSETPVF_MG;
8961
4608196e 8962 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
ef50df4b
GS
8963 SvSETMAGIC(sv);
8964}
8965
cea2e8a9 8966#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
8967
8968/* pTHX_ magic can't cope with varargs, so this is a no-context
8969 * version of the main function, (which may itself be aliased to us).
8970 * Don't access this version directly.
8971 */
8972
cea2e8a9 8973void
89e38212 8974Perl_sv_catpvf_nocontext(SV *const sv, const char *const pat, ...)
cea2e8a9
GS
8975{
8976 dTHX;
8977 va_list args;
7918f24d
NC
8978
8979 PERL_ARGS_ASSERT_SV_CATPVF_NOCONTEXT;
8980
cea2e8a9 8981 va_start(args, pat);
c5be433b 8982 sv_vcatpvf(sv, pat, &args);
cea2e8a9
GS
8983 va_end(args);
8984}
8985
645c22ef
DM
8986/* pTHX_ magic can't cope with varargs, so this is a no-context
8987 * version of the main function, (which may itself be aliased to us).
8988 * Don't access this version directly.
8989 */
8990
cea2e8a9 8991void
89e38212 8992Perl_sv_catpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
cea2e8a9
GS
8993{
8994 dTHX;
8995 va_list args;
7918f24d
NC
8996
8997 PERL_ARGS_ASSERT_SV_CATPVF_MG_NOCONTEXT;
8998
cea2e8a9 8999 va_start(args, pat);
c5be433b 9000 sv_vcatpvf_mg(sv, pat, &args);
cea2e8a9 9001 va_end(args);
cea2e8a9
GS
9002}
9003#endif
9004
954c1994
GS
9005/*
9006=for apidoc sv_catpvf
9007
d5ce4a7c
GA
9008Processes its arguments like C<sprintf> and appends the formatted
9009output to an SV. If the appended data contains "wide" characters
9010(including, but not limited to, SVs with a UTF-8 PV formatted with %s,
9011and characters >255 formatted with %c), the original SV might get
bffc3d17 9012upgraded to UTF-8. Handles 'get' magic, but not 'set' magic. See
cdd94ca7
NC
9013C<sv_catpvf_mg>. If the original SV was UTF-8, the pattern should be
9014valid UTF-8; if the original SV was bytes, the pattern should be too.
954c1994 9015
d5ce4a7c 9016=cut */
954c1994 9017
46fc3d4c 9018void
66ceb532 9019Perl_sv_catpvf(pTHX_ SV *const sv, const char *const pat, ...)
46fc3d4c 9020{
9021 va_list args;
7918f24d
NC
9022
9023 PERL_ARGS_ASSERT_SV_CATPVF;
9024
46fc3d4c 9025 va_start(args, pat);
c5be433b 9026 sv_vcatpvf(sv, pat, &args);
46fc3d4c 9027 va_end(args);
9028}
9029
bffc3d17
SH
9030/*
9031=for apidoc sv_vcatpvf
9032
9033Processes its arguments like C<vsprintf> and appends the formatted output
9034to an SV. Does not handle 'set' magic. See C<sv_vcatpvf_mg>.
9035
9036Usually used via its frontend C<sv_catpvf>.
9037
9038=cut
9039*/
645c22ef 9040
ef50df4b 9041void
66ceb532 9042Perl_sv_vcatpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
c5be433b 9043{
7918f24d
NC
9044 PERL_ARGS_ASSERT_SV_VCATPVF;
9045
4608196e 9046 sv_vcatpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
c5be433b
GS
9047}
9048
954c1994
GS
9049/*
9050=for apidoc sv_catpvf_mg
9051
9052Like C<sv_catpvf>, but also handles 'set' magic.
9053
9054=cut
9055*/
9056
c5be433b 9057void
66ceb532 9058Perl_sv_catpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
ef50df4b
GS
9059{
9060 va_list args;
7918f24d
NC
9061
9062 PERL_ARGS_ASSERT_SV_CATPVF_MG;
9063
ef50df4b 9064 va_start(args, pat);
c5be433b 9065 sv_vcatpvf_mg(sv, pat, &args);
ef50df4b 9066 va_end(args);
c5be433b
GS
9067}
9068
bffc3d17
SH
9069/*
9070=for apidoc sv_vcatpvf_mg
9071
9072Like C<sv_vcatpvf>, but also handles 'set' magic.
9073
9074Usually used via its frontend C<sv_catpvf_mg>.
9075
9076=cut
9077*/
645c22ef 9078
c5be433b 9079void
66ceb532 9080Perl_sv_vcatpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
c5be433b 9081{
7918f24d
NC
9082 PERL_ARGS_ASSERT_SV_VCATPVF_MG;
9083
4608196e 9084 sv_vcatpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
ef50df4b
GS
9085 SvSETMAGIC(sv);
9086}
9087
954c1994
GS
9088/*
9089=for apidoc sv_vsetpvfn
9090
bffc3d17 9091Works like C<sv_vcatpvfn> but copies the text into the SV instead of
954c1994
GS
9092appending it.
9093
bffc3d17 9094Usually used via one of its frontends C<sv_vsetpvf> and C<sv_vsetpvf_mg>.
645c22ef 9095
954c1994
GS
9096=cut
9097*/
9098
46fc3d4c 9099void
66ceb532
SS
9100Perl_sv_vsetpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
9101 va_list *const args, SV **const svargs, const I32 svmax, bool *const maybe_tainted)
46fc3d4c 9102{
7918f24d
NC
9103 PERL_ARGS_ASSERT_SV_VSETPVFN;
9104
76f68e9b 9105 sv_setpvs(sv, "");
7d5ea4e7 9106 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted);
46fc3d4c 9107}
9108
2d00ba3b 9109STATIC I32
66ceb532 9110S_expect_number(pTHX_ char **const pattern)
211dfcf1 9111{
97aff369 9112 dVAR;
211dfcf1 9113 I32 var = 0;
7918f24d
NC
9114
9115 PERL_ARGS_ASSERT_EXPECT_NUMBER;
9116
211dfcf1
HS
9117 switch (**pattern) {
9118 case '1': case '2': case '3':
9119 case '4': case '5': case '6':
9120 case '7': case '8': case '9':
2fba7546
GA
9121 var = *(*pattern)++ - '0';
9122 while (isDIGIT(**pattern)) {
5f66b61c 9123 const I32 tmp = var * 10 + (*(*pattern)++ - '0');
2fba7546
GA
9124 if (tmp < var)
9125 Perl_croak(aTHX_ "Integer overflow in format string for %s", (PL_op ? OP_NAME(PL_op) : "sv_vcatpvfn"));
9126 var = tmp;
9127 }
211dfcf1
HS
9128 }
9129 return var;
9130}
211dfcf1 9131
c445ea15 9132STATIC char *
66ceb532 9133S_F0convert(NV nv, char *const endbuf, STRLEN *const len)
4151a5fe 9134{
a3b680e6 9135 const int neg = nv < 0;
4151a5fe 9136 UV uv;
4151a5fe 9137
7918f24d
NC
9138 PERL_ARGS_ASSERT_F0CONVERT;
9139
4151a5fe
IZ
9140 if (neg)
9141 nv = -nv;
9142 if (nv < UV_MAX) {
b464bac0 9143 char *p = endbuf;
4151a5fe 9144 nv += 0.5;
028f8eaa 9145 uv = (UV)nv;
4151a5fe
IZ
9146 if (uv & 1 && uv == nv)
9147 uv--; /* Round to even */
9148 do {
a3b680e6 9149 const unsigned dig = uv % 10;
4151a5fe
IZ
9150 *--p = '0' + dig;
9151 } while (uv /= 10);
9152 if (neg)
9153 *--p = '-';
9154 *len = endbuf - p;
9155 return p;
9156 }
bd61b366 9157 return NULL;
4151a5fe
IZ
9158}
9159
9160
954c1994
GS
9161/*
9162=for apidoc sv_vcatpvfn
9163
9164Processes its arguments like C<vsprintf> and appends the formatted output
9165to an SV. Uses an array of SVs if the C style variable argument list is
9166missing (NULL). When running with taint checks enabled, indicates via
9167C<maybe_tainted> if results are untrustworthy (often due to the use of
9168locales).
9169
bffc3d17 9170Usually used via one of its frontends C<sv_vcatpvf> and C<sv_vcatpvf_mg>.
645c22ef 9171
954c1994
GS
9172=cut
9173*/
9174
8896765a
RB
9175
9176#define VECTORIZE_ARGS vecsv = va_arg(*args, SV*);\
9177 vecstr = (U8*)SvPV_const(vecsv,veclen);\
9178 vec_utf8 = DO_UTF8(vecsv);
9179
1ef29b0e
RGS
9180/* XXX maybe_tainted is never assigned to, so the doc above is lying. */
9181
46fc3d4c 9182void
66ceb532
SS
9183Perl_sv_vcatpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
9184 va_list *const args, SV **const svargs, const I32 svmax, bool *const maybe_tainted)
46fc3d4c 9185{
97aff369 9186 dVAR;
46fc3d4c 9187 char *p;
9188 char *q;
a3b680e6 9189 const char *patend;
fc36a67e 9190 STRLEN origlen;
46fc3d4c 9191 I32 svix = 0;
27da23d5 9192 static const char nullstr[] = "(null)";
a0714e2c 9193 SV *argsv = NULL;
b464bac0
AL
9194 bool has_utf8 = DO_UTF8(sv); /* has the result utf8? */
9195 const bool pat_utf8 = has_utf8; /* the pattern is in utf8? */
a0714e2c 9196 SV *nsv = NULL;
4151a5fe
IZ
9197 /* Times 4: a decimal digit takes more than 3 binary digits.
9198 * NV_DIG: mantissa takes than many decimal digits.
9199 * Plus 32: Playing safe. */
9200 char ebuf[IV_DIG * 4 + NV_DIG + 32];
9201 /* large enough for "%#.#f" --chip */
9202 /* what about long double NVs? --jhi */
db79b45b 9203
7918f24d 9204 PERL_ARGS_ASSERT_SV_VCATPVFN;
53c1dcc0
AL
9205 PERL_UNUSED_ARG(maybe_tainted);
9206
46fc3d4c 9207 /* no matter what, this is a string now */
fc36a67e 9208 (void)SvPV_force(sv, origlen);
46fc3d4c 9209
8896765a 9210 /* special-case "", "%s", and "%-p" (SVf - see below) */
46fc3d4c 9211 if (patlen == 0)
9212 return;
0dbb1585 9213 if (patlen == 2 && pat[0] == '%' && pat[1] == 's') {
2d03de9c
AL
9214 if (args) {
9215 const char * const s = va_arg(*args, char*);
9216 sv_catpv(sv, s ? s : nullstr);
9217 }
9218 else if (svix < svmax) {
9219 sv_catsv(sv, *svargs);
2d03de9c
AL
9220 }
9221 return;
0dbb1585 9222 }
8896765a
RB
9223 if (args && patlen == 3 && pat[0] == '%' &&
9224 pat[1] == '-' && pat[2] == 'p') {
daba3364 9225 argsv = MUTABLE_SV(va_arg(*args, void*));
8896765a 9226 sv_catsv(sv, argsv);
8896765a 9227 return;
46fc3d4c 9228 }
9229
1d917b39 9230#ifndef USE_LONG_DOUBLE
4151a5fe 9231 /* special-case "%.<number>[gf]" */
7af36d83 9232 if ( !args && patlen <= 5 && pat[0] == '%' && pat[1] == '.'
4151a5fe
IZ
9233 && (pat[patlen-1] == 'g' || pat[patlen-1] == 'f') ) {
9234 unsigned digits = 0;
9235 const char *pp;
9236
9237 pp = pat + 2;
9238 while (*pp >= '0' && *pp <= '9')
9239 digits = 10 * digits + (*pp++ - '0');
028f8eaa 9240 if (pp - pat == (int)patlen - 1) {
4151a5fe
IZ
9241 NV nv;
9242
7af36d83 9243 if (svix < svmax)
4151a5fe
IZ
9244 nv = SvNV(*svargs);
9245 else
9246 return;
9247 if (*pp == 'g') {
2873255c
NC
9248 /* Add check for digits != 0 because it seems that some
9249 gconverts are buggy in this case, and we don't yet have
9250 a Configure test for this. */
9251 if (digits && digits < sizeof(ebuf) - NV_DIG - 10) {
9252 /* 0, point, slack */
2e59c212 9253 Gconvert(nv, (int)digits, 0, ebuf);
4151a5fe
IZ
9254 sv_catpv(sv, ebuf);
9255 if (*ebuf) /* May return an empty string for digits==0 */
9256 return;
9257 }
9258 } else if (!digits) {
9259 STRLEN l;
9260
9261 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
9262 sv_catpvn(sv, p, l);
9263 return;
9264 }
9265 }
9266 }
9267 }
1d917b39 9268#endif /* !USE_LONG_DOUBLE */
4151a5fe 9269
2cf2cfc6 9270 if (!args && svix < svmax && DO_UTF8(*svargs))
205f51d8 9271 has_utf8 = TRUE;
2cf2cfc6 9272
46fc3d4c 9273 patend = (char*)pat + patlen;
9274 for (p = (char*)pat; p < patend; p = q) {
9275 bool alt = FALSE;
9276 bool left = FALSE;
b22c7a20 9277 bool vectorize = FALSE;
211dfcf1 9278 bool vectorarg = FALSE;
2cf2cfc6 9279 bool vec_utf8 = FALSE;
46fc3d4c 9280 char fill = ' ';
9281 char plus = 0;
9282 char intsize = 0;
9283 STRLEN width = 0;
fc36a67e 9284 STRLEN zeros = 0;
46fc3d4c 9285 bool has_precis = FALSE;
9286 STRLEN precis = 0;
c445ea15 9287 const I32 osvix = svix;
2cf2cfc6 9288 bool is_utf8 = FALSE; /* is this item utf8? */
20f6aaab
AS
9289#ifdef HAS_LDBL_SPRINTF_BUG
9290 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
205f51d8 9291 with sfio - Allen <allens@cpan.org> */
20f6aaab
AS
9292 bool fix_ldbl_sprintf_bug = FALSE;
9293#endif
205f51d8 9294
46fc3d4c 9295 char esignbuf[4];
89ebb4a3 9296 U8 utf8buf[UTF8_MAXBYTES+1];
46fc3d4c 9297 STRLEN esignlen = 0;
9298
bd61b366 9299 const char *eptr = NULL;
1d1ac7bc 9300 const char *fmtstart;
fc36a67e 9301 STRLEN elen = 0;
a0714e2c 9302 SV *vecsv = NULL;
4608196e 9303 const U8 *vecstr = NULL;
b22c7a20 9304 STRLEN veclen = 0;
934abaf1 9305 char c = 0;
46fc3d4c 9306 int i;
9c5ffd7c 9307 unsigned base = 0;
8c8eb53c
RB
9308 IV iv = 0;
9309 UV uv = 0;
9e5b023a
JH
9310 /* we need a long double target in case HAS_LONG_DOUBLE but
9311 not USE_LONG_DOUBLE
9312 */
35fff930 9313#if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE
9e5b023a
JH
9314 long double nv;
9315#else
65202027 9316 NV nv;
9e5b023a 9317#endif
46fc3d4c 9318 STRLEN have;
9319 STRLEN need;
9320 STRLEN gap;
7af36d83 9321 const char *dotstr = ".";
b22c7a20 9322 STRLEN dotstrlen = 1;
211dfcf1 9323 I32 efix = 0; /* explicit format parameter index */
eb3fce90 9324 I32 ewix = 0; /* explicit width index */
211dfcf1
HS
9325 I32 epix = 0; /* explicit precision index */
9326 I32 evix = 0; /* explicit vector index */
eb3fce90 9327 bool asterisk = FALSE;
46fc3d4c 9328
211dfcf1 9329 /* echo everything up to the next format specification */
46fc3d4c 9330 for (q = p; q < patend && *q != '%'; ++q) ;
9331 if (q > p) {
db79b45b
JH
9332 if (has_utf8 && !pat_utf8)
9333 sv_catpvn_utf8_upgrade(sv, p, q - p, nsv);
9334 else
9335 sv_catpvn(sv, p, q - p);
46fc3d4c 9336 p = q;
9337 }
9338 if (q++ >= patend)
9339 break;
9340
1d1ac7bc
MHM
9341 fmtstart = q;
9342
211dfcf1
HS
9343/*
9344 We allow format specification elements in this order:
9345 \d+\$ explicit format parameter index
9346 [-+ 0#]+ flags
a472f209 9347 v|\*(\d+\$)?v vector with optional (optionally specified) arg
f3583277 9348 0 flag (as above): repeated to allow "v02"
211dfcf1
HS
9349 \d+|\*(\d+\$)? width using optional (optionally specified) arg
9350 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
9351 [hlqLV] size
8896765a
RB
9352 [%bcdefginopsuxDFOUX] format (mandatory)
9353*/
9354
9355 if (args) {
9356/*
9357 As of perl5.9.3, printf format checking is on by default.
9358 Internally, perl uses %p formats to provide an escape to
9359 some extended formatting. This block deals with those
9360 extensions: if it does not match, (char*)q is reset and
9361 the normal format processing code is used.
9362
9363 Currently defined extensions are:
9364 %p include pointer address (standard)
9365 %-p (SVf) include an SV (previously %_)
9366 %-<num>p include an SV with precision <num>
8896765a
RB
9367 %<num>p reserved for future extensions
9368
9369 Robin Barker 2005-07-14
f46d31f2
RB
9370
9371 %1p (VDf) removed. RMB 2007-10-19
211dfcf1 9372*/
8896765a
RB
9373 char* r = q;
9374 bool sv = FALSE;
9375 STRLEN n = 0;
9376 if (*q == '-')
9377 sv = *q++;
c445ea15 9378 n = expect_number(&q);
8896765a
RB
9379 if (*q++ == 'p') {
9380 if (sv) { /* SVf */
9381 if (n) {
9382 precis = n;
9383 has_precis = TRUE;
9384 }
daba3364 9385 argsv = MUTABLE_SV(va_arg(*args, void*));
4ea561bc 9386 eptr = SvPV_const(argsv, elen);
8896765a
RB
9387 if (DO_UTF8(argsv))
9388 is_utf8 = TRUE;
9389 goto string;
9390 }
8896765a
RB
9391 else if (n) {
9392 if (ckWARN_d(WARN_INTERNAL))
9393 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
9394 "internal %%<num>p might conflict with future printf extensions");
9395 }
9396 }
9397 q = r;
9398 }
9399
c445ea15 9400 if ( (width = expect_number(&q)) ) {
211dfcf1
HS
9401 if (*q == '$') {
9402 ++q;
9403 efix = width;
9404 } else {
9405 goto gotwidth;
9406 }
9407 }
9408
fc36a67e 9409 /* FLAGS */
9410
46fc3d4c 9411 while (*q) {
9412 switch (*q) {
9413 case ' ':
9414 case '+':
9911cee9
TS
9415 if (plus == '+' && *q == ' ') /* '+' over ' ' */
9416 q++;
9417 else
9418 plus = *q++;
46fc3d4c 9419 continue;
9420
9421 case '-':
9422 left = TRUE;
9423 q++;
9424 continue;
9425
9426 case '0':
9427 fill = *q++;
9428 continue;
9429
9430 case '#':
9431 alt = TRUE;
9432 q++;
9433 continue;
9434
fc36a67e 9435 default:
9436 break;
9437 }
9438 break;
9439 }
46fc3d4c 9440
211dfcf1 9441 tryasterisk:
eb3fce90 9442 if (*q == '*') {
211dfcf1 9443 q++;
c445ea15 9444 if ( (ewix = expect_number(&q)) )
211dfcf1
HS
9445 if (*q++ != '$')
9446 goto unknown;
eb3fce90 9447 asterisk = TRUE;
211dfcf1
HS
9448 }
9449 if (*q == 'v') {
eb3fce90 9450 q++;
211dfcf1
HS
9451 if (vectorize)
9452 goto unknown;
9cbac4c7 9453 if ((vectorarg = asterisk)) {
211dfcf1
HS
9454 evix = ewix;
9455 ewix = 0;
9456 asterisk = FALSE;
9457 }
9458 vectorize = TRUE;
9459 goto tryasterisk;
eb3fce90
JH
9460 }
9461
211dfcf1 9462 if (!asterisk)
858a90f9 9463 {
7a5fa8a2 9464 if( *q == '0' )
f3583277 9465 fill = *q++;
c445ea15 9466 width = expect_number(&q);
858a90f9 9467 }
211dfcf1
HS
9468
9469 if (vectorize) {
9470 if (vectorarg) {
9471 if (args)
9472 vecsv = va_arg(*args, SV*);
7ad96abb
NC
9473 else if (evix) {
9474 vecsv = (evix > 0 && evix <= svmax)
9475 ? svargs[evix-1] : &PL_sv_undef;
9476 } else {
9477 vecsv = svix < svmax ? svargs[svix++] : &PL_sv_undef;
9478 }
245d4a47 9479 dotstr = SvPV_const(vecsv, dotstrlen);
640283f5
NC
9480 /* Keep the DO_UTF8 test *after* the SvPV call, else things go
9481 bad with tied or overloaded values that return UTF8. */
211dfcf1 9482 if (DO_UTF8(vecsv))
2cf2cfc6 9483 is_utf8 = TRUE;
640283f5
NC
9484 else if (has_utf8) {
9485 vecsv = sv_mortalcopy(vecsv);
9486 sv_utf8_upgrade(vecsv);
9487 dotstr = SvPV_const(vecsv, dotstrlen);
9488 is_utf8 = TRUE;
9489 }
211dfcf1
HS
9490 }
9491 if (args) {
8896765a 9492 VECTORIZE_ARGS
eb3fce90 9493 }
7ad96abb 9494 else if (efix ? (efix > 0 && efix <= svmax) : svix < svmax) {
211dfcf1 9495 vecsv = svargs[efix ? efix-1 : svix++];
245d4a47 9496 vecstr = (U8*)SvPV_const(vecsv,veclen);
2cf2cfc6 9497 vec_utf8 = DO_UTF8(vecsv);
96b8f7ce
JP
9498
9499 /* if this is a version object, we need to convert
9500 * back into v-string notation and then let the
9501 * vectorize happen normally
d7aa5382 9502 */
96b8f7ce
JP
9503 if (sv_derived_from(vecsv, "version")) {
9504 char *version = savesvpv(vecsv);
85fbaab2 9505 if ( hv_exists(MUTABLE_HV(SvRV(vecsv)), "alpha", 5 ) ) {
34ba6322
SP
9506 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
9507 "vector argument not supported with alpha versions");
9508 goto unknown;
9509 }
96b8f7ce 9510 vecsv = sv_newmortal();
65b06e02 9511 scan_vstring(version, version + veclen, vecsv);
96b8f7ce
JP
9512 vecstr = (U8*)SvPV_const(vecsv, veclen);
9513 vec_utf8 = DO_UTF8(vecsv);
9514 Safefree(version);
d7aa5382 9515 }
211dfcf1
HS
9516 }
9517 else {
9518 vecstr = (U8*)"";
9519 veclen = 0;
9520 }
eb3fce90 9521 }
fc36a67e 9522
eb3fce90 9523 if (asterisk) {
fc36a67e 9524 if (args)
9525 i = va_arg(*args, int);
9526 else
eb3fce90
JH
9527 i = (ewix ? ewix <= svmax : svix < svmax) ?
9528 SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
fc36a67e 9529 left |= (i < 0);
9530 width = (i < 0) ? -i : i;
fc36a67e 9531 }
211dfcf1 9532 gotwidth:
fc36a67e 9533
9534 /* PRECISION */
46fc3d4c 9535
fc36a67e 9536 if (*q == '.') {
9537 q++;
9538 if (*q == '*') {
211dfcf1 9539 q++;
c445ea15 9540 if ( ((epix = expect_number(&q))) && (*q++ != '$') )
7b8dd722
HS
9541 goto unknown;
9542 /* XXX: todo, support specified precision parameter */
9543 if (epix)
211dfcf1 9544 goto unknown;
46fc3d4c 9545 if (args)
9546 i = va_arg(*args, int);
9547 else
eb3fce90
JH
9548 i = (ewix ? ewix <= svmax : svix < svmax)
9549 ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
9911cee9
TS
9550 precis = i;
9551 has_precis = !(i < 0);
fc36a67e 9552 }
9553 else {
9554 precis = 0;
9555 while (isDIGIT(*q))
9556 precis = precis * 10 + (*q++ - '0');
9911cee9 9557 has_precis = TRUE;
fc36a67e 9558 }
fc36a67e 9559 }
46fc3d4c 9560
fc36a67e 9561 /* SIZE */
46fc3d4c 9562
fc36a67e 9563 switch (*q) {
c623ac67
GS
9564#ifdef WIN32
9565 case 'I': /* Ix, I32x, and I64x */
9566# ifdef WIN64
9567 if (q[1] == '6' && q[2] == '4') {
9568 q += 3;
9569 intsize = 'q';
9570 break;
9571 }
9572# endif
9573 if (q[1] == '3' && q[2] == '2') {
9574 q += 3;
9575 break;
9576 }
9577# ifdef WIN64
9578 intsize = 'q';
9579# endif
9580 q++;
9581 break;
9582#endif
9e5b023a 9583#if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
6f9bb7fd 9584 case 'L': /* Ld */
5f66b61c 9585 /*FALLTHROUGH*/
e5c81feb 9586#ifdef HAS_QUAD
6f9bb7fd 9587 case 'q': /* qd */
9e5b023a 9588#endif
6f9bb7fd
GS
9589 intsize = 'q';
9590 q++;
9591 break;
9592#endif
fc36a67e 9593 case 'l':
9e5b023a 9594#if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
205f51d8 9595 if (*(q + 1) == 'l') { /* lld, llf */
fc36a67e 9596 intsize = 'q';
9597 q += 2;
46fc3d4c 9598 break;
cf2093f6 9599 }
fc36a67e 9600#endif
5f66b61c 9601 /*FALLTHROUGH*/
fc36a67e 9602 case 'h':
5f66b61c 9603 /*FALLTHROUGH*/
fc36a67e 9604 case 'V':
9605 intsize = *q++;
46fc3d4c 9606 break;
9607 }
9608
fc36a67e 9609 /* CONVERSION */
9610
211dfcf1
HS
9611 if (*q == '%') {
9612 eptr = q++;
9613 elen = 1;
26372e71
GA
9614 if (vectorize) {
9615 c = '%';
9616 goto unknown;
9617 }
211dfcf1
HS
9618 goto string;
9619 }
9620
26372e71 9621 if (!vectorize && !args) {
86c51f8b
NC
9622 if (efix) {
9623 const I32 i = efix-1;
9624 argsv = (i >= 0 && i < svmax) ? svargs[i] : &PL_sv_undef;
9625 } else {
9626 argsv = (svix >= 0 && svix < svmax)
9627 ? svargs[svix++] : &PL_sv_undef;
9628 }
863811b2 9629 }
211dfcf1 9630
46fc3d4c 9631 switch (c = *q++) {
9632
9633 /* STRINGS */
9634
46fc3d4c 9635 case 'c':
26372e71
GA
9636 if (vectorize)
9637 goto unknown;
4ea561bc 9638 uv = (args) ? va_arg(*args, int) : SvIV(argsv);
1bd104fb
JH
9639 if ((uv > 255 ||
9640 (!UNI_IS_INVARIANT(uv) && SvUTF8(sv)))
0064a8a9 9641 && !IN_BYTES) {
dfe13c55 9642 eptr = (char*)utf8buf;
9041c2e3 9643 elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf;
2cf2cfc6 9644 is_utf8 = TRUE;
7e2040f0
GS
9645 }
9646 else {
9647 c = (char)uv;
9648 eptr = &c;
9649 elen = 1;
a0ed51b3 9650 }
46fc3d4c 9651 goto string;
9652
46fc3d4c 9653 case 's':
26372e71
GA
9654 if (vectorize)
9655 goto unknown;
9656 if (args) {
fc36a67e 9657 eptr = va_arg(*args, char*);
c635e13b 9658 if (eptr)
9659 elen = strlen(eptr);
9660 else {
27da23d5 9661 eptr = (char *)nullstr;
c635e13b 9662 elen = sizeof nullstr - 1;
9663 }
46fc3d4c 9664 }
211dfcf1 9665 else {
4ea561bc 9666 eptr = SvPV_const(argsv, elen);
7e2040f0 9667 if (DO_UTF8(argsv)) {
c494f1f4 9668 STRLEN old_precis = precis;
a0ed51b3 9669 if (has_precis && precis < elen) {
c494f1f4 9670 STRLEN ulen = sv_len_utf8(argsv);
9ef5ed94 9671 I32 p = precis > ulen ? ulen : precis;
7e2040f0 9672 sv_pos_u2b(argsv, &p, 0); /* sticks at end */
a0ed51b3
LW
9673 precis = p;
9674 }
9675 if (width) { /* fudge width (can't fudge elen) */
59b61096
AV
9676 if (has_precis && precis < elen)
9677 width += precis - old_precis;
9678 else
9679 width += elen - sv_len_utf8(argsv);
a0ed51b3 9680 }
2cf2cfc6 9681 is_utf8 = TRUE;
a0ed51b3
LW
9682 }
9683 }
fc36a67e 9684
46fc3d4c 9685 string:
9ef5ed94 9686 if (has_precis && precis < elen)
46fc3d4c 9687 elen = precis;
9688 break;
9689
9690 /* INTEGERS */
9691
fc36a67e 9692 case 'p':
be75b157 9693 if (alt || vectorize)
c2e66d9e 9694 goto unknown;
211dfcf1 9695 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
fc36a67e 9696 base = 16;
9697 goto integer;
9698
46fc3d4c 9699 case 'D':
29fe7a80 9700#ifdef IV_IS_QUAD
22f3ae8c 9701 intsize = 'q';
29fe7a80 9702#else
46fc3d4c 9703 intsize = 'l';
29fe7a80 9704#endif
5f66b61c 9705 /*FALLTHROUGH*/
46fc3d4c 9706 case 'd':
9707 case 'i':
8896765a
RB
9708#if vdNUMBER
9709 format_vd:
9710#endif
b22c7a20 9711 if (vectorize) {
ba210ebe 9712 STRLEN ulen;
211dfcf1
HS
9713 if (!veclen)
9714 continue;
2cf2cfc6
A
9715 if (vec_utf8)
9716 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
9717 UTF8_ALLOW_ANYUV);
b22c7a20 9718 else {
e83d50c9 9719 uv = *vecstr;
b22c7a20
GS
9720 ulen = 1;
9721 }
9722 vecstr += ulen;
9723 veclen -= ulen;
e83d50c9
JP
9724 if (plus)
9725 esignbuf[esignlen++] = plus;
b22c7a20
GS
9726 }
9727 else if (args) {
46fc3d4c 9728 switch (intsize) {
9729 case 'h': iv = (short)va_arg(*args, int); break;
46fc3d4c 9730 case 'l': iv = va_arg(*args, long); break;
fc36a67e 9731 case 'V': iv = va_arg(*args, IV); break;
b10c0dba 9732 default: iv = va_arg(*args, int); break;
53f65a9e 9733 case 'q':
cf2093f6 9734#ifdef HAS_QUAD
53f65a9e
HS
9735 iv = va_arg(*args, Quad_t); break;
9736#else
9737 goto unknown;
cf2093f6 9738#endif
46fc3d4c 9739 }
9740 }
9741 else {
4ea561bc 9742 IV tiv = SvIV(argsv); /* work around GCC bug #13488 */
46fc3d4c 9743 switch (intsize) {
b10c0dba
MHM
9744 case 'h': iv = (short)tiv; break;
9745 case 'l': iv = (long)tiv; break;
9746 case 'V':
9747 default: iv = tiv; break;
53f65a9e 9748 case 'q':
cf2093f6 9749#ifdef HAS_QUAD
53f65a9e
HS
9750 iv = (Quad_t)tiv; break;
9751#else
9752 goto unknown;
cf2093f6 9753#endif
46fc3d4c 9754 }
9755 }
e83d50c9
JP
9756 if ( !vectorize ) /* we already set uv above */
9757 {
9758 if (iv >= 0) {
9759 uv = iv;
9760 if (plus)
9761 esignbuf[esignlen++] = plus;
9762 }
9763 else {
9764 uv = -iv;
9765 esignbuf[esignlen++] = '-';
9766 }
46fc3d4c 9767 }
9768 base = 10;
9769 goto integer;
9770
fc36a67e 9771 case 'U':
29fe7a80 9772#ifdef IV_IS_QUAD
22f3ae8c 9773 intsize = 'q';
29fe7a80 9774#else
fc36a67e 9775 intsize = 'l';
29fe7a80 9776#endif
5f66b61c 9777 /*FALLTHROUGH*/
fc36a67e 9778 case 'u':
9779 base = 10;
9780 goto uns_integer;
9781
7ff06cc7 9782 case 'B':
4f19785b
WSI
9783 case 'b':
9784 base = 2;
9785 goto uns_integer;
9786
46fc3d4c 9787 case 'O':
29fe7a80 9788#ifdef IV_IS_QUAD
22f3ae8c 9789 intsize = 'q';
29fe7a80 9790#else
46fc3d4c 9791 intsize = 'l';
29fe7a80 9792#endif
5f66b61c 9793 /*FALLTHROUGH*/
46fc3d4c 9794 case 'o':
9795 base = 8;
9796 goto uns_integer;
9797
9798 case 'X':
46fc3d4c 9799 case 'x':
9800 base = 16;
46fc3d4c 9801
9802 uns_integer:
b22c7a20 9803 if (vectorize) {
ba210ebe 9804 STRLEN ulen;
b22c7a20 9805 vector:
211dfcf1
HS
9806 if (!veclen)
9807 continue;
2cf2cfc6
A
9808 if (vec_utf8)
9809 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
9810 UTF8_ALLOW_ANYUV);
b22c7a20 9811 else {
a05b299f 9812 uv = *vecstr;
b22c7a20
GS
9813 ulen = 1;
9814 }
9815 vecstr += ulen;
9816 veclen -= ulen;
9817 }
9818 else if (args) {
46fc3d4c 9819 switch (intsize) {
9820 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
46fc3d4c 9821 case 'l': uv = va_arg(*args, unsigned long); break;
fc36a67e 9822 case 'V': uv = va_arg(*args, UV); break;
b10c0dba 9823 default: uv = va_arg(*args, unsigned); break;
53f65a9e 9824 case 'q':
cf2093f6 9825#ifdef HAS_QUAD
53f65a9e
HS
9826 uv = va_arg(*args, Uquad_t); break;
9827#else
9828 goto unknown;
cf2093f6 9829#endif
46fc3d4c 9830 }
9831 }
9832 else {
4ea561bc 9833 UV tuv = SvUV(argsv); /* work around GCC bug #13488 */
46fc3d4c 9834 switch (intsize) {
b10c0dba
MHM
9835 case 'h': uv = (unsigned short)tuv; break;
9836 case 'l': uv = (unsigned long)tuv; break;
9837 case 'V':
9838 default: uv = tuv; break;
53f65a9e 9839 case 'q':
cf2093f6 9840#ifdef HAS_QUAD
53f65a9e
HS
9841 uv = (Uquad_t)tuv; break;
9842#else
9843 goto unknown;
cf2093f6 9844#endif
46fc3d4c 9845 }
9846 }
9847
9848 integer:
4d84ee25
NC
9849 {
9850 char *ptr = ebuf + sizeof ebuf;
1387f30c
DD
9851 bool tempalt = uv ? alt : FALSE; /* Vectors can't change alt */
9852 zeros = 0;
9853
4d84ee25
NC
9854 switch (base) {
9855 unsigned dig;
9856 case 16:
14eb61ab 9857 p = (char *)((c == 'X') ? PL_hexdigit + 16 : PL_hexdigit);
4d84ee25
NC
9858 do {
9859 dig = uv & 15;
9860 *--ptr = p[dig];
9861 } while (uv >>= 4);
1387f30c 9862 if (tempalt) {
4d84ee25
NC
9863 esignbuf[esignlen++] = '0';
9864 esignbuf[esignlen++] = c; /* 'x' or 'X' */
9865 }
9866 break;
9867 case 8:
9868 do {
9869 dig = uv & 7;
9870 *--ptr = '0' + dig;
9871 } while (uv >>= 3);
9872 if (alt && *ptr != '0')
9873 *--ptr = '0';
9874 break;
9875 case 2:
9876 do {
9877 dig = uv & 1;
9878 *--ptr = '0' + dig;
9879 } while (uv >>= 1);
1387f30c 9880 if (tempalt) {
4d84ee25 9881 esignbuf[esignlen++] = '0';
7ff06cc7 9882 esignbuf[esignlen++] = c;
4d84ee25
NC
9883 }
9884 break;
9885 default: /* it had better be ten or less */
9886 do {
9887 dig = uv % base;
9888 *--ptr = '0' + dig;
9889 } while (uv /= base);
9890 break;
46fc3d4c 9891 }
4d84ee25
NC
9892 elen = (ebuf + sizeof ebuf) - ptr;
9893 eptr = ptr;
9894 if (has_precis) {
9895 if (precis > elen)
9896 zeros = precis - elen;
e6bb52fd
TS
9897 else if (precis == 0 && elen == 1 && *eptr == '0'
9898 && !(base == 8 && alt)) /* "%#.0o" prints "0" */
4d84ee25 9899 elen = 0;
9911cee9
TS
9900
9901 /* a precision nullifies the 0 flag. */
9902 if (fill == '0')
9903 fill = ' ';
eda88b6d 9904 }
c10ed8b9 9905 }
46fc3d4c 9906 break;
9907
9908 /* FLOATING POINT */
9909
fc36a67e 9910 case 'F':
9911 c = 'f'; /* maybe %F isn't supported here */
5f66b61c 9912 /*FALLTHROUGH*/
46fc3d4c 9913 case 'e': case 'E':
fc36a67e 9914 case 'f':
46fc3d4c 9915 case 'g': case 'G':
26372e71
GA
9916 if (vectorize)
9917 goto unknown;
46fc3d4c 9918
9919 /* This is evil, but floating point is even more evil */
9920
9e5b023a
JH
9921 /* for SV-style calling, we can only get NV
9922 for C-style calling, we assume %f is double;
9923 for simplicity we allow any of %Lf, %llf, %qf for long double
9924 */
9925 switch (intsize) {
9926 case 'V':
9927#if defined(USE_LONG_DOUBLE)
9928 intsize = 'q';
9929#endif
9930 break;
8a2e3f14 9931/* [perl #20339] - we should accept and ignore %lf rather than die */
00e17364 9932 case 'l':
5f66b61c 9933 /*FALLTHROUGH*/
9e5b023a
JH
9934 default:
9935#if defined(USE_LONG_DOUBLE)
9936 intsize = args ? 0 : 'q';
9937#endif
9938 break;
9939 case 'q':
9940#if defined(HAS_LONG_DOUBLE)
9941 break;
9942#else
5f66b61c 9943 /*FALLTHROUGH*/
9e5b023a
JH
9944#endif
9945 case 'h':
9e5b023a
JH
9946 goto unknown;
9947 }
9948
9949 /* now we need (long double) if intsize == 'q', else (double) */
26372e71 9950 nv = (args) ?
35fff930
JH
9951#if LONG_DOUBLESIZE > DOUBLESIZE
9952 intsize == 'q' ?
205f51d8
AS
9953 va_arg(*args, long double) :
9954 va_arg(*args, double)
35fff930 9955#else
205f51d8 9956 va_arg(*args, double)
35fff930 9957#endif
4ea561bc 9958 : SvNV(argsv);
fc36a67e 9959
9960 need = 0;
3952c29a
NC
9961 /* nv * 0 will be NaN for NaN, +Inf and -Inf, and 0 for anything
9962 else. frexp() has some unspecified behaviour for those three */
9963 if (c != 'e' && c != 'E' && (nv * 0) == 0) {
fc36a67e 9964 i = PERL_INT_MIN;
9e5b023a
JH
9965 /* FIXME: if HAS_LONG_DOUBLE but not USE_LONG_DOUBLE this
9966 will cast our (long double) to (double) */
73b309ea 9967 (void)Perl_frexp(nv, &i);
fc36a67e 9968 if (i == PERL_INT_MIN)
cea2e8a9 9969 Perl_die(aTHX_ "panic: frexp");
c635e13b 9970 if (i > 0)
fc36a67e 9971 need = BIT_DIGITS(i);
9972 }
9973 need += has_precis ? precis : 6; /* known default */
20f6aaab 9974
fc36a67e 9975 if (need < width)
9976 need = width;
9977
20f6aaab
AS
9978#ifdef HAS_LDBL_SPRINTF_BUG
9979 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
205f51d8
AS
9980 with sfio - Allen <allens@cpan.org> */
9981
9982# ifdef DBL_MAX
9983# define MY_DBL_MAX DBL_MAX
9984# else /* XXX guessing! HUGE_VAL may be defined as infinity, so not using */
9985# if DOUBLESIZE >= 8
9986# define MY_DBL_MAX 1.7976931348623157E+308L
9987# else
9988# define MY_DBL_MAX 3.40282347E+38L
9989# endif
9990# endif
9991
9992# ifdef HAS_LDBL_SPRINTF_BUG_LESS1 /* only between -1L & 1L - Allen */
9993# define MY_DBL_MAX_BUG 1L
20f6aaab 9994# else
205f51d8 9995# define MY_DBL_MAX_BUG MY_DBL_MAX
20f6aaab 9996# endif
20f6aaab 9997
205f51d8
AS
9998# ifdef DBL_MIN
9999# define MY_DBL_MIN DBL_MIN
10000# else /* XXX guessing! -Allen */
10001# if DOUBLESIZE >= 8
10002# define MY_DBL_MIN 2.2250738585072014E-308L
10003# else
10004# define MY_DBL_MIN 1.17549435E-38L
10005# endif
10006# endif
20f6aaab 10007
205f51d8
AS
10008 if ((intsize == 'q') && (c == 'f') &&
10009 ((nv < MY_DBL_MAX_BUG) && (nv > -MY_DBL_MAX_BUG)) &&
10010 (need < DBL_DIG)) {
10011 /* it's going to be short enough that
10012 * long double precision is not needed */
10013
10014 if ((nv <= 0L) && (nv >= -0L))
10015 fix_ldbl_sprintf_bug = TRUE; /* 0 is 0 - easiest */
10016 else {
10017 /* would use Perl_fp_class as a double-check but not
10018 * functional on IRIX - see perl.h comments */
10019
10020 if ((nv >= MY_DBL_MIN) || (nv <= -MY_DBL_MIN)) {
10021 /* It's within the range that a double can represent */
10022#if defined(DBL_MAX) && !defined(DBL_MIN)
10023 if ((nv >= ((long double)1/DBL_MAX)) ||
10024 (nv <= (-(long double)1/DBL_MAX)))
20f6aaab 10025#endif
205f51d8 10026 fix_ldbl_sprintf_bug = TRUE;
20f6aaab 10027 }
205f51d8
AS
10028 }
10029 if (fix_ldbl_sprintf_bug == TRUE) {
10030 double temp;
10031
10032 intsize = 0;
10033 temp = (double)nv;
10034 nv = (NV)temp;
10035 }
20f6aaab 10036 }
205f51d8
AS
10037
10038# undef MY_DBL_MAX
10039# undef MY_DBL_MAX_BUG
10040# undef MY_DBL_MIN
10041
20f6aaab
AS
10042#endif /* HAS_LDBL_SPRINTF_BUG */
10043
46fc3d4c 10044 need += 20; /* fudge factor */
80252599
GS
10045 if (PL_efloatsize < need) {
10046 Safefree(PL_efloatbuf);
10047 PL_efloatsize = need + 20; /* more fudge */
a02a5408 10048 Newx(PL_efloatbuf, PL_efloatsize, char);
7d5ea4e7 10049 PL_efloatbuf[0] = '\0';
46fc3d4c 10050 }
10051
4151a5fe
IZ
10052 if ( !(width || left || plus || alt) && fill != '0'
10053 && has_precis && intsize != 'q' ) { /* Shortcuts */
2873255c
NC
10054 /* See earlier comment about buggy Gconvert when digits,
10055 aka precis is 0 */
10056 if ( c == 'g' && precis) {
2e59c212 10057 Gconvert((NV)nv, (int)precis, 0, PL_efloatbuf);
4150c189
NC
10058 /* May return an empty string for digits==0 */
10059 if (*PL_efloatbuf) {
10060 elen = strlen(PL_efloatbuf);
4151a5fe 10061 goto float_converted;
4150c189 10062 }
4151a5fe
IZ
10063 } else if ( c == 'f' && !precis) {
10064 if ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
10065 break;
10066 }
10067 }
4d84ee25
NC
10068 {
10069 char *ptr = ebuf + sizeof ebuf;
10070 *--ptr = '\0';
10071 *--ptr = c;
10072 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
9e5b023a 10073#if defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
4d84ee25
NC
10074 if (intsize == 'q') {
10075 /* Copy the one or more characters in a long double
10076 * format before the 'base' ([efgEFG]) character to
10077 * the format string. */
10078 static char const prifldbl[] = PERL_PRIfldbl;
10079 char const *p = prifldbl + sizeof(prifldbl) - 3;
10080 while (p >= prifldbl) { *--ptr = *p--; }
10081 }
65202027 10082#endif
4d84ee25
NC
10083 if (has_precis) {
10084 base = precis;
10085 do { *--ptr = '0' + (base % 10); } while (base /= 10);
10086 *--ptr = '.';
10087 }
10088 if (width) {
10089 base = width;
10090 do { *--ptr = '0' + (base % 10); } while (base /= 10);
10091 }
10092 if (fill == '0')
10093 *--ptr = fill;
10094 if (left)
10095 *--ptr = '-';
10096 if (plus)
10097 *--ptr = plus;
10098 if (alt)
10099 *--ptr = '#';
10100 *--ptr = '%';
10101
10102 /* No taint. Otherwise we are in the strange situation
10103 * where printf() taints but print($float) doesn't.
10104 * --jhi */
9e5b023a 10105#if defined(HAS_LONG_DOUBLE)
4150c189 10106 elen = ((intsize == 'q')
d9fad198
JH
10107 ? my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, nv)
10108 : my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, (double)nv));
9e5b023a 10109#else
4150c189 10110 elen = my_sprintf(PL_efloatbuf, ptr, nv);
9e5b023a 10111#endif
4d84ee25 10112 }
4151a5fe 10113 float_converted:
80252599 10114 eptr = PL_efloatbuf;
46fc3d4c 10115 break;
10116
fc36a67e 10117 /* SPECIAL */
10118
10119 case 'n':
26372e71
GA
10120 if (vectorize)
10121 goto unknown;
fc36a67e 10122 i = SvCUR(sv) - origlen;
26372e71 10123 if (args) {
c635e13b 10124 switch (intsize) {
10125 case 'h': *(va_arg(*args, short*)) = i; break;
10126 default: *(va_arg(*args, int*)) = i; break;
10127 case 'l': *(va_arg(*args, long*)) = i; break;
10128 case 'V': *(va_arg(*args, IV*)) = i; break;
53f65a9e 10129 case 'q':
cf2093f6 10130#ifdef HAS_QUAD
53f65a9e
HS
10131 *(va_arg(*args, Quad_t*)) = i; break;
10132#else
10133 goto unknown;
cf2093f6 10134#endif
c635e13b 10135 }
fc36a67e 10136 }
9dd79c3f 10137 else
211dfcf1 10138 sv_setuv_mg(argsv, (UV)i);
fc36a67e 10139 continue; /* not "break" */
10140
10141 /* UNKNOWN */
10142
46fc3d4c 10143 default:
fc36a67e 10144 unknown:
041457d9
DM
10145 if (!args
10146 && (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)
10147 && ckWARN(WARN_PRINTF))
10148 {
c4420975 10149 SV * const msg = sv_newmortal();
35c1215d
NC
10150 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
10151 (PL_op->op_type == OP_PRTF) ? "" : "s");
1d1ac7bc
MHM
10152 if (fmtstart < patend) {
10153 const char * const fmtend = q < patend ? q : patend;
10154 const char * f;
10155 sv_catpvs(msg, "\"%");
10156 for (f = fmtstart; f < fmtend; f++) {
10157 if (isPRINT(*f)) {
10158 sv_catpvn(msg, f, 1);
10159 } else {
10160 Perl_sv_catpvf(aTHX_ msg,
10161 "\\%03"UVof, (UV)*f & 0xFF);
10162 }
10163 }
10164 sv_catpvs(msg, "\"");
10165 } else {
396482e1 10166 sv_catpvs(msg, "end of string");
1d1ac7bc 10167 }
be2597df 10168 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%"SVf, SVfARG(msg)); /* yes, this is reentrant */
c635e13b 10169 }
fb73857a 10170
10171 /* output mangled stuff ... */
10172 if (c == '\0')
10173 --q;
46fc3d4c 10174 eptr = p;
10175 elen = q - p;
fb73857a 10176
10177 /* ... right here, because formatting flags should not apply */
10178 SvGROW(sv, SvCUR(sv) + elen + 1);
10179 p = SvEND(sv);
4459522c 10180 Copy(eptr, p, elen, char);
fb73857a 10181 p += elen;
10182 *p = '\0';
3f7c398e 10183 SvCUR_set(sv, p - SvPVX_const(sv));
58e33a90 10184 svix = osvix;
fb73857a 10185 continue; /* not "break" */
46fc3d4c 10186 }
10187
cc61b222
TS
10188 if (is_utf8 != has_utf8) {
10189 if (is_utf8) {
10190 if (SvCUR(sv))
10191 sv_utf8_upgrade(sv);
10192 }
10193 else {
10194 const STRLEN old_elen = elen;
59cd0e26 10195 SV * const nsv = newSVpvn_flags(eptr, elen, SVs_TEMP);
cc61b222
TS
10196 sv_utf8_upgrade(nsv);
10197 eptr = SvPVX_const(nsv);
10198 elen = SvCUR(nsv);
10199
10200 if (width) { /* fudge width (can't fudge elen) */
10201 width += elen - old_elen;
10202 }
10203 is_utf8 = TRUE;
10204 }
10205 }
10206
6c94ec8b 10207 have = esignlen + zeros + elen;
ed2b91d2 10208 if (have < zeros)
f1f66076 10209 Perl_croak_nocontext("%s", PL_memory_wrap);
6c94ec8b 10210
46fc3d4c 10211 need = (have > width ? have : width);
10212 gap = need - have;
10213
d2641cbd 10214 if (need >= (((STRLEN)~0) - SvCUR(sv) - dotstrlen - 1))
f1f66076 10215 Perl_croak_nocontext("%s", PL_memory_wrap);
b22c7a20 10216 SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1);
46fc3d4c 10217 p = SvEND(sv);
10218 if (esignlen && fill == '0') {
53c1dcc0 10219 int i;
eb160463 10220 for (i = 0; i < (int)esignlen; i++)
46fc3d4c 10221 *p++ = esignbuf[i];
10222 }
10223 if (gap && !left) {
10224 memset(p, fill, gap);
10225 p += gap;
10226 }
10227 if (esignlen && fill != '0') {
53c1dcc0 10228 int i;
eb160463 10229 for (i = 0; i < (int)esignlen; i++)
46fc3d4c 10230 *p++ = esignbuf[i];
10231 }
fc36a67e 10232 if (zeros) {
53c1dcc0 10233 int i;
fc36a67e 10234 for (i = zeros; i; i--)
10235 *p++ = '0';
10236 }
46fc3d4c 10237 if (elen) {
4459522c 10238 Copy(eptr, p, elen, char);
46fc3d4c 10239 p += elen;
10240 }
10241 if (gap && left) {
10242 memset(p, ' ', gap);
10243 p += gap;
10244 }
b22c7a20
GS
10245 if (vectorize) {
10246 if (veclen) {
4459522c 10247 Copy(dotstr, p, dotstrlen, char);
b22c7a20
GS
10248 p += dotstrlen;
10249 }
10250 else
10251 vectorize = FALSE; /* done iterating over vecstr */
10252 }
2cf2cfc6
A
10253 if (is_utf8)
10254 has_utf8 = TRUE;
10255 if (has_utf8)
7e2040f0 10256 SvUTF8_on(sv);
46fc3d4c 10257 *p = '\0';
3f7c398e 10258 SvCUR_set(sv, p - SvPVX_const(sv));
b22c7a20
GS
10259 if (vectorize) {
10260 esignlen = 0;
10261 goto vector;
10262 }
46fc3d4c 10263 }
10264}
51371543 10265
645c22ef
DM
10266/* =========================================================================
10267
10268=head1 Cloning an interpreter
10269
10270All the macros and functions in this section are for the private use of
10271the main function, perl_clone().
10272
f2fc5c80 10273The foo_dup() functions make an exact copy of an existing foo thingy.
645c22ef
DM
10274During the course of a cloning, a hash table is used to map old addresses
10275to new addresses. The table is created and manipulated with the
10276ptr_table_* functions.
10277
10278=cut
10279
3e8320cc 10280 * =========================================================================*/
645c22ef
DM
10281
10282
1d7c1841
GS
10283#if defined(USE_ITHREADS)
10284
d4c19fe8 10285/* XXX Remove this so it doesn't have to go thru the macro and return for nothing */
1d7c1841
GS
10286#ifndef GpREFCNT_inc
10287# define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
10288#endif
10289
10290
a41cc44e 10291/* Certain cases in Perl_ss_dup have been merged, by relying on the fact
3e07292d 10292 that currently av_dup, gv_dup and hv_dup are the same as sv_dup.
538f2e76
NC
10293 If this changes, please unmerge ss_dup.
10294 Likewise, sv_dup_inc_multiple() relies on this fact. */
d2d73c3e 10295#define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t))
7f466ec7 10296#define sv_dup_inc_NN(s,t) SvREFCNT_inc_NN(sv_dup(s,t))
502c6561
NC
10297#define av_dup(s,t) MUTABLE_AV(sv_dup((const SV *)s,t))
10298#define av_dup_inc(s,t) MUTABLE_AV(SvREFCNT_inc(sv_dup((const SV *)s,t)))
85fbaab2
NC
10299#define hv_dup(s,t) MUTABLE_HV(sv_dup((const SV *)s,t))
10300#define hv_dup_inc(s,t) MUTABLE_HV(SvREFCNT_inc(sv_dup((const SV *)s,t)))
daba3364 10301#define cv_dup(s,t) MUTABLE_CV(sv_dup((const SV *)s,t))
ea726b52 10302#define cv_dup_inc(s,t) MUTABLE_CV(SvREFCNT_inc(sv_dup((const SV *)s,t)))
daba3364 10303#define io_dup(s,t) MUTABLE_IO(sv_dup((const SV *)s,t))
a45c7426 10304#define io_dup_inc(s,t) MUTABLE_IO(SvREFCNT_inc(sv_dup((const SV *)s,t)))
159b6efe
NC
10305#define gv_dup(s,t) MUTABLE_GV(sv_dup((const SV *)s,t))
10306#define gv_dup_inc(s,t) MUTABLE_GV(SvREFCNT_inc(sv_dup((const SV *)s,t)))
6136c704
AL
10307#define SAVEPV(p) ((p) ? savepv(p) : NULL)
10308#define SAVEPVN(p,n) ((p) ? savepvn(p,n) : NULL)
8cf8f3d1 10309
199e78b7
DM
10310/* clone a parser */
10311
10312yy_parser *
66ceb532 10313Perl_parser_dup(pTHX_ const yy_parser *const proto, CLONE_PARAMS *const param)
199e78b7
DM
10314{
10315 yy_parser *parser;
10316
7918f24d
NC
10317 PERL_ARGS_ASSERT_PARSER_DUP;
10318
199e78b7
DM
10319 if (!proto)
10320 return NULL;
10321
7c197c94
DM
10322 /* look for it in the table first */
10323 parser = (yy_parser *)ptr_table_fetch(PL_ptr_table, proto);
10324 if (parser)
10325 return parser;
10326
10327 /* create anew and remember what it is */
199e78b7 10328 Newxz(parser, 1, yy_parser);
7c197c94 10329 ptr_table_store(PL_ptr_table, proto, parser);
199e78b7
DM
10330
10331 parser->yyerrstatus = 0;
10332 parser->yychar = YYEMPTY; /* Cause a token to be read. */
10333
10334 /* XXX these not yet duped */
10335 parser->old_parser = NULL;
10336 parser->stack = NULL;
10337 parser->ps = NULL;
10338 parser->stack_size = 0;
10339 /* XXX parser->stack->state = 0; */
10340
10341 /* XXX eventually, just Copy() most of the parser struct ? */
10342
10343 parser->lex_brackets = proto->lex_brackets;
10344 parser->lex_casemods = proto->lex_casemods;
10345 parser->lex_brackstack = savepvn(proto->lex_brackstack,
10346 (proto->lex_brackets < 120 ? 120 : proto->lex_brackets));
10347 parser->lex_casestack = savepvn(proto->lex_casestack,
10348 (proto->lex_casemods < 12 ? 12 : proto->lex_casemods));
10349 parser->lex_defer = proto->lex_defer;
10350 parser->lex_dojoin = proto->lex_dojoin;
10351 parser->lex_expect = proto->lex_expect;
10352 parser->lex_formbrack = proto->lex_formbrack;
10353 parser->lex_inpat = proto->lex_inpat;
10354 parser->lex_inwhat = proto->lex_inwhat;
10355 parser->lex_op = proto->lex_op;
10356 parser->lex_repl = sv_dup_inc(proto->lex_repl, param);
10357 parser->lex_starts = proto->lex_starts;
10358 parser->lex_stuff = sv_dup_inc(proto->lex_stuff, param);
10359 parser->multi_close = proto->multi_close;
10360 parser->multi_open = proto->multi_open;
10361 parser->multi_start = proto->multi_start;
670a9cb2 10362 parser->multi_end = proto->multi_end;
199e78b7
DM
10363 parser->pending_ident = proto->pending_ident;
10364 parser->preambled = proto->preambled;
10365 parser->sublex_info = proto->sublex_info; /* XXX not quite right */
bdc0bf6f 10366 parser->linestr = sv_dup_inc(proto->linestr, param);
53a7735b
DM
10367 parser->expect = proto->expect;
10368 parser->copline = proto->copline;
f06b5848 10369 parser->last_lop_op = proto->last_lop_op;
bc177e6b 10370 parser->lex_state = proto->lex_state;
2f9285f8 10371 parser->rsfp = fp_dup(proto->rsfp, '<', param);
5486870f
DM
10372 /* rsfp_filters entries have fake IoDIRP() */
10373 parser->rsfp_filters= av_dup_inc(proto->rsfp_filters, param);
12bd6ede
DM
10374 parser->in_my = proto->in_my;
10375 parser->in_my_stash = hv_dup(proto->in_my_stash, param);
13765c85 10376 parser->error_count = proto->error_count;
bc177e6b 10377
53a7735b 10378
f06b5848
DM
10379 parser->linestr = sv_dup_inc(proto->linestr, param);
10380
10381 {
1e05feb3
AL
10382 char * const ols = SvPVX(proto->linestr);
10383 char * const ls = SvPVX(parser->linestr);
f06b5848
DM
10384
10385 parser->bufptr = ls + (proto->bufptr >= ols ?
10386 proto->bufptr - ols : 0);
10387 parser->oldbufptr = ls + (proto->oldbufptr >= ols ?
10388 proto->oldbufptr - ols : 0);
10389 parser->oldoldbufptr= ls + (proto->oldoldbufptr >= ols ?
10390 proto->oldoldbufptr - ols : 0);
10391 parser->linestart = ls + (proto->linestart >= ols ?
10392 proto->linestart - ols : 0);
10393 parser->last_uni = ls + (proto->last_uni >= ols ?
10394 proto->last_uni - ols : 0);
10395 parser->last_lop = ls + (proto->last_lop >= ols ?
10396 proto->last_lop - ols : 0);
10397
10398 parser->bufend = ls + SvCUR(parser->linestr);
10399 }
199e78b7 10400
14047fc9
DM
10401 Copy(proto->tokenbuf, parser->tokenbuf, 256, char);
10402
2f9285f8 10403
199e78b7
DM
10404#ifdef PERL_MAD
10405 parser->endwhite = proto->endwhite;
10406 parser->faketokens = proto->faketokens;
10407 parser->lasttoke = proto->lasttoke;
10408 parser->nextwhite = proto->nextwhite;
10409 parser->realtokenstart = proto->realtokenstart;
10410 parser->skipwhite = proto->skipwhite;
10411 parser->thisclose = proto->thisclose;
10412 parser->thismad = proto->thismad;
10413 parser->thisopen = proto->thisopen;
10414 parser->thisstuff = proto->thisstuff;
10415 parser->thistoken = proto->thistoken;
10416 parser->thiswhite = proto->thiswhite;
fb205e7a
DM
10417
10418 Copy(proto->nexttoke, parser->nexttoke, 5, NEXTTOKE);
10419 parser->curforce = proto->curforce;
10420#else
10421 Copy(proto->nextval, parser->nextval, 5, YYSTYPE);
10422 Copy(proto->nexttype, parser->nexttype, 5, I32);
10423 parser->nexttoke = proto->nexttoke;
199e78b7
DM
10424#endif
10425 return parser;
10426}
10427
d2d73c3e 10428
d2d73c3e 10429/* duplicate a file handle */
645c22ef 10430
1d7c1841 10431PerlIO *
3be3cdd6 10432Perl_fp_dup(pTHX_ PerlIO *const fp, const char type, CLONE_PARAMS *const param)
1d7c1841
GS
10433{
10434 PerlIO *ret;
53c1dcc0 10435
7918f24d 10436 PERL_ARGS_ASSERT_FP_DUP;
53c1dcc0 10437 PERL_UNUSED_ARG(type);
73d840c0 10438
1d7c1841
GS
10439 if (!fp)
10440 return (PerlIO*)NULL;
10441
10442 /* look for it in the table first */
10443 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
10444 if (ret)
10445 return ret;
10446
10447 /* create anew and remember what it is */
ecdeb87c 10448 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
1d7c1841
GS
10449 ptr_table_store(PL_ptr_table, fp, ret);
10450 return ret;
10451}
10452
645c22ef
DM
10453/* duplicate a directory handle */
10454
1d7c1841 10455DIR *
66ceb532 10456Perl_dirp_dup(pTHX_ DIR *const dp)
1d7c1841 10457{
96a5add6 10458 PERL_UNUSED_CONTEXT;
1d7c1841
GS
10459 if (!dp)
10460 return (DIR*)NULL;
10461 /* XXX TODO */
10462 return dp;
10463}
10464
ff276b08 10465/* duplicate a typeglob */
645c22ef 10466
1d7c1841 10467GP *
66ceb532 10468Perl_gp_dup(pTHX_ GP *const gp, CLONE_PARAMS *const param)
1d7c1841
GS
10469{
10470 GP *ret;
b37c2d43 10471
7918f24d
NC
10472 PERL_ARGS_ASSERT_GP_DUP;
10473
1d7c1841
GS
10474 if (!gp)
10475 return (GP*)NULL;
10476 /* look for it in the table first */
10477 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
10478 if (ret)
10479 return ret;
10480
10481 /* create anew and remember what it is */
a02a5408 10482 Newxz(ret, 1, GP);
1d7c1841
GS
10483 ptr_table_store(PL_ptr_table, gp, ret);
10484
10485 /* clone */
46d65037
NC
10486 /* ret->gp_refcnt must be 0 before any other dups are called. We're relying
10487 on Newxz() to do this for us. */
d2d73c3e
AB
10488 ret->gp_sv = sv_dup_inc(gp->gp_sv, param);
10489 ret->gp_io = io_dup_inc(gp->gp_io, param);
10490 ret->gp_form = cv_dup_inc(gp->gp_form, param);
10491 ret->gp_av = av_dup_inc(gp->gp_av, param);
10492 ret->gp_hv = hv_dup_inc(gp->gp_hv, param);
10493 ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
10494 ret->gp_cv = cv_dup_inc(gp->gp_cv, param);
1d7c1841 10495 ret->gp_cvgen = gp->gp_cvgen;
1d7c1841 10496 ret->gp_line = gp->gp_line;
566771cc 10497 ret->gp_file_hek = hek_dup(gp->gp_file_hek, param);
1d7c1841
GS
10498 return ret;
10499}
10500
645c22ef
DM
10501/* duplicate a chain of magic */
10502
1d7c1841 10503MAGIC *
b88ec9b8 10504Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *const param)
1d7c1841 10505{
c160a186 10506 MAGIC *mgret = NULL;
0228edf6 10507 MAGIC **mgprev_p = &mgret;
7918f24d
NC
10508
10509 PERL_ARGS_ASSERT_MG_DUP;
10510
1d7c1841
GS
10511 for (; mg; mg = mg->mg_moremagic) {
10512 MAGIC *nmg;
45f7fcc8 10513 Newx(nmg, 1, MAGIC);
0228edf6
NC
10514 *mgprev_p = nmg;
10515 mgprev_p = &(nmg->mg_moremagic);
10516
45f7fcc8
NC
10517 /* There was a comment "XXX copy dynamic vtable?" but as we don't have
10518 dynamic vtables, I'm not sure why Sarathy wrote it. The comment dates
10519 from the original commit adding Perl_mg_dup() - revision 4538.
10520 Similarly there is the annotation "XXX random ptr?" next to the
10521 assignment to nmg->mg_ptr. */
10522 *nmg = *mg;
10523
288b8c02 10524 /* FIXME for plugins
45f7fcc8
NC
10525 if (nmg->mg_type == PERL_MAGIC_qr) {
10526 nmg->mg_obj = MUTABLE_SV(CALLREGDUPE((REGEXP*)nmg->mg_obj, param));
1d7c1841 10527 }
288b8c02
NC
10528 else
10529 */
45f7fcc8 10530 if(nmg->mg_type == PERL_MAGIC_backref) {
d7cbc7b5
NC
10531 /* The backref AV has its reference count deliberately bumped by
10532 1. */
502c6561 10533 nmg->mg_obj
45f7fcc8 10534 = SvREFCNT_inc(av_dup_inc((const AV *) nmg->mg_obj, param));
05bd4103 10535 }
1d7c1841 10536 else {
45f7fcc8
NC
10537 nmg->mg_obj = (nmg->mg_flags & MGf_REFCOUNTED)
10538 ? sv_dup_inc(nmg->mg_obj, param)
10539 : sv_dup(nmg->mg_obj, param);
10540 }
10541
10542 if (nmg->mg_ptr && nmg->mg_type != PERL_MAGIC_regex_global) {
10543 if (nmg->mg_len > 0) {
10544 nmg->mg_ptr = SAVEPVN(nmg->mg_ptr, nmg->mg_len);
10545 if (nmg->mg_type == PERL_MAGIC_overload_table &&
10546 AMT_AMAGIC((AMT*)nmg->mg_ptr))
14befaf4 10547 {
0bcc34c2 10548 AMT * const namtp = (AMT*)nmg->mg_ptr;
538f2e76
NC
10549 sv_dup_inc_multiple((SV**)(namtp->table),
10550 (SV**)(namtp->table), NofAMmeth, param);
1d7c1841
GS
10551 }
10552 }
45f7fcc8
NC
10553 else if (nmg->mg_len == HEf_SVKEY)
10554 nmg->mg_ptr = (char*)sv_dup_inc((const SV *)nmg->mg_ptr, param);
1d7c1841 10555 }
45f7fcc8 10556 if ((nmg->mg_flags & MGf_DUP) && nmg->mg_virtual && nmg->mg_virtual->svt_dup) {
68795e93
NIS
10557 CALL_FPTR(nmg->mg_virtual->svt_dup)(aTHX_ nmg, param);
10558 }
1d7c1841
GS
10559 }
10560 return mgret;
10561}
10562
4674ade5
NC
10563#endif /* USE_ITHREADS */
10564
645c22ef
DM
10565/* create a new pointer-mapping table */
10566
1d7c1841
GS
10567PTR_TBL_t *
10568Perl_ptr_table_new(pTHX)
10569{
10570 PTR_TBL_t *tbl;
96a5add6
AL
10571 PERL_UNUSED_CONTEXT;
10572
b3a120bf 10573 Newx(tbl, 1, PTR_TBL_t);
1d7c1841
GS
10574 tbl->tbl_max = 511;
10575 tbl->tbl_items = 0;
a02a5408 10576 Newxz(tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
1d7c1841
GS
10577 return tbl;
10578}
10579
7119fd33
NC
10580#define PTR_TABLE_HASH(ptr) \
10581 ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17)))
134ca3d6 10582
93e68bfb
JC
10583/*
10584 we use the PTE_SVSLOT 'reservation' made above, both here (in the
10585 following define) and at call to new_body_inline made below in
10586 Perl_ptr_table_store()
10587 */
10588
10589#define del_pte(p) del_body_type(p, PTE_SVSLOT)
32e691d0 10590
645c22ef
DM
10591/* map an existing pointer using a table */
10592
7bf61b54 10593STATIC PTR_TBL_ENT_t *
1eb6e4ca 10594S_ptr_table_find(PTR_TBL_t *const tbl, const void *const sv)
7918f24d 10595{
1d7c1841 10596 PTR_TBL_ENT_t *tblent;
4373e329 10597 const UV hash = PTR_TABLE_HASH(sv);
7918f24d
NC
10598
10599 PERL_ARGS_ASSERT_PTR_TABLE_FIND;
10600
1d7c1841
GS
10601 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
10602 for (; tblent; tblent = tblent->next) {
10603 if (tblent->oldval == sv)
7bf61b54 10604 return tblent;
1d7c1841 10605 }
d4c19fe8 10606 return NULL;
7bf61b54
NC
10607}
10608
10609void *
1eb6e4ca 10610Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *const tbl, const void *const sv)
7bf61b54 10611{
b0e6ae5b 10612 PTR_TBL_ENT_t const *const tblent = ptr_table_find(tbl, sv);
7918f24d
NC
10613
10614 PERL_ARGS_ASSERT_PTR_TABLE_FETCH;
96a5add6 10615 PERL_UNUSED_CONTEXT;
7918f24d 10616
d4c19fe8 10617 return tblent ? tblent->newval : NULL;
1d7c1841
GS
10618}
10619
645c22ef
DM
10620/* add a new entry to a pointer-mapping table */
10621
1d7c1841 10622void
1eb6e4ca 10623Perl_ptr_table_store(pTHX_ PTR_TBL_t *const tbl, const void *const oldsv, void *const newsv)
1d7c1841 10624{
0c9fdfe0 10625 PTR_TBL_ENT_t *tblent = ptr_table_find(tbl, oldsv);
7918f24d
NC
10626
10627 PERL_ARGS_ASSERT_PTR_TABLE_STORE;
96a5add6 10628 PERL_UNUSED_CONTEXT;
1d7c1841 10629
7bf61b54
NC
10630 if (tblent) {
10631 tblent->newval = newsv;
10632 } else {
10633 const UV entry = PTR_TABLE_HASH(oldsv) & tbl->tbl_max;
10634
d2a0f284
JC
10635 new_body_inline(tblent, PTE_SVSLOT);
10636
7bf61b54
NC
10637 tblent->oldval = oldsv;
10638 tblent->newval = newsv;
10639 tblent->next = tbl->tbl_ary[entry];
10640 tbl->tbl_ary[entry] = tblent;
10641 tbl->tbl_items++;
10642 if (tblent->next && tbl->tbl_items > tbl->tbl_max)
10643 ptr_table_split(tbl);
1d7c1841 10644 }
1d7c1841
GS
10645}
10646
645c22ef
DM
10647/* double the hash bucket size of an existing ptr table */
10648
1d7c1841 10649void
1eb6e4ca 10650Perl_ptr_table_split(pTHX_ PTR_TBL_t *const tbl)
1d7c1841
GS
10651{
10652 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
4373e329 10653 const UV oldsize = tbl->tbl_max + 1;
1d7c1841
GS
10654 UV newsize = oldsize * 2;
10655 UV i;
7918f24d
NC
10656
10657 PERL_ARGS_ASSERT_PTR_TABLE_SPLIT;
96a5add6 10658 PERL_UNUSED_CONTEXT;
1d7c1841
GS
10659
10660 Renew(ary, newsize, PTR_TBL_ENT_t*);
10661 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
10662 tbl->tbl_max = --newsize;
10663 tbl->tbl_ary = ary;
10664 for (i=0; i < oldsize; i++, ary++) {
10665 PTR_TBL_ENT_t **curentp, **entp, *ent;
10666 if (!*ary)
10667 continue;
10668 curentp = ary + oldsize;
10669 for (entp = ary, ent = *ary; ent; ent = *entp) {
134ca3d6 10670 if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
1d7c1841
GS
10671 *entp = ent->next;
10672 ent->next = *curentp;
10673 *curentp = ent;
10674 continue;
10675 }
10676 else
10677 entp = &ent->next;
10678 }
10679 }
10680}
10681
645c22ef
DM
10682/* remove all the entries from a ptr table */
10683
a0739874 10684void
1eb6e4ca 10685Perl_ptr_table_clear(pTHX_ PTR_TBL_t *const tbl)
a0739874 10686{
d5cefff9 10687 if (tbl && tbl->tbl_items) {
c445ea15 10688 register PTR_TBL_ENT_t * const * const array = tbl->tbl_ary;
d5cefff9 10689 UV riter = tbl->tbl_max;
a0739874 10690
d5cefff9
NC
10691 do {
10692 PTR_TBL_ENT_t *entry = array[riter];
ab1e7f95 10693
d5cefff9 10694 while (entry) {
00b6aa41 10695 PTR_TBL_ENT_t * const oentry = entry;
d5cefff9
NC
10696 entry = entry->next;
10697 del_pte(oentry);
10698 }
10699 } while (riter--);
a0739874 10700
d5cefff9
NC
10701 tbl->tbl_items = 0;
10702 }
a0739874
DM
10703}
10704
645c22ef
DM
10705/* clear and free a ptr table */
10706
a0739874 10707void
1eb6e4ca 10708Perl_ptr_table_free(pTHX_ PTR_TBL_t *const tbl)
a0739874
DM
10709{
10710 if (!tbl) {
10711 return;
10712 }
10713 ptr_table_clear(tbl);
10714 Safefree(tbl->tbl_ary);
10715 Safefree(tbl);
10716}
10717
4674ade5 10718#if defined(USE_ITHREADS)
5bd07a3d 10719
83841fad 10720void
1eb6e4ca 10721Perl_rvpv_dup(pTHX_ SV *const dstr, const SV *const sstr, CLONE_PARAMS *const param)
83841fad 10722{
7918f24d
NC
10723 PERL_ARGS_ASSERT_RVPV_DUP;
10724
83841fad 10725 if (SvROK(sstr)) {
b162af07 10726 SvRV_set(dstr, SvWEAKREF(sstr)
f19a12a3
MHM
10727 ? sv_dup(SvRV_const(sstr), param)
10728 : sv_dup_inc(SvRV_const(sstr), param));
f880fe2f 10729
83841fad 10730 }
3f7c398e 10731 else if (SvPVX_const(sstr)) {
83841fad
NIS
10732 /* Has something there */
10733 if (SvLEN(sstr)) {
68795e93 10734 /* Normal PV - clone whole allocated space */
3f7c398e 10735 SvPV_set(dstr, SAVEPVN(SvPVX_const(sstr), SvLEN(sstr)-1));
d3d0e6f1
NC
10736 if (SvREADONLY(sstr) && SvFAKE(sstr)) {
10737 /* Not that normal - actually sstr is copy on write.
10738 But we are a true, independant SV, so: */
10739 SvREADONLY_off(dstr);
10740 SvFAKE_off(dstr);
10741 }
68795e93 10742 }
83841fad
NIS
10743 else {
10744 /* Special case - not normally malloced for some reason */
f7877b28
NC
10745 if (isGV_with_GP(sstr)) {
10746 /* Don't need to do anything here. */
10747 }
10748 else if ((SvREADONLY(sstr) && SvFAKE(sstr))) {
ef10be65
NC
10749 /* A "shared" PV - clone it as "shared" PV */
10750 SvPV_set(dstr,
10751 HEK_KEY(hek_dup(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)),
10752 param)));
83841fad
NIS
10753 }
10754 else {
10755 /* Some other special case - random pointer */
d2c6dc5e 10756 SvPV_set(dstr, (char *) SvPVX_const(sstr));
d3d0e6f1 10757 }
83841fad
NIS
10758 }
10759 }
10760 else {
4608196e 10761 /* Copy the NULL */
4df7f6af 10762 SvPV_set(dstr, NULL);
83841fad
NIS
10763 }
10764}
10765
538f2e76
NC
10766/* duplicate a list of SVs. source and dest may point to the same memory. */
10767static SV **
10768S_sv_dup_inc_multiple(pTHX_ SV *const *source, SV **dest,
10769 SSize_t items, CLONE_PARAMS *const param)
10770{
10771 PERL_ARGS_ASSERT_SV_DUP_INC_MULTIPLE;
10772
10773 while (items-- > 0) {
10774 *dest++ = sv_dup_inc(*source++, param);
10775 }
10776
10777 return dest;
10778}
10779
662fb8b2
NC
10780/* duplicate an SV of any type (including AV, HV etc) */
10781
1d7c1841 10782SV *
1eb6e4ca 10783Perl_sv_dup(pTHX_ const SV *const sstr, CLONE_PARAMS *const param)
1d7c1841 10784{
27da23d5 10785 dVAR;
1d7c1841
GS
10786 SV *dstr;
10787
7918f24d
NC
10788 PERL_ARGS_ASSERT_SV_DUP;
10789
bfd95973
NC
10790 if (!sstr)
10791 return NULL;
10792 if (SvTYPE(sstr) == SVTYPEMASK) {
10793#ifdef DEBUG_LEAKING_SCALARS_ABORT
10794 abort();
10795#endif
6136c704 10796 return NULL;
bfd95973 10797 }
1d7c1841 10798 /* look for it in the table first */
daba3364 10799 dstr = MUTABLE_SV(ptr_table_fetch(PL_ptr_table, sstr));
1d7c1841
GS
10800 if (dstr)
10801 return dstr;
10802
0405e91e
AB
10803 if(param->flags & CLONEf_JOIN_IN) {
10804 /** We are joining here so we don't want do clone
10805 something that is bad **/
eb86f8b3 10806 if (SvTYPE(sstr) == SVt_PVHV) {
9bde8eb0 10807 const HEK * const hvname = HvNAME_HEK(sstr);
eb86f8b3
AL
10808 if (hvname)
10809 /** don't clone stashes if they already exist **/
daba3364 10810 return MUTABLE_SV(gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname), 0));
0405e91e
AB
10811 }
10812 }
10813
1d7c1841
GS
10814 /* create anew and remember what it is */
10815 new_SV(dstr);
fd0854ff
DM
10816
10817#ifdef DEBUG_LEAKING_SCALARS
10818 dstr->sv_debug_optype = sstr->sv_debug_optype;
10819 dstr->sv_debug_line = sstr->sv_debug_line;
10820 dstr->sv_debug_inpad = sstr->sv_debug_inpad;
10821 dstr->sv_debug_cloned = 1;
fd0854ff 10822 dstr->sv_debug_file = savepv(sstr->sv_debug_file);
fd0854ff
DM
10823#endif
10824
1d7c1841
GS
10825 ptr_table_store(PL_ptr_table, sstr, dstr);
10826
10827 /* clone */
10828 SvFLAGS(dstr) = SvFLAGS(sstr);
10829 SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */
10830 SvREFCNT(dstr) = 0; /* must be before any other dups! */
10831
10832#ifdef DEBUGGING
3f7c398e 10833 if (SvANY(sstr) && PL_watch_pvx && SvPVX_const(sstr) == PL_watch_pvx)
1d7c1841 10834 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
6c9570dc 10835 (void*)PL_watch_pvx, SvPVX_const(sstr));
1d7c1841
GS
10836#endif
10837
9660f481
DM
10838 /* don't clone objects whose class has asked us not to */
10839 if (SvOBJECT(sstr) && ! (SvFLAGS(SvSTASH(sstr)) & SVphv_CLONEABLE)) {
33de8e4a 10840 SvFLAGS(dstr) = 0;
9660f481
DM
10841 return dstr;
10842 }
10843
1d7c1841
GS
10844 switch (SvTYPE(sstr)) {
10845 case SVt_NULL:
10846 SvANY(dstr) = NULL;
10847 break;
10848 case SVt_IV:
339049b0 10849 SvANY(dstr) = (XPVIV*)((char*)&(dstr->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
4df7f6af
NC
10850 if(SvROK(sstr)) {
10851 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10852 } else {
10853 SvIV_set(dstr, SvIVX(sstr));
10854 }
1d7c1841
GS
10855 break;
10856 case SVt_NV:
10857 SvANY(dstr) = new_XNV();
9d6ce603 10858 SvNV_set(dstr, SvNVX(sstr));
1d7c1841 10859 break;
cecf5685 10860 /* case SVt_BIND: */
662fb8b2
NC
10861 default:
10862 {
10863 /* These are all the types that need complex bodies allocating. */
662fb8b2 10864 void *new_body;
2bcc16b3
NC
10865 const svtype sv_type = SvTYPE(sstr);
10866 const struct body_details *const sv_type_details
10867 = bodies_by_type + sv_type;
662fb8b2 10868
93e68bfb 10869 switch (sv_type) {
662fb8b2 10870 default:
bb263b4e 10871 Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(sstr));
662fb8b2
NC
10872 break;
10873
662fb8b2 10874 case SVt_PVGV:
c22188b4
NC
10875 case SVt_PVIO:
10876 case SVt_PVFM:
10877 case SVt_PVHV:
10878 case SVt_PVAV:
662fb8b2 10879 case SVt_PVCV:
662fb8b2 10880 case SVt_PVLV:
5c35adbb 10881 case SVt_REGEXP:
662fb8b2 10882 case SVt_PVMG:
662fb8b2 10883 case SVt_PVNV:
662fb8b2 10884 case SVt_PVIV:
662fb8b2 10885 case SVt_PV:
d2a0f284 10886 assert(sv_type_details->body_size);
c22188b4 10887 if (sv_type_details->arena) {
d2a0f284 10888 new_body_inline(new_body, sv_type);
c22188b4 10889 new_body
b9502f15 10890 = (void*)((char*)new_body - sv_type_details->offset);
c22188b4
NC
10891 } else {
10892 new_body = new_NOARENA(sv_type_details);
10893 }
1d7c1841 10894 }
662fb8b2
NC
10895 assert(new_body);
10896 SvANY(dstr) = new_body;
10897
2bcc16b3 10898#ifndef PURIFY
b9502f15
NC
10899 Copy(((char*)SvANY(sstr)) + sv_type_details->offset,
10900 ((char*)SvANY(dstr)) + sv_type_details->offset,
f32993d6 10901 sv_type_details->copy, char);
2bcc16b3
NC
10902#else
10903 Copy(((char*)SvANY(sstr)),
10904 ((char*)SvANY(dstr)),
d2a0f284 10905 sv_type_details->body_size + sv_type_details->offset, char);
2bcc16b3 10906#endif
662fb8b2 10907
f7877b28
NC
10908 if (sv_type != SVt_PVAV && sv_type != SVt_PVHV
10909 && !isGV_with_GP(dstr))
662fb8b2
NC
10910 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
10911
10912 /* The Copy above means that all the source (unduplicated) pointers
10913 are now in the destination. We can check the flags and the
10914 pointers in either, but it's possible that there's less cache
10915 missing by always going for the destination.
10916 FIXME - instrument and check that assumption */
f32993d6 10917 if (sv_type >= SVt_PVMG) {
885ffcb3 10918 if ((sv_type == SVt_PVMG) && SvPAD_OUR(dstr)) {
73d95100 10919 SvOURSTASH_set(dstr, hv_dup_inc(SvOURSTASH(dstr), param));
e736a858 10920 } else if (SvMAGIC(dstr))
662fb8b2
NC
10921 SvMAGIC_set(dstr, mg_dup(SvMAGIC(dstr), param));
10922 if (SvSTASH(dstr))
10923 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(dstr), param));
1d7c1841 10924 }
662fb8b2 10925
f32993d6
NC
10926 /* The cast silences a GCC warning about unhandled types. */
10927 switch ((int)sv_type) {
662fb8b2
NC
10928 case SVt_PV:
10929 break;
10930 case SVt_PVIV:
10931 break;
10932 case SVt_PVNV:
10933 break;
10934 case SVt_PVMG:
10935 break;
5c35adbb 10936 case SVt_REGEXP:
288b8c02 10937 /* FIXME for plugins */
d2f13c59 10938 re_dup_guts((REGEXP*) sstr, (REGEXP*) dstr, param);
f708cfc1 10939 break;
662fb8b2
NC
10940 case SVt_PVLV:
10941 /* XXX LvTARGOFF sometimes holds PMOP* when DEBUGGING */
10942 if (LvTYPE(dstr) == 't') /* for tie: unrefcnted fake (SV**) */
10943 LvTARG(dstr) = dstr;
10944 else if (LvTYPE(dstr) == 'T') /* for tie: fake HE */
daba3364 10945 LvTARG(dstr) = MUTABLE_SV(he_dup((HE*)LvTARG(dstr), 0, param));
662fb8b2
NC
10946 else
10947 LvTARG(dstr) = sv_dup_inc(LvTARG(dstr), param);
662fb8b2 10948 case SVt_PVGV:
cecf5685 10949 if(isGV_with_GP(sstr)) {
566771cc 10950 GvNAME_HEK(dstr) = hek_dup(GvNAME_HEK(dstr), param);
39cb70dc
NC
10951 /* Don't call sv_add_backref here as it's going to be
10952 created as part of the magic cloning of the symbol
10953 table. */
f7877b28
NC
10954 /* Danger Will Robinson - GvGP(dstr) isn't initialised
10955 at the point of this comment. */
39cb70dc 10956 GvSTASH(dstr) = hv_dup(GvSTASH(dstr), param);
f7877b28
NC
10957 GvGP(dstr) = gp_dup(GvGP(sstr), param);
10958 (void)GpREFCNT_inc(GvGP(dstr));
10959 } else
10960 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
662fb8b2
NC
10961 break;
10962 case SVt_PVIO:
10963 IoIFP(dstr) = fp_dup(IoIFP(dstr), IoTYPE(dstr), param);
10964 if (IoOFP(dstr) == IoIFP(sstr))
10965 IoOFP(dstr) = IoIFP(dstr);
10966 else
10967 IoOFP(dstr) = fp_dup(IoOFP(dstr), IoTYPE(dstr), param);
5486870f 10968 /* PL_parser->rsfp_filters entries have fake IoDIRP() */
662fb8b2
NC
10969 if(IoFLAGS(dstr) & IOf_FAKE_DIRP) {
10970 /* I have no idea why fake dirp (rsfps)
10971 should be treated differently but otherwise
10972 we end up with leaks -- sky*/
10973 IoTOP_GV(dstr) = gv_dup_inc(IoTOP_GV(dstr), param);
10974 IoFMT_GV(dstr) = gv_dup_inc(IoFMT_GV(dstr), param);
10975 IoBOTTOM_GV(dstr) = gv_dup_inc(IoBOTTOM_GV(dstr), param);
10976 } else {
10977 IoTOP_GV(dstr) = gv_dup(IoTOP_GV(dstr), param);
10978 IoFMT_GV(dstr) = gv_dup(IoFMT_GV(dstr), param);
10979 IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(dstr), param);
100ce7e1
NC
10980 if (IoDIRP(dstr)) {
10981 IoDIRP(dstr) = dirp_dup(IoDIRP(dstr));
10982 } else {
6f207bd3 10983 NOOP;
100ce7e1
NC
10984 /* IoDIRP(dstr) is already a copy of IoDIRP(sstr) */
10985 }
662fb8b2
NC
10986 }
10987 IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(dstr));
10988 IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(dstr));
10989 IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(dstr));
10990 break;
10991 case SVt_PVAV:
2779b694
KB
10992 /* avoid cloning an empty array */
10993 if (AvARRAY((const AV *)sstr) && AvFILLp((const AV *)sstr) >= 0) {
662fb8b2 10994 SV **dst_ary, **src_ary;
502c6561 10995 SSize_t items = AvFILLp((const AV *)sstr) + 1;
662fb8b2 10996
502c6561
NC
10997 src_ary = AvARRAY((const AV *)sstr);
10998 Newxz(dst_ary, AvMAX((const AV *)sstr)+1, SV*);
662fb8b2 10999 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
502c6561
NC
11000 AvARRAY(MUTABLE_AV(dstr)) = dst_ary;
11001 AvALLOC((const AV *)dstr) = dst_ary;
11002 if (AvREAL((const AV *)sstr)) {
538f2e76
NC
11003 dst_ary = sv_dup_inc_multiple(src_ary, dst_ary, items,
11004 param);
662fb8b2
NC
11005 }
11006 else {
11007 while (items-- > 0)
11008 *dst_ary++ = sv_dup(*src_ary++, param);
11009 }
502c6561 11010 items = AvMAX((const AV *)sstr) - AvFILLp((const AV *)sstr);
662fb8b2
NC
11011 while (items-- > 0) {
11012 *dst_ary++ = &PL_sv_undef;
11013 }
bfcb3514 11014 }
662fb8b2 11015 else {
502c6561
NC
11016 AvARRAY(MUTABLE_AV(dstr)) = NULL;
11017 AvALLOC((const AV *)dstr) = (SV**)NULL;
2779b694
KB
11018 AvMAX( (const AV *)dstr) = -1;
11019 AvFILLp((const AV *)dstr) = -1;
b79f7545 11020 }
662fb8b2
NC
11021 break;
11022 case SVt_PVHV:
1d193675 11023 if (HvARRAY((const HV *)sstr)) {
7e265ef3
AL
11024 STRLEN i = 0;
11025 const bool sharekeys = !!HvSHAREKEYS(sstr);
11026 XPVHV * const dxhv = (XPVHV*)SvANY(dstr);
11027 XPVHV * const sxhv = (XPVHV*)SvANY(sstr);
11028 char *darray;
11029 Newx(darray, PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1)
11030 + (SvOOK(sstr) ? sizeof(struct xpvhv_aux) : 0),
11031 char);
11032 HvARRAY(dstr) = (HE**)darray;
11033 while (i <= sxhv->xhv_max) {
11034 const HE * const source = HvARRAY(sstr)[i];
11035 HvARRAY(dstr)[i] = source
11036 ? he_dup(source, sharekeys, param) : 0;
11037 ++i;
11038 }
11039 if (SvOOK(sstr)) {
11040 HEK *hvname;
11041 const struct xpvhv_aux * const saux = HvAUX(sstr);
11042 struct xpvhv_aux * const daux = HvAUX(dstr);
11043 /* This flag isn't copied. */
11044 /* SvOOK_on(hv) attacks the IV flags. */
11045 SvFLAGS(dstr) |= SVf_OOK;
11046
11047 hvname = saux->xhv_name;
566771cc 11048 daux->xhv_name = hek_dup(hvname, param);
7e265ef3
AL
11049
11050 daux->xhv_riter = saux->xhv_riter;
11051 daux->xhv_eiter = saux->xhv_eiter
11052 ? he_dup(saux->xhv_eiter,
11053 (bool)!!HvSHAREKEYS(sstr), param) : 0;
b17f5ab7 11054 /* backref array needs refcnt=2; see sv_add_backref */
7e265ef3
AL
11055 daux->xhv_backreferences =
11056 saux->xhv_backreferences
502c6561 11057 ? MUTABLE_AV(SvREFCNT_inc(
daba3364 11058 sv_dup_inc((const SV *)saux->xhv_backreferences, param)))
86f55936 11059 : 0;
e1a479c5
BB
11060
11061 daux->xhv_mro_meta = saux->xhv_mro_meta
11062 ? mro_meta_dup(saux->xhv_mro_meta, param)
11063 : 0;
11064
7e265ef3
AL
11065 /* Record stashes for possible cloning in Perl_clone(). */
11066 if (hvname)
11067 av_push(param->stashes, dstr);
662fb8b2 11068 }
662fb8b2 11069 }
7e265ef3 11070 else
85fbaab2 11071 HvARRAY(MUTABLE_HV(dstr)) = NULL;
662fb8b2 11072 break;
662fb8b2 11073 case SVt_PVCV:
bb172083
NC
11074 if (!(param->flags & CLONEf_COPY_STACKS)) {
11075 CvDEPTH(dstr) = 0;
11076 }
11077 case SVt_PVFM:
662fb8b2
NC
11078 /* NOTE: not refcounted */
11079 CvSTASH(dstr) = hv_dup(CvSTASH(dstr), param);
11080 OP_REFCNT_LOCK;
d04ba589
NC
11081 if (!CvISXSUB(dstr))
11082 CvROOT(dstr) = OpREFCNT_inc(CvROOT(dstr));
662fb8b2 11083 OP_REFCNT_UNLOCK;
cfae286e 11084 if (CvCONST(dstr) && CvISXSUB(dstr)) {
d32faaf3 11085 CvXSUBANY(dstr).any_ptr =
daba3364 11086 sv_dup_inc((const SV *)CvXSUBANY(dstr).any_ptr, param);
662fb8b2
NC
11087 }
11088 /* don't dup if copying back - CvGV isn't refcounted, so the
11089 * duped GV may never be freed. A bit of a hack! DAPM */
11090 CvGV(dstr) = (param->flags & CLONEf_JOIN_IN) ?
a0714e2c 11091 NULL : gv_dup(CvGV(dstr), param) ;
662fb8b2
NC
11092 PAD_DUP(CvPADLIST(dstr), CvPADLIST(sstr), param);
11093 CvOUTSIDE(dstr) =
11094 CvWEAKOUTSIDE(sstr)
11095 ? cv_dup( CvOUTSIDE(dstr), param)
11096 : cv_dup_inc(CvOUTSIDE(dstr), param);
aed2304a 11097 if (!CvISXSUB(dstr))
662fb8b2
NC
11098 CvFILE(dstr) = SAVEPV(CvFILE(dstr));
11099 break;
bfcb3514 11100 }
1d7c1841 11101 }
1d7c1841
GS
11102 }
11103
11104 if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO)
11105 ++PL_sv_objcount;
11106
11107 return dstr;
d2d73c3e 11108 }
1d7c1841 11109
645c22ef
DM
11110/* duplicate a context */
11111
1d7c1841 11112PERL_CONTEXT *
a8fc9800 11113Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
1d7c1841
GS
11114{
11115 PERL_CONTEXT *ncxs;
11116
7918f24d
NC
11117 PERL_ARGS_ASSERT_CX_DUP;
11118
1d7c1841
GS
11119 if (!cxs)
11120 return (PERL_CONTEXT*)NULL;
11121
11122 /* look for it in the table first */
11123 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
11124 if (ncxs)
11125 return ncxs;
11126
11127 /* create anew and remember what it is */
c2d565bf 11128 Newx(ncxs, max + 1, PERL_CONTEXT);
1d7c1841 11129 ptr_table_store(PL_ptr_table, cxs, ncxs);
c2d565bf 11130 Copy(cxs, ncxs, max + 1, PERL_CONTEXT);
1d7c1841
GS
11131
11132 while (ix >= 0) {
c445ea15 11133 PERL_CONTEXT * const ncx = &ncxs[ix];
c2d565bf 11134 if (CxTYPE(ncx) == CXt_SUBST) {
1d7c1841
GS
11135 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
11136 }
11137 else {
c2d565bf 11138 switch (CxTYPE(ncx)) {
1d7c1841 11139 case CXt_SUB:
c2d565bf
NC
11140 ncx->blk_sub.cv = (ncx->blk_sub.olddepth == 0
11141 ? cv_dup_inc(ncx->blk_sub.cv, param)
11142 : cv_dup(ncx->blk_sub.cv,param));
bafb2adc 11143 ncx->blk_sub.argarray = (CxHASARGS(ncx)
c2d565bf
NC
11144 ? av_dup_inc(ncx->blk_sub.argarray,
11145 param)
7d49f689 11146 : NULL);
c2d565bf
NC
11147 ncx->blk_sub.savearray = av_dup_inc(ncx->blk_sub.savearray,
11148 param);
d8d97e70 11149 ncx->blk_sub.oldcomppad = (PAD*)ptr_table_fetch(PL_ptr_table,
c2d565bf 11150 ncx->blk_sub.oldcomppad);
1d7c1841
GS
11151 break;
11152 case CXt_EVAL:
c2d565bf
NC
11153 ncx->blk_eval.old_namesv = sv_dup_inc(ncx->blk_eval.old_namesv,
11154 param);
11155 ncx->blk_eval.cur_text = sv_dup(ncx->blk_eval.cur_text, param);
1d7c1841 11156 break;
d01136d6 11157 case CXt_LOOP_LAZYSV:
d01136d6
BS
11158 ncx->blk_loop.state_u.lazysv.end
11159 = sv_dup_inc(ncx->blk_loop.state_u.lazysv.end, param);
840fe433
NC
11160 /* We are taking advantage of av_dup_inc and sv_dup_inc
11161 actually being the same function, and order equivalance of
11162 the two unions.
11163 We can assert the later [but only at run time :-(] */
11164 assert ((void *) &ncx->blk_loop.state_u.ary.ary ==
11165 (void *) &ncx->blk_loop.state_u.lazysv.cur);
3b719c58 11166 case CXt_LOOP_FOR:
d01136d6
BS
11167 ncx->blk_loop.state_u.ary.ary
11168 = av_dup_inc(ncx->blk_loop.state_u.ary.ary, param);
11169 case CXt_LOOP_LAZYIV:
3b719c58 11170 case CXt_LOOP_PLAIN:
e846cb92
NC
11171 if (CxPADLOOP(ncx)) {
11172 ncx->blk_loop.oldcomppad
11173 = (PAD*)ptr_table_fetch(PL_ptr_table,
11174 ncx->blk_loop.oldcomppad);
11175 } else {
11176 ncx->blk_loop.oldcomppad
159b6efe
NC
11177 = (PAD*)gv_dup((const GV *)ncx->blk_loop.oldcomppad,
11178 param);
e846cb92 11179 }
1d7c1841
GS
11180 break;
11181 case CXt_FORMAT:
f9c764c5
NC
11182 ncx->blk_format.cv = cv_dup(ncx->blk_format.cv, param);
11183 ncx->blk_format.gv = gv_dup(ncx->blk_format.gv, param);
11184 ncx->blk_format.dfoutgv = gv_dup_inc(ncx->blk_format.dfoutgv,
c2d565bf 11185 param);
1d7c1841
GS
11186 break;
11187 case CXt_BLOCK:
11188 case CXt_NULL:
11189 break;
11190 }
11191 }
11192 --ix;
11193 }
11194 return ncxs;
11195}
11196
645c22ef
DM
11197/* duplicate a stack info structure */
11198
1d7c1841 11199PERL_SI *
a8fc9800 11200Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
1d7c1841
GS
11201{
11202 PERL_SI *nsi;
11203
7918f24d
NC
11204 PERL_ARGS_ASSERT_SI_DUP;
11205
1d7c1841
GS
11206 if (!si)
11207 return (PERL_SI*)NULL;
11208
11209 /* look for it in the table first */
11210 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
11211 if (nsi)
11212 return nsi;
11213
11214 /* create anew and remember what it is */
a02a5408 11215 Newxz(nsi, 1, PERL_SI);
1d7c1841
GS
11216 ptr_table_store(PL_ptr_table, si, nsi);
11217
d2d73c3e 11218 nsi->si_stack = av_dup_inc(si->si_stack, param);
1d7c1841
GS
11219 nsi->si_cxix = si->si_cxix;
11220 nsi->si_cxmax = si->si_cxmax;
d2d73c3e 11221 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
1d7c1841 11222 nsi->si_type = si->si_type;
d2d73c3e
AB
11223 nsi->si_prev = si_dup(si->si_prev, param);
11224 nsi->si_next = si_dup(si->si_next, param);
1d7c1841
GS
11225 nsi->si_markoff = si->si_markoff;
11226
11227 return nsi;
11228}
11229
11230#define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
11231#define TOPINT(ss,ix) ((ss)[ix].any_i32)
11232#define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
11233#define TOPLONG(ss,ix) ((ss)[ix].any_long)
11234#define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
11235#define TOPIV(ss,ix) ((ss)[ix].any_iv)
38d8b13e
HS
11236#define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool)
11237#define TOPBOOL(ss,ix) ((ss)[ix].any_bool)
1d7c1841
GS
11238#define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
11239#define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
11240#define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
11241#define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
11242#define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
11243#define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
11244
11245/* XXXXX todo */
11246#define pv_dup_inc(p) SAVEPV(p)
11247#define pv_dup(p) SAVEPV(p)
11248#define svp_dup_inc(p,pp) any_dup(p,pp)
11249
645c22ef
DM
11250/* map any object to the new equivent - either something in the
11251 * ptr table, or something in the interpreter structure
11252 */
11253
1d7c1841 11254void *
53c1dcc0 11255Perl_any_dup(pTHX_ void *v, const PerlInterpreter *proto_perl)
1d7c1841
GS
11256{
11257 void *ret;
11258
7918f24d
NC
11259 PERL_ARGS_ASSERT_ANY_DUP;
11260
1d7c1841
GS
11261 if (!v)
11262 return (void*)NULL;
11263
11264 /* look for it in the table first */
11265 ret = ptr_table_fetch(PL_ptr_table, v);
11266 if (ret)
11267 return ret;
11268
11269 /* see if it is part of the interpreter structure */
11270 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
acfe0abc 11271 ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
05ec9bb3 11272 else {
1d7c1841 11273 ret = v;
05ec9bb3 11274 }
1d7c1841
GS
11275
11276 return ret;
11277}
11278
645c22ef
DM
11279/* duplicate the save stack */
11280
1d7c1841 11281ANY *
a8fc9800 11282Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
1d7c1841 11283{
53d44271 11284 dVAR;
907b3e23
DM
11285 ANY * const ss = proto_perl->Isavestack;
11286 const I32 max = proto_perl->Isavestack_max;
11287 I32 ix = proto_perl->Isavestack_ix;
1d7c1841 11288 ANY *nss;
daba3364 11289 const SV *sv;
1d193675
NC
11290 const GV *gv;
11291 const AV *av;
11292 const HV *hv;
1d7c1841
GS
11293 void* ptr;
11294 int intval;
11295 long longval;
11296 GP *gp;
11297 IV iv;
b24356f5 11298 I32 i;
c4e33207 11299 char *c = NULL;
1d7c1841 11300 void (*dptr) (void*);
acfe0abc 11301 void (*dxptr) (pTHX_ void*);
1d7c1841 11302
7918f24d
NC
11303 PERL_ARGS_ASSERT_SS_DUP;
11304
a02a5408 11305 Newxz(nss, max, ANY);
1d7c1841
GS
11306
11307 while (ix > 0) {
b24356f5
NC
11308 const I32 type = POPINT(ss,ix);
11309 TOPINT(nss,ix) = type;
11310 switch (type) {
3e07292d 11311 case SAVEt_HELEM: /* hash element */
daba3364 11312 sv = (const SV *)POPPTR(ss,ix);
3e07292d
NC
11313 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
11314 /* fall through */
1d7c1841 11315 case SAVEt_ITEM: /* normal string */
a41cc44e 11316 case SAVEt_SV: /* scalar reference */
daba3364 11317 sv = (const SV *)POPPTR(ss,ix);
d2d73c3e 11318 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
3e07292d
NC
11319 /* fall through */
11320 case SAVEt_FREESV:
11321 case SAVEt_MORTALIZESV:
daba3364 11322 sv = (const SV *)POPPTR(ss,ix);
d2d73c3e 11323 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 11324 break;
05ec9bb3
NIS
11325 case SAVEt_SHARED_PVREF: /* char* in shared space */
11326 c = (char*)POPPTR(ss,ix);
11327 TOPPTR(nss,ix) = savesharedpv(c);
11328 ptr = POPPTR(ss,ix);
11329 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11330 break;
1d7c1841
GS
11331 case SAVEt_GENERIC_SVREF: /* generic sv */
11332 case SAVEt_SVREF: /* scalar reference */
daba3364 11333 sv = (const SV *)POPPTR(ss,ix);
d2d73c3e 11334 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
11335 ptr = POPPTR(ss,ix);
11336 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
11337 break;
a41cc44e 11338 case SAVEt_HV: /* hash reference */
1d7c1841 11339 case SAVEt_AV: /* array reference */
daba3364 11340 sv = (const SV *) POPPTR(ss,ix);
337d28f5 11341 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
3e07292d
NC
11342 /* fall through */
11343 case SAVEt_COMPPAD:
11344 case SAVEt_NSTAB:
daba3364 11345 sv = (const SV *) POPPTR(ss,ix);
3e07292d 11346 TOPPTR(nss,ix) = sv_dup(sv, param);
1d7c1841
GS
11347 break;
11348 case SAVEt_INT: /* int reference */
11349 ptr = POPPTR(ss,ix);
11350 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11351 intval = (int)POPINT(ss,ix);
11352 TOPINT(nss,ix) = intval;
11353 break;
11354 case SAVEt_LONG: /* long reference */
11355 ptr = POPPTR(ss,ix);
11356 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
3e07292d
NC
11357 /* fall through */
11358 case SAVEt_CLEARSV:
1d7c1841
GS
11359 longval = (long)POPLONG(ss,ix);
11360 TOPLONG(nss,ix) = longval;
11361 break;
11362 case SAVEt_I32: /* I32 reference */
11363 case SAVEt_I16: /* I16 reference */
11364 case SAVEt_I8: /* I8 reference */
88effcc9 11365 case SAVEt_COP_ARYBASE: /* call CopARYBASE_set */
1d7c1841
GS
11366 ptr = POPPTR(ss,ix);
11367 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
1ccabee8 11368 i = POPINT(ss,ix);
1d7c1841
GS
11369 TOPINT(nss,ix) = i;
11370 break;
11371 case SAVEt_IV: /* IV reference */
11372 ptr = POPPTR(ss,ix);
11373 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11374 iv = POPIV(ss,ix);
11375 TOPIV(nss,ix) = iv;
11376 break;
a41cc44e
NC
11377 case SAVEt_HPTR: /* HV* reference */
11378 case SAVEt_APTR: /* AV* reference */
1d7c1841
GS
11379 case SAVEt_SPTR: /* SV* reference */
11380 ptr = POPPTR(ss,ix);
11381 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
daba3364 11382 sv = (const SV *)POPPTR(ss,ix);
d2d73c3e 11383 TOPPTR(nss,ix) = sv_dup(sv, param);
1d7c1841
GS
11384 break;
11385 case SAVEt_VPTR: /* random* reference */
11386 ptr = POPPTR(ss,ix);
11387 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11388 ptr = POPPTR(ss,ix);
11389 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11390 break;
b03d03b0 11391 case SAVEt_GENERIC_PVREF: /* generic char* */
1d7c1841
GS
11392 case SAVEt_PPTR: /* char* reference */
11393 ptr = POPPTR(ss,ix);
11394 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11395 c = (char*)POPPTR(ss,ix);
11396 TOPPTR(nss,ix) = pv_dup(c);
11397 break;
1d7c1841
GS
11398 case SAVEt_GP: /* scalar reference */
11399 gp = (GP*)POPPTR(ss,ix);
d2d73c3e 11400 TOPPTR(nss,ix) = gp = gp_dup(gp, param);
1d7c1841 11401 (void)GpREFCNT_inc(gp);
159b6efe 11402 gv = (const GV *)POPPTR(ss,ix);
2ed3c8fc 11403 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
1d7c1841 11404 break;
1d7c1841
GS
11405 case SAVEt_FREEOP:
11406 ptr = POPPTR(ss,ix);
11407 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
11408 /* these are assumed to be refcounted properly */
53c1dcc0 11409 OP *o;
1d7c1841
GS
11410 switch (((OP*)ptr)->op_type) {
11411 case OP_LEAVESUB:
11412 case OP_LEAVESUBLV:
11413 case OP_LEAVEEVAL:
11414 case OP_LEAVE:
11415 case OP_SCOPE:
11416 case OP_LEAVEWRITE:
e977893f
GS
11417 TOPPTR(nss,ix) = ptr;
11418 o = (OP*)ptr;
d3c72c2a 11419 OP_REFCNT_LOCK;
594cd643 11420 (void) OpREFCNT_inc(o);
d3c72c2a 11421 OP_REFCNT_UNLOCK;
1d7c1841
GS
11422 break;
11423 default:
5f66b61c 11424 TOPPTR(nss,ix) = NULL;
1d7c1841
GS
11425 break;
11426 }
11427 }
11428 else
5f66b61c 11429 TOPPTR(nss,ix) = NULL;
1d7c1841 11430 break;
1d7c1841 11431 case SAVEt_DELETE:
1d193675 11432 hv = (const HV *)POPPTR(ss,ix);
d2d73c3e 11433 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
35d4f826
NC
11434 i = POPINT(ss,ix);
11435 TOPINT(nss,ix) = i;
8e41545f
NC
11436 /* Fall through */
11437 case SAVEt_FREEPV:
1d7c1841
GS
11438 c = (char*)POPPTR(ss,ix);
11439 TOPPTR(nss,ix) = pv_dup_inc(c);
35d4f826 11440 break;
3e07292d 11441 case SAVEt_STACK_POS: /* Position on Perl stack */
1d7c1841
GS
11442 i = POPINT(ss,ix);
11443 TOPINT(nss,ix) = i;
11444 break;
11445 case SAVEt_DESTRUCTOR:
11446 ptr = POPPTR(ss,ix);
11447 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
11448 dptr = POPDPTR(ss,ix);
8141890a
JH
11449 TOPDPTR(nss,ix) = DPTR2FPTR(void (*)(void*),
11450 any_dup(FPTR2DPTR(void *, dptr),
11451 proto_perl));
1d7c1841
GS
11452 break;
11453 case SAVEt_DESTRUCTOR_X:
11454 ptr = POPPTR(ss,ix);
11455 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
11456 dxptr = POPDXPTR(ss,ix);
8141890a
JH
11457 TOPDXPTR(nss,ix) = DPTR2FPTR(void (*)(pTHX_ void*),
11458 any_dup(FPTR2DPTR(void *, dxptr),
11459 proto_perl));
1d7c1841
GS
11460 break;
11461 case SAVEt_REGCONTEXT:
11462 case SAVEt_ALLOC:
11463 i = POPINT(ss,ix);
11464 TOPINT(nss,ix) = i;
11465 ix -= i;
11466 break;
1d7c1841 11467 case SAVEt_AELEM: /* array element */
daba3364 11468 sv = (const SV *)POPPTR(ss,ix);
d2d73c3e 11469 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
11470 i = POPINT(ss,ix);
11471 TOPINT(nss,ix) = i;
502c6561 11472 av = (const AV *)POPPTR(ss,ix);
d2d73c3e 11473 TOPPTR(nss,ix) = av_dup_inc(av, param);
1d7c1841 11474 break;
1d7c1841
GS
11475 case SAVEt_OP:
11476 ptr = POPPTR(ss,ix);
11477 TOPPTR(nss,ix) = ptr;
11478 break;
11479 case SAVEt_HINTS:
b3ca2e83 11480 ptr = POPPTR(ss,ix);
080ac856 11481 if (ptr) {
7b6dd8c3 11482 HINTS_REFCNT_LOCK;
080ac856 11483 ((struct refcounted_he *)ptr)->refcounted_he_refcnt++;
7b6dd8c3
NC
11484 HINTS_REFCNT_UNLOCK;
11485 }
cbb1fbea 11486 TOPPTR(nss,ix) = ptr;
601cee3b
NC
11487 i = POPINT(ss,ix);
11488 TOPINT(nss,ix) = i;
a8f8b6a7 11489 if (i & HINT_LOCALIZE_HH) {
1d193675 11490 hv = (const HV *)POPPTR(ss,ix);
a8f8b6a7
NC
11491 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
11492 }
1d7c1841 11493 break;
09edbca0 11494 case SAVEt_PADSV_AND_MORTALIZE:
c3564e5c
GS
11495 longval = (long)POPLONG(ss,ix);
11496 TOPLONG(nss,ix) = longval;
11497 ptr = POPPTR(ss,ix);
11498 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
daba3364 11499 sv = (const SV *)POPPTR(ss,ix);
09edbca0 11500 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
c3564e5c 11501 break;
a1bb4754 11502 case SAVEt_BOOL:
38d8b13e 11503 ptr = POPPTR(ss,ix);
b9609c01 11504 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
38d8b13e 11505 longval = (long)POPBOOL(ss,ix);
b9609c01 11506 TOPBOOL(nss,ix) = (bool)longval;
a1bb4754 11507 break;
8bd2680e
MHM
11508 case SAVEt_SET_SVFLAGS:
11509 i = POPINT(ss,ix);
11510 TOPINT(nss,ix) = i;
11511 i = POPINT(ss,ix);
11512 TOPINT(nss,ix) = i;
daba3364 11513 sv = (const SV *)POPPTR(ss,ix);
8bd2680e
MHM
11514 TOPPTR(nss,ix) = sv_dup(sv, param);
11515 break;
5bfb7d0e
NC
11516 case SAVEt_RE_STATE:
11517 {
11518 const struct re_save_state *const old_state
11519 = (struct re_save_state *)
11520 (ss + ix - SAVESTACK_ALLOC_FOR_RE_SAVE_STATE);
11521 struct re_save_state *const new_state
11522 = (struct re_save_state *)
11523 (nss + ix - SAVESTACK_ALLOC_FOR_RE_SAVE_STATE);
11524
11525 Copy(old_state, new_state, 1, struct re_save_state);
11526 ix -= SAVESTACK_ALLOC_FOR_RE_SAVE_STATE;
11527
11528 new_state->re_state_bostr
11529 = pv_dup(old_state->re_state_bostr);
11530 new_state->re_state_reginput
11531 = pv_dup(old_state->re_state_reginput);
5bfb7d0e
NC
11532 new_state->re_state_regeol
11533 = pv_dup(old_state->re_state_regeol);
f0ab9afb
NC
11534 new_state->re_state_regoffs
11535 = (regexp_paren_pair*)
11536 any_dup(old_state->re_state_regoffs, proto_perl);
5bfb7d0e 11537 new_state->re_state_reglastparen
11b79775
DD
11538 = (U32*) any_dup(old_state->re_state_reglastparen,
11539 proto_perl);
5bfb7d0e 11540 new_state->re_state_reglastcloseparen
11b79775 11541 = (U32*)any_dup(old_state->re_state_reglastcloseparen,
5bfb7d0e 11542 proto_perl);
5bfb7d0e
NC
11543 /* XXX This just has to be broken. The old save_re_context
11544 code did SAVEGENERICPV(PL_reg_start_tmp);
11545 PL_reg_start_tmp is char **.
11546 Look above to what the dup code does for
11547 SAVEt_GENERIC_PVREF
11548 It can never have worked.
11549 So this is merely a faithful copy of the exiting bug: */
11550 new_state->re_state_reg_start_tmp
11551 = (char **) pv_dup((char *)
11552 old_state->re_state_reg_start_tmp);
11553 /* I assume that it only ever "worked" because no-one called
11554 (pseudo)fork while the regexp engine had re-entered itself.
11555 */
5bfb7d0e
NC
11556#ifdef PERL_OLD_COPY_ON_WRITE
11557 new_state->re_state_nrs
11558 = sv_dup(old_state->re_state_nrs, param);
11559#endif
11560 new_state->re_state_reg_magic
11b79775
DD
11561 = (MAGIC*) any_dup(old_state->re_state_reg_magic,
11562 proto_perl);
5bfb7d0e 11563 new_state->re_state_reg_oldcurpm
11b79775
DD
11564 = (PMOP*) any_dup(old_state->re_state_reg_oldcurpm,
11565 proto_perl);
5bfb7d0e 11566 new_state->re_state_reg_curpm
11b79775
DD
11567 = (PMOP*) any_dup(old_state->re_state_reg_curpm,
11568 proto_perl);
5bfb7d0e
NC
11569 new_state->re_state_reg_oldsaved
11570 = pv_dup(old_state->re_state_reg_oldsaved);
11571 new_state->re_state_reg_poscache
11572 = pv_dup(old_state->re_state_reg_poscache);
5bfb7d0e
NC
11573 new_state->re_state_reg_starttry
11574 = pv_dup(old_state->re_state_reg_starttry);
5bfb7d0e
NC
11575 break;
11576 }
68da3b2f
NC
11577 case SAVEt_COMPILE_WARNINGS:
11578 ptr = POPPTR(ss,ix);
11579 TOPPTR(nss,ix) = DUP_WARNINGS((STRLEN*)ptr);
7b6dd8c3 11580 break;
7c197c94
DM
11581 case SAVEt_PARSER:
11582 ptr = POPPTR(ss,ix);
456084a8 11583 TOPPTR(nss,ix) = parser_dup((const yy_parser*)ptr, param);
7c197c94 11584 break;
1d7c1841 11585 default:
147bc374
NC
11586 Perl_croak(aTHX_
11587 "panic: ss_dup inconsistency (%"IVdf")", (IV) type);
1d7c1841
GS
11588 }
11589 }
11590
bd81e77b
NC
11591 return nss;
11592}
11593
11594
11595/* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
11596 * flag to the result. This is done for each stash before cloning starts,
11597 * so we know which stashes want their objects cloned */
11598
11599static void
f30de749 11600do_mark_cloneable_stash(pTHX_ SV *const sv)
bd81e77b 11601{
1d193675 11602 const HEK * const hvname = HvNAME_HEK((const HV *)sv);
bd81e77b 11603 if (hvname) {
85fbaab2 11604 GV* const cloner = gv_fetchmethod_autoload(MUTABLE_HV(sv), "CLONE_SKIP", 0);
bd81e77b
NC
11605 SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
11606 if (cloner && GvCV(cloner)) {
11607 dSP;
11608 UV status;
11609
11610 ENTER;
11611 SAVETMPS;
11612 PUSHMARK(SP);
6e449a3a 11613 mXPUSHs(newSVhek(hvname));
bd81e77b 11614 PUTBACK;
daba3364 11615 call_sv(MUTABLE_SV(GvCV(cloner)), G_SCALAR);
bd81e77b
NC
11616 SPAGAIN;
11617 status = POPu;
11618 PUTBACK;
11619 FREETMPS;
11620 LEAVE;
11621 if (status)
11622 SvFLAGS(sv) &= ~SVphv_CLONEABLE;
11623 }
11624 }
11625}
11626
11627
11628
11629/*
11630=for apidoc perl_clone
11631
11632Create and return a new interpreter by cloning the current one.
11633
11634perl_clone takes these flags as parameters:
11635
11636CLONEf_COPY_STACKS - is used to, well, copy the stacks also,
11637without it we only clone the data and zero the stacks,
11638with it we copy the stacks and the new perl interpreter is
11639ready to run at the exact same point as the previous one.
11640The pseudo-fork code uses COPY_STACKS while the
878090d5 11641threads->create doesn't.
bd81e77b
NC
11642
11643CLONEf_KEEP_PTR_TABLE
11644perl_clone keeps a ptr_table with the pointer of the old
11645variable as a key and the new variable as a value,
11646this allows it to check if something has been cloned and not
11647clone it again but rather just use the value and increase the
11648refcount. If KEEP_PTR_TABLE is not set then perl_clone will kill
11649the ptr_table using the function
11650C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>,
11651reason to keep it around is if you want to dup some of your own
11652variable who are outside the graph perl scans, example of this
11653code is in threads.xs create
11654
11655CLONEf_CLONE_HOST
11656This is a win32 thing, it is ignored on unix, it tells perls
11657win32host code (which is c++) to clone itself, this is needed on
11658win32 if you want to run two threads at the same time,
11659if you just want to do some stuff in a separate perl interpreter
11660and then throw it away and return to the original one,
11661you don't need to do anything.
11662
11663=cut
11664*/
11665
11666/* XXX the above needs expanding by someone who actually understands it ! */
11667EXTERN_C PerlInterpreter *
11668perl_clone_host(PerlInterpreter* proto_perl, UV flags);
11669
11670PerlInterpreter *
11671perl_clone(PerlInterpreter *proto_perl, UV flags)
11672{
11673 dVAR;
11674#ifdef PERL_IMPLICIT_SYS
11675
7918f24d
NC
11676 PERL_ARGS_ASSERT_PERL_CLONE;
11677
bd81e77b
NC
11678 /* perlhost.h so we need to call into it
11679 to clone the host, CPerlHost should have a c interface, sky */
11680
11681 if (flags & CLONEf_CLONE_HOST) {
11682 return perl_clone_host(proto_perl,flags);
11683 }
11684 return perl_clone_using(proto_perl, flags,
11685 proto_perl->IMem,
11686 proto_perl->IMemShared,
11687 proto_perl->IMemParse,
11688 proto_perl->IEnv,
11689 proto_perl->IStdIO,
11690 proto_perl->ILIO,
11691 proto_perl->IDir,
11692 proto_perl->ISock,
11693 proto_perl->IProc);
11694}
11695
11696PerlInterpreter *
11697perl_clone_using(PerlInterpreter *proto_perl, UV flags,
11698 struct IPerlMem* ipM, struct IPerlMem* ipMS,
11699 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
11700 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
11701 struct IPerlDir* ipD, struct IPerlSock* ipS,
11702 struct IPerlProc* ipP)
11703{
11704 /* XXX many of the string copies here can be optimized if they're
11705 * constants; they need to be allocated as common memory and just
11706 * their pointers copied. */
11707
11708 IV i;
11709 CLONE_PARAMS clone_params;
5f66b61c 11710 CLONE_PARAMS* const param = &clone_params;
bd81e77b 11711
5f66b61c 11712 PerlInterpreter * const my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
7918f24d
NC
11713
11714 PERL_ARGS_ASSERT_PERL_CLONE_USING;
11715
bd81e77b
NC
11716 /* for each stash, determine whether its objects should be cloned */
11717 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
11718 PERL_SET_THX(my_perl);
11719
11720# ifdef DEBUGGING
7e337ee0 11721 PoisonNew(my_perl, 1, PerlInterpreter);
5f66b61c
AL
11722 PL_op = NULL;
11723 PL_curcop = NULL;
bd81e77b
NC
11724 PL_markstack = 0;
11725 PL_scopestack = 0;
11726 PL_savestack = 0;
11727 PL_savestack_ix = 0;
11728 PL_savestack_max = -1;
11729 PL_sig_pending = 0;
b8328dae 11730 PL_parser = NULL;
bd81e77b
NC
11731 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
11732# else /* !DEBUGGING */
11733 Zero(my_perl, 1, PerlInterpreter);
11734# endif /* DEBUGGING */
11735
11736 /* host pointers */
11737 PL_Mem = ipM;
11738 PL_MemShared = ipMS;
11739 PL_MemParse = ipMP;
11740 PL_Env = ipE;
11741 PL_StdIO = ipStd;
11742 PL_LIO = ipLIO;
11743 PL_Dir = ipD;
11744 PL_Sock = ipS;
11745 PL_Proc = ipP;
11746#else /* !PERL_IMPLICIT_SYS */
11747 IV i;
11748 CLONE_PARAMS clone_params;
11749 CLONE_PARAMS* param = &clone_params;
5f66b61c 11750 PerlInterpreter * const my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
7918f24d
NC
11751
11752 PERL_ARGS_ASSERT_PERL_CLONE;
11753
bd81e77b
NC
11754 /* for each stash, determine whether its objects should be cloned */
11755 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
11756 PERL_SET_THX(my_perl);
11757
11758# ifdef DEBUGGING
7e337ee0 11759 PoisonNew(my_perl, 1, PerlInterpreter);
5f66b61c
AL
11760 PL_op = NULL;
11761 PL_curcop = NULL;
bd81e77b
NC
11762 PL_markstack = 0;
11763 PL_scopestack = 0;
11764 PL_savestack = 0;
11765 PL_savestack_ix = 0;
11766 PL_savestack_max = -1;
11767 PL_sig_pending = 0;
b8328dae 11768 PL_parser = NULL;
bd81e77b
NC
11769 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
11770# else /* !DEBUGGING */
11771 Zero(my_perl, 1, PerlInterpreter);
11772# endif /* DEBUGGING */
11773#endif /* PERL_IMPLICIT_SYS */
11774 param->flags = flags;
11775 param->proto_perl = proto_perl;
11776
7cb608b5
NC
11777 INIT_TRACK_MEMPOOL(my_perl->Imemory_debug_header, my_perl);
11778
fdda85ca 11779 PL_body_arenas = NULL;
bd81e77b
NC
11780 Zero(&PL_body_roots, 1, PL_body_roots);
11781
11782 PL_nice_chunk = NULL;
11783 PL_nice_chunk_size = 0;
11784 PL_sv_count = 0;
11785 PL_sv_objcount = 0;
a0714e2c
SS
11786 PL_sv_root = NULL;
11787 PL_sv_arenaroot = NULL;
bd81e77b
NC
11788
11789 PL_debug = proto_perl->Idebug;
11790
11791 PL_hash_seed = proto_perl->Ihash_seed;
11792 PL_rehash_seed = proto_perl->Irehash_seed;
11793
11794#ifdef USE_REENTRANT_API
11795 /* XXX: things like -Dm will segfault here in perlio, but doing
11796 * PERL_SET_CONTEXT(proto_perl);
11797 * breaks too many other things
11798 */
11799 Perl_reentrant_init(aTHX);
11800#endif
11801
11802 /* create SV map for pointer relocation */
11803 PL_ptr_table = ptr_table_new();
11804
11805 /* initialize these special pointers as early as possible */
11806 SvANY(&PL_sv_undef) = NULL;
11807 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
11808 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL;
11809 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
11810
11811 SvANY(&PL_sv_no) = new_XPVNV();
11812 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
11813 SvFLAGS(&PL_sv_no) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
11814 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
bb7a0f54 11815 SvPV_set(&PL_sv_no, savepvn(PL_No, 0));
bd81e77b
NC
11816 SvCUR_set(&PL_sv_no, 0);
11817 SvLEN_set(&PL_sv_no, 1);
11818 SvIV_set(&PL_sv_no, 0);
11819 SvNV_set(&PL_sv_no, 0);
11820 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
11821
11822 SvANY(&PL_sv_yes) = new_XPVNV();
11823 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
11824 SvFLAGS(&PL_sv_yes) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
11825 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
bb7a0f54 11826 SvPV_set(&PL_sv_yes, savepvn(PL_Yes, 1));
bd81e77b
NC
11827 SvCUR_set(&PL_sv_yes, 1);
11828 SvLEN_set(&PL_sv_yes, 2);
11829 SvIV_set(&PL_sv_yes, 1);
11830 SvNV_set(&PL_sv_yes, 1);
11831 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
11832
11833 /* create (a non-shared!) shared string table */
11834 PL_strtab = newHV();
11835 HvSHAREKEYS_off(PL_strtab);
11836 hv_ksplit(PL_strtab, HvTOTALKEYS(proto_perl->Istrtab));
11837 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
11838
11839 PL_compiling = proto_perl->Icompiling;
11840
11841 /* These two PVs will be free'd special way so must set them same way op.c does */
11842 PL_compiling.cop_stashpv = savesharedpv(PL_compiling.cop_stashpv);
11843 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_stashpv, PL_compiling.cop_stashpv);
11844
11845 PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file);
11846 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file);
11847
11848 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
72dc9ed5 11849 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
c28fe1ec 11850 if (PL_compiling.cop_hints_hash) {
cbb1fbea 11851 HINTS_REFCNT_LOCK;
c28fe1ec 11852 PL_compiling.cop_hints_hash->refcounted_he_refcnt++;
cbb1fbea
NC
11853 HINTS_REFCNT_UNLOCK;
11854 }
907b3e23 11855 PL_curcop = (COP*)any_dup(proto_perl->Icurcop, proto_perl);
5892a4d4
NC
11856#ifdef PERL_DEBUG_READONLY_OPS
11857 PL_slabs = NULL;
11858 PL_slab_count = 0;
11859#endif
bd81e77b
NC
11860
11861 /* pseudo environmental stuff */
11862 PL_origargc = proto_perl->Iorigargc;
11863 PL_origargv = proto_perl->Iorigargv;
11864
11865 param->stashes = newAV(); /* Setup array of objects to call clone on */
11866
11867 /* Set tainting stuff before PerlIO_debug can possibly get called */
11868 PL_tainting = proto_perl->Itainting;
11869 PL_taint_warn = proto_perl->Itaint_warn;
11870
11871#ifdef PERLIO_LAYERS
11872 /* Clone PerlIO tables as soon as we can handle general xx_dup() */
11873 PerlIO_clone(aTHX_ proto_perl, param);
11874#endif
11875
11876 PL_envgv = gv_dup(proto_perl->Ienvgv, param);
11877 PL_incgv = gv_dup(proto_perl->Iincgv, param);
11878 PL_hintgv = gv_dup(proto_perl->Ihintgv, param);
11879 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
11880 PL_diehook = sv_dup_inc(proto_perl->Idiehook, param);
11881 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param);
11882
11883 /* switches */
11884 PL_minus_c = proto_perl->Iminus_c;
11885 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param);
11886 PL_localpatches = proto_perl->Ilocalpatches;
11887 PL_splitstr = proto_perl->Isplitstr;
bd81e77b
NC
11888 PL_minus_n = proto_perl->Iminus_n;
11889 PL_minus_p = proto_perl->Iminus_p;
11890 PL_minus_l = proto_perl->Iminus_l;
11891 PL_minus_a = proto_perl->Iminus_a;
bc9b29db 11892 PL_minus_E = proto_perl->Iminus_E;
bd81e77b
NC
11893 PL_minus_F = proto_perl->Iminus_F;
11894 PL_doswitches = proto_perl->Idoswitches;
11895 PL_dowarn = proto_perl->Idowarn;
11896 PL_doextract = proto_perl->Idoextract;
11897 PL_sawampersand = proto_perl->Isawampersand;
11898 PL_unsafe = proto_perl->Iunsafe;
11899 PL_inplace = SAVEPV(proto_perl->Iinplace);
11900 PL_e_script = sv_dup_inc(proto_perl->Ie_script, param);
11901 PL_perldb = proto_perl->Iperldb;
11902 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
11903 PL_exit_flags = proto_perl->Iexit_flags;
11904
11905 /* magical thingies */
11906 /* XXX time(&PL_basetime) when asked for? */
11907 PL_basetime = proto_perl->Ibasetime;
11908 PL_formfeed = sv_dup(proto_perl->Iformfeed, param);
11909
11910 PL_maxsysfd = proto_perl->Imaxsysfd;
bd81e77b
NC
11911 PL_statusvalue = proto_perl->Istatusvalue;
11912#ifdef VMS
11913 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
11914#else
11915 PL_statusvalue_posix = proto_perl->Istatusvalue_posix;
11916#endif
11917 PL_encoding = sv_dup(proto_perl->Iencoding, param);
11918
76f68e9b
MHM
11919 sv_setpvs(PERL_DEBUG_PAD(0), ""); /* For regex debugging. */
11920 sv_setpvs(PERL_DEBUG_PAD(1), ""); /* ext/re needs these */
11921 sv_setpvs(PERL_DEBUG_PAD(2), ""); /* even without DEBUGGING. */
bd81e77b 11922
84da74a7 11923
f9f4320a 11924 /* RE engine related */
84da74a7
YO
11925 Zero(&PL_reg_state, 1, struct re_save_state);
11926 PL_reginterp_cnt = 0;
11927 PL_regmatch_slab = NULL;
11928
bd81e77b 11929 /* Clone the regex array */
937c6efd
NC
11930 /* ORANGE FIXME for plugins, probably in the SV dup code.
11931 newSViv(PTR2IV(CALLREGDUPE(
11932 INT2PTR(REGEXP *, SvIVX(regex)), param))))
11933 */
11934 PL_regex_padav = av_dup_inc(proto_perl->Iregex_padav, param);
bd81e77b
NC
11935 PL_regex_pad = AvARRAY(PL_regex_padav);
11936
11937 /* shortcuts to various I/O objects */
e23d9e2f 11938 PL_ofsgv = gv_dup(proto_perl->Iofsgv, param);
bd81e77b
NC
11939 PL_stdingv = gv_dup(proto_perl->Istdingv, param);
11940 PL_stderrgv = gv_dup(proto_perl->Istderrgv, param);
11941 PL_defgv = gv_dup(proto_perl->Idefgv, param);
11942 PL_argvgv = gv_dup(proto_perl->Iargvgv, param);
11943 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param);
11944 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param);
1d7c1841 11945
bd81e77b
NC
11946 /* shortcuts to regexp stuff */
11947 PL_replgv = gv_dup(proto_perl->Ireplgv, param);
9660f481 11948
bd81e77b
NC
11949 /* shortcuts to misc objects */
11950 PL_errgv = gv_dup(proto_perl->Ierrgv, param);
9660f481 11951
bd81e77b
NC
11952 /* shortcuts to debugging objects */
11953 PL_DBgv = gv_dup(proto_perl->IDBgv, param);
11954 PL_DBline = gv_dup(proto_perl->IDBline, param);
11955 PL_DBsub = gv_dup(proto_perl->IDBsub, param);
11956 PL_DBsingle = sv_dup(proto_perl->IDBsingle, param);
11957 PL_DBtrace = sv_dup(proto_perl->IDBtrace, param);
11958 PL_DBsignal = sv_dup(proto_perl->IDBsignal, param);
bd81e77b 11959 PL_dbargs = av_dup(proto_perl->Idbargs, param);
9660f481 11960
bd81e77b 11961 /* symbol tables */
907b3e23
DM
11962 PL_defstash = hv_dup_inc(proto_perl->Idefstash, param);
11963 PL_curstash = hv_dup(proto_perl->Icurstash, param);
bd81e77b
NC
11964 PL_debstash = hv_dup(proto_perl->Idebstash, param);
11965 PL_globalstash = hv_dup(proto_perl->Iglobalstash, param);
11966 PL_curstname = sv_dup_inc(proto_perl->Icurstname, param);
11967
11968 PL_beginav = av_dup_inc(proto_perl->Ibeginav, param);
11969 PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param);
11970 PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param);
3c10abe3
AG
11971 PL_unitcheckav = av_dup_inc(proto_perl->Iunitcheckav, param);
11972 PL_unitcheckav_save = av_dup_inc(proto_perl->Iunitcheckav_save, param);
bd81e77b
NC
11973 PL_endav = av_dup_inc(proto_perl->Iendav, param);
11974 PL_checkav = av_dup_inc(proto_perl->Icheckav, param);
11975 PL_initav = av_dup_inc(proto_perl->Iinitav, param);
11976
11977 PL_sub_generation = proto_perl->Isub_generation;
dd69841b 11978 PL_isarev = hv_dup_inc(proto_perl->Iisarev, param);
bd81e77b
NC
11979
11980 /* funky return mechanisms */
11981 PL_forkprocess = proto_perl->Iforkprocess;
11982
11983 /* subprocess state */
11984 PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param);
11985
11986 /* internal state */
11987 PL_maxo = proto_perl->Imaxo;
11988 if (proto_perl->Iop_mask)
11989 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
11990 else
bd61b366 11991 PL_op_mask = NULL;
bd81e77b
NC
11992 /* PL_asserting = proto_perl->Iasserting; */
11993
11994 /* current interpreter roots */
11995 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param);
d3c72c2a 11996 OP_REFCNT_LOCK;
bd81e77b 11997 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
d3c72c2a 11998 OP_REFCNT_UNLOCK;
bd81e77b
NC
11999 PL_main_start = proto_perl->Imain_start;
12000 PL_eval_root = proto_perl->Ieval_root;
12001 PL_eval_start = proto_perl->Ieval_start;
12002
12003 /* runtime control stuff */
12004 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
bd81e77b
NC
12005
12006 PL_filemode = proto_perl->Ifilemode;
12007 PL_lastfd = proto_perl->Ilastfd;
12008 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
12009 PL_Argv = NULL;
bd61b366 12010 PL_Cmd = NULL;
bd81e77b 12011 PL_gensym = proto_perl->Igensym;
bd81e77b
NC
12012 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param);
12013 PL_laststatval = proto_perl->Ilaststatval;
12014 PL_laststype = proto_perl->Ilaststype;
a0714e2c 12015 PL_mess_sv = NULL;
bd81e77b
NC
12016
12017 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param);
12018
12019 /* interpreter atexit processing */
12020 PL_exitlistlen = proto_perl->Iexitlistlen;
12021 if (PL_exitlistlen) {
12022 Newx(PL_exitlist, PL_exitlistlen, PerlExitListEntry);
12023 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
9660f481 12024 }
bd81e77b
NC
12025 else
12026 PL_exitlist = (PerlExitListEntry*)NULL;
f16dd614
DM
12027
12028 PL_my_cxt_size = proto_perl->Imy_cxt_size;
4c901e72 12029 if (PL_my_cxt_size) {
f16dd614
DM
12030 Newx(PL_my_cxt_list, PL_my_cxt_size, void *);
12031 Copy(proto_perl->Imy_cxt_list, PL_my_cxt_list, PL_my_cxt_size, void *);
53d44271 12032#ifdef PERL_GLOBAL_STRUCT_PRIVATE
bae1192d 12033 Newx(PL_my_cxt_keys, PL_my_cxt_size, const char *);
53d44271
JH
12034 Copy(proto_perl->Imy_cxt_keys, PL_my_cxt_keys, PL_my_cxt_size, char *);
12035#endif
f16dd614 12036 }
53d44271 12037 else {
f16dd614 12038 PL_my_cxt_list = (void**)NULL;
53d44271 12039#ifdef PERL_GLOBAL_STRUCT_PRIVATE
bae1192d 12040 PL_my_cxt_keys = (const char**)NULL;
53d44271
JH
12041#endif
12042 }
bd81e77b
NC
12043 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param);
12044 PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param);
12045 PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param);
12046
12047 PL_profiledata = NULL;
9660f481 12048
bd81e77b 12049 PL_compcv = cv_dup(proto_perl->Icompcv, param);
9660f481 12050
bd81e77b 12051 PAD_CLONE_VARS(proto_perl, param);
9660f481 12052
bd81e77b
NC
12053#ifdef HAVE_INTERP_INTERN
12054 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
12055#endif
645c22ef 12056
bd81e77b
NC
12057 /* more statics moved here */
12058 PL_generation = proto_perl->Igeneration;
12059 PL_DBcv = cv_dup(proto_perl->IDBcv, param);
645c22ef 12060
bd81e77b
NC
12061 PL_in_clean_objs = proto_perl->Iin_clean_objs;
12062 PL_in_clean_all = proto_perl->Iin_clean_all;
6a78b4db 12063
bd81e77b
NC
12064 PL_uid = proto_perl->Iuid;
12065 PL_euid = proto_perl->Ieuid;
12066 PL_gid = proto_perl->Igid;
12067 PL_egid = proto_perl->Iegid;
12068 PL_nomemok = proto_perl->Inomemok;
12069 PL_an = proto_perl->Ian;
12070 PL_evalseq = proto_perl->Ievalseq;
12071 PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */
12072 PL_origalen = proto_perl->Iorigalen;
12073#ifdef PERL_USES_PL_PIDSTATUS
12074 PL_pidstatus = newHV(); /* XXX flag for cloning? */
12075#endif
12076 PL_osname = SAVEPV(proto_perl->Iosname);
12077 PL_sighandlerp = proto_perl->Isighandlerp;
6a78b4db 12078
bd81e77b 12079 PL_runops = proto_perl->Irunops;
6a78b4db 12080
199e78b7
DM
12081 PL_parser = parser_dup(proto_perl->Iparser, param);
12082
bd81e77b
NC
12083 PL_subline = proto_perl->Isubline;
12084 PL_subname = sv_dup_inc(proto_perl->Isubname, param);
c43294b8 12085
bd81e77b
NC
12086#ifdef FCRYPT
12087 PL_cryptseen = proto_perl->Icryptseen;
12088#endif
1d7c1841 12089
bd81e77b 12090 PL_hints = proto_perl->Ihints;
1d7c1841 12091
bd81e77b 12092 PL_amagic_generation = proto_perl->Iamagic_generation;
d2d73c3e 12093
bd81e77b
NC
12094#ifdef USE_LOCALE_COLLATE
12095 PL_collation_ix = proto_perl->Icollation_ix;
12096 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
12097 PL_collation_standard = proto_perl->Icollation_standard;
12098 PL_collxfrm_base = proto_perl->Icollxfrm_base;
12099 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
12100#endif /* USE_LOCALE_COLLATE */
1d7c1841 12101
bd81e77b
NC
12102#ifdef USE_LOCALE_NUMERIC
12103 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
12104 PL_numeric_standard = proto_perl->Inumeric_standard;
12105 PL_numeric_local = proto_perl->Inumeric_local;
12106 PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param);
12107#endif /* !USE_LOCALE_NUMERIC */
1d7c1841 12108
bd81e77b
NC
12109 /* utf8 character classes */
12110 PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum, param);
12111 PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc, param);
12112 PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii, param);
12113 PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha, param);
12114 PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space, param);
12115 PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl, param);
12116 PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph, param);
12117 PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit, param);
12118 PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper, param);
12119 PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower, param);
12120 PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print, param);
12121 PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct, param);
12122 PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit, param);
12123 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param);
12124 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param);
12125 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param);
12126 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param);
12127 PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param);
12128 PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param);
12129 PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param);
1d7c1841 12130
bd81e77b
NC
12131 /* Did the locale setup indicate UTF-8? */
12132 PL_utf8locale = proto_perl->Iutf8locale;
12133 /* Unicode features (see perlrun/-C) */
12134 PL_unicode = proto_perl->Iunicode;
1d7c1841 12135
bd81e77b
NC
12136 /* Pre-5.8 signals control */
12137 PL_signals = proto_perl->Isignals;
1d7c1841 12138
bd81e77b
NC
12139 /* times() ticks per second */
12140 PL_clocktick = proto_perl->Iclocktick;
1d7c1841 12141
bd81e77b
NC
12142 /* Recursion stopper for PerlIO_find_layer */
12143 PL_in_load_module = proto_perl->Iin_load_module;
8df990a8 12144
bd81e77b
NC
12145 /* sort() routine */
12146 PL_sort_RealCmp = proto_perl->Isort_RealCmp;
e5dd39fc 12147
bd81e77b
NC
12148 /* Not really needed/useful since the reenrant_retint is "volatile",
12149 * but do it for consistency's sake. */
12150 PL_reentrant_retint = proto_perl->Ireentrant_retint;
1d7c1841 12151
bd81e77b
NC
12152 /* Hooks to shared SVs and locks. */
12153 PL_sharehook = proto_perl->Isharehook;
12154 PL_lockhook = proto_perl->Ilockhook;
12155 PL_unlockhook = proto_perl->Iunlockhook;
12156 PL_threadhook = proto_perl->Ithreadhook;
eba16661 12157 PL_destroyhook = proto_perl->Idestroyhook;
1d7c1841 12158
bd81e77b
NC
12159#ifdef THREADS_HAVE_PIDS
12160 PL_ppid = proto_perl->Ippid;
12161#endif
1d7c1841 12162
bd81e77b 12163 /* swatch cache */
5c284bb0 12164 PL_last_swash_hv = NULL; /* reinits on demand */
bd81e77b
NC
12165 PL_last_swash_klen = 0;
12166 PL_last_swash_key[0]= '\0';
12167 PL_last_swash_tmps = (U8*)NULL;
12168 PL_last_swash_slen = 0;
1d7c1841 12169
bd81e77b
NC
12170 PL_glob_index = proto_perl->Iglob_index;
12171 PL_srand_called = proto_perl->Isrand_called;
05ec9bb3 12172
bd81e77b
NC
12173 if (proto_perl->Ipsig_pend) {
12174 Newxz(PL_psig_pend, SIG_SIZE, int);
12175 }
12176 else {
12177 PL_psig_pend = (int*)NULL;
12178 }
05ec9bb3 12179
d525a7b2
NC
12180 if (proto_perl->Ipsig_name) {
12181 Newx(PL_psig_name, 2 * SIG_SIZE, SV*);
12182 sv_dup_inc_multiple(proto_perl->Ipsig_name, PL_psig_name, 2 * SIG_SIZE,
538f2e76 12183 param);
d525a7b2 12184 PL_psig_ptr = PL_psig_name + SIG_SIZE;
bd81e77b
NC
12185 }
12186 else {
12187 PL_psig_ptr = (SV**)NULL;
12188 PL_psig_name = (SV**)NULL;
12189 }
05ec9bb3 12190
907b3e23 12191 /* intrpvar.h stuff */
1d7c1841 12192
bd81e77b
NC
12193 if (flags & CLONEf_COPY_STACKS) {
12194 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
907b3e23
DM
12195 PL_tmps_ix = proto_perl->Itmps_ix;
12196 PL_tmps_max = proto_perl->Itmps_max;
12197 PL_tmps_floor = proto_perl->Itmps_floor;
e92c6be8
NC
12198 Newx(PL_tmps_stack, PL_tmps_max, SV*);
12199 sv_dup_inc_multiple(proto_perl->Itmps_stack, PL_tmps_stack, PL_tmps_ix,
12200 param);
d2d73c3e 12201
bd81e77b 12202 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
907b3e23 12203 i = proto_perl->Imarkstack_max - proto_perl->Imarkstack;
bd81e77b 12204 Newxz(PL_markstack, i, I32);
907b3e23
DM
12205 PL_markstack_max = PL_markstack + (proto_perl->Imarkstack_max
12206 - proto_perl->Imarkstack);
12207 PL_markstack_ptr = PL_markstack + (proto_perl->Imarkstack_ptr
12208 - proto_perl->Imarkstack);
12209 Copy(proto_perl->Imarkstack, PL_markstack,
bd81e77b 12210 PL_markstack_ptr - PL_markstack + 1, I32);
d2d73c3e 12211
bd81e77b
NC
12212 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
12213 * NOTE: unlike the others! */
907b3e23
DM
12214 PL_scopestack_ix = proto_perl->Iscopestack_ix;
12215 PL_scopestack_max = proto_perl->Iscopestack_max;
bd81e77b 12216 Newxz(PL_scopestack, PL_scopestack_max, I32);
907b3e23 12217 Copy(proto_perl->Iscopestack, PL_scopestack, PL_scopestack_ix, I32);
d419787a 12218
bd81e77b 12219 /* NOTE: si_dup() looks at PL_markstack */
907b3e23 12220 PL_curstackinfo = si_dup(proto_perl->Icurstackinfo, param);
d2d73c3e 12221
bd81e77b 12222 /* PL_curstack = PL_curstackinfo->si_stack; */
907b3e23
DM
12223 PL_curstack = av_dup(proto_perl->Icurstack, param);
12224 PL_mainstack = av_dup(proto_perl->Imainstack, param);
1d7c1841 12225
bd81e77b
NC
12226 /* next PUSHs() etc. set *(PL_stack_sp+1) */
12227 PL_stack_base = AvARRAY(PL_curstack);
907b3e23
DM
12228 PL_stack_sp = PL_stack_base + (proto_perl->Istack_sp
12229 - proto_perl->Istack_base);
bd81e77b 12230 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
1d7c1841 12231
bd81e77b
NC
12232 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
12233 * NOTE: unlike the others! */
907b3e23
DM
12234 PL_savestack_ix = proto_perl->Isavestack_ix;
12235 PL_savestack_max = proto_perl->Isavestack_max;
bd81e77b
NC
12236 /*Newxz(PL_savestack, PL_savestack_max, ANY);*/
12237 PL_savestack = ss_dup(proto_perl, param);
12238 }
12239 else {
12240 init_stacks();
12241 ENTER; /* perl_destruct() wants to LEAVE; */
34394ecd
DM
12242
12243 /* although we're not duplicating the tmps stack, we should still
12244 * add entries for any SVs on the tmps stack that got cloned by a
12245 * non-refcount means (eg a temp in @_); otherwise they will be
12246 * orphaned
12247 */
907b3e23 12248 for (i = 0; i<= proto_perl->Itmps_ix; i++) {
daba3364
NC
12249 SV * const nsv = MUTABLE_SV(ptr_table_fetch(PL_ptr_table,
12250 proto_perl->Itmps_stack[i]));
34394ecd
DM
12251 if (nsv && !SvREFCNT(nsv)) {
12252 EXTEND_MORTAL(1);
b37c2d43 12253 PL_tmps_stack[++PL_tmps_ix] = SvREFCNT_inc_simple(nsv);
34394ecd
DM
12254 }
12255 }
bd81e77b 12256 }
1d7c1841 12257
907b3e23 12258 PL_start_env = proto_perl->Istart_env; /* XXXXXX */
bd81e77b 12259 PL_top_env = &PL_start_env;
1d7c1841 12260
907b3e23 12261 PL_op = proto_perl->Iop;
4a4c6fe3 12262
a0714e2c 12263 PL_Sv = NULL;
bd81e77b 12264 PL_Xpv = (XPV*)NULL;
24792b8d 12265 my_perl->Ina = proto_perl->Ina;
1fcf4c12 12266
907b3e23
DM
12267 PL_statbuf = proto_perl->Istatbuf;
12268 PL_statcache = proto_perl->Istatcache;
12269 PL_statgv = gv_dup(proto_perl->Istatgv, param);
12270 PL_statname = sv_dup_inc(proto_perl->Istatname, param);
bd81e77b 12271#ifdef HAS_TIMES
907b3e23 12272 PL_timesbuf = proto_perl->Itimesbuf;
bd81e77b 12273#endif
1d7c1841 12274
907b3e23
DM
12275 PL_tainted = proto_perl->Itainted;
12276 PL_curpm = proto_perl->Icurpm; /* XXX No PMOP ref count */
12277 PL_rs = sv_dup_inc(proto_perl->Irs, param);
12278 PL_last_in_gv = gv_dup(proto_perl->Ilast_in_gv, param);
907b3e23
DM
12279 PL_defoutgv = gv_dup_inc(proto_perl->Idefoutgv, param);
12280 PL_chopset = proto_perl->Ichopset; /* XXX never deallocated */
12281 PL_toptarget = sv_dup_inc(proto_perl->Itoptarget, param);
12282 PL_bodytarget = sv_dup_inc(proto_perl->Ibodytarget, param);
12283 PL_formtarget = sv_dup(proto_perl->Iformtarget, param);
12284
12285 PL_restartop = proto_perl->Irestartop;
12286 PL_in_eval = proto_perl->Iin_eval;
12287 PL_delaymagic = proto_perl->Idelaymagic;
12288 PL_dirty = proto_perl->Idirty;
12289 PL_localizing = proto_perl->Ilocalizing;
12290
12291 PL_errors = sv_dup_inc(proto_perl->Ierrors, param);
4608196e 12292 PL_hv_fetch_ent_mh = NULL;
907b3e23 12293 PL_modcount = proto_perl->Imodcount;
5f66b61c 12294 PL_lastgotoprobe = NULL;
907b3e23 12295 PL_dumpindent = proto_perl->Idumpindent;
1d7c1841 12296
907b3e23
DM
12297 PL_sortcop = (OP*)any_dup(proto_perl->Isortcop, proto_perl);
12298 PL_sortstash = hv_dup(proto_perl->Isortstash, param);
12299 PL_firstgv = gv_dup(proto_perl->Ifirstgv, param);
12300 PL_secondgv = gv_dup(proto_perl->Isecondgv, param);
bd61b366 12301 PL_efloatbuf = NULL; /* reinits on demand */
bd81e77b 12302 PL_efloatsize = 0; /* reinits on demand */
d2d73c3e 12303
bd81e77b 12304 /* regex stuff */
1d7c1841 12305
bd81e77b
NC
12306 PL_screamfirst = NULL;
12307 PL_screamnext = NULL;
12308 PL_maxscream = -1; /* reinits on demand */
a0714e2c 12309 PL_lastscream = NULL;
1d7c1841 12310
1d7c1841 12311
907b3e23 12312 PL_regdummy = proto_perl->Iregdummy;
bd81e77b
NC
12313 PL_colorset = 0; /* reinits PL_colors[] */
12314 /*PL_colors[6] = {0,0,0,0,0,0};*/
1d7c1841 12315
84da74a7 12316
1d7c1841 12317
bd81e77b 12318 /* Pluggable optimizer */
907b3e23 12319 PL_peepp = proto_perl->Ipeepp;
f37b8c3f
VP
12320 /* op_free() hook */
12321 PL_opfreehook = proto_perl->Iopfreehook;
1d7c1841 12322
bd81e77b 12323 PL_stashcache = newHV();
1d7c1841 12324
b7185faf 12325 PL_watchaddr = (char **) ptr_table_fetch(PL_ptr_table,
907b3e23 12326 proto_perl->Iwatchaddr);
b7185faf
DM
12327 PL_watchok = PL_watchaddr ? * PL_watchaddr : NULL;
12328 if (PL_debug && PL_watchaddr) {
12329 PerlIO_printf(Perl_debug_log,
12330 "WATCHING: %"UVxf" cloned as %"UVxf" with value %"UVxf"\n",
907b3e23 12331 PTR2UV(proto_perl->Iwatchaddr), PTR2UV(PL_watchaddr),
b7185faf
DM
12332 PTR2UV(PL_watchok));
12333 }
12334
a3e6e81e
NC
12335 PL_registered_mros = hv_dup_inc(proto_perl->Iregistered_mros, param);
12336
bd81e77b
NC
12337 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
12338 ptr_table_free(PL_ptr_table);
12339 PL_ptr_table = NULL;
12340 }
1d7c1841 12341
bd81e77b
NC
12342 /* Call the ->CLONE method, if it exists, for each of the stashes
12343 identified by sv_dup() above.
12344 */
12345 while(av_len(param->stashes) != -1) {
85fbaab2 12346 HV* const stash = MUTABLE_HV(av_shift(param->stashes));
bd81e77b
NC
12347 GV* const cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
12348 if (cloner && GvCV(cloner)) {
12349 dSP;
12350 ENTER;
12351 SAVETMPS;
12352 PUSHMARK(SP);
6e449a3a 12353 mXPUSHs(newSVhek(HvNAME_HEK(stash)));
bd81e77b 12354 PUTBACK;
daba3364 12355 call_sv(MUTABLE_SV(GvCV(cloner)), G_DISCARD);
bd81e77b
NC
12356 FREETMPS;
12357 LEAVE;
12358 }
1d7c1841 12359 }
1d7c1841 12360
bd81e77b 12361 SvREFCNT_dec(param->stashes);
1d7c1841 12362
bd81e77b
NC
12363 /* orphaned? eg threads->new inside BEGIN or use */
12364 if (PL_compcv && ! SvREFCNT(PL_compcv)) {
b37c2d43 12365 SvREFCNT_inc_simple_void(PL_compcv);
bd81e77b
NC
12366 SAVEFREESV(PL_compcv);
12367 }
dd2155a4 12368
bd81e77b
NC
12369 return my_perl;
12370}
1d7c1841 12371
bd81e77b 12372#endif /* USE_ITHREADS */
1d7c1841 12373
bd81e77b
NC
12374/*
12375=head1 Unicode Support
1d7c1841 12376
bd81e77b 12377=for apidoc sv_recode_to_utf8
1d7c1841 12378
bd81e77b
NC
12379The encoding is assumed to be an Encode object, on entry the PV
12380of the sv is assumed to be octets in that encoding, and the sv
12381will be converted into Unicode (and UTF-8).
1d7c1841 12382
bd81e77b
NC
12383If the sv already is UTF-8 (or if it is not POK), or if the encoding
12384is not a reference, nothing is done to the sv. If the encoding is not
12385an C<Encode::XS> Encoding object, bad things will happen.
12386(See F<lib/encoding.pm> and L<Encode>).
1d7c1841 12387
bd81e77b 12388The PV of the sv is returned.
1d7c1841 12389
bd81e77b 12390=cut */
1d7c1841 12391
bd81e77b
NC
12392char *
12393Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
12394{
12395 dVAR;
7918f24d
NC
12396
12397 PERL_ARGS_ASSERT_SV_RECODE_TO_UTF8;
12398
bd81e77b
NC
12399 if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
12400 SV *uni;
12401 STRLEN len;
12402 const char *s;
12403 dSP;
12404 ENTER;
12405 SAVETMPS;
12406 save_re_context();
12407 PUSHMARK(sp);
12408 EXTEND(SP, 3);
12409 XPUSHs(encoding);
12410 XPUSHs(sv);
12411/*
12412 NI-S 2002/07/09
12413 Passing sv_yes is wrong - it needs to be or'ed set of constants
12414 for Encode::XS, while UTf-8 decode (currently) assumes a true value means
12415 remove converted chars from source.
1d7c1841 12416
bd81e77b 12417 Both will default the value - let them.
1d7c1841 12418
bd81e77b
NC
12419 XPUSHs(&PL_sv_yes);
12420*/
12421 PUTBACK;
12422 call_method("decode", G_SCALAR);
12423 SPAGAIN;
12424 uni = POPs;
12425 PUTBACK;
12426 s = SvPV_const(uni, len);
12427 if (s != SvPVX_const(sv)) {
12428 SvGROW(sv, len + 1);
12429 Move(s, SvPVX(sv), len + 1, char);
12430 SvCUR_set(sv, len);
12431 }
12432 FREETMPS;
12433 LEAVE;
12434 SvUTF8_on(sv);
12435 return SvPVX(sv);
389edf32 12436 }
bd81e77b
NC
12437 return SvPOKp(sv) ? SvPVX(sv) : NULL;
12438}
1d7c1841 12439
bd81e77b
NC
12440/*
12441=for apidoc sv_cat_decode
1d7c1841 12442
bd81e77b
NC
12443The encoding is assumed to be an Encode object, the PV of the ssv is
12444assumed to be octets in that encoding and decoding the input starts
12445from the position which (PV + *offset) pointed to. The dsv will be
12446concatenated the decoded UTF-8 string from ssv. Decoding will terminate
12447when the string tstr appears in decoding output or the input ends on
12448the PV of the ssv. The value which the offset points will be modified
12449to the last input position on the ssv.
1d7c1841 12450
bd81e77b 12451Returns TRUE if the terminator was found, else returns FALSE.
1d7c1841 12452
bd81e77b
NC
12453=cut */
12454
12455bool
12456Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
12457 SV *ssv, int *offset, char *tstr, int tlen)
12458{
12459 dVAR;
12460 bool ret = FALSE;
7918f24d
NC
12461
12462 PERL_ARGS_ASSERT_SV_CAT_DECODE;
12463
bd81e77b
NC
12464 if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding) && offset) {
12465 SV *offsv;
12466 dSP;
12467 ENTER;
12468 SAVETMPS;
12469 save_re_context();
12470 PUSHMARK(sp);
12471 EXTEND(SP, 6);
12472 XPUSHs(encoding);
12473 XPUSHs(dsv);
12474 XPUSHs(ssv);
6e449a3a
MHM
12475 offsv = newSViv(*offset);
12476 mXPUSHs(offsv);
12477 mXPUSHp(tstr, tlen);
bd81e77b
NC
12478 PUTBACK;
12479 call_method("cat_decode", G_SCALAR);
12480 SPAGAIN;
12481 ret = SvTRUE(TOPs);
12482 *offset = SvIV(offsv);
12483 PUTBACK;
12484 FREETMPS;
12485 LEAVE;
389edf32 12486 }
bd81e77b
NC
12487 else
12488 Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
12489 return ret;
1d7c1841 12490
bd81e77b 12491}
1d7c1841 12492
bd81e77b
NC
12493/* ---------------------------------------------------------------------
12494 *
12495 * support functions for report_uninit()
12496 */
1d7c1841 12497
bd81e77b
NC
12498/* the maxiumum size of array or hash where we will scan looking
12499 * for the undefined element that triggered the warning */
1d7c1841 12500
bd81e77b 12501#define FUV_MAX_SEARCH_SIZE 1000
1d7c1841 12502
bd81e77b
NC
12503/* Look for an entry in the hash whose value has the same SV as val;
12504 * If so, return a mortal copy of the key. */
1d7c1841 12505
bd81e77b 12506STATIC SV*
6c1b357c 12507S_find_hash_subscript(pTHX_ const HV *const hv, const SV *const val)
bd81e77b
NC
12508{
12509 dVAR;
12510 register HE **array;
12511 I32 i;
6c3182a5 12512
7918f24d
NC
12513 PERL_ARGS_ASSERT_FIND_HASH_SUBSCRIPT;
12514
bd81e77b
NC
12515 if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
12516 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
a0714e2c 12517 return NULL;
6c3182a5 12518
bd81e77b 12519 array = HvARRAY(hv);
6c3182a5 12520
bd81e77b
NC
12521 for (i=HvMAX(hv); i>0; i--) {
12522 register HE *entry;
12523 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
12524 if (HeVAL(entry) != val)
12525 continue;
12526 if ( HeVAL(entry) == &PL_sv_undef ||
12527 HeVAL(entry) == &PL_sv_placeholder)
12528 continue;
12529 if (!HeKEY(entry))
a0714e2c 12530 return NULL;
bd81e77b
NC
12531 if (HeKLEN(entry) == HEf_SVKEY)
12532 return sv_mortalcopy(HeKEY_sv(entry));
a663657d 12533 return sv_2mortal(newSVhek(HeKEY_hek(entry)));
bd81e77b
NC
12534 }
12535 }
a0714e2c 12536 return NULL;
bd81e77b 12537}
6c3182a5 12538
bd81e77b
NC
12539/* Look for an entry in the array whose value has the same SV as val;
12540 * If so, return the index, otherwise return -1. */
6c3182a5 12541
bd81e77b 12542STATIC I32
6c1b357c 12543S_find_array_subscript(pTHX_ const AV *const av, const SV *const val)
bd81e77b 12544{
97aff369 12545 dVAR;
7918f24d
NC
12546
12547 PERL_ARGS_ASSERT_FIND_ARRAY_SUBSCRIPT;
12548
bd81e77b
NC
12549 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
12550 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
12551 return -1;
57c6e6d2 12552
4a021917
AL
12553 if (val != &PL_sv_undef) {
12554 SV ** const svp = AvARRAY(av);
12555 I32 i;
12556
12557 for (i=AvFILLp(av); i>=0; i--)
12558 if (svp[i] == val)
12559 return i;
bd81e77b
NC
12560 }
12561 return -1;
12562}
15a5279a 12563
bd81e77b
NC
12564/* S_varname(): return the name of a variable, optionally with a subscript.
12565 * If gv is non-zero, use the name of that global, along with gvtype (one
12566 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
12567 * targ. Depending on the value of the subscript_type flag, return:
12568 */
bce260cd 12569
bd81e77b
NC
12570#define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
12571#define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
12572#define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
12573#define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
bce260cd 12574
bd81e77b 12575STATIC SV*
6c1b357c
NC
12576S_varname(pTHX_ const GV *const gv, const char gvtype, PADOFFSET targ,
12577 const SV *const keyname, I32 aindex, int subscript_type)
bd81e77b 12578{
1d7c1841 12579
bd81e77b
NC
12580 SV * const name = sv_newmortal();
12581 if (gv) {
12582 char buffer[2];
12583 buffer[0] = gvtype;
12584 buffer[1] = 0;
1d7c1841 12585
bd81e77b 12586 /* as gv_fullname4(), but add literal '^' for $^FOO names */
66fe0623 12587
bd81e77b 12588 gv_fullname4(name, gv, buffer, 0);
1d7c1841 12589
bd81e77b
NC
12590 if ((unsigned int)SvPVX(name)[1] <= 26) {
12591 buffer[0] = '^';
12592 buffer[1] = SvPVX(name)[1] + 'A' - 1;
1d7c1841 12593
bd81e77b
NC
12594 /* Swap the 1 unprintable control character for the 2 byte pretty
12595 version - ie substr($name, 1, 1) = $buffer; */
12596 sv_insert(name, 1, 1, buffer, 2);
1d7c1841 12597 }
bd81e77b
NC
12598 }
12599 else {
289b91d9 12600 CV * const cv = find_runcv(NULL);
bd81e77b
NC
12601 SV *sv;
12602 AV *av;
1d7c1841 12603
bd81e77b 12604 if (!cv || !CvPADLIST(cv))
a0714e2c 12605 return NULL;
502c6561 12606 av = MUTABLE_AV((*av_fetch(CvPADLIST(cv), 0, FALSE)));
bd81e77b 12607 sv = *av_fetch(av, targ, FALSE);
f8503592 12608 sv_setpvn(name, SvPV_nolen_const(sv), SvCUR(sv));
bd81e77b 12609 }
1d7c1841 12610
bd81e77b 12611 if (subscript_type == FUV_SUBSCRIPT_HASH) {
561b68a9 12612 SV * const sv = newSV(0);
bd81e77b
NC
12613 *SvPVX(name) = '$';
12614 Perl_sv_catpvf(aTHX_ name, "{%s}",
12615 pv_display(sv,SvPVX_const(keyname), SvCUR(keyname), 0, 32));
12616 SvREFCNT_dec(sv);
12617 }
12618 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
12619 *SvPVX(name) = '$';
12620 Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
12621 }
84335ee9
NC
12622 else if (subscript_type == FUV_SUBSCRIPT_WITHIN) {
12623 /* We know that name has no magic, so can use 0 instead of SV_GMAGIC */
12624 Perl_sv_insert_flags(aTHX_ name, 0, 0, STR_WITH_LEN("within "), 0);
12625 }
1d7c1841 12626
bd81e77b
NC
12627 return name;
12628}
1d7c1841 12629
1d7c1841 12630
bd81e77b
NC
12631/*
12632=for apidoc find_uninit_var
1d7c1841 12633
bd81e77b
NC
12634Find the name of the undefined variable (if any) that caused the operator o
12635to issue a "Use of uninitialized value" warning.
12636If match is true, only return a name if it's value matches uninit_sv.
12637So roughly speaking, if a unary operator (such as OP_COS) generates a
12638warning, then following the direct child of the op may yield an
12639OP_PADSV or OP_GV that gives the name of the undefined variable. On the
12640other hand, with OP_ADD there are two branches to follow, so we only print
12641the variable name if we get an exact match.
1d7c1841 12642
bd81e77b 12643The name is returned as a mortal SV.
1d7c1841 12644
bd81e77b
NC
12645Assumes that PL_op is the op that originally triggered the error, and that
12646PL_comppad/PL_curpad points to the currently executing pad.
1d7c1841 12647
bd81e77b
NC
12648=cut
12649*/
1d7c1841 12650
bd81e77b 12651STATIC SV *
6c1b357c
NC
12652S_find_uninit_var(pTHX_ const OP *const obase, const SV *const uninit_sv,
12653 bool match)
bd81e77b
NC
12654{
12655 dVAR;
12656 SV *sv;
6c1b357c
NC
12657 const GV *gv;
12658 const OP *o, *o2, *kid;
1d7c1841 12659
bd81e77b
NC
12660 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
12661 uninit_sv == &PL_sv_placeholder)))
a0714e2c 12662 return NULL;
1d7c1841 12663
bd81e77b 12664 switch (obase->op_type) {
1d7c1841 12665
bd81e77b
NC
12666 case OP_RV2AV:
12667 case OP_RV2HV:
12668 case OP_PADAV:
12669 case OP_PADHV:
12670 {
12671 const bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
12672 const bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV);
12673 I32 index = 0;
a0714e2c 12674 SV *keysv = NULL;
bd81e77b 12675 int subscript_type = FUV_SUBSCRIPT_WITHIN;
1d7c1841 12676
bd81e77b
NC
12677 if (pad) { /* @lex, %lex */
12678 sv = PAD_SVl(obase->op_targ);
a0714e2c 12679 gv = NULL;
bd81e77b
NC
12680 }
12681 else {
12682 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
12683 /* @global, %global */
12684 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
12685 if (!gv)
12686 break;
daba3364 12687 sv = hash ? MUTABLE_SV(GvHV(gv)): MUTABLE_SV(GvAV(gv));
bd81e77b
NC
12688 }
12689 else /* @{expr}, %{expr} */
12690 return find_uninit_var(cUNOPx(obase)->op_first,
12691 uninit_sv, match);
12692 }
1d7c1841 12693
bd81e77b
NC
12694 /* attempt to find a match within the aggregate */
12695 if (hash) {
85fbaab2 12696 keysv = find_hash_subscript((const HV*)sv, uninit_sv);
bd81e77b
NC
12697 if (keysv)
12698 subscript_type = FUV_SUBSCRIPT_HASH;
12699 }
12700 else {
502c6561 12701 index = find_array_subscript((const AV *)sv, uninit_sv);
bd81e77b
NC
12702 if (index >= 0)
12703 subscript_type = FUV_SUBSCRIPT_ARRAY;
12704 }
1d7c1841 12705
bd81e77b
NC
12706 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
12707 break;
1d7c1841 12708
bd81e77b
NC
12709 return varname(gv, hash ? '%' : '@', obase->op_targ,
12710 keysv, index, subscript_type);
12711 }
1d7c1841 12712
bd81e77b
NC
12713 case OP_PADSV:
12714 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
12715 break;
a0714e2c
SS
12716 return varname(NULL, '$', obase->op_targ,
12717 NULL, 0, FUV_SUBSCRIPT_NONE);
1d7c1841 12718
bd81e77b
NC
12719 case OP_GVSV:
12720 gv = cGVOPx_gv(obase);
12721 if (!gv || (match && GvSV(gv) != uninit_sv))
12722 break;
a0714e2c 12723 return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
1d7c1841 12724
bd81e77b
NC
12725 case OP_AELEMFAST:
12726 if (obase->op_flags & OPf_SPECIAL) { /* lexical array */
12727 if (match) {
12728 SV **svp;
502c6561 12729 AV *av = MUTABLE_AV(PAD_SV(obase->op_targ));
bd81e77b
NC
12730 if (!av || SvRMAGICAL(av))
12731 break;
12732 svp = av_fetch(av, (I32)obase->op_private, FALSE);
12733 if (!svp || *svp != uninit_sv)
12734 break;
12735 }
a0714e2c
SS
12736 return varname(NULL, '$', obase->op_targ,
12737 NULL, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
bd81e77b
NC
12738 }
12739 else {
12740 gv = cGVOPx_gv(obase);
12741 if (!gv)
12742 break;
12743 if (match) {
12744 SV **svp;
6c1b357c 12745 AV *const av = GvAV(gv);
bd81e77b
NC
12746 if (!av || SvRMAGICAL(av))
12747 break;
12748 svp = av_fetch(av, (I32)obase->op_private, FALSE);
12749 if (!svp || *svp != uninit_sv)
12750 break;
12751 }
12752 return varname(gv, '$', 0,
a0714e2c 12753 NULL, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
bd81e77b
NC
12754 }
12755 break;
1d7c1841 12756
bd81e77b
NC
12757 case OP_EXISTS:
12758 o = cUNOPx(obase)->op_first;
12759 if (!o || o->op_type != OP_NULL ||
12760 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
12761 break;
12762 return find_uninit_var(cBINOPo->op_last, uninit_sv, match);
a2efc822 12763
bd81e77b
NC
12764 case OP_AELEM:
12765 case OP_HELEM:
12766 if (PL_op == obase)
12767 /* $a[uninit_expr] or $h{uninit_expr} */
12768 return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match);
081fc587 12769
a0714e2c 12770 gv = NULL;
bd81e77b
NC
12771 o = cBINOPx(obase)->op_first;
12772 kid = cBINOPx(obase)->op_last;
8cf8f3d1 12773
bd81e77b 12774 /* get the av or hv, and optionally the gv */
a0714e2c 12775 sv = NULL;
bd81e77b
NC
12776 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
12777 sv = PAD_SV(o->op_targ);
12778 }
12779 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
12780 && cUNOPo->op_first->op_type == OP_GV)
12781 {
12782 gv = cGVOPx_gv(cUNOPo->op_first);
12783 if (!gv)
12784 break;
daba3364
NC
12785 sv = o->op_type
12786 == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(GvAV(gv));
bd81e77b
NC
12787 }
12788 if (!sv)
12789 break;
12790
12791 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
12792 /* index is constant */
12793 if (match) {
12794 if (SvMAGICAL(sv))
12795 break;
12796 if (obase->op_type == OP_HELEM) {
85fbaab2 12797 HE* he = hv_fetch_ent(MUTABLE_HV(sv), cSVOPx_sv(kid), 0, 0);
bd81e77b
NC
12798 if (!he || HeVAL(he) != uninit_sv)
12799 break;
12800 }
12801 else {
502c6561 12802 SV * const * const svp = av_fetch(MUTABLE_AV(sv), SvIV(cSVOPx_sv(kid)), FALSE);
bd81e77b
NC
12803 if (!svp || *svp != uninit_sv)
12804 break;
12805 }
12806 }
12807 if (obase->op_type == OP_HELEM)
12808 return varname(gv, '%', o->op_targ,
12809 cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH);
12810 else
a0714e2c 12811 return varname(gv, '@', o->op_targ, NULL,
bd81e77b 12812 SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY);
bd81e77b
NC
12813 }
12814 else {
12815 /* index is an expression;
12816 * attempt to find a match within the aggregate */
12817 if (obase->op_type == OP_HELEM) {
85fbaab2 12818 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
bd81e77b
NC
12819 if (keysv)
12820 return varname(gv, '%', o->op_targ,
12821 keysv, 0, FUV_SUBSCRIPT_HASH);
12822 }
12823 else {
502c6561
NC
12824 const I32 index
12825 = find_array_subscript((const AV *)sv, uninit_sv);
bd81e77b
NC
12826 if (index >= 0)
12827 return varname(gv, '@', o->op_targ,
a0714e2c 12828 NULL, index, FUV_SUBSCRIPT_ARRAY);
bd81e77b
NC
12829 }
12830 if (match)
12831 break;
12832 return varname(gv,
12833 (o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
12834 ? '@' : '%',
a0714e2c 12835 o->op_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
f284b03f 12836 }
bd81e77b 12837 break;
dc507217 12838
bd81e77b
NC
12839 case OP_AASSIGN:
12840 /* only examine RHS */
12841 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match);
6d26897e 12842
bd81e77b
NC
12843 case OP_OPEN:
12844 o = cUNOPx(obase)->op_first;
12845 if (o->op_type == OP_PUSHMARK)
12846 o = o->op_sibling;
1d7c1841 12847
bd81e77b
NC
12848 if (!o->op_sibling) {
12849 /* one-arg version of open is highly magical */
a0ae6670 12850
bd81e77b
NC
12851 if (o->op_type == OP_GV) { /* open FOO; */
12852 gv = cGVOPx_gv(o);
12853 if (match && GvSV(gv) != uninit_sv)
12854 break;
12855 return varname(gv, '$', 0,
a0714e2c 12856 NULL, 0, FUV_SUBSCRIPT_NONE);
bd81e77b
NC
12857 }
12858 /* other possibilities not handled are:
12859 * open $x; or open my $x; should return '${*$x}'
12860 * open expr; should return '$'.expr ideally
12861 */
12862 break;
12863 }
12864 goto do_op;
ccfc67b7 12865
bd81e77b
NC
12866 /* ops where $_ may be an implicit arg */
12867 case OP_TRANS:
12868 case OP_SUBST:
12869 case OP_MATCH:
12870 if ( !(obase->op_flags & OPf_STACKED)) {
12871 if (uninit_sv == ((obase->op_private & OPpTARGET_MY)
12872 ? PAD_SVl(obase->op_targ)
12873 : DEFSV))
12874 {
12875 sv = sv_newmortal();
76f68e9b 12876 sv_setpvs(sv, "$_");
bd81e77b
NC
12877 return sv;
12878 }
12879 }
12880 goto do_op;
9f4817db 12881
bd81e77b
NC
12882 case OP_PRTF:
12883 case OP_PRINT:
3ef1310e 12884 case OP_SAY:
fa8d1836 12885 match = 1; /* print etc can return undef on defined args */
bd81e77b
NC
12886 /* skip filehandle as it can't produce 'undef' warning */
12887 o = cUNOPx(obase)->op_first;
12888 if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK)
12889 o = o->op_sibling->op_sibling;
12890 goto do_op2;
9f4817db 12891
9f4817db 12892
50edf520 12893 case OP_ENTEREVAL: /* could be eval $undef or $x='$undef'; eval $x */
bd81e77b 12894 case OP_RV2SV:
8b0dea50
DM
12895 case OP_CUSTOM: /* XS or custom code could trigger random warnings */
12896
12897 /* the following ops are capable of returning PL_sv_undef even for
12898 * defined arg(s) */
12899
12900 case OP_BACKTICK:
12901 case OP_PIPE_OP:
12902 case OP_FILENO:
12903 case OP_BINMODE:
12904 case OP_TIED:
12905 case OP_GETC:
12906 case OP_SYSREAD:
12907 case OP_SEND:
12908 case OP_IOCTL:
12909 case OP_SOCKET:
12910 case OP_SOCKPAIR:
12911 case OP_BIND:
12912 case OP_CONNECT:
12913 case OP_LISTEN:
12914 case OP_ACCEPT:
12915 case OP_SHUTDOWN:
12916 case OP_SSOCKOPT:
12917 case OP_GETPEERNAME:
12918 case OP_FTRREAD:
12919 case OP_FTRWRITE:
12920 case OP_FTREXEC:
12921 case OP_FTROWNED:
12922 case OP_FTEREAD:
12923 case OP_FTEWRITE:
12924 case OP_FTEEXEC:
12925 case OP_FTEOWNED:
12926 case OP_FTIS:
12927 case OP_FTZERO:
12928 case OP_FTSIZE:
12929 case OP_FTFILE:
12930 case OP_FTDIR:
12931 case OP_FTLINK:
12932 case OP_FTPIPE:
12933 case OP_FTSOCK:
12934 case OP_FTBLK:
12935 case OP_FTCHR:
12936 case OP_FTTTY:
12937 case OP_FTSUID:
12938 case OP_FTSGID:
12939 case OP_FTSVTX:
12940 case OP_FTTEXT:
12941 case OP_FTBINARY:
12942 case OP_FTMTIME:
12943 case OP_FTATIME:
12944 case OP_FTCTIME:
12945 case OP_READLINK:
12946 case OP_OPEN_DIR:
12947 case OP_READDIR:
12948 case OP_TELLDIR:
12949 case OP_SEEKDIR:
12950 case OP_REWINDDIR:
12951 case OP_CLOSEDIR:
12952 case OP_GMTIME:
12953 case OP_ALARM:
12954 case OP_SEMGET:
12955 case OP_GETLOGIN:
12956 case OP_UNDEF:
12957 case OP_SUBSTR:
12958 case OP_AEACH:
12959 case OP_EACH:
12960 case OP_SORT:
12961 case OP_CALLER:
12962 case OP_DOFILE:
fa8d1836
DM
12963 case OP_PROTOTYPE:
12964 case OP_NCMP:
12965 case OP_SMARTMATCH:
12966 case OP_UNPACK:
12967 case OP_SYSOPEN:
12968 case OP_SYSSEEK:
8b0dea50 12969 match = 1;
bd81e77b 12970 goto do_op;
9f4817db 12971
7697b7e7
DM
12972 case OP_ENTERSUB:
12973 case OP_GOTO:
a2fb3d36
DM
12974 /* XXX tmp hack: these two may call an XS sub, and currently
12975 XS subs don't have a SUB entry on the context stack, so CV and
12976 pad determination goes wrong, and BAD things happen. So, just
12977 don't try to determine the value under those circumstances.
7697b7e7
DM
12978 Need a better fix at dome point. DAPM 11/2007 */
12979 break;
12980
4f187fc9
VP
12981 case OP_FLIP:
12982 case OP_FLOP:
12983 {
12984 GV * const gv = gv_fetchpvs(".", GV_NOTQUAL, SVt_PV);
12985 if (gv && GvSV(gv) == uninit_sv)
12986 return newSVpvs_flags("$.", SVs_TEMP);
12987 goto do_op;
12988 }
8b0dea50 12989
cc4b8646
DM
12990 case OP_POS:
12991 /* def-ness of rval pos() is independent of the def-ness of its arg */
12992 if ( !(obase->op_flags & OPf_MOD))
12993 break;
12994
bd81e77b
NC
12995 case OP_SCHOMP:
12996 case OP_CHOMP:
12997 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
84bafc02 12998 return newSVpvs_flags("${$/}", SVs_TEMP);
5f66b61c 12999 /*FALLTHROUGH*/
5d170f3a 13000
bd81e77b
NC
13001 default:
13002 do_op:
13003 if (!(obase->op_flags & OPf_KIDS))
13004 break;
13005 o = cUNOPx(obase)->op_first;
13006
13007 do_op2:
13008 if (!o)
13009 break;
f9893866 13010
bd81e77b
NC
13011 /* if all except one arg are constant, or have no side-effects,
13012 * or are optimized away, then it's unambiguous */
5f66b61c 13013 o2 = NULL;
bd81e77b 13014 for (kid=o; kid; kid = kid->op_sibling) {
e15d5972
AL
13015 if (kid) {
13016 const OPCODE type = kid->op_type;
13017 if ( (type == OP_CONST && SvOK(cSVOPx_sv(kid)))
13018 || (type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
13019 || (type == OP_PUSHMARK)
bd81e77b 13020 )
bd81e77b 13021 continue;
e15d5972 13022 }
bd81e77b 13023 if (o2) { /* more than one found */
5f66b61c 13024 o2 = NULL;
bd81e77b
NC
13025 break;
13026 }
13027 o2 = kid;
13028 }
13029 if (o2)
13030 return find_uninit_var(o2, uninit_sv, match);
7a5fa8a2 13031
bd81e77b
NC
13032 /* scan all args */
13033 while (o) {
13034 sv = find_uninit_var(o, uninit_sv, 1);
13035 if (sv)
13036 return sv;
13037 o = o->op_sibling;
d0063567 13038 }
bd81e77b 13039 break;
f9893866 13040 }
a0714e2c 13041 return NULL;
9f4817db
JH
13042}
13043
220e2d4e 13044
bd81e77b
NC
13045/*
13046=for apidoc report_uninit
68795e93 13047
bd81e77b 13048Print appropriate "Use of uninitialized variable" warning
220e2d4e 13049
bd81e77b
NC
13050=cut
13051*/
220e2d4e 13052
bd81e77b 13053void
b3dbd76e 13054Perl_report_uninit(pTHX_ const SV *uninit_sv)
220e2d4e 13055{
97aff369 13056 dVAR;
bd81e77b 13057 if (PL_op) {
a0714e2c 13058 SV* varname = NULL;
bd81e77b
NC
13059 if (uninit_sv) {
13060 varname = find_uninit_var(PL_op, uninit_sv,0);
13061 if (varname)
13062 sv_insert(varname, 0, 0, " ", 1);
13063 }
13064 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
13065 varname ? SvPV_nolen_const(varname) : "",
13066 " in ", OP_DESC(PL_op));
220e2d4e 13067 }
a73e8557 13068 else
bd81e77b
NC
13069 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
13070 "", "", "");
220e2d4e 13071}
f9893866 13072
241d1a3b
NC
13073/*
13074 * Local variables:
13075 * c-indentation-style: bsd
13076 * c-basic-offset: 4
13077 * indent-tabs-mode: t
13078 * End:
13079 *
37442d52
RGS
13080 * ex: set ts=8 sts=4 sw=4 noet:
13081 */