This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
GvFILE() cannot be a pointer to the memory owned by the COP, because
[perl5.git] / sv.c
CommitLineData
a0d0e21e 1/* sv.c
79072805 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
b94e2f88 4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
79072805
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
a0d0e21e 9 * "I wonder what the Entish is for 'yes' and 'no'," he thought.
645c22ef
DM
10 *
11 *
5e045b90
AMS
12 * This file contains the code that creates, manipulates and destroys
13 * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the
14 * structure of an SV, so their creation and destruction is handled
15 * here; higher-level functions are in av.c, hv.c, and so on. Opcode
16 * level functions (eg. substr, split, join) for each of the types are
17 * in the pp*.c files.
79072805
LW
18 */
19
20#include "EXTERN.h"
864dbfa3 21#define PERL_IN_SV_C
79072805 22#include "perl.h"
d2f185dc 23#include "regcomp.h"
79072805 24
51371543 25#define FCALL *f
2c5424a7 26
2f8ed50e
OS
27#ifdef __Lynx__
28/* Missing proto on LynxOS */
29 char *gconvert(double, int, int, char *);
30#endif
31
e23c8137 32#ifdef PERL_UTF8_CACHE_ASSERT
ab455f60 33/* if adding more checks watch out for the following tests:
e23c8137
JH
34 * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
35 * lib/utf8.t lib/Unicode/Collate/t/index.t
36 * --jhi
37 */
6f207bd3 38# define ASSERT_UTF8_CACHE(cache) \
ab455f60
NC
39 STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); \
40 assert((cache)[2] <= (cache)[3]); \
41 assert((cache)[3] <= (cache)[1]);} \
42 } STMT_END
e23c8137 43#else
6f207bd3 44# define ASSERT_UTF8_CACHE(cache) NOOP
e23c8137
JH
45#endif
46
f8c7b90f 47#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 48#define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
607fa7f2 49#define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next))
b5ccf5f2 50/* This is a pessimistic view. Scalar must be purely a read-write PV to copy-
765f542d 51 on-write. */
765f542d 52#endif
645c22ef
DM
53
54/* ============================================================================
55
56=head1 Allocation and deallocation of SVs.
57
d2a0f284
JC
58An SV (or AV, HV, etc.) is allocated in two parts: the head (struct
59sv, av, hv...) contains type and reference count information, and for
60many types, a pointer to the body (struct xrv, xpv, xpviv...), which
61contains fields specific to each type. Some types store all they need
62in the head, so don't have a body.
63
64In all but the most memory-paranoid configuations (ex: PURIFY), heads
65and bodies are allocated out of arenas, which by default are
66approximately 4K chunks of memory parcelled up into N heads or bodies.
93e68bfb
JC
67Sv-bodies are allocated by their sv-type, guaranteeing size
68consistency needed to allocate safely from arrays.
69
d2a0f284
JC
70For SV-heads, the first slot in each arena is reserved, and holds a
71link to the next arena, some flags, and a note of the number of slots.
72Snaked through each arena chain is a linked list of free items; when
73this becomes empty, an extra arena is allocated and divided up into N
74items which are threaded into the free list.
75
76SV-bodies are similar, but they use arena-sets by default, which
77separate the link and info from the arena itself, and reclaim the 1st
78slot in the arena. SV-bodies are further described later.
645c22ef
DM
79
80The following global variables are associated with arenas:
81
82 PL_sv_arenaroot pointer to list of SV arenas
83 PL_sv_root pointer to list of free SV structures
84
d2a0f284
JC
85 PL_body_arenas head of linked-list of body arenas
86 PL_body_roots[] array of pointers to list of free bodies of svtype
87 arrays are indexed by the svtype needed
93e68bfb 88
d2a0f284
JC
89A few special SV heads are not allocated from an arena, but are
90instead directly created in the interpreter structure, eg PL_sv_undef.
93e68bfb
JC
91The size of arenas can be changed from the default by setting
92PERL_ARENA_SIZE appropriately at compile time.
645c22ef
DM
93
94The SV arena serves the secondary purpose of allowing still-live SVs
95to be located and destroyed during final cleanup.
96
97At the lowest level, the macros new_SV() and del_SV() grab and free
98an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
99to return the SV to the free list with error checking.) new_SV() calls
100more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
101SVs in the free list have their SvTYPE field set to all ones.
102
ff276b08 103At the time of very final cleanup, sv_free_arenas() is called from
645c22ef 104perl_destruct() to physically free all the arenas allocated since the
6a93a7e5 105start of the interpreter.
645c22ef
DM
106
107Manipulation of any of the PL_*root pointers is protected by enclosing
108LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
109if threads are enabled.
110
111The function visit() scans the SV arenas list, and calls a specified
112function for each SV it finds which is still live - ie which has an SvTYPE
113other than all 1's, and a non-zero SvREFCNT. visit() is used by the
114following functions (specified as [function that calls visit()] / [function
115called by visit() for each SV]):
116
117 sv_report_used() / do_report_used()
f2524eef 118 dump all remaining SVs (debugging aid)
645c22ef
DM
119
120 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
121 Attempt to free all objects pointed to by RVs,
122 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
123 try to do the same for all objects indirectly
124 referenced by typeglobs too. Called once from
125 perl_destruct(), prior to calling sv_clean_all()
126 below.
127
128 sv_clean_all() / do_clean_all()
129 SvREFCNT_dec(sv) each remaining SV, possibly
130 triggering an sv_free(). It also sets the
131 SVf_BREAK flag on the SV to indicate that the
132 refcnt has been artificially lowered, and thus
133 stopping sv_free() from giving spurious warnings
134 about SVs which unexpectedly have a refcnt
135 of zero. called repeatedly from perl_destruct()
136 until there are no SVs left.
137
93e68bfb 138=head2 Arena allocator API Summary
645c22ef
DM
139
140Private API to rest of sv.c
141
142 new_SV(), del_SV(),
143
144 new_XIV(), del_XIV(),
145 new_XNV(), del_XNV(),
146 etc
147
148Public API:
149
8cf8f3d1 150 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
645c22ef 151
645c22ef
DM
152=cut
153
154============================================================================ */
155
4561caa4
CS
156/*
157 * "A time to plant, and a time to uproot what was planted..."
158 */
159
77354fb4
NC
160/*
161 * nice_chunk and nice_chunk size need to be set
162 * and queried under the protection of sv_mutex
163 */
164void
165Perl_offer_nice_chunk(pTHX_ void *chunk, U32 chunk_size)
166{
97aff369 167 dVAR;
77354fb4
NC
168 void *new_chunk;
169 U32 new_chunk_size;
170 LOCK_SV_MUTEX;
171 new_chunk = (void *)(chunk);
172 new_chunk_size = (chunk_size);
173 if (new_chunk_size > PL_nice_chunk_size) {
174 Safefree(PL_nice_chunk);
175 PL_nice_chunk = (char *) new_chunk;
176 PL_nice_chunk_size = new_chunk_size;
177 } else {
178 Safefree(chunk);
179 }
180 UNLOCK_SV_MUTEX;
181}
cac9b346 182
fd0854ff 183#ifdef DEBUG_LEAKING_SCALARS
22162ca8 184# define FREE_SV_DEBUG_FILE(sv) Safefree((sv)->sv_debug_file)
fd0854ff
DM
185#else
186# define FREE_SV_DEBUG_FILE(sv)
187#endif
188
48614a46
NC
189#ifdef PERL_POISON
190# define SvARENA_CHAIN(sv) ((sv)->sv_u.svu_rv)
191/* Whilst I'd love to do this, it seems that things like to check on
192 unreferenced scalars
7e337ee0 193# define POSION_SV_HEAD(sv) PoisonNew(sv, 1, struct STRUCT_SV)
48614a46 194*/
7e337ee0
JH
195# define POSION_SV_HEAD(sv) PoisonNew(&SvANY(sv), 1, void *), \
196 PoisonNew(&SvREFCNT(sv), 1, U32)
48614a46
NC
197#else
198# define SvARENA_CHAIN(sv) SvANY(sv)
199# define POSION_SV_HEAD(sv)
200#endif
201
053fc874
GS
202#define plant_SV(p) \
203 STMT_START { \
fd0854ff 204 FREE_SV_DEBUG_FILE(p); \
48614a46
NC
205 POSION_SV_HEAD(p); \
206 SvARENA_CHAIN(p) = (void *)PL_sv_root; \
053fc874
GS
207 SvFLAGS(p) = SVTYPEMASK; \
208 PL_sv_root = (p); \
209 --PL_sv_count; \
210 } STMT_END
a0d0e21e 211
fba3b22e 212/* sv_mutex must be held while calling uproot_SV() */
053fc874
GS
213#define uproot_SV(p) \
214 STMT_START { \
215 (p) = PL_sv_root; \
bb7bbd9c 216 PL_sv_root = (SV*)SvARENA_CHAIN(p); \
053fc874
GS
217 ++PL_sv_count; \
218 } STMT_END
219
645c22ef 220
cac9b346
NC
221/* make some more SVs by adding another arena */
222
223/* sv_mutex must be held while calling more_sv() */
224STATIC SV*
225S_more_sv(pTHX)
226{
97aff369 227 dVAR;
cac9b346
NC
228 SV* sv;
229
230 if (PL_nice_chunk) {
231 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
bd61b366 232 PL_nice_chunk = NULL;
cac9b346
NC
233 PL_nice_chunk_size = 0;
234 }
235 else {
236 char *chunk; /* must use New here to match call to */
d2a0f284 237 Newx(chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
2e7ed132 238 sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
cac9b346
NC
239 }
240 uproot_SV(sv);
241 return sv;
242}
243
645c22ef
DM
244/* new_SV(): return a new, empty SV head */
245
eba0f806
DM
246#ifdef DEBUG_LEAKING_SCALARS
247/* provide a real function for a debugger to play with */
248STATIC SV*
249S_new_SV(pTHX)
250{
251 SV* sv;
252
253 LOCK_SV_MUTEX;
254 if (PL_sv_root)
255 uproot_SV(sv);
256 else
cac9b346 257 sv = S_more_sv(aTHX);
eba0f806
DM
258 UNLOCK_SV_MUTEX;
259 SvANY(sv) = 0;
260 SvREFCNT(sv) = 1;
261 SvFLAGS(sv) = 0;
fd0854ff
DM
262 sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
263 sv->sv_debug_line = (U16) ((PL_copline == NOLINE) ?
264 (PL_curcop ? CopLINE(PL_curcop) : 0) : PL_copline);
265 sv->sv_debug_inpad = 0;
266 sv->sv_debug_cloned = 0;
fd0854ff 267 sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL;
fd0854ff 268
eba0f806
DM
269 return sv;
270}
271# define new_SV(p) (p)=S_new_SV(aTHX)
272
273#else
274# define new_SV(p) \
053fc874
GS
275 STMT_START { \
276 LOCK_SV_MUTEX; \
277 if (PL_sv_root) \
278 uproot_SV(p); \
279 else \
cac9b346 280 (p) = S_more_sv(aTHX); \
053fc874
GS
281 UNLOCK_SV_MUTEX; \
282 SvANY(p) = 0; \
283 SvREFCNT(p) = 1; \
284 SvFLAGS(p) = 0; \
285 } STMT_END
eba0f806 286#endif
463ee0b2 287
645c22ef
DM
288
289/* del_SV(): return an empty SV head to the free list */
290
a0d0e21e 291#ifdef DEBUGGING
4561caa4 292
053fc874
GS
293#define del_SV(p) \
294 STMT_START { \
295 LOCK_SV_MUTEX; \
aea4f609 296 if (DEBUG_D_TEST) \
053fc874
GS
297 del_sv(p); \
298 else \
299 plant_SV(p); \
300 UNLOCK_SV_MUTEX; \
301 } STMT_END
a0d0e21e 302
76e3520e 303STATIC void
cea2e8a9 304S_del_sv(pTHX_ SV *p)
463ee0b2 305{
97aff369 306 dVAR;
aea4f609 307 if (DEBUG_D_TEST) {
4633a7c4 308 SV* sva;
a3b680e6 309 bool ok = 0;
3280af22 310 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
53c1dcc0
AL
311 const SV * const sv = sva + 1;
312 const SV * const svend = &sva[SvREFCNT(sva)];
c0ff570e 313 if (p >= sv && p < svend) {
a0d0e21e 314 ok = 1;
c0ff570e
NC
315 break;
316 }
a0d0e21e
LW
317 }
318 if (!ok) {
0453d815 319 if (ckWARN_d(WARN_INTERNAL))
9014280d 320 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
472d47bc
SB
321 "Attempt to free non-arena SV: 0x%"UVxf
322 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
a0d0e21e
LW
323 return;
324 }
325 }
4561caa4 326 plant_SV(p);
463ee0b2 327}
a0d0e21e 328
4561caa4
CS
329#else /* ! DEBUGGING */
330
331#define del_SV(p) plant_SV(p)
332
333#endif /* DEBUGGING */
463ee0b2 334
645c22ef
DM
335
336/*
ccfc67b7
JH
337=head1 SV Manipulation Functions
338
645c22ef
DM
339=for apidoc sv_add_arena
340
341Given a chunk of memory, link it to the head of the list of arenas,
342and split it into a list of free SVs.
343
344=cut
345*/
346
4633a7c4 347void
864dbfa3 348Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
463ee0b2 349{
97aff369 350 dVAR;
0bd48802 351 SV* const sva = (SV*)ptr;
463ee0b2
LW
352 register SV* sv;
353 register SV* svend;
4633a7c4
LW
354
355 /* The first SV in an arena isn't an SV. */
3280af22 356 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
4633a7c4
LW
357 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
358 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
359
3280af22
NIS
360 PL_sv_arenaroot = sva;
361 PL_sv_root = sva + 1;
4633a7c4
LW
362
363 svend = &sva[SvREFCNT(sva) - 1];
364 sv = sva + 1;
463ee0b2 365 while (sv < svend) {
48614a46 366 SvARENA_CHAIN(sv) = (void *)(SV*)(sv + 1);
03e36789 367#ifdef DEBUGGING
978b032e 368 SvREFCNT(sv) = 0;
03e36789
NC
369#endif
370 /* Must always set typemask because it's awlays checked in on cleanup
371 when the arenas are walked looking for objects. */
8990e307 372 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
373 sv++;
374 }
48614a46 375 SvARENA_CHAIN(sv) = 0;
03e36789
NC
376#ifdef DEBUGGING
377 SvREFCNT(sv) = 0;
378#endif
4633a7c4
LW
379 SvFLAGS(sv) = SVTYPEMASK;
380}
381
055972dc
DM
382/* visit(): call the named function for each non-free SV in the arenas
383 * whose flags field matches the flags/mask args. */
645c22ef 384
5226ed68 385STATIC I32
055972dc 386S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
8990e307 387{
97aff369 388 dVAR;
4633a7c4 389 SV* sva;
5226ed68 390 I32 visited = 0;
8990e307 391
3280af22 392 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
53c1dcc0 393 register const SV * const svend = &sva[SvREFCNT(sva)];
a3b680e6 394 register SV* sv;
4561caa4 395 for (sv = sva + 1; sv < svend; ++sv) {
055972dc
DM
396 if (SvTYPE(sv) != SVTYPEMASK
397 && (sv->sv_flags & mask) == flags
398 && SvREFCNT(sv))
399 {
acfe0abc 400 (FCALL)(aTHX_ sv);
5226ed68
JH
401 ++visited;
402 }
8990e307
LW
403 }
404 }
5226ed68 405 return visited;
8990e307
LW
406}
407
758a08c3
JH
408#ifdef DEBUGGING
409
645c22ef
DM
410/* called by sv_report_used() for each live SV */
411
412static void
acfe0abc 413do_report_used(pTHX_ SV *sv)
645c22ef
DM
414{
415 if (SvTYPE(sv) != SVTYPEMASK) {
416 PerlIO_printf(Perl_debug_log, "****\n");
417 sv_dump(sv);
418 }
419}
758a08c3 420#endif
645c22ef
DM
421
422/*
423=for apidoc sv_report_used
424
425Dump the contents of all SVs not yet freed. (Debugging aid).
426
427=cut
428*/
429
8990e307 430void
864dbfa3 431Perl_sv_report_used(pTHX)
4561caa4 432{
ff270d3a 433#ifdef DEBUGGING
055972dc 434 visit(do_report_used, 0, 0);
96a5add6
AL
435#else
436 PERL_UNUSED_CONTEXT;
ff270d3a 437#endif
4561caa4
CS
438}
439
645c22ef
DM
440/* called by sv_clean_objs() for each live SV */
441
442static void
e15faf7d 443do_clean_objs(pTHX_ SV *ref)
645c22ef 444{
97aff369 445 dVAR;
823a54a3
AL
446 if (SvROK(ref)) {
447 SV * const target = SvRV(ref);
448 if (SvOBJECT(target)) {
449 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(ref)));
450 if (SvWEAKREF(ref)) {
451 sv_del_backref(target, ref);
452 SvWEAKREF_off(ref);
453 SvRV_set(ref, NULL);
454 } else {
455 SvROK_off(ref);
456 SvRV_set(ref, NULL);
457 SvREFCNT_dec(target);
458 }
645c22ef
DM
459 }
460 }
461
462 /* XXX Might want to check arrays, etc. */
463}
464
465/* called by sv_clean_objs() for each live SV */
466
467#ifndef DISABLE_DESTRUCTOR_KLUDGE
468static void
acfe0abc 469do_clean_named_objs(pTHX_ SV *sv)
645c22ef 470{
97aff369 471 dVAR;
f7877b28 472 if (SvTYPE(sv) == SVt_PVGV && isGV_with_GP(sv) && GvGP(sv)) {
c69033f2
NC
473 if ((
474#ifdef PERL_DONT_CREATE_GVSV
475 GvSV(sv) &&
476#endif
477 SvOBJECT(GvSV(sv))) ||
645c22ef
DM
478 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
479 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
480 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
481 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
482 {
483 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
ec5f3c78 484 SvFLAGS(sv) |= SVf_BREAK;
645c22ef
DM
485 SvREFCNT_dec(sv);
486 }
487 }
488}
489#endif
490
491/*
492=for apidoc sv_clean_objs
493
494Attempt to destroy all objects not yet freed
495
496=cut
497*/
498
4561caa4 499void
864dbfa3 500Perl_sv_clean_objs(pTHX)
4561caa4 501{
97aff369 502 dVAR;
3280af22 503 PL_in_clean_objs = TRUE;
055972dc 504 visit(do_clean_objs, SVf_ROK, SVf_ROK);
4561caa4 505#ifndef DISABLE_DESTRUCTOR_KLUDGE
2d0f3c12 506 /* some barnacles may yet remain, clinging to typeglobs */
055972dc 507 visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
4561caa4 508#endif
3280af22 509 PL_in_clean_objs = FALSE;
4561caa4
CS
510}
511
645c22ef
DM
512/* called by sv_clean_all() for each live SV */
513
514static void
acfe0abc 515do_clean_all(pTHX_ SV *sv)
645c22ef 516{
97aff369 517 dVAR;
645c22ef
DM
518 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
519 SvFLAGS(sv) |= SVf_BREAK;
0e705b3b 520 if (PL_comppad == (AV*)sv) {
7d49f689 521 PL_comppad = NULL;
4608196e 522 PL_curpad = NULL;
0e705b3b 523 }
645c22ef
DM
524 SvREFCNT_dec(sv);
525}
526
527/*
528=for apidoc sv_clean_all
529
530Decrement the refcnt of each remaining SV, possibly triggering a
531cleanup. This function may have to be called multiple times to free
ff276b08 532SVs which are in complex self-referential hierarchies.
645c22ef
DM
533
534=cut
535*/
536
5226ed68 537I32
864dbfa3 538Perl_sv_clean_all(pTHX)
8990e307 539{
97aff369 540 dVAR;
5226ed68 541 I32 cleaned;
3280af22 542 PL_in_clean_all = TRUE;
055972dc 543 cleaned = visit(do_clean_all, 0,0);
3280af22 544 PL_in_clean_all = FALSE;
5226ed68 545 return cleaned;
8990e307 546}
463ee0b2 547
5e258f8c
JC
548/*
549 ARENASETS: a meta-arena implementation which separates arena-info
550 into struct arena_set, which contains an array of struct
551 arena_descs, each holding info for a single arena. By separating
552 the meta-info from the arena, we recover the 1st slot, formerly
553 borrowed for list management. The arena_set is about the size of an
554 arena, avoiding the needless malloc overhead of a naive linked-list
555
556 The cost is 1 arena-set malloc per ~320 arena-mallocs, + the unused
557 memory in the last arena-set (1/2 on average). In trade, we get
558 back the 1st slot in each arena (ie 1.7% of a CV-arena, less for
d2a0f284
JC
559 smaller types). The recovery of the wasted space allows use of
560 small arenas for large, rare body types,
5e258f8c 561*/
5e258f8c 562struct arena_desc {
398c677b
NC
563 char *arena; /* the raw storage, allocated aligned */
564 size_t size; /* its size ~4k typ */
565 int unit_type; /* useful for arena audits */
5e258f8c
JC
566 /* info for sv-heads (eventually)
567 int count, flags;
568 */
569};
570
e6148039
NC
571struct arena_set;
572
573/* Get the maximum number of elements in set[] such that struct arena_set
574 will fit within PERL_ARENA_SIZE, which is probabably just under 4K, and
575 therefore likely to be 1 aligned memory page. */
576
577#define ARENAS_PER_SET ((PERL_ARENA_SIZE - sizeof(struct arena_set*) \
578 - 2 * sizeof(int)) / sizeof (struct arena_desc))
5e258f8c
JC
579
580struct arena_set {
581 struct arena_set* next;
582 int set_size; /* ie ARENAS_PER_SET */
583 int curr; /* index of next available arena-desc */
584 struct arena_desc set[ARENAS_PER_SET];
585};
586
645c22ef
DM
587/*
588=for apidoc sv_free_arenas
589
590Deallocate the memory used by all arenas. Note that all the individual SV
591heads and bodies within the arenas must already have been freed.
592
593=cut
594*/
4633a7c4 595void
864dbfa3 596Perl_sv_free_arenas(pTHX)
4633a7c4 597{
97aff369 598 dVAR;
4633a7c4
LW
599 SV* sva;
600 SV* svanext;
93e68bfb 601 int i;
4633a7c4
LW
602
603 /* Free arenas here, but be careful about fake ones. (We assume
604 contiguity of the fake ones with the corresponding real ones.) */
605
3280af22 606 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
4633a7c4
LW
607 svanext = (SV*) SvANY(sva);
608 while (svanext && SvFAKE(svanext))
609 svanext = (SV*) SvANY(svanext);
610
611 if (!SvFAKE(sva))
1df70142 612 Safefree(sva);
4633a7c4 613 }
93e68bfb 614
5e258f8c
JC
615 {
616 struct arena_set *next, *aroot = (struct arena_set*) PL_body_arenas;
617
618 for (; aroot; aroot = next) {
96a5add6 619 const int max = aroot->curr;
5e258f8c
JC
620 for (i=0; i<max; i++) {
621 assert(aroot->set[i].arena);
622 Safefree(aroot->set[i].arena);
623 }
624 next = aroot->next;
625 Safefree(aroot);
626 }
627 }
dc8220bf 628 PL_body_arenas = 0;
fdda85ca 629
232d1c15 630 for (i=0; i<PERL_ARENA_ROOTS_SIZE; i++)
93e68bfb 631 PL_body_roots[i] = 0;
93e68bfb 632
43c5f42d 633 Safefree(PL_nice_chunk);
bd61b366 634 PL_nice_chunk = NULL;
3280af22
NIS
635 PL_nice_chunk_size = 0;
636 PL_sv_arenaroot = 0;
637 PL_sv_root = 0;
4633a7c4
LW
638}
639
bd81e77b
NC
640/*
641 Here are mid-level routines that manage the allocation of bodies out
642 of the various arenas. There are 5 kinds of arenas:
29489e7c 643
bd81e77b
NC
644 1. SV-head arenas, which are discussed and handled above
645 2. regular body arenas
646 3. arenas for reduced-size bodies
647 4. Hash-Entry arenas
648 5. pte arenas (thread related)
29489e7c 649
bd81e77b
NC
650 Arena types 2 & 3 are chained by body-type off an array of
651 arena-root pointers, which is indexed by svtype. Some of the
652 larger/less used body types are malloced singly, since a large
653 unused block of them is wasteful. Also, several svtypes dont have
654 bodies; the data fits into the sv-head itself. The arena-root
655 pointer thus has a few unused root-pointers (which may be hijacked
656 later for arena types 4,5)
29489e7c 657
bd81e77b
NC
658 3 differs from 2 as an optimization; some body types have several
659 unused fields in the front of the structure (which are kept in-place
660 for consistency). These bodies can be allocated in smaller chunks,
661 because the leading fields arent accessed. Pointers to such bodies
662 are decremented to point at the unused 'ghost' memory, knowing that
663 the pointers are used with offsets to the real memory.
29489e7c 664
bd81e77b
NC
665 HE, HEK arenas are managed separately, with separate code, but may
666 be merge-able later..
667
668 PTE arenas are not sv-bodies, but they share these mid-level
669 mechanics, so are considered here. The new mid-level mechanics rely
670 on the sv_type of the body being allocated, so we just reserve one
671 of the unused body-slots for PTEs, then use it in those (2) PTE
672 contexts below (line ~10k)
673*/
674
bd26d9a3 675/* get_arena(size): this creates custom-sized arenas
5e258f8c
JC
676 TBD: export properly for hv.c: S_more_he().
677*/
678void*
679Perl_get_arena(pTHX_ int arena_size)
680{
7a89be66 681 dVAR;
5e258f8c 682 struct arena_desc* adesc;
476a1e16 683 struct arena_set *newroot, **aroot = (struct arena_set**) &PL_body_arenas;
5e258f8c
JC
684 int curr;
685
476a1e16
JC
686 /* shouldnt need this
687 if (!arena_size) arena_size = PERL_ARENA_SIZE;
688 */
5e258f8c
JC
689
690 /* may need new arena-set to hold new arena */
476a1e16 691 if (!*aroot || (*aroot)->curr >= (*aroot)->set_size) {
5e258f8c
JC
692 Newxz(newroot, 1, struct arena_set);
693 newroot->set_size = ARENAS_PER_SET;
476a1e16
JC
694 newroot->next = *aroot;
695 *aroot = newroot;
ca0270c4 696 DEBUG_m(PerlIO_printf(Perl_debug_log, "new arenaset %p\n", (void*)*aroot));
5e258f8c
JC
697 }
698
699 /* ok, now have arena-set with at least 1 empty/available arena-desc */
476a1e16
JC
700 curr = (*aroot)->curr++;
701 adesc = &((*aroot)->set[curr]);
5e258f8c
JC
702 assert(!adesc->arena);
703
5e258f8c
JC
704 Newxz(adesc->arena, arena_size, char);
705 adesc->size = arena_size;
d2a0f284
JC
706 DEBUG_m(PerlIO_printf(Perl_debug_log, "arena %d added: %p size %d\n",
707 curr, adesc->arena, arena_size));
5e258f8c
JC
708
709 return adesc->arena;
5e258f8c
JC
710}
711
53c1dcc0 712
bd81e77b 713/* return a thing to the free list */
29489e7c 714
bd81e77b
NC
715#define del_body(thing, root) \
716 STMT_START { \
00b6aa41 717 void ** const thing_copy = (void **)thing;\
bd81e77b
NC
718 LOCK_SV_MUTEX; \
719 *thing_copy = *root; \
720 *root = (void*)thing_copy; \
721 UNLOCK_SV_MUTEX; \
722 } STMT_END
29489e7c 723
bd81e77b 724/*
d2a0f284
JC
725
726=head1 SV-Body Allocation
727
728Allocation of SV-bodies is similar to SV-heads, differing as follows;
729the allocation mechanism is used for many body types, so is somewhat
730more complicated, it uses arena-sets, and has no need for still-live
731SV detection.
732
733At the outermost level, (new|del)_X*V macros return bodies of the
734appropriate type. These macros call either (new|del)_body_type or
735(new|del)_body_allocated macro pairs, depending on specifics of the
736type. Most body types use the former pair, the latter pair is used to
737allocate body types with "ghost fields".
738
739"ghost fields" are fields that are unused in certain types, and
740consequently dont need to actually exist. They are declared because
741they're part of a "base type", which allows use of functions as
742methods. The simplest examples are AVs and HVs, 2 aggregate types
743which don't use the fields which support SCALAR semantics.
744
745For these types, the arenas are carved up into *_allocated size
746chunks, we thus avoid wasted memory for those unaccessed members.
747When bodies are allocated, we adjust the pointer back in memory by the
748size of the bit not allocated, so it's as if we allocated the full
749structure. (But things will all go boom if you write to the part that
750is "not there", because you'll be overwriting the last members of the
751preceding structure in memory.)
752
753We calculate the correction using the STRUCT_OFFSET macro. For
754example, if xpv_allocated is the same structure as XPV then the two
755OFFSETs sum to zero, and the pointer is unchanged. If the allocated
756structure is smaller (no initial NV actually allocated) then the net
757effect is to subtract the size of the NV from the pointer, to return a
758new pointer as if an initial NV were actually allocated.
759
760This is the same trick as was used for NV and IV bodies. Ironically it
761doesn't need to be used for NV bodies any more, because NV is now at
762the start of the structure. IV bodies don't need it either, because
763they are no longer allocated.
764
765In turn, the new_body_* allocators call S_new_body(), which invokes
766new_body_inline macro, which takes a lock, and takes a body off the
767linked list at PL_body_roots[sv_type], calling S_more_bodies() if
768necessary to refresh an empty list. Then the lock is released, and
769the body is returned.
770
771S_more_bodies calls get_arena(), and carves it up into an array of N
772bodies, which it strings into a linked list. It looks up arena-size
773and body-size from the body_details table described below, thus
774supporting the multiple body-types.
775
776If PURIFY is defined, or PERL_ARENA_SIZE=0, arenas are not used, and
777the (new|del)_X*V macros are mapped directly to malloc/free.
778
779*/
780
781/*
782
783For each sv-type, struct body_details bodies_by_type[] carries
784parameters which control these aspects of SV handling:
785
786Arena_size determines whether arenas are used for this body type, and if
787so, how big they are. PURIFY or PERL_ARENA_SIZE=0 set this field to
788zero, forcing individual mallocs and frees.
789
790Body_size determines how big a body is, and therefore how many fit into
791each arena. Offset carries the body-pointer adjustment needed for
792*_allocated body types, and is used in *_allocated macros.
793
794But its main purpose is to parameterize info needed in
795Perl_sv_upgrade(). The info here dramatically simplifies the function
796vs the implementation in 5.8.7, making it table-driven. All fields
797are used for this, except for arena_size.
798
799For the sv-types that have no bodies, arenas are not used, so those
800PL_body_roots[sv_type] are unused, and can be overloaded. In
801something of a special case, SVt_NULL is borrowed for HE arenas;
802PL_body_roots[SVt_NULL] is filled by S_more_he, but the
803bodies_by_type[SVt_NULL] slot is not used, as the table is not
804available in hv.c,
805
806PTEs also use arenas, but are never seen in Perl_sv_upgrade.
807Nonetheless, they get their own slot in bodies_by_type[SVt_NULL], so
808they can just use the same allocation semantics. At first, PTEs were
809also overloaded to a non-body sv-type, but this yielded hard-to-find
810malloc bugs, so was simplified by claiming a new slot. This choice
811has no consequence at this time.
812
29489e7c
DM
813*/
814
bd81e77b 815struct body_details {
0fb58b32 816 U8 body_size; /* Size to allocate */
10666ae3 817 U8 copy; /* Size of structure to copy (may be shorter) */
0fb58b32 818 U8 offset;
10666ae3
NC
819 unsigned int type : 4; /* We have space for a sanity check. */
820 unsigned int cant_upgrade : 1; /* Cannot upgrade this type */
821 unsigned int zero_nv : 1; /* zero the NV when upgrading from this */
822 unsigned int arena : 1; /* Allocated from an arena */
823 size_t arena_size; /* Size of arena to allocate */
bd81e77b 824};
29489e7c 825
bd81e77b
NC
826#define HADNV FALSE
827#define NONV TRUE
29489e7c 828
d2a0f284 829
bd81e77b
NC
830#ifdef PURIFY
831/* With -DPURFIY we allocate everything directly, and don't use arenas.
832 This seems a rather elegant way to simplify some of the code below. */
833#define HASARENA FALSE
834#else
835#define HASARENA TRUE
836#endif
837#define NOARENA FALSE
29489e7c 838
d2a0f284
JC
839/* Size the arenas to exactly fit a given number of bodies. A count
840 of 0 fits the max number bodies into a PERL_ARENA_SIZE.block,
841 simplifying the default. If count > 0, the arena is sized to fit
842 only that many bodies, allowing arenas to be used for large, rare
843 bodies (XPVFM, XPVIO) without undue waste. The arena size is
844 limited by PERL_ARENA_SIZE, so we can safely oversize the
845 declarations.
846 */
95db5f15
MB
847#define FIT_ARENA0(body_size) \
848 ((size_t)(PERL_ARENA_SIZE / body_size) * body_size)
849#define FIT_ARENAn(count,body_size) \
850 ( count * body_size <= PERL_ARENA_SIZE) \
851 ? count * body_size \
852 : FIT_ARENA0 (body_size)
853#define FIT_ARENA(count,body_size) \
854 count \
855 ? FIT_ARENAn (count, body_size) \
856 : FIT_ARENA0 (body_size)
d2a0f284 857
bd81e77b 858/* A macro to work out the offset needed to subtract from a pointer to (say)
29489e7c 859
bd81e77b
NC
860typedef struct {
861 STRLEN xpv_cur;
862 STRLEN xpv_len;
863} xpv_allocated;
29489e7c 864
bd81e77b 865to make its members accessible via a pointer to (say)
29489e7c 866
bd81e77b
NC
867struct xpv {
868 NV xnv_nv;
869 STRLEN xpv_cur;
870 STRLEN xpv_len;
871};
29489e7c 872
bd81e77b 873*/
29489e7c 874
bd81e77b
NC
875#define relative_STRUCT_OFFSET(longer, shorter, member) \
876 (STRUCT_OFFSET(shorter, member) - STRUCT_OFFSET(longer, member))
29489e7c 877
bd81e77b
NC
878/* Calculate the length to copy. Specifically work out the length less any
879 final padding the compiler needed to add. See the comment in sv_upgrade
880 for why copying the padding proved to be a bug. */
29489e7c 881
bd81e77b
NC
882#define copy_length(type, last_member) \
883 STRUCT_OFFSET(type, last_member) \
884 + sizeof (((type*)SvANY((SV*)0))->last_member)
29489e7c 885
bd81e77b 886static const struct body_details bodies_by_type[] = {
10666ae3
NC
887 { sizeof(HE), 0, 0, SVt_NULL,
888 FALSE, NONV, NOARENA, FIT_ARENA(0, sizeof(HE)) },
d2a0f284
JC
889
890 /* IVs are in the head, so the allocation size is 0.
891 However, the slot is overloaded for PTEs. */
892 { sizeof(struct ptr_tbl_ent), /* This is used for PTEs. */
893 sizeof(IV), /* This is used to copy out the IV body. */
10666ae3 894 STRUCT_OFFSET(XPVIV, xiv_iv), SVt_IV, FALSE, NONV,
d2a0f284
JC
895 NOARENA /* IVS don't need an arena */,
896 /* But PTEs need to know the size of their arena */
897 FIT_ARENA(0, sizeof(struct ptr_tbl_ent))
898 },
899
bd81e77b 900 /* 8 bytes on most ILP32 with IEEE doubles */
10666ae3 901 { sizeof(NV), sizeof(NV), 0, SVt_NV, FALSE, HADNV, HASARENA,
d2a0f284
JC
902 FIT_ARENA(0, sizeof(NV)) },
903
904 /* RVs are in the head now. */
10666ae3 905 { 0, 0, 0, SVt_RV, FALSE, NONV, NOARENA, 0 },
d2a0f284 906
bd81e77b 907 /* 8 bytes on most ILP32 with IEEE doubles */
d2a0f284
JC
908 { sizeof(xpv_allocated),
909 copy_length(XPV, xpv_len)
910 - relative_STRUCT_OFFSET(xpv_allocated, XPV, xpv_cur),
911 + relative_STRUCT_OFFSET(xpv_allocated, XPV, xpv_cur),
10666ae3 912 SVt_PV, FALSE, NONV, HASARENA, FIT_ARENA(0, sizeof(xpv_allocated)) },
d2a0f284 913
bd81e77b 914 /* 12 */
d2a0f284
JC
915 { sizeof(xpviv_allocated),
916 copy_length(XPVIV, xiv_u)
917 - relative_STRUCT_OFFSET(xpviv_allocated, XPVIV, xpv_cur),
918 + relative_STRUCT_OFFSET(xpviv_allocated, XPVIV, xpv_cur),
10666ae3 919 SVt_PVIV, FALSE, NONV, HASARENA, FIT_ARENA(0, sizeof(xpviv_allocated)) },
d2a0f284 920
bd81e77b 921 /* 20 */
10666ae3 922 { sizeof(XPVNV), copy_length(XPVNV, xiv_u), 0, SVt_PVNV, FALSE, HADNV,
d2a0f284
JC
923 HASARENA, FIT_ARENA(0, sizeof(XPVNV)) },
924
bd81e77b 925 /* 28 */
10666ae3 926 { sizeof(XPVMG), copy_length(XPVMG, xmg_stash), 0, SVt_PVMG, FALSE, HADNV,
d2a0f284
JC
927 HASARENA, FIT_ARENA(0, sizeof(XPVMG)) },
928
bd81e77b 929 /* 36 */
10666ae3 930 { sizeof(XPVBM), sizeof(XPVBM), 0, SVt_PVBM, TRUE, HADNV,
d2a0f284
JC
931 HASARENA, FIT_ARENA(0, sizeof(XPVBM)) },
932
bd81e77b 933 /* 48 */
10666ae3 934 { sizeof(XPVGV), sizeof(XPVGV), 0, SVt_PVGV, TRUE, HADNV,
d2a0f284
JC
935 HASARENA, FIT_ARENA(0, sizeof(XPVGV)) },
936
bd81e77b 937 /* 64 */
10666ae3 938 { sizeof(XPVLV), sizeof(XPVLV), 0, SVt_PVLV, TRUE, HADNV,
d2a0f284
JC
939 HASARENA, FIT_ARENA(0, sizeof(XPVLV)) },
940
941 { sizeof(xpvav_allocated),
942 copy_length(XPVAV, xmg_stash)
943 - relative_STRUCT_OFFSET(xpvav_allocated, XPVAV, xav_fill),
944 + relative_STRUCT_OFFSET(xpvav_allocated, XPVAV, xav_fill),
10666ae3 945 SVt_PVAV, TRUE, HADNV, HASARENA, FIT_ARENA(0, sizeof(xpvav_allocated)) },
d2a0f284
JC
946
947 { sizeof(xpvhv_allocated),
948 copy_length(XPVHV, xmg_stash)
949 - relative_STRUCT_OFFSET(xpvhv_allocated, XPVHV, xhv_fill),
950 + relative_STRUCT_OFFSET(xpvhv_allocated, XPVHV, xhv_fill),
10666ae3 951 SVt_PVHV, TRUE, HADNV, HASARENA, FIT_ARENA(0, sizeof(xpvhv_allocated)) },
d2a0f284 952
c84c4652 953 /* 56 */
4115f141 954 { sizeof(xpvcv_allocated), sizeof(xpvcv_allocated),
c84c4652 955 + relative_STRUCT_OFFSET(xpvcv_allocated, XPVCV, xpv_cur),
10666ae3 956 SVt_PVCV, TRUE, NONV, HASARENA, FIT_ARENA(0, sizeof(xpvcv_allocated)) },
d2a0f284 957
4115f141 958 { sizeof(xpvfm_allocated), sizeof(xpvfm_allocated),
3038937b 959 + relative_STRUCT_OFFSET(xpvfm_allocated, XPVFM, xpv_cur),
10666ae3 960 SVt_PVFM, TRUE, NONV, NOARENA, FIT_ARENA(20, sizeof(xpvfm_allocated)) },
d2a0f284
JC
961
962 /* XPVIO is 84 bytes, fits 48x */
10666ae3 963 { sizeof(XPVIO), sizeof(XPVIO), 0, SVt_PVIO, TRUE, HADNV,
d2a0f284 964 HASARENA, FIT_ARENA(24, sizeof(XPVIO)) },
bd81e77b 965};
29489e7c 966
d2a0f284
JC
967#define new_body_type(sv_type) \
968 (void *)((char *)S_new_body(aTHX_ sv_type))
29489e7c 969
bd81e77b
NC
970#define del_body_type(p, sv_type) \
971 del_body(p, &PL_body_roots[sv_type])
29489e7c 972
29489e7c 973
bd81e77b 974#define new_body_allocated(sv_type) \
d2a0f284 975 (void *)((char *)S_new_body(aTHX_ sv_type) \
bd81e77b 976 - bodies_by_type[sv_type].offset)
29489e7c 977
bd81e77b
NC
978#define del_body_allocated(p, sv_type) \
979 del_body(p + bodies_by_type[sv_type].offset, &PL_body_roots[sv_type])
29489e7c 980
29489e7c 981
bd81e77b
NC
982#define my_safemalloc(s) (void*)safemalloc(s)
983#define my_safecalloc(s) (void*)safecalloc(s, 1)
984#define my_safefree(p) safefree((char*)p)
29489e7c 985
bd81e77b 986#ifdef PURIFY
29489e7c 987
bd81e77b
NC
988#define new_XNV() my_safemalloc(sizeof(XPVNV))
989#define del_XNV(p) my_safefree(p)
29489e7c 990
bd81e77b
NC
991#define new_XPVNV() my_safemalloc(sizeof(XPVNV))
992#define del_XPVNV(p) my_safefree(p)
29489e7c 993
bd81e77b
NC
994#define new_XPVAV() my_safemalloc(sizeof(XPVAV))
995#define del_XPVAV(p) my_safefree(p)
29489e7c 996
bd81e77b
NC
997#define new_XPVHV() my_safemalloc(sizeof(XPVHV))
998#define del_XPVHV(p) my_safefree(p)
29489e7c 999
bd81e77b
NC
1000#define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1001#define del_XPVMG(p) my_safefree(p)
29489e7c 1002
bd81e77b
NC
1003#define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1004#define del_XPVGV(p) my_safefree(p)
29489e7c 1005
bd81e77b 1006#else /* !PURIFY */
29489e7c 1007
bd81e77b
NC
1008#define new_XNV() new_body_type(SVt_NV)
1009#define del_XNV(p) del_body_type(p, SVt_NV)
29489e7c 1010
bd81e77b
NC
1011#define new_XPVNV() new_body_type(SVt_PVNV)
1012#define del_XPVNV(p) del_body_type(p, SVt_PVNV)
29489e7c 1013
bd81e77b
NC
1014#define new_XPVAV() new_body_allocated(SVt_PVAV)
1015#define del_XPVAV(p) del_body_allocated(p, SVt_PVAV)
645c22ef 1016
bd81e77b
NC
1017#define new_XPVHV() new_body_allocated(SVt_PVHV)
1018#define del_XPVHV(p) del_body_allocated(p, SVt_PVHV)
645c22ef 1019
bd81e77b
NC
1020#define new_XPVMG() new_body_type(SVt_PVMG)
1021#define del_XPVMG(p) del_body_type(p, SVt_PVMG)
645c22ef 1022
bd81e77b
NC
1023#define new_XPVGV() new_body_type(SVt_PVGV)
1024#define del_XPVGV(p) del_body_type(p, SVt_PVGV)
1d7c1841 1025
bd81e77b 1026#endif /* PURIFY */
93e68bfb 1027
bd81e77b 1028/* no arena for you! */
93e68bfb 1029
bd81e77b 1030#define new_NOARENA(details) \
d2a0f284 1031 my_safemalloc((details)->body_size + (details)->offset)
bd81e77b 1032#define new_NOARENAZ(details) \
d2a0f284
JC
1033 my_safecalloc((details)->body_size + (details)->offset)
1034
10666ae3
NC
1035#ifdef DEBUGGING
1036static bool done_sanity_check;
1037#endif
1038
d2a0f284
JC
1039STATIC void *
1040S_more_bodies (pTHX_ svtype sv_type)
1041{
1042 dVAR;
1043 void ** const root = &PL_body_roots[sv_type];
96a5add6 1044 const struct body_details * const bdp = &bodies_by_type[sv_type];
d2a0f284
JC
1045 const size_t body_size = bdp->body_size;
1046 char *start;
1047 const char *end;
1048
1049 assert(bdp->arena_size);
10666ae3
NC
1050
1051#ifdef DEBUGGING
1052 if (!done_sanity_check) {
ea471437 1053 unsigned int i = SVt_LAST;
10666ae3
NC
1054
1055 done_sanity_check = TRUE;
1056
1057 while (i--)
1058 assert (bodies_by_type[i].type == i);
1059 }
1060#endif
1061
d2a0f284
JC
1062 start = (char*) Perl_get_arena(aTHX_ bdp->arena_size);
1063
1064 end = start + bdp->arena_size - body_size;
1065
d2a0f284
JC
1066 /* computed count doesnt reflect the 1st slot reservation */
1067 DEBUG_m(PerlIO_printf(Perl_debug_log,
1068 "arena %p end %p arena-size %d type %d size %d ct %d\n",
1069 start, end, bdp->arena_size, sv_type, body_size,
1070 bdp->arena_size / body_size));
d2a0f284
JC
1071
1072 *root = (void *)start;
1073
1074 while (start < end) {
1075 char * const next = start + body_size;
1076 *(void**) start = (void *)next;
1077 start = next;
1078 }
1079 *(void **)start = 0;
1080
1081 return *root;
1082}
1083
1084/* grab a new thing from the free list, allocating more if necessary.
1085 The inline version is used for speed in hot routines, and the
1086 function using it serves the rest (unless PURIFY).
1087*/
1088#define new_body_inline(xpv, sv_type) \
1089 STMT_START { \
1090 void ** const r3wt = &PL_body_roots[sv_type]; \
1091 LOCK_SV_MUTEX; \
1092 xpv = *((void **)(r3wt)) \
d4c19fe8 1093 ? *((void **)(r3wt)) : more_bodies(sv_type); \
d2a0f284
JC
1094 *(r3wt) = *(void**)(xpv); \
1095 UNLOCK_SV_MUTEX; \
1096 } STMT_END
1097
1098#ifndef PURIFY
1099
1100STATIC void *
1101S_new_body(pTHX_ svtype sv_type)
1102{
1103 dVAR;
1104 void *xpv;
1105 new_body_inline(xpv, sv_type);
1106 return xpv;
1107}
1108
1109#endif
93e68bfb 1110
bd81e77b
NC
1111/*
1112=for apidoc sv_upgrade
93e68bfb 1113
bd81e77b
NC
1114Upgrade an SV to a more complex form. Generally adds a new body type to the
1115SV, then copies across as much information as possible from the old body.
1116You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
93e68bfb 1117
bd81e77b 1118=cut
93e68bfb 1119*/
93e68bfb 1120
bd81e77b
NC
1121void
1122Perl_sv_upgrade(pTHX_ register SV *sv, U32 new_type)
cac9b346 1123{
97aff369 1124 dVAR;
bd81e77b
NC
1125 void* old_body;
1126 void* new_body;
1127 const U32 old_type = SvTYPE(sv);
d2a0f284 1128 const struct body_details *new_type_details;
bd81e77b
NC
1129 const struct body_details *const old_type_details
1130 = bodies_by_type + old_type;
cac9b346 1131
bd81e77b
NC
1132 if (new_type != SVt_PV && SvIsCOW(sv)) {
1133 sv_force_normal_flags(sv, 0);
1134 }
cac9b346 1135
bd81e77b
NC
1136 if (old_type == new_type)
1137 return;
cac9b346 1138
bd81e77b
NC
1139 if (old_type > new_type)
1140 Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
1141 (int)old_type, (int)new_type);
cac9b346 1142
cac9b346 1143
bd81e77b 1144 old_body = SvANY(sv);
de042e1d 1145
bd81e77b
NC
1146 /* Copying structures onto other structures that have been neatly zeroed
1147 has a subtle gotcha. Consider XPVMG
cac9b346 1148
bd81e77b
NC
1149 +------+------+------+------+------+-------+-------+
1150 | NV | CUR | LEN | IV | MAGIC | STASH |
1151 +------+------+------+------+------+-------+-------+
1152 0 4 8 12 16 20 24 28
645c22ef 1153
bd81e77b
NC
1154 where NVs are aligned to 8 bytes, so that sizeof that structure is
1155 actually 32 bytes long, with 4 bytes of padding at the end:
08742458 1156
bd81e77b
NC
1157 +------+------+------+------+------+-------+-------+------+
1158 | NV | CUR | LEN | IV | MAGIC | STASH | ??? |
1159 +------+------+------+------+------+-------+-------+------+
1160 0 4 8 12 16 20 24 28 32
08742458 1161
bd81e77b 1162 so what happens if you allocate memory for this structure:
30f9da9e 1163
bd81e77b
NC
1164 +------+------+------+------+------+-------+-------+------+------+...
1165 | NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME |
1166 +------+------+------+------+------+-------+-------+------+------+...
1167 0 4 8 12 16 20 24 28 32 36
bfc44f79 1168
bd81e77b
NC
1169 zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
1170 expect, because you copy the area marked ??? onto GP. Now, ??? may have
1171 started out as zero once, but it's quite possible that it isn't. So now,
1172 rather than a nicely zeroed GP, you have it pointing somewhere random.
1173 Bugs ensue.
bfc44f79 1174
bd81e77b
NC
1175 (In fact, GP ends up pointing at a previous GP structure, because the
1176 principle cause of the padding in XPVMG getting garbage is a copy of
1177 sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob)
30f9da9e 1178
bd81e77b
NC
1179 So we are careful and work out the size of used parts of all the
1180 structures. */
bfc44f79 1181
bd81e77b
NC
1182 switch (old_type) {
1183 case SVt_NULL:
1184 break;
1185 case SVt_IV:
1186 if (new_type < SVt_PVIV) {
1187 new_type = (new_type == SVt_NV)
1188 ? SVt_PVNV : SVt_PVIV;
bd81e77b
NC
1189 }
1190 break;
1191 case SVt_NV:
1192 if (new_type < SVt_PVNV) {
1193 new_type = SVt_PVNV;
bd81e77b
NC
1194 }
1195 break;
1196 case SVt_RV:
1197 break;
1198 case SVt_PV:
1199 assert(new_type > SVt_PV);
1200 assert(SVt_IV < SVt_PV);
1201 assert(SVt_NV < SVt_PV);
1202 break;
1203 case SVt_PVIV:
1204 break;
1205 case SVt_PVNV:
1206 break;
1207 case SVt_PVMG:
1208 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1209 there's no way that it can be safely upgraded, because perl.c
1210 expects to Safefree(SvANY(PL_mess_sv)) */
1211 assert(sv != PL_mess_sv);
1212 /* This flag bit is used to mean other things in other scalar types.
1213 Given that it only has meaning inside the pad, it shouldn't be set
1214 on anything that can get upgraded. */
00b1698f 1215 assert(!SvPAD_TYPED(sv));
bd81e77b
NC
1216 break;
1217 default:
1218 if (old_type_details->cant_upgrade)
c81225bc
NC
1219 Perl_croak(aTHX_ "Can't upgrade %s (%" UVuf ") to %" UVuf,
1220 sv_reftype(sv, 0), (UV) old_type, (UV) new_type);
bd81e77b 1221 }
2fa1109b 1222 new_type_details = bodies_by_type + new_type;
645c22ef 1223
bd81e77b
NC
1224 SvFLAGS(sv) &= ~SVTYPEMASK;
1225 SvFLAGS(sv) |= new_type;
932e9ff9 1226
ab4416c0
NC
1227 /* This can't happen, as SVt_NULL is <= all values of new_type, so one of
1228 the return statements above will have triggered. */
1229 assert (new_type != SVt_NULL);
bd81e77b 1230 switch (new_type) {
bd81e77b
NC
1231 case SVt_IV:
1232 assert(old_type == SVt_NULL);
1233 SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
1234 SvIV_set(sv, 0);
1235 return;
1236 case SVt_NV:
1237 assert(old_type == SVt_NULL);
1238 SvANY(sv) = new_XNV();
1239 SvNV_set(sv, 0);
1240 return;
1241 case SVt_RV:
1242 assert(old_type == SVt_NULL);
1243 SvANY(sv) = &sv->sv_u.svu_rv;
1244 SvRV_set(sv, 0);
1245 return;
1246 case SVt_PVHV:
bd81e77b 1247 case SVt_PVAV:
d2a0f284 1248 assert(new_type_details->body_size);
c1ae03ae
NC
1249
1250#ifndef PURIFY
1251 assert(new_type_details->arena);
d2a0f284 1252 assert(new_type_details->arena_size);
c1ae03ae 1253 /* This points to the start of the allocated area. */
d2a0f284
JC
1254 new_body_inline(new_body, new_type);
1255 Zero(new_body, new_type_details->body_size, char);
c1ae03ae
NC
1256 new_body = ((char *)new_body) - new_type_details->offset;
1257#else
1258 /* We always allocated the full length item with PURIFY. To do this
1259 we fake things so that arena is false for all 16 types.. */
1260 new_body = new_NOARENAZ(new_type_details);
1261#endif
1262 SvANY(sv) = new_body;
1263 if (new_type == SVt_PVAV) {
1264 AvMAX(sv) = -1;
1265 AvFILLp(sv) = -1;
1266 AvREAL_only(sv);
1267 }
aeb18a1e 1268
bd81e77b
NC
1269 /* SVt_NULL isn't the only thing upgraded to AV or HV.
1270 The target created by newSVrv also is, and it can have magic.
1271 However, it never has SvPVX set.
1272 */
1273 if (old_type >= SVt_RV) {
1274 assert(SvPVX_const(sv) == 0);
1275 }
aeb18a1e 1276
bd81e77b
NC
1277 /* Could put this in the else clause below, as PVMG must have SvPVX
1278 0 already (the assertion above) */
6136c704 1279 SvPV_set(sv, NULL);
93e68bfb 1280
bd81e77b 1281 if (old_type >= SVt_PVMG) {
e736a858 1282 SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_u.xmg_magic);
bd81e77b 1283 SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
bd81e77b
NC
1284 }
1285 break;
93e68bfb 1286
93e68bfb 1287
bd81e77b
NC
1288 case SVt_PVIV:
1289 /* XXX Is this still needed? Was it ever needed? Surely as there is
1290 no route from NV to PVIV, NOK can never be true */
1291 assert(!SvNOKp(sv));
1292 assert(!SvNOK(sv));
1293 case SVt_PVIO:
1294 case SVt_PVFM:
1295 case SVt_PVBM:
1296 case SVt_PVGV:
1297 case SVt_PVCV:
1298 case SVt_PVLV:
1299 case SVt_PVMG:
1300 case SVt_PVNV:
1301 case SVt_PV:
93e68bfb 1302
d2a0f284 1303 assert(new_type_details->body_size);
bd81e77b
NC
1304 /* We always allocated the full length item with PURIFY. To do this
1305 we fake things so that arena is false for all 16 types.. */
1306 if(new_type_details->arena) {
1307 /* This points to the start of the allocated area. */
d2a0f284
JC
1308 new_body_inline(new_body, new_type);
1309 Zero(new_body, new_type_details->body_size, char);
bd81e77b
NC
1310 new_body = ((char *)new_body) - new_type_details->offset;
1311 } else {
1312 new_body = new_NOARENAZ(new_type_details);
1313 }
1314 SvANY(sv) = new_body;
5e2fc214 1315
bd81e77b 1316 if (old_type_details->copy) {
f9ba3d20
NC
1317 /* There is now the potential for an upgrade from something without
1318 an offset (PVNV or PVMG) to something with one (PVCV, PVFM) */
1319 int offset = old_type_details->offset;
1320 int length = old_type_details->copy;
1321
1322 if (new_type_details->offset > old_type_details->offset) {
d4c19fe8 1323 const int difference
f9ba3d20
NC
1324 = new_type_details->offset - old_type_details->offset;
1325 offset += difference;
1326 length -= difference;
1327 }
1328 assert (length >= 0);
1329
1330 Copy((char *)old_body + offset, (char *)new_body + offset, length,
1331 char);
bd81e77b
NC
1332 }
1333
1334#ifndef NV_ZERO_IS_ALLBITS_ZERO
f2524eef 1335 /* If NV 0.0 is stores as all bits 0 then Zero() already creates a
e5ce394c
NC
1336 * correct 0.0 for us. Otherwise, if the old body didn't have an
1337 * NV slot, but the new one does, then we need to initialise the
1338 * freshly created NV slot with whatever the correct bit pattern is
1339 * for 0.0 */
1340 if (old_type_details->zero_nv && !new_type_details->zero_nv)
bd81e77b 1341 SvNV_set(sv, 0);
82048762 1342#endif
5e2fc214 1343
bd81e77b 1344 if (new_type == SVt_PVIO)
f2524eef 1345 IoPAGE_LEN(sv) = 60;
bd81e77b 1346 if (old_type < SVt_RV)
6136c704 1347 SvPV_set(sv, NULL);
bd81e77b
NC
1348 break;
1349 default:
afd78fd5
JH
1350 Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu",
1351 (unsigned long)new_type);
bd81e77b 1352 }
73171d91 1353
d2a0f284
JC
1354 if (old_type_details->arena) {
1355 /* If there was an old body, then we need to free it.
1356 Note that there is an assumption that all bodies of types that
1357 can be upgraded came from arenas. Only the more complex non-
1358 upgradable types are allowed to be directly malloc()ed. */
bd81e77b
NC
1359#ifdef PURIFY
1360 my_safefree(old_body);
1361#else
1362 del_body((void*)((char*)old_body + old_type_details->offset),
1363 &PL_body_roots[old_type]);
1364#endif
1365 }
1366}
73171d91 1367
bd81e77b
NC
1368/*
1369=for apidoc sv_backoff
73171d91 1370
bd81e77b
NC
1371Remove any string offset. You should normally use the C<SvOOK_off> macro
1372wrapper instead.
73171d91 1373
bd81e77b 1374=cut
73171d91
NC
1375*/
1376
bd81e77b
NC
1377int
1378Perl_sv_backoff(pTHX_ register SV *sv)
1379{
96a5add6 1380 PERL_UNUSED_CONTEXT;
bd81e77b
NC
1381 assert(SvOOK(sv));
1382 assert(SvTYPE(sv) != SVt_PVHV);
1383 assert(SvTYPE(sv) != SVt_PVAV);
1384 if (SvIVX(sv)) {
1385 const char * const s = SvPVX_const(sv);
1386 SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
1387 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
1388 SvIV_set(sv, 0);
1389 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1390 }
1391 SvFLAGS(sv) &= ~SVf_OOK;
1392 return 0;
1393}
73171d91 1394
bd81e77b
NC
1395/*
1396=for apidoc sv_grow
73171d91 1397
bd81e77b
NC
1398Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1399upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1400Use the C<SvGROW> wrapper instead.
93e68bfb 1401
bd81e77b
NC
1402=cut
1403*/
93e68bfb 1404
bd81e77b
NC
1405char *
1406Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
1407{
1408 register char *s;
93e68bfb 1409
5db06880
NC
1410 if (PL_madskills && newlen >= 0x100000) {
1411 PerlIO_printf(Perl_debug_log,
1412 "Allocation too large: %"UVxf"\n", (UV)newlen);
1413 }
bd81e77b
NC
1414#ifdef HAS_64K_LIMIT
1415 if (newlen >= 0x10000) {
1416 PerlIO_printf(Perl_debug_log,
1417 "Allocation too large: %"UVxf"\n", (UV)newlen);
1418 my_exit(1);
1419 }
1420#endif /* HAS_64K_LIMIT */
1421 if (SvROK(sv))
1422 sv_unref(sv);
1423 if (SvTYPE(sv) < SVt_PV) {
1424 sv_upgrade(sv, SVt_PV);
1425 s = SvPVX_mutable(sv);
1426 }
1427 else if (SvOOK(sv)) { /* pv is offset? */
1428 sv_backoff(sv);
1429 s = SvPVX_mutable(sv);
1430 if (newlen > SvLEN(sv))
1431 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1432#ifdef HAS_64K_LIMIT
1433 if (newlen >= 0x10000)
1434 newlen = 0xFFFF;
1435#endif
1436 }
1437 else
1438 s = SvPVX_mutable(sv);
aeb18a1e 1439
bd81e77b
NC
1440 if (newlen > SvLEN(sv)) { /* need more room? */
1441 newlen = PERL_STRLEN_ROUNDUP(newlen);
1442 if (SvLEN(sv) && s) {
1443#ifdef MYMALLOC
1444 const STRLEN l = malloced_size((void*)SvPVX_const(sv));
1445 if (newlen <= l) {
1446 SvLEN_set(sv, l);
1447 return s;
1448 } else
1449#endif
1450 s = saferealloc(s, newlen);
1451 }
1452 else {
1453 s = safemalloc(newlen);
1454 if (SvPVX_const(sv) && SvCUR(sv)) {
1455 Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
1456 }
1457 }
1458 SvPV_set(sv, s);
1459 SvLEN_set(sv, newlen);
1460 }
1461 return s;
1462}
aeb18a1e 1463
bd81e77b
NC
1464/*
1465=for apidoc sv_setiv
932e9ff9 1466
bd81e77b
NC
1467Copies an integer into the given SV, upgrading first if necessary.
1468Does not handle 'set' magic. See also C<sv_setiv_mg>.
463ee0b2 1469
bd81e77b
NC
1470=cut
1471*/
463ee0b2 1472
bd81e77b
NC
1473void
1474Perl_sv_setiv(pTHX_ register SV *sv, IV i)
1475{
97aff369 1476 dVAR;
bd81e77b
NC
1477 SV_CHECK_THINKFIRST_COW_DROP(sv);
1478 switch (SvTYPE(sv)) {
1479 case SVt_NULL:
1480 sv_upgrade(sv, SVt_IV);
1481 break;
1482 case SVt_NV:
1483 sv_upgrade(sv, SVt_PVNV);
1484 break;
1485 case SVt_RV:
1486 case SVt_PV:
1487 sv_upgrade(sv, SVt_PVIV);
1488 break;
463ee0b2 1489
bd81e77b
NC
1490 case SVt_PVGV:
1491 case SVt_PVAV:
1492 case SVt_PVHV:
1493 case SVt_PVCV:
1494 case SVt_PVFM:
1495 case SVt_PVIO:
1496 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1497 OP_DESC(PL_op));
1498 }
1499 (void)SvIOK_only(sv); /* validate number */
1500 SvIV_set(sv, i);
1501 SvTAINT(sv);
1502}
932e9ff9 1503
bd81e77b
NC
1504/*
1505=for apidoc sv_setiv_mg
d33b2eba 1506
bd81e77b 1507Like C<sv_setiv>, but also handles 'set' magic.
1c846c1f 1508
bd81e77b
NC
1509=cut
1510*/
d33b2eba 1511
bd81e77b
NC
1512void
1513Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
1514{
1515 sv_setiv(sv,i);
1516 SvSETMAGIC(sv);
1517}
727879eb 1518
bd81e77b
NC
1519/*
1520=for apidoc sv_setuv
d33b2eba 1521
bd81e77b
NC
1522Copies an unsigned integer into the given SV, upgrading first if necessary.
1523Does not handle 'set' magic. See also C<sv_setuv_mg>.
9b94d1dd 1524
bd81e77b
NC
1525=cut
1526*/
d33b2eba 1527
bd81e77b
NC
1528void
1529Perl_sv_setuv(pTHX_ register SV *sv, UV u)
1530{
1531 /* With these two if statements:
1532 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d33b2eba 1533
bd81e77b
NC
1534 without
1535 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1c846c1f 1536
bd81e77b
NC
1537 If you wish to remove them, please benchmark to see what the effect is
1538 */
1539 if (u <= (UV)IV_MAX) {
1540 sv_setiv(sv, (IV)u);
1541 return;
1542 }
1543 sv_setiv(sv, 0);
1544 SvIsUV_on(sv);
1545 SvUV_set(sv, u);
1546}
d33b2eba 1547
bd81e77b
NC
1548/*
1549=for apidoc sv_setuv_mg
727879eb 1550
bd81e77b 1551Like C<sv_setuv>, but also handles 'set' magic.
9b94d1dd 1552
bd81e77b
NC
1553=cut
1554*/
5e2fc214 1555
bd81e77b
NC
1556void
1557Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
1558{
1559 sv_setiv(sv, 0);
1560 SvIsUV_on(sv);
1561 sv_setuv(sv,u);
1562 SvSETMAGIC(sv);
1563}
5e2fc214 1564
954c1994 1565/*
bd81e77b 1566=for apidoc sv_setnv
954c1994 1567
bd81e77b
NC
1568Copies a double into the given SV, upgrading first if necessary.
1569Does not handle 'set' magic. See also C<sv_setnv_mg>.
954c1994
GS
1570
1571=cut
1572*/
1573
63f97190 1574void
bd81e77b 1575Perl_sv_setnv(pTHX_ register SV *sv, NV num)
79072805 1576{
97aff369 1577 dVAR;
bd81e77b
NC
1578 SV_CHECK_THINKFIRST_COW_DROP(sv);
1579 switch (SvTYPE(sv)) {
79072805 1580 case SVt_NULL:
79072805 1581 case SVt_IV:
bd81e77b 1582 sv_upgrade(sv, SVt_NV);
79072805 1583 break;
ed6116ce 1584 case SVt_RV:
79072805 1585 case SVt_PV:
79072805 1586 case SVt_PVIV:
bd81e77b 1587 sv_upgrade(sv, SVt_PVNV);
79072805 1588 break;
bd4b1eb5 1589
bd4b1eb5 1590 case SVt_PVGV:
bd81e77b
NC
1591 case SVt_PVAV:
1592 case SVt_PVHV:
79072805 1593 case SVt_PVCV:
bd81e77b
NC
1594 case SVt_PVFM:
1595 case SVt_PVIO:
1596 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1597 OP_NAME(PL_op));
2068cd4d 1598 }
bd81e77b
NC
1599 SvNV_set(sv, num);
1600 (void)SvNOK_only(sv); /* validate number */
1601 SvTAINT(sv);
79072805
LW
1602}
1603
645c22ef 1604/*
bd81e77b 1605=for apidoc sv_setnv_mg
645c22ef 1606
bd81e77b 1607Like C<sv_setnv>, but also handles 'set' magic.
645c22ef
DM
1608
1609=cut
1610*/
1611
bd81e77b
NC
1612void
1613Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
79072805 1614{
bd81e77b
NC
1615 sv_setnv(sv,num);
1616 SvSETMAGIC(sv);
79072805
LW
1617}
1618
bd81e77b
NC
1619/* Print an "isn't numeric" warning, using a cleaned-up,
1620 * printable version of the offending string
1621 */
954c1994 1622
bd81e77b
NC
1623STATIC void
1624S_not_a_number(pTHX_ SV *sv)
79072805 1625{
97aff369 1626 dVAR;
bd81e77b
NC
1627 SV *dsv;
1628 char tmpbuf[64];
1629 const char *pv;
94463019
JH
1630
1631 if (DO_UTF8(sv)) {
396482e1 1632 dsv = sv_2mortal(newSVpvs(""));
94463019
JH
1633 pv = sv_uni_display(dsv, sv, 10, 0);
1634 } else {
1635 char *d = tmpbuf;
551405c4 1636 const char * const limit = tmpbuf + sizeof(tmpbuf) - 8;
94463019
JH
1637 /* each *s can expand to 4 chars + "...\0",
1638 i.e. need room for 8 chars */
ecdeb87c 1639
00b6aa41
AL
1640 const char *s = SvPVX_const(sv);
1641 const char * const end = s + SvCUR(sv);
1642 for ( ; s < end && d < limit; s++ ) {
94463019
JH
1643 int ch = *s & 0xFF;
1644 if (ch & 128 && !isPRINT_LC(ch)) {
1645 *d++ = 'M';
1646 *d++ = '-';
1647 ch &= 127;
1648 }
1649 if (ch == '\n') {
1650 *d++ = '\\';
1651 *d++ = 'n';
1652 }
1653 else if (ch == '\r') {
1654 *d++ = '\\';
1655 *d++ = 'r';
1656 }
1657 else if (ch == '\f') {
1658 *d++ = '\\';
1659 *d++ = 'f';
1660 }
1661 else if (ch == '\\') {
1662 *d++ = '\\';
1663 *d++ = '\\';
1664 }
1665 else if (ch == '\0') {
1666 *d++ = '\\';
1667 *d++ = '0';
1668 }
1669 else if (isPRINT_LC(ch))
1670 *d++ = ch;
1671 else {
1672 *d++ = '^';
1673 *d++ = toCTRL(ch);
1674 }
1675 }
1676 if (s < end) {
1677 *d++ = '.';
1678 *d++ = '.';
1679 *d++ = '.';
1680 }
1681 *d = '\0';
1682 pv = tmpbuf;
a0d0e21e 1683 }
a0d0e21e 1684
533c011a 1685 if (PL_op)
9014280d 1686 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019
JH
1687 "Argument \"%s\" isn't numeric in %s", pv,
1688 OP_DESC(PL_op));
a0d0e21e 1689 else
9014280d 1690 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019 1691 "Argument \"%s\" isn't numeric", pv);
a0d0e21e
LW
1692}
1693
c2988b20
NC
1694/*
1695=for apidoc looks_like_number
1696
645c22ef
DM
1697Test if the content of an SV looks like a number (or is a number).
1698C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1699non-numeric warning), even if your atof() doesn't grok them.
c2988b20
NC
1700
1701=cut
1702*/
1703
1704I32
1705Perl_looks_like_number(pTHX_ SV *sv)
1706{
a3b680e6 1707 register const char *sbegin;
c2988b20
NC
1708 STRLEN len;
1709
1710 if (SvPOK(sv)) {
3f7c398e 1711 sbegin = SvPVX_const(sv);
c2988b20
NC
1712 len = SvCUR(sv);
1713 }
1714 else if (SvPOKp(sv))
83003860 1715 sbegin = SvPV_const(sv, len);
c2988b20 1716 else
e0ab1c0e 1717 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
c2988b20
NC
1718 return grok_number(sbegin, len, NULL);
1719}
25da4f38 1720
19f6321d
NC
1721STATIC bool
1722S_glob_2number(pTHX_ GV * const gv)
180488f8
NC
1723{
1724 const U32 wasfake = SvFLAGS(gv) & SVf_FAKE;
1725 SV *const buffer = sv_newmortal();
1726
1727 /* FAKE globs can get coerced, so need to turn this off temporarily if it
1728 is on. */
1729 SvFAKE_off(gv);
1730 gv_efullname3(buffer, gv, "*");
1731 SvFLAGS(gv) |= wasfake;
1732
675c862f
AL
1733 /* We know that all GVs stringify to something that is not-a-number,
1734 so no need to test that. */
1735 if (ckWARN(WARN_NUMERIC))
1736 not_a_number(buffer);
1737 /* We just want something true to return, so that S_sv_2iuv_common
1738 can tail call us and return true. */
19f6321d 1739 return TRUE;
675c862f
AL
1740}
1741
1742STATIC char *
19f6321d 1743S_glob_2pv(pTHX_ GV * const gv, STRLEN * const len)
675c862f
AL
1744{
1745 const U32 wasfake = SvFLAGS(gv) & SVf_FAKE;
1746 SV *const buffer = sv_newmortal();
1747
1748 /* FAKE globs can get coerced, so need to turn this off temporarily if it
1749 is on. */
1750 SvFAKE_off(gv);
1751 gv_efullname3(buffer, gv, "*");
1752 SvFLAGS(gv) |= wasfake;
1753
1754 assert(SvPOK(buffer));
1755 *len = SvCUR(buffer);
1756 return SvPVX(buffer);
180488f8
NC
1757}
1758
25da4f38
IZ
1759/* Actually, ISO C leaves conversion of UV to IV undefined, but
1760 until proven guilty, assume that things are not that bad... */
1761
645c22ef
DM
1762/*
1763 NV_PRESERVES_UV:
1764
1765 As 64 bit platforms often have an NV that doesn't preserve all bits of
28e5dec8
JH
1766 an IV (an assumption perl has been based on to date) it becomes necessary
1767 to remove the assumption that the NV always carries enough precision to
1768 recreate the IV whenever needed, and that the NV is the canonical form.
1769 Instead, IV/UV and NV need to be given equal rights. So as to not lose
645c22ef 1770 precision as a side effect of conversion (which would lead to insanity
28e5dec8
JH
1771 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1772 1) to distinguish between IV/UV/NV slots that have cached a valid
1773 conversion where precision was lost and IV/UV/NV slots that have a
1774 valid conversion which has lost no precision
645c22ef 1775 2) to ensure that if a numeric conversion to one form is requested that
28e5dec8
JH
1776 would lose precision, the precise conversion (or differently
1777 imprecise conversion) is also performed and cached, to prevent
1778 requests for different numeric formats on the same SV causing
1779 lossy conversion chains. (lossless conversion chains are perfectly
1780 acceptable (still))
1781
1782
1783 flags are used:
1784 SvIOKp is true if the IV slot contains a valid value
1785 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1786 SvNOKp is true if the NV slot contains a valid value
1787 SvNOK is true only if the NV value is accurate
1788
1789 so
645c22ef 1790 while converting from PV to NV, check to see if converting that NV to an
28e5dec8
JH
1791 IV(or UV) would lose accuracy over a direct conversion from PV to
1792 IV(or UV). If it would, cache both conversions, return NV, but mark
1793 SV as IOK NOKp (ie not NOK).
1794
645c22ef 1795 While converting from PV to IV, check to see if converting that IV to an
28e5dec8
JH
1796 NV would lose accuracy over a direct conversion from PV to NV. If it
1797 would, cache both conversions, flag similarly.
1798
1799 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1800 correctly because if IV & NV were set NV *always* overruled.
645c22ef
DM
1801 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1802 changes - now IV and NV together means that the two are interchangeable:
28e5dec8 1803 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
d460ef45 1804
645c22ef
DM
1805 The benefit of this is that operations such as pp_add know that if
1806 SvIOK is true for both left and right operands, then integer addition
1807 can be used instead of floating point (for cases where the result won't
1808 overflow). Before, floating point was always used, which could lead to
28e5dec8
JH
1809 loss of precision compared with integer addition.
1810
1811 * making IV and NV equal status should make maths accurate on 64 bit
1812 platforms
1813 * may speed up maths somewhat if pp_add and friends start to use
645c22ef 1814 integers when possible instead of fp. (Hopefully the overhead in
28e5dec8
JH
1815 looking for SvIOK and checking for overflow will not outweigh the
1816 fp to integer speedup)
1817 * will slow down integer operations (callers of SvIV) on "inaccurate"
1818 values, as the change from SvIOK to SvIOKp will cause a call into
1819 sv_2iv each time rather than a macro access direct to the IV slot
1820 * should speed up number->string conversion on integers as IV is
645c22ef 1821 favoured when IV and NV are equally accurate
28e5dec8
JH
1822
1823 ####################################################################
645c22ef
DM
1824 You had better be using SvIOK_notUV if you want an IV for arithmetic:
1825 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1826 On the other hand, SvUOK is true iff UV.
28e5dec8
JH
1827 ####################################################################
1828
645c22ef 1829 Your mileage will vary depending your CPU's relative fp to integer
28e5dec8
JH
1830 performance ratio.
1831*/
1832
1833#ifndef NV_PRESERVES_UV
645c22ef
DM
1834# define IS_NUMBER_UNDERFLOW_IV 1
1835# define IS_NUMBER_UNDERFLOW_UV 2
1836# define IS_NUMBER_IV_AND_UV 2
1837# define IS_NUMBER_OVERFLOW_IV 4
1838# define IS_NUMBER_OVERFLOW_UV 5
1839
1840/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
28e5dec8
JH
1841
1842/* For sv_2nv these three cases are "SvNOK and don't bother casting" */
1843STATIC int
645c22ef 1844S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
28e5dec8 1845{
97aff369 1846 dVAR;
3f7c398e 1847 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
1848 if (SvNVX(sv) < (NV)IV_MIN) {
1849 (void)SvIOKp_on(sv);
1850 (void)SvNOK_on(sv);
45977657 1851 SvIV_set(sv, IV_MIN);
28e5dec8
JH
1852 return IS_NUMBER_UNDERFLOW_IV;
1853 }
1854 if (SvNVX(sv) > (NV)UV_MAX) {
1855 (void)SvIOKp_on(sv);
1856 (void)SvNOK_on(sv);
1857 SvIsUV_on(sv);
607fa7f2 1858 SvUV_set(sv, UV_MAX);
28e5dec8
JH
1859 return IS_NUMBER_OVERFLOW_UV;
1860 }
c2988b20
NC
1861 (void)SvIOKp_on(sv);
1862 (void)SvNOK_on(sv);
1863 /* Can't use strtol etc to convert this string. (See truth table in
1864 sv_2iv */
1865 if (SvNVX(sv) <= (UV)IV_MAX) {
45977657 1866 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
1867 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
1868 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
1869 } else {
1870 /* Integer is imprecise. NOK, IOKp */
1871 }
1872 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
1873 }
1874 SvIsUV_on(sv);
607fa7f2 1875 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
1876 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
1877 if (SvUVX(sv) == UV_MAX) {
1878 /* As we know that NVs don't preserve UVs, UV_MAX cannot
1879 possibly be preserved by NV. Hence, it must be overflow.
1880 NOK, IOKp */
1881 return IS_NUMBER_OVERFLOW_UV;
1882 }
1883 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
1884 } else {
1885 /* Integer is imprecise. NOK, IOKp */
28e5dec8 1886 }
c2988b20 1887 return IS_NUMBER_OVERFLOW_IV;
28e5dec8 1888}
645c22ef
DM
1889#endif /* !NV_PRESERVES_UV*/
1890
af359546
NC
1891STATIC bool
1892S_sv_2iuv_common(pTHX_ SV *sv) {
97aff369 1893 dVAR;
af359546 1894 if (SvNOKp(sv)) {
28e5dec8
JH
1895 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
1896 * without also getting a cached IV/UV from it at the same time
1897 * (ie PV->NV conversion should detect loss of accuracy and cache
af359546
NC
1898 * IV or UV at same time to avoid this. */
1899 /* IV-over-UV optimisation - choose to cache IV if possible */
25da4f38
IZ
1900
1901 if (SvTYPE(sv) == SVt_NV)
1902 sv_upgrade(sv, SVt_PVNV);
1903
28e5dec8
JH
1904 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
1905 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
1906 certainly cast into the IV range at IV_MAX, whereas the correct
1907 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
1908 cases go to UV */
cab190d4
JD
1909#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1910 if (Perl_isnan(SvNVX(sv))) {
1911 SvUV_set(sv, 0);
1912 SvIsUV_on(sv);
fdbe6d7c 1913 return FALSE;
cab190d4 1914 }
cab190d4 1915#endif
28e5dec8 1916 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 1917 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
1918 if (SvNVX(sv) == (NV) SvIVX(sv)
1919#ifndef NV_PRESERVES_UV
1920 && (((UV)1 << NV_PRESERVES_UV_BITS) >
1921 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
1922 /* Don't flag it as "accurately an integer" if the number
1923 came from a (by definition imprecise) NV operation, and
1924 we're outside the range of NV integer precision */
1925#endif
1926 ) {
1927 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
1928 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 1929 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
1930 PTR2UV(sv),
1931 SvNVX(sv),
1932 SvIVX(sv)));
1933
1934 } else {
1935 /* IV not precise. No need to convert from PV, as NV
1936 conversion would already have cached IV if it detected
1937 that PV->IV would be better than PV->NV->IV
1938 flags already correct - don't set public IOK. */
1939 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 1940 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
1941 PTR2UV(sv),
1942 SvNVX(sv),
1943 SvIVX(sv)));
1944 }
1945 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
1946 but the cast (NV)IV_MIN rounds to a the value less (more
1947 negative) than IV_MIN which happens to be equal to SvNVX ??
1948 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
1949 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
1950 (NV)UVX == NVX are both true, but the values differ. :-(
1951 Hopefully for 2s complement IV_MIN is something like
1952 0x8000000000000000 which will be exact. NWC */
d460ef45 1953 }
25da4f38 1954 else {
607fa7f2 1955 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
1956 if (
1957 (SvNVX(sv) == (NV) SvUVX(sv))
1958#ifndef NV_PRESERVES_UV
1959 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
1960 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
1961 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
1962 /* Don't flag it as "accurately an integer" if the number
1963 came from a (by definition imprecise) NV operation, and
1964 we're outside the range of NV integer precision */
1965#endif
1966 )
1967 SvIOK_on(sv);
25da4f38 1968 SvIsUV_on(sv);
1c846c1f 1969 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 1970 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 1971 PTR2UV(sv),
57def98f
JH
1972 SvUVX(sv),
1973 SvUVX(sv)));
25da4f38 1974 }
748a9306
LW
1975 }
1976 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 1977 UV value;
504618e9 1978 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
af359546 1979 /* We want to avoid a possible problem when we cache an IV/ a UV which
25da4f38 1980 may be later translated to an NV, and the resulting NV is not
c2988b20
NC
1981 the same as the direct translation of the initial string
1982 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
1983 be careful to ensure that the value with the .456 is around if the
1984 NV value is requested in the future).
1c846c1f 1985
af359546 1986 This means that if we cache such an IV/a UV, we need to cache the
25da4f38 1987 NV as well. Moreover, we trade speed for space, and do not
28e5dec8 1988 cache the NV if we are sure it's not needed.
25da4f38 1989 */
16b7a9a4 1990
c2988b20
NC
1991 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
1992 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
1993 == IS_NUMBER_IN_UV) {
5e045b90 1994 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8
JH
1995 if (SvTYPE(sv) < SVt_PVIV)
1996 sv_upgrade(sv, SVt_PVIV);
f7bbb42a 1997 (void)SvIOK_on(sv);
c2988b20
NC
1998 } else if (SvTYPE(sv) < SVt_PVNV)
1999 sv_upgrade(sv, SVt_PVNV);
28e5dec8 2000
f2524eef 2001 /* If NVs preserve UVs then we only use the UV value if we know that
c2988b20
NC
2002 we aren't going to call atof() below. If NVs don't preserve UVs
2003 then the value returned may have more precision than atof() will
2004 return, even though value isn't perfectly accurate. */
2005 if ((numtype & (IS_NUMBER_IN_UV
2006#ifdef NV_PRESERVES_UV
2007 | IS_NUMBER_NOT_INT
2008#endif
2009 )) == IS_NUMBER_IN_UV) {
2010 /* This won't turn off the public IOK flag if it was set above */
2011 (void)SvIOKp_on(sv);
2012
2013 if (!(numtype & IS_NUMBER_NEG)) {
2014 /* positive */;
2015 if (value <= (UV)IV_MAX) {
45977657 2016 SvIV_set(sv, (IV)value);
c2988b20 2017 } else {
af359546 2018 /* it didn't overflow, and it was positive. */
607fa7f2 2019 SvUV_set(sv, value);
c2988b20
NC
2020 SvIsUV_on(sv);
2021 }
2022 } else {
2023 /* 2s complement assumption */
2024 if (value <= (UV)IV_MIN) {
45977657 2025 SvIV_set(sv, -(IV)value);
c2988b20
NC
2026 } else {
2027 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2028 I'm assuming it will be rare. */
c2988b20
NC
2029 if (SvTYPE(sv) < SVt_PVNV)
2030 sv_upgrade(sv, SVt_PVNV);
2031 SvNOK_on(sv);
2032 SvIOK_off(sv);
2033 SvIOKp_on(sv);
9d6ce603 2034 SvNV_set(sv, -(NV)value);
45977657 2035 SvIV_set(sv, IV_MIN);
c2988b20
NC
2036 }
2037 }
2038 }
2039 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2040 will be in the previous block to set the IV slot, and the next
2041 block to set the NV slot. So no else here. */
2042
2043 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2044 != IS_NUMBER_IN_UV) {
2045 /* It wasn't an (integer that doesn't overflow the UV). */
3f7c398e 2046 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8 2047
c2988b20
NC
2048 if (! numtype && ckWARN(WARN_NUMERIC))
2049 not_a_number(sv);
28e5dec8 2050
65202027 2051#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2052 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2053 PTR2UV(sv), SvNVX(sv)));
65202027 2054#else
1779d84d 2055 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
c2988b20 2056 PTR2UV(sv), SvNVX(sv)));
65202027 2057#endif
28e5dec8 2058
28e5dec8 2059#ifdef NV_PRESERVES_UV
af359546
NC
2060 (void)SvIOKp_on(sv);
2061 (void)SvNOK_on(sv);
2062 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2063 SvIV_set(sv, I_V(SvNVX(sv)));
2064 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2065 SvIOK_on(sv);
2066 } else {
6f207bd3 2067 NOOP; /* Integer is imprecise. NOK, IOKp */
af359546
NC
2068 }
2069 /* UV will not work better than IV */
2070 } else {
2071 if (SvNVX(sv) > (NV)UV_MAX) {
2072 SvIsUV_on(sv);
2073 /* Integer is inaccurate. NOK, IOKp, is UV */
2074 SvUV_set(sv, UV_MAX);
af359546
NC
2075 } else {
2076 SvUV_set(sv, U_V(SvNVX(sv)));
2077 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2078 NV preservse UV so can do correct comparison. */
2079 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2080 SvIOK_on(sv);
af359546 2081 } else {
6f207bd3 2082 NOOP; /* Integer is imprecise. NOK, IOKp, is UV */
af359546
NC
2083 }
2084 }
4b0c9573 2085 SvIsUV_on(sv);
af359546 2086 }
28e5dec8 2087#else /* NV_PRESERVES_UV */
c2988b20
NC
2088 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2089 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
af359546 2090 /* The IV/UV slot will have been set from value returned by
c2988b20
NC
2091 grok_number above. The NV slot has just been set using
2092 Atof. */
560b0c46 2093 SvNOK_on(sv);
c2988b20
NC
2094 assert (SvIOKp(sv));
2095 } else {
2096 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2097 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2098 /* Small enough to preserve all bits. */
2099 (void)SvIOKp_on(sv);
2100 SvNOK_on(sv);
45977657 2101 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2102 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2103 SvIOK_on(sv);
2104 /* Assumption: first non-preserved integer is < IV_MAX,
2105 this NV is in the preserved range, therefore: */
2106 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2107 < (UV)IV_MAX)) {
32fdb065 2108 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
2109 }
2110 } else {
2111 /* IN_UV NOT_INT
2112 0 0 already failed to read UV.
2113 0 1 already failed to read UV.
2114 1 0 you won't get here in this case. IV/UV
2115 slot set, public IOK, Atof() unneeded.
2116 1 1 already read UV.
2117 so there's no point in sv_2iuv_non_preserve() attempting
2118 to use atol, strtol, strtoul etc. */
40a17c4c 2119 sv_2iuv_non_preserve (sv, numtype);
c2988b20
NC
2120 }
2121 }
28e5dec8 2122#endif /* NV_PRESERVES_UV */
25da4f38 2123 }
af359546
NC
2124 }
2125 else {
675c862f 2126 if (isGV_with_GP(sv))
a0933d07 2127 return glob_2number((GV *)sv);
180488f8 2128
af359546
NC
2129 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2130 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2131 report_uninit(sv);
2132 }
25da4f38
IZ
2133 if (SvTYPE(sv) < SVt_IV)
2134 /* Typically the caller expects that sv_any is not NULL now. */
2135 sv_upgrade(sv, SVt_IV);
af359546
NC
2136 /* Return 0 from the caller. */
2137 return TRUE;
2138 }
2139 return FALSE;
2140}
2141
2142/*
2143=for apidoc sv_2iv_flags
2144
2145Return the integer value of an SV, doing any necessary string
2146conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2147Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2148
2149=cut
2150*/
2151
2152IV
2153Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags)
2154{
97aff369 2155 dVAR;
af359546 2156 if (!sv)
a0d0e21e 2157 return 0;
af359546
NC
2158 if (SvGMAGICAL(sv)) {
2159 if (flags & SV_GMAGIC)
2160 mg_get(sv);
2161 if (SvIOKp(sv))
2162 return SvIVX(sv);
2163 if (SvNOKp(sv)) {
2164 return I_V(SvNVX(sv));
2165 }
71c558c3
NC
2166 if (SvPOKp(sv) && SvLEN(sv)) {
2167 UV value;
2168 const int numtype
2169 = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2170
2171 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2172 == IS_NUMBER_IN_UV) {
2173 /* It's definitely an integer */
2174 if (numtype & IS_NUMBER_NEG) {
2175 if (value < (UV)IV_MIN)
2176 return -(IV)value;
2177 } else {
2178 if (value < (UV)IV_MAX)
2179 return (IV)value;
2180 }
2181 }
2182 if (!numtype) {
2183 if (ckWARN(WARN_NUMERIC))
2184 not_a_number(sv);
2185 }
2186 return I_V(Atof(SvPVX_const(sv)));
2187 }
1c7ff15e
NC
2188 if (SvROK(sv)) {
2189 goto return_rok;
af359546 2190 }
1c7ff15e
NC
2191 assert(SvTYPE(sv) >= SVt_PVMG);
2192 /* This falls through to the report_uninit inside S_sv_2iuv_common. */
4cb1ec55 2193 } else if (SvTHINKFIRST(sv)) {
af359546 2194 if (SvROK(sv)) {
1c7ff15e 2195 return_rok:
af359546
NC
2196 if (SvAMAGIC(sv)) {
2197 SV * const tmpstr=AMG_CALLun(sv,numer);
2198 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2199 return SvIV(tmpstr);
2200 }
2201 }
2202 return PTR2IV(SvRV(sv));
2203 }
2204 if (SvIsCOW(sv)) {
2205 sv_force_normal_flags(sv, 0);
2206 }
2207 if (SvREADONLY(sv) && !SvOK(sv)) {
2208 if (ckWARN(WARN_UNINITIALIZED))
2209 report_uninit(sv);
2210 return 0;
2211 }
2212 }
2213 if (!SvIOKp(sv)) {
2214 if (S_sv_2iuv_common(aTHX_ sv))
2215 return 0;
79072805 2216 }
1d7c1841
GS
2217 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2218 PTR2UV(sv),SvIVX(sv)));
25da4f38 2219 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
2220}
2221
645c22ef 2222/*
891f9566 2223=for apidoc sv_2uv_flags
645c22ef
DM
2224
2225Return the unsigned integer value of an SV, doing any necessary string
891f9566
YST
2226conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2227Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
645c22ef
DM
2228
2229=cut
2230*/
2231
ff68c719 2232UV
891f9566 2233Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags)
ff68c719 2234{
97aff369 2235 dVAR;
ff68c719
PP
2236 if (!sv)
2237 return 0;
2238 if (SvGMAGICAL(sv)) {
891f9566
YST
2239 if (flags & SV_GMAGIC)
2240 mg_get(sv);
ff68c719
PP
2241 if (SvIOKp(sv))
2242 return SvUVX(sv);
2243 if (SvNOKp(sv))
2244 return U_V(SvNVX(sv));
71c558c3
NC
2245 if (SvPOKp(sv) && SvLEN(sv)) {
2246 UV value;
2247 const int numtype
2248 = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2249
2250 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2251 == IS_NUMBER_IN_UV) {
2252 /* It's definitely an integer */
2253 if (!(numtype & IS_NUMBER_NEG))
2254 return value;
2255 }
2256 if (!numtype) {
2257 if (ckWARN(WARN_NUMERIC))
2258 not_a_number(sv);
2259 }
2260 return U_V(Atof(SvPVX_const(sv)));
2261 }
1c7ff15e
NC
2262 if (SvROK(sv)) {
2263 goto return_rok;
3fe9a6f1 2264 }
1c7ff15e
NC
2265 assert(SvTYPE(sv) >= SVt_PVMG);
2266 /* This falls through to the report_uninit inside S_sv_2iuv_common. */
4cb1ec55 2267 } else if (SvTHINKFIRST(sv)) {
ff68c719 2268 if (SvROK(sv)) {
1c7ff15e 2269 return_rok:
deb46114
NC
2270 if (SvAMAGIC(sv)) {
2271 SV *const tmpstr = AMG_CALLun(sv,numer);
2272 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2273 return SvUV(tmpstr);
2274 }
2275 }
2276 return PTR2UV(SvRV(sv));
ff68c719 2277 }
765f542d
NC
2278 if (SvIsCOW(sv)) {
2279 sv_force_normal_flags(sv, 0);
8a818333 2280 }
0336b60e 2281 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2282 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2283 report_uninit(sv);
ff68c719
PP
2284 return 0;
2285 }
2286 }
af359546
NC
2287 if (!SvIOKp(sv)) {
2288 if (S_sv_2iuv_common(aTHX_ sv))
2289 return 0;
ff68c719 2290 }
25da4f38 2291
1d7c1841
GS
2292 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2293 PTR2UV(sv),SvUVX(sv)));
25da4f38 2294 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719
PP
2295}
2296
645c22ef
DM
2297/*
2298=for apidoc sv_2nv
2299
2300Return the num value of an SV, doing any necessary string or integer
2301conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
2302macros.
2303
2304=cut
2305*/
2306
65202027 2307NV
864dbfa3 2308Perl_sv_2nv(pTHX_ register SV *sv)
79072805 2309{
97aff369 2310 dVAR;
79072805
LW
2311 if (!sv)
2312 return 0.0;
8990e307 2313 if (SvGMAGICAL(sv)) {
463ee0b2
LW
2314 mg_get(sv);
2315 if (SvNOKp(sv))
2316 return SvNVX(sv);
0aa395f8 2317 if ((SvPOKp(sv) && SvLEN(sv)) && !SvIOKp(sv)) {
041457d9 2318 if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) &&
504618e9 2319 !grok_number(SvPVX_const(sv), SvCUR(sv), NULL))
a0d0e21e 2320 not_a_number(sv);
3f7c398e 2321 return Atof(SvPVX_const(sv));
a0d0e21e 2322 }
25da4f38 2323 if (SvIOKp(sv)) {
1c846c1f 2324 if (SvIsUV(sv))
65202027 2325 return (NV)SvUVX(sv);
25da4f38 2326 else
65202027 2327 return (NV)SvIVX(sv);
47a72cb8
NC
2328 }
2329 if (SvROK(sv)) {
2330 goto return_rok;
2331 }
2332 assert(SvTYPE(sv) >= SVt_PVMG);
2333 /* This falls through to the report_uninit near the end of the
2334 function. */
2335 } else if (SvTHINKFIRST(sv)) {
a0d0e21e 2336 if (SvROK(sv)) {
47a72cb8 2337 return_rok:
deb46114
NC
2338 if (SvAMAGIC(sv)) {
2339 SV *const tmpstr = AMG_CALLun(sv,numer);
2340 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2341 return SvNV(tmpstr);
2342 }
2343 }
2344 return PTR2NV(SvRV(sv));
a0d0e21e 2345 }
765f542d
NC
2346 if (SvIsCOW(sv)) {
2347 sv_force_normal_flags(sv, 0);
8a818333 2348 }
0336b60e 2349 if (SvREADONLY(sv) && !SvOK(sv)) {
599cee73 2350 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2351 report_uninit(sv);
ed6116ce
LW
2352 return 0.0;
2353 }
79072805
LW
2354 }
2355 if (SvTYPE(sv) < SVt_NV) {
7e25a7e9
NC
2356 /* The logic to use SVt_PVNV if necessary is in sv_upgrade. */
2357 sv_upgrade(sv, SVt_NV);
906f284f 2358#ifdef USE_LONG_DOUBLE
097ee67d 2359 DEBUG_c({
f93f4e46 2360 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2361 PerlIO_printf(Perl_debug_log,
2362 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2363 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2364 RESTORE_NUMERIC_LOCAL();
2365 });
65202027 2366#else
572bbb43 2367 DEBUG_c({
f93f4e46 2368 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2369 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
1d7c1841 2370 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2371 RESTORE_NUMERIC_LOCAL();
2372 });
572bbb43 2373#endif
79072805
LW
2374 }
2375 else if (SvTYPE(sv) < SVt_PVNV)
2376 sv_upgrade(sv, SVt_PVNV);
59d8ce62
NC
2377 if (SvNOKp(sv)) {
2378 return SvNVX(sv);
61604483 2379 }
59d8ce62 2380 if (SvIOKp(sv)) {
9d6ce603 2381 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
28e5dec8
JH
2382#ifdef NV_PRESERVES_UV
2383 SvNOK_on(sv);
2384#else
2385 /* Only set the public NV OK flag if this NV preserves the IV */
2386 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2387 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2388 : (SvIVX(sv) == I_V(SvNVX(sv))))
2389 SvNOK_on(sv);
2390 else
2391 SvNOKp_on(sv);
2392#endif
93a17b20 2393 }
748a9306 2394 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2395 UV value;
3f7c398e 2396 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
041457d9 2397 if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC))
a0d0e21e 2398 not_a_number(sv);
28e5dec8 2399#ifdef NV_PRESERVES_UV
c2988b20
NC
2400 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2401 == IS_NUMBER_IN_UV) {
5e045b90 2402 /* It's definitely an integer */
9d6ce603 2403 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
c2988b20 2404 } else
3f7c398e 2405 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
2406 SvNOK_on(sv);
2407#else
3f7c398e 2408 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
2409 /* Only set the public NV OK flag if this NV preserves the value in
2410 the PV at least as well as an IV/UV would.
2411 Not sure how to do this 100% reliably. */
2412 /* if that shift count is out of range then Configure's test is
2413 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2414 UV_BITS */
2415 if (((UV)1 << NV_PRESERVES_UV_BITS) >
c2988b20 2416 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
28e5dec8 2417 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
c2988b20
NC
2418 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2419 /* Can't use strtol etc to convert this string, so don't try.
2420 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2421 SvNOK_on(sv);
2422 } else {
2423 /* value has been set. It may not be precise. */
2424 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
2425 /* 2s complement assumption for (UV)IV_MIN */
2426 SvNOK_on(sv); /* Integer is too negative. */
2427 } else {
2428 SvNOKp_on(sv);
2429 SvIOKp_on(sv);
6fa402ec 2430
c2988b20 2431 if (numtype & IS_NUMBER_NEG) {
45977657 2432 SvIV_set(sv, -(IV)value);
c2988b20 2433 } else if (value <= (UV)IV_MAX) {
45977657 2434 SvIV_set(sv, (IV)value);
c2988b20 2435 } else {
607fa7f2 2436 SvUV_set(sv, value);
c2988b20
NC
2437 SvIsUV_on(sv);
2438 }
2439
2440 if (numtype & IS_NUMBER_NOT_INT) {
2441 /* I believe that even if the original PV had decimals,
2442 they are lost beyond the limit of the FP precision.
2443 However, neither is canonical, so both only get p
2444 flags. NWC, 2000/11/25 */
2445 /* Both already have p flags, so do nothing */
2446 } else {
66a1b24b 2447 const NV nv = SvNVX(sv);
c2988b20
NC
2448 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2449 if (SvIVX(sv) == I_V(nv)) {
2450 SvNOK_on(sv);
c2988b20 2451 } else {
c2988b20
NC
2452 /* It had no "." so it must be integer. */
2453 }
00b6aa41 2454 SvIOK_on(sv);
c2988b20
NC
2455 } else {
2456 /* between IV_MAX and NV(UV_MAX).
2457 Could be slightly > UV_MAX */
6fa402ec 2458
c2988b20
NC
2459 if (numtype & IS_NUMBER_NOT_INT) {
2460 /* UV and NV both imprecise. */
2461 } else {
66a1b24b 2462 const UV nv_as_uv = U_V(nv);
c2988b20
NC
2463
2464 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2465 SvNOK_on(sv);
c2988b20 2466 }
00b6aa41 2467 SvIOK_on(sv);
c2988b20
NC
2468 }
2469 }
2470 }
2471 }
2472 }
28e5dec8 2473#endif /* NV_PRESERVES_UV */
93a17b20 2474 }
79072805 2475 else {
f7877b28 2476 if (isGV_with_GP(sv)) {
19f6321d 2477 glob_2number((GV *)sv);
180488f8
NC
2478 return 0.0;
2479 }
2480
041457d9 2481 if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED))
29489e7c 2482 report_uninit(sv);
7e25a7e9
NC
2483 assert (SvTYPE(sv) >= SVt_NV);
2484 /* Typically the caller expects that sv_any is not NULL now. */
2485 /* XXX Ilya implies that this is a bug in callers that assume this
2486 and ideally should be fixed. */
a0d0e21e 2487 return 0.0;
79072805 2488 }
572bbb43 2489#if defined(USE_LONG_DOUBLE)
097ee67d 2490 DEBUG_c({
f93f4e46 2491 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2492 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2493 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2494 RESTORE_NUMERIC_LOCAL();
2495 });
65202027 2496#else
572bbb43 2497 DEBUG_c({
f93f4e46 2498 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2499 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
1d7c1841 2500 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2501 RESTORE_NUMERIC_LOCAL();
2502 });
572bbb43 2503#endif
463ee0b2 2504 return SvNVX(sv);
79072805
LW
2505}
2506
645c22ef
DM
2507/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2508 * UV as a string towards the end of buf, and return pointers to start and
2509 * end of it.
2510 *
2511 * We assume that buf is at least TYPE_CHARS(UV) long.
2512 */
2513
864dbfa3 2514static char *
aec46f14 2515S_uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
25da4f38 2516{
25da4f38 2517 char *ptr = buf + TYPE_CHARS(UV);
823a54a3 2518 char * const ebuf = ptr;
25da4f38 2519 int sign;
25da4f38
IZ
2520
2521 if (is_uv)
2522 sign = 0;
2523 else if (iv >= 0) {
2524 uv = iv;
2525 sign = 0;
2526 } else {
2527 uv = -iv;
2528 sign = 1;
2529 }
2530 do {
eb160463 2531 *--ptr = '0' + (char)(uv % 10);
25da4f38
IZ
2532 } while (uv /= 10);
2533 if (sign)
2534 *--ptr = '-';
2535 *peob = ebuf;
2536 return ptr;
2537}
2538
9af30d34
NC
2539/* stringify_regexp(): private routine for use by sv_2pv_flags(): converts
2540 * a regexp to its stringified form.
2541 */
2542
2543static char *
2544S_stringify_regexp(pTHX_ SV *sv, MAGIC *mg, STRLEN *lp) {
97aff369 2545 dVAR;
00b6aa41 2546 const regexp * const re = (regexp *)mg->mg_obj;
9af30d34
NC
2547
2548 if (!mg->mg_ptr) {
2549 const char *fptr = "msix";
2550 char reflags[6];
2551 char ch;
2552 int left = 0;
2553 int right = 4;
00b6aa41 2554 bool need_newline = 0;
9af30d34
NC
2555 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
2556
2557 while((ch = *fptr++)) {
2558 if(reganch & 1) {
2559 reflags[left++] = ch;
2560 }
2561 else {
2562 reflags[right--] = ch;
2563 }
2564 reganch >>= 1;
2565 }
2566 if(left != 4) {
2567 reflags[left] = '-';
2568 left = 5;
2569 }
2570
2571 mg->mg_len = re->prelen + 4 + left;
2572 /*
2573 * If /x was used, we have to worry about a regex ending with a
2574 * comment later being embedded within another regex. If so, we don't
2575 * want this regex's "commentization" to leak out to the right part of
2576 * the enclosing regex, we must cap it with a newline.
2577 *
2578 * So, if /x was used, we scan backwards from the end of the regex. If
2579 * we find a '#' before we find a newline, we need to add a newline
2580 * ourself. If we find a '\n' first (or if we don't find '#' or '\n'),
2581 * we don't need to add anything. -jfriedl
2582 */
2583 if (PMf_EXTENDED & re->reganch) {
2584 const char *endptr = re->precomp + re->prelen;
2585 while (endptr >= re->precomp) {
2586 const char c = *(endptr--);
2587 if (c == '\n')
2588 break; /* don't need another */
2589 if (c == '#') {
2590 /* we end while in a comment, so we need a newline */
2591 mg->mg_len++; /* save space for it */
2592 need_newline = 1; /* note to add it */
2593 break;
2594 }
2595 }
2596 }
2597
2598 Newx(mg->mg_ptr, mg->mg_len + 1 + left, char);
2599 mg->mg_ptr[0] = '(';
2600 mg->mg_ptr[1] = '?';
2601 Copy(reflags, mg->mg_ptr+2, left, char);
2602 *(mg->mg_ptr+left+2) = ':';
2603 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
2604 if (need_newline)
2605 mg->mg_ptr[mg->mg_len - 2] = '\n';
2606 mg->mg_ptr[mg->mg_len - 1] = ')';
2607 mg->mg_ptr[mg->mg_len] = 0;
2608 }
2609 PL_reginterp_cnt += re->program[0].next_off;
2610
2611 if (re->reganch & ROPT_UTF8)
2612 SvUTF8_on(sv);
2613 else
2614 SvUTF8_off(sv);
2615 if (lp)
2616 *lp = mg->mg_len;
2617 return mg->mg_ptr;
2618}
2619
645c22ef
DM
2620/*
2621=for apidoc sv_2pv_flags
2622
ff276b08 2623Returns a pointer to the string value of an SV, and sets *lp to its length.
645c22ef
DM
2624If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
2625if necessary.
2626Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
2627usually end up here too.
2628
2629=cut
2630*/
2631
8d6d96c1
HS
2632char *
2633Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
2634{
97aff369 2635 dVAR;
79072805 2636 register char *s;
79072805 2637
463ee0b2 2638 if (!sv) {
cdb061a3
NC
2639 if (lp)
2640 *lp = 0;
73d840c0 2641 return (char *)"";
463ee0b2 2642 }
8990e307 2643 if (SvGMAGICAL(sv)) {
8d6d96c1
HS
2644 if (flags & SV_GMAGIC)
2645 mg_get(sv);
463ee0b2 2646 if (SvPOKp(sv)) {
cdb061a3
NC
2647 if (lp)
2648 *lp = SvCUR(sv);
10516c54
NC
2649 if (flags & SV_MUTABLE_RETURN)
2650 return SvPVX_mutable(sv);
4d84ee25
NC
2651 if (flags & SV_CONST_RETURN)
2652 return (char *)SvPVX_const(sv);
463ee0b2
LW
2653 return SvPVX(sv);
2654 }
75dfc8ec
NC
2655 if (SvIOKp(sv) || SvNOKp(sv)) {
2656 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
75dfc8ec
NC
2657 STRLEN len;
2658
2659 if (SvIOKp(sv)) {
e80fed9d
JH
2660 len = SvIsUV(sv)
2661#ifdef USE_SNPRINTF
2662 ? snprintf(tbuf, sizeof(tbuf), "%"UVuf, (UV)SvUVX(sv))
2663 : snprintf(tbuf, sizeof(tbuf), "%"IVdf, (IV)SvIVX(sv));
2664#else
2665 ? my_sprintf(tbuf, "%"UVuf, (UV)SvUVX(sv))
2666 : my_sprintf(tbuf, "%"IVdf, (IV)SvIVX(sv));
2667#endif /* #ifdef USE_SNPRINTF */
75dfc8ec 2668 } else {
e8ada2d0
NC
2669 Gconvert(SvNVX(sv), NV_DIG, 0, tbuf);
2670 len = strlen(tbuf);
75dfc8ec 2671 }
b5b886f0
NC
2672 assert(!SvROK(sv));
2673 {
75dfc8ec
NC
2674 dVAR;
2675
2676#ifdef FIXNEGATIVEZERO
e8ada2d0
NC
2677 if (len == 2 && tbuf[0] == '-' && tbuf[1] == '0') {
2678 tbuf[0] = '0';
2679 tbuf[1] = 0;
75dfc8ec
NC
2680 len = 1;
2681 }
2682#endif
2683 SvUPGRADE(sv, SVt_PV);
2684 if (lp)
2685 *lp = len;
2686 s = SvGROW_mutable(sv, len + 1);
2687 SvCUR_set(sv, len);
2688 SvPOKp_on(sv);
e8ada2d0 2689 return memcpy(s, tbuf, len + 1);
75dfc8ec 2690 }
463ee0b2 2691 }
1c7ff15e
NC
2692 if (SvROK(sv)) {
2693 goto return_rok;
2694 }
2695 assert(SvTYPE(sv) >= SVt_PVMG);
2696 /* This falls through to the report_uninit near the end of the
2697 function. */
2698 } else if (SvTHINKFIRST(sv)) {
ed6116ce 2699 if (SvROK(sv)) {
1c7ff15e 2700 return_rok:
deb46114
NC
2701 if (SvAMAGIC(sv)) {
2702 SV *const tmpstr = AMG_CALLun(sv,string);
2703 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2704 /* Unwrap this: */
2705 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr);
2706 */
2707
2708 char *pv;
2709 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
2710 if (flags & SV_CONST_RETURN) {
2711 pv = (char *) SvPVX_const(tmpstr);
2712 } else {
2713 pv = (flags & SV_MUTABLE_RETURN)
2714 ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
2715 }
2716 if (lp)
2717 *lp = SvCUR(tmpstr);
50adf7d2 2718 } else {
deb46114 2719 pv = sv_2pv_flags(tmpstr, lp, flags);
50adf7d2 2720 }
deb46114
NC
2721 if (SvUTF8(tmpstr))
2722 SvUTF8_on(sv);
2723 else
2724 SvUTF8_off(sv);
2725 return pv;
50adf7d2 2726 }
deb46114
NC
2727 }
2728 {
75dfc8ec 2729 SV *tsv;
f9277f47 2730 MAGIC *mg;
d8eae41e
NC
2731 const SV *const referent = (SV*)SvRV(sv);
2732
2733 if (!referent) {
396482e1 2734 tsv = sv_2mortal(newSVpvs("NULLREF"));
042dae7a
NC
2735 } else if (SvTYPE(referent) == SVt_PVMG
2736 && ((SvFLAGS(referent) &
2737 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
2738 == (SVs_OBJECT|SVs_SMG))
2739 && (mg = mg_find(referent, PERL_MAGIC_qr))) {
c445ea15 2740 return stringify_regexp(sv, mg, lp);
d8eae41e
NC
2741 } else {
2742 const char *const typestr = sv_reftype(referent, 0);
2743
2744 tsv = sv_newmortal();
2745 if (SvOBJECT(referent)) {
2746 const char *const name = HvNAME_get(SvSTASH(referent));
2747 Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
2748 name ? name : "__ANON__" , typestr,
2749 PTR2UV(referent));
2750 }
2751 else
2752 Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr,
2753 PTR2UV(referent));
c080367d 2754 }
042dae7a
NC
2755 if (lp)
2756 *lp = SvCUR(tsv);
2757 return SvPVX(tsv);
463ee0b2 2758 }
79072805 2759 }
0336b60e 2760 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2761 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2762 report_uninit(sv);
cdb061a3
NC
2763 if (lp)
2764 *lp = 0;
73d840c0 2765 return (char *)"";
79072805 2766 }
79072805 2767 }
28e5dec8
JH
2768 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
2769 /* I'm assuming that if both IV and NV are equally valid then
2770 converting the IV is going to be more efficient */
e1ec3a88
AL
2771 const U32 isIOK = SvIOK(sv);
2772 const U32 isUIOK = SvIsUV(sv);
28e5dec8
JH
2773 char buf[TYPE_CHARS(UV)];
2774 char *ebuf, *ptr;
2775
2776 if (SvTYPE(sv) < SVt_PVIV)
2777 sv_upgrade(sv, SVt_PVIV);
4ea1d550 2778 ptr = uiv_2buf(buf, SvIVX(sv), SvUVX(sv), isUIOK, &ebuf);
5902b6a9
NC
2779 /* inlined from sv_setpvn */
2780 SvGROW_mutable(sv, (STRLEN)(ebuf - ptr + 1));
4d84ee25 2781 Move(ptr,SvPVX_mutable(sv),ebuf - ptr,char);
28e5dec8
JH
2782 SvCUR_set(sv, ebuf - ptr);
2783 s = SvEND(sv);
2784 *s = '\0';
2785 if (isIOK)
2786 SvIOK_on(sv);
2787 else
2788 SvIOKp_on(sv);
2789 if (isUIOK)
2790 SvIsUV_on(sv);
2791 }
2792 else if (SvNOKp(sv)) {
c81271c3 2793 const int olderrno = errno;
79072805
LW
2794 if (SvTYPE(sv) < SVt_PVNV)
2795 sv_upgrade(sv, SVt_PVNV);
1c846c1f 2796 /* The +20 is pure guesswork. Configure test needed. --jhi */
5902b6a9 2797 s = SvGROW_mutable(sv, NV_DIG + 20);
c81271c3 2798 /* some Xenix systems wipe out errno here */
79072805 2799#ifdef apollo
463ee0b2 2800 if (SvNVX(sv) == 0.0)
79072805
LW
2801 (void)strcpy(s,"0");
2802 else
2803#endif /*apollo*/
bbce6d69 2804 {
2d4389e4 2805 Gconvert(SvNVX(sv), NV_DIG, 0, s);
bbce6d69 2806 }
79072805 2807 errno = olderrno;
a0d0e21e
LW
2808#ifdef FIXNEGATIVEZERO
2809 if (*s == '-' && s[1] == '0' && !s[2])
2810 strcpy(s,"0");
2811#endif
79072805
LW
2812 while (*s) s++;
2813#ifdef hcx
2814 if (s[-1] == '.')
46fc3d4c 2815 *--s = '\0';
79072805
LW
2816#endif
2817 }
79072805 2818 else {
675c862f 2819 if (isGV_with_GP(sv))
19f6321d 2820 return glob_2pv((GV *)sv, lp);
180488f8 2821
041457d9 2822 if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED))
29489e7c 2823 report_uninit(sv);
cdb061a3 2824 if (lp)
00b6aa41 2825 *lp = 0;
25da4f38
IZ
2826 if (SvTYPE(sv) < SVt_PV)
2827 /* Typically the caller expects that sv_any is not NULL now. */
2828 sv_upgrade(sv, SVt_PV);
73d840c0 2829 return (char *)"";
79072805 2830 }
cdb061a3 2831 {
823a54a3 2832 const STRLEN len = s - SvPVX_const(sv);
cdb061a3
NC
2833 if (lp)
2834 *lp = len;
2835 SvCUR_set(sv, len);
2836 }
79072805 2837 SvPOK_on(sv);
1d7c1841 2838 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3f7c398e 2839 PTR2UV(sv),SvPVX_const(sv)));
4d84ee25
NC
2840 if (flags & SV_CONST_RETURN)
2841 return (char *)SvPVX_const(sv);
10516c54
NC
2842 if (flags & SV_MUTABLE_RETURN)
2843 return SvPVX_mutable(sv);
463ee0b2
LW
2844 return SvPVX(sv);
2845}
2846
645c22ef 2847/*
6050d10e
JP
2848=for apidoc sv_copypv
2849
2850Copies a stringified representation of the source SV into the
2851destination SV. Automatically performs any necessary mg_get and
54f0641b 2852coercion of numeric values into strings. Guaranteed to preserve
6050d10e 2853UTF-8 flag even from overloaded objects. Similar in nature to
54f0641b
NIS
2854sv_2pv[_flags] but operates directly on an SV instead of just the
2855string. Mostly uses sv_2pv_flags to do its work, except when that
6050d10e
JP
2856would lose the UTF-8'ness of the PV.
2857
2858=cut
2859*/
2860
2861void
2862Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
2863{
446eaa42 2864 STRLEN len;
53c1dcc0 2865 const char * const s = SvPV_const(ssv,len);
cb50f42d 2866 sv_setpvn(dsv,s,len);
446eaa42 2867 if (SvUTF8(ssv))
cb50f42d 2868 SvUTF8_on(dsv);
446eaa42 2869 else
cb50f42d 2870 SvUTF8_off(dsv);
6050d10e
JP
2871}
2872
2873/*
645c22ef
DM
2874=for apidoc sv_2pvbyte
2875
2876Return a pointer to the byte-encoded representation of the SV, and set *lp
1e54db1a 2877to its length. May cause the SV to be downgraded from UTF-8 as a
645c22ef
DM
2878side-effect.
2879
2880Usually accessed via the C<SvPVbyte> macro.
2881
2882=cut
2883*/
2884
7340a771
GS
2885char *
2886Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
2887{
0875d2fe 2888 sv_utf8_downgrade(sv,0);
97972285 2889 return lp ? SvPV(sv,*lp) : SvPV_nolen(sv);
7340a771
GS
2890}
2891
645c22ef 2892/*
035cbb0e
RGS
2893=for apidoc sv_2pvutf8
2894
2895Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
2896to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
2897
2898Usually accessed via the C<SvPVutf8> macro.
2899
2900=cut
2901*/
645c22ef 2902
7340a771
GS
2903char *
2904Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
2905{
035cbb0e
RGS
2906 sv_utf8_upgrade(sv);
2907 return lp ? SvPV(sv,*lp) : SvPV_nolen(sv);
7340a771 2908}
1c846c1f 2909
7ee2227d 2910
645c22ef
DM
2911/*
2912=for apidoc sv_2bool
2913
2914This function is only called on magical items, and is only used by
8cf8f3d1 2915sv_true() or its macro equivalent.
645c22ef
DM
2916
2917=cut
2918*/
2919
463ee0b2 2920bool
864dbfa3 2921Perl_sv_2bool(pTHX_ register SV *sv)
463ee0b2 2922{
97aff369 2923 dVAR;
5b295bef 2924 SvGETMAGIC(sv);
463ee0b2 2925
a0d0e21e
LW
2926 if (!SvOK(sv))
2927 return 0;
2928 if (SvROK(sv)) {
fabdb6c0
AL
2929 if (SvAMAGIC(sv)) {
2930 SV * const tmpsv = AMG_CALLun(sv,bool_);
2931 if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
2932 return (bool)SvTRUE(tmpsv);
2933 }
2934 return SvRV(sv) != 0;
a0d0e21e 2935 }
463ee0b2 2936 if (SvPOKp(sv)) {
53c1dcc0
AL
2937 register XPV* const Xpvtmp = (XPV*)SvANY(sv);
2938 if (Xpvtmp &&
339049b0 2939 (*sv->sv_u.svu_pv > '0' ||
11343788 2940 Xpvtmp->xpv_cur > 1 ||
339049b0 2941 (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0')))
463ee0b2
LW
2942 return 1;
2943 else
2944 return 0;
2945 }
2946 else {
2947 if (SvIOKp(sv))
2948 return SvIVX(sv) != 0;
2949 else {
2950 if (SvNOKp(sv))
2951 return SvNVX(sv) != 0.0;
180488f8 2952 else {
f7877b28 2953 if (isGV_with_GP(sv))
180488f8
NC
2954 return TRUE;
2955 else
2956 return FALSE;
2957 }
463ee0b2
LW
2958 }
2959 }
79072805
LW
2960}
2961
c461cf8f
JH
2962/*
2963=for apidoc sv_utf8_upgrade
2964
78ea37eb 2965Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 2966Forces the SV to string form if it is not already.
4411f3b6
NIS
2967Always sets the SvUTF8 flag to avoid future validity checks even
2968if all the bytes have hibit clear.
c461cf8f 2969
13a6c0e0
JH
2970This is not as a general purpose byte encoding to Unicode interface:
2971use the Encode extension for that.
2972
8d6d96c1
HS
2973=for apidoc sv_utf8_upgrade_flags
2974
78ea37eb 2975Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 2976Forces the SV to string form if it is not already.
8d6d96c1
HS
2977Always sets the SvUTF8 flag to avoid future validity checks even
2978if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
2979will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
2980C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
2981
13a6c0e0
JH
2982This is not as a general purpose byte encoding to Unicode interface:
2983use the Encode extension for that.
2984
8d6d96c1
HS
2985=cut
2986*/
2987
2988STRLEN
2989Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
2990{
97aff369 2991 dVAR;
808c356f
RGS
2992 if (sv == &PL_sv_undef)
2993 return 0;
e0e62c2a
NIS
2994 if (!SvPOK(sv)) {
2995 STRLEN len = 0;
d52b7888
NC
2996 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
2997 (void) sv_2pv_flags(sv,&len, flags);
2998 if (SvUTF8(sv))
2999 return len;
3000 } else {
3001 (void) SvPV_force(sv,len);
3002 }
e0e62c2a 3003 }
4411f3b6 3004
f5cee72b 3005 if (SvUTF8(sv)) {
5fec3b1d 3006 return SvCUR(sv);
f5cee72b 3007 }
5fec3b1d 3008
765f542d
NC
3009 if (SvIsCOW(sv)) {
3010 sv_force_normal_flags(sv, 0);
db42d148
NIS
3011 }
3012
88632417 3013 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
799ef3cb 3014 sv_recode_to_utf8(sv, PL_encoding);
9f4817db 3015 else { /* Assume Latin-1/EBCDIC */
c4e7c712
NC
3016 /* This function could be much more efficient if we
3017 * had a FLAG in SVs to signal if there are any hibit
3018 * chars in the PV. Given that there isn't such a flag
3019 * make the loop as fast as possible. */
00b6aa41 3020 const U8 * const s = (U8 *) SvPVX_const(sv);
c4420975 3021 const U8 * const e = (U8 *) SvEND(sv);
93524f2b 3022 const U8 *t = s;
c4e7c712
NC
3023
3024 while (t < e) {
53c1dcc0 3025 const U8 ch = *t++;
00b6aa41
AL
3026 /* Check for hi bit */
3027 if (!NATIVE_IS_INVARIANT(ch)) {
3028 STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */
3029 U8 * const recoded = bytes_to_utf8((U8*)s, &len);
3030
3031 SvPV_free(sv); /* No longer using what was there before. */
3032 SvPV_set(sv, (char*)recoded);
3033 SvCUR_set(sv, len - 1);
3034 SvLEN_set(sv, len); /* No longer know the real size. */
c4e7c712 3035 break;
00b6aa41 3036 }
c4e7c712
NC
3037 }
3038 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3039 SvUTF8_on(sv);
560a288e 3040 }
4411f3b6 3041 return SvCUR(sv);
560a288e
GS
3042}
3043
c461cf8f
JH
3044/*
3045=for apidoc sv_utf8_downgrade
3046
78ea37eb
ST
3047Attempts to convert the PV of an SV from characters to bytes.
3048If the PV contains a character beyond byte, this conversion will fail;
3049in this case, either returns false or, if C<fail_ok> is not
c461cf8f
JH
3050true, croaks.
3051
13a6c0e0
JH
3052This is not as a general purpose Unicode to byte encoding interface:
3053use the Encode extension for that.
3054
c461cf8f
JH
3055=cut
3056*/
3057
560a288e
GS
3058bool
3059Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3060{
97aff369 3061 dVAR;
78ea37eb 3062 if (SvPOKp(sv) && SvUTF8(sv)) {
fa301091 3063 if (SvCUR(sv)) {
03cfe0ae 3064 U8 *s;
652088fc 3065 STRLEN len;
fa301091 3066
765f542d
NC
3067 if (SvIsCOW(sv)) {
3068 sv_force_normal_flags(sv, 0);
3069 }
03cfe0ae
NIS
3070 s = (U8 *) SvPV(sv, len);
3071 if (!utf8_to_bytes(s, &len)) {
fa301091
JH
3072 if (fail_ok)
3073 return FALSE;
3074 else {
3075 if (PL_op)
3076 Perl_croak(aTHX_ "Wide character in %s",
53e06cf0 3077 OP_DESC(PL_op));
fa301091
JH
3078 else
3079 Perl_croak(aTHX_ "Wide character");
3080 }
4b3603a4 3081 }
b162af07 3082 SvCUR_set(sv, len);
67e989fb 3083 }
560a288e 3084 }
ffebcc3e 3085 SvUTF8_off(sv);
560a288e
GS
3086 return TRUE;
3087}
3088
c461cf8f
JH
3089/*
3090=for apidoc sv_utf8_encode
3091
78ea37eb
ST
3092Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3093flag off so that it looks like octets again.
c461cf8f
JH
3094
3095=cut
3096*/
3097
560a288e
GS
3098void
3099Perl_sv_utf8_encode(pTHX_ register SV *sv)
3100{
4411f3b6 3101 (void) sv_utf8_upgrade(sv);
4c94c214
NC
3102 if (SvIsCOW(sv)) {
3103 sv_force_normal_flags(sv, 0);
3104 }
3105 if (SvREADONLY(sv)) {
3106 Perl_croak(aTHX_ PL_no_modify);
3107 }
560a288e
GS
3108 SvUTF8_off(sv);
3109}
3110
4411f3b6
NIS
3111/*
3112=for apidoc sv_utf8_decode
3113
78ea37eb
ST
3114If the PV of the SV is an octet sequence in UTF-8
3115and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3116so that it looks like a character. If the PV contains only single-byte
3117characters, the C<SvUTF8> flag stays being off.
3118Scans PV for validity and returns false if the PV is invalid UTF-8.
4411f3b6
NIS
3119
3120=cut
3121*/
3122
560a288e
GS
3123bool
3124Perl_sv_utf8_decode(pTHX_ register SV *sv)
3125{
78ea37eb 3126 if (SvPOKp(sv)) {
93524f2b
NC
3127 const U8 *c;
3128 const U8 *e;
9cbac4c7 3129
645c22ef
DM
3130 /* The octets may have got themselves encoded - get them back as
3131 * bytes
3132 */
3133 if (!sv_utf8_downgrade(sv, TRUE))
560a288e
GS
3134 return FALSE;
3135
3136 /* it is actually just a matter of turning the utf8 flag on, but
3137 * we want to make sure everything inside is valid utf8 first.
3138 */
93524f2b 3139 c = (const U8 *) SvPVX_const(sv);
63cd0674 3140 if (!is_utf8_string(c, SvCUR(sv)+1))
67e989fb 3141 return FALSE;
93524f2b 3142 e = (const U8 *) SvEND(sv);
511c2ff0 3143 while (c < e) {
b64e5050 3144 const U8 ch = *c++;
c4d5f83a 3145 if (!UTF8_IS_INVARIANT(ch)) {
67e989fb
JH
3146 SvUTF8_on(sv);
3147 break;
3148 }
560a288e 3149 }
560a288e
GS
3150 }
3151 return TRUE;
3152}
3153
954c1994
GS
3154/*
3155=for apidoc sv_setsv
3156
645c22ef
DM
3157Copies the contents of the source SV C<ssv> into the destination SV
3158C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3159function if the source SV needs to be reused. Does not handle 'set' magic.
3160Loosely speaking, it performs a copy-by-value, obliterating any previous
3161content of the destination.
3162
3163You probably want to use one of the assortment of wrappers, such as
3164C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3165C<SvSetMagicSV_nosteal>.
3166
8d6d96c1
HS
3167=for apidoc sv_setsv_flags
3168
645c22ef
DM
3169Copies the contents of the source SV C<ssv> into the destination SV
3170C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3171function if the source SV needs to be reused. Does not handle 'set' magic.
3172Loosely speaking, it performs a copy-by-value, obliterating any previous
3173content of the destination.
3174If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
5fcdf167
NC
3175C<ssv> if appropriate, else not. If the C<flags> parameter has the
3176C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
3177and C<sv_setsv_nomg> are implemented in terms of this function.
645c22ef
DM
3178
3179You probably want to use one of the assortment of wrappers, such as
3180C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3181C<SvSetMagicSV_nosteal>.
3182
3183This is the primary function for copying scalars, and most other
3184copy-ish functions and macros use this underneath.
8d6d96c1
HS
3185
3186=cut
3187*/
3188
5d0301b7 3189static void
2eb42952 3190S_glob_assign_glob(pTHX_ SV *dstr, SV *sstr, const int dtype)
5d0301b7
NC
3191{
3192 if (dtype != SVt_PVGV) {
3193 const char * const name = GvNAME(sstr);
3194 const STRLEN len = GvNAMELEN(sstr);
3195 /* don't upgrade SVt_PVLV: it can hold a glob */
f7877b28
NC
3196 if (dtype != SVt_PVLV) {
3197 if (dtype >= SVt_PV) {
3198 SvPV_free(dstr);
3199 SvPV_set(dstr, 0);
3200 SvLEN_set(dstr, 0);
3201 SvCUR_set(dstr, 0);
3202 }
5d0301b7 3203 sv_upgrade(dstr, SVt_PVGV);
dedf8e73
NC
3204 (void)SvOK_off(dstr);
3205 SvSCREAM_on(dstr);
f7877b28 3206 }
5d0301b7
NC
3207 GvSTASH(dstr) = GvSTASH(sstr);
3208 if (GvSTASH(dstr))
3209 Perl_sv_add_backref(aTHX_ (SV*)GvSTASH(dstr), dstr);
ae8cc45f 3210 gv_name_set((GV *)dstr, name, len, GV_ADD);
5d0301b7
NC
3211 SvFAKE_on(dstr); /* can coerce to non-glob */
3212 }
3213
3214#ifdef GV_UNIQUE_CHECK
3215 if (GvUNIQUE((GV*)dstr)) {
3216 Perl_croak(aTHX_ PL_no_modify);
3217 }
3218#endif
3219
f7877b28
NC
3220 gp_free((GV*)dstr);
3221 SvSCREAM_off(dstr);
5d0301b7 3222 (void)SvOK_off(dstr);
f7877b28 3223 SvSCREAM_on(dstr);
dedf8e73 3224 GvINTRO_off(dstr); /* one-shot flag */
5d0301b7
NC
3225 GvGP(dstr) = gp_ref(GvGP(sstr));
3226 if (SvTAINTED(sstr))
3227 SvTAINT(dstr);
3228 if (GvIMPORTED(dstr) != GVf_IMPORTED
3229 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3230 {
3231 GvIMPORTED_on(dstr);
3232 }
3233 GvMULTI_on(dstr);
3234 return;
3235}
3236
b8473700 3237static void
2eb42952 3238S_glob_assign_ref(pTHX_ SV *dstr, SV *sstr) {
b8473700
NC
3239 SV * const sref = SvREFCNT_inc(SvRV(sstr));
3240 SV *dref = NULL;
3241 const int intro = GvINTRO(dstr);
2440974c 3242 SV **location;
3386d083 3243 U8 import_flag = 0;
27242d61
NC
3244 const U32 stype = SvTYPE(sref);
3245
b8473700
NC
3246
3247#ifdef GV_UNIQUE_CHECK
3248 if (GvUNIQUE((GV*)dstr)) {
3249 Perl_croak(aTHX_ PL_no_modify);
3250 }
3251#endif
3252
3253 if (intro) {
3254 GvINTRO_off(dstr); /* one-shot flag */
3255 GvLINE(dstr) = CopLINE(PL_curcop);
3256 GvEGV(dstr) = (GV*)dstr;
3257 }
3258 GvMULTI_on(dstr);
27242d61 3259 switch (stype) {
b8473700 3260 case SVt_PVCV:
27242d61
NC
3261 location = (SV **) &GvCV(dstr);
3262 import_flag = GVf_IMPORTED_CV;
3263 goto common;
3264 case SVt_PVHV:
3265 location = (SV **) &GvHV(dstr);
3266 import_flag = GVf_IMPORTED_HV;
3267 goto common;
3268 case SVt_PVAV:
3269 location = (SV **) &GvAV(dstr);
3270 import_flag = GVf_IMPORTED_AV;
3271 goto common;
3272 case SVt_PVIO:
3273 location = (SV **) &GvIOp(dstr);
3274 goto common;
3275 case SVt_PVFM:
3276 location = (SV **) &GvFORM(dstr);
3277 default:
3278 location = &GvSV(dstr);
3279 import_flag = GVf_IMPORTED_SV;
3280 common:
b8473700 3281 if (intro) {
27242d61
NC
3282 if (stype == SVt_PVCV) {
3283 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
3284 SvREFCNT_dec(GvCV(dstr));
3285 GvCV(dstr) = NULL;
3286 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3287 PL_sub_generation++;
3288 }
b8473700 3289 }
27242d61 3290 SAVEGENERICSV(*location);
b8473700
NC
3291 }
3292 else
27242d61
NC
3293 dref = *location;
3294 if (stype == SVt_PVCV && *location != sref) {
3295 CV* const cv = (CV*)*location;
b8473700
NC
3296 if (cv) {
3297 if (!GvCVGEN((GV*)dstr) &&
3298 (CvROOT(cv) || CvXSUB(cv)))
3299 {
3300 /* Redefining a sub - warning is mandatory if
3301 it was a const and its value changed. */
3302 if (CvCONST(cv) && CvCONST((CV*)sref)
3303 && cv_const_sv(cv) == cv_const_sv((CV*)sref)) {
6f207bd3 3304 NOOP;
b8473700
NC
3305 /* They are 2 constant subroutines generated from
3306 the same constant. This probably means that
3307 they are really the "same" proxy subroutine
3308 instantiated in 2 places. Most likely this is
3309 when a constant is exported twice. Don't warn.
3310 */
3311 }
3312 else if (ckWARN(WARN_REDEFINE)
3313 || (CvCONST(cv)
3314 && (!CvCONST((CV*)sref)
3315 || sv_cmp(cv_const_sv(cv),
3316 cv_const_sv((CV*)sref))))) {
3317 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
3318 CvCONST(cv)
3319 ? "Constant subroutine %s::%s redefined"
3320 : "Subroutine %s::%s redefined",
3321 HvNAME_get(GvSTASH((GV*)dstr)),
3322 GvENAME((GV*)dstr));
3323 }
3324 }
3325 if (!intro)
cbf82dd0
NC
3326 cv_ckproto_len(cv, (GV*)dstr,
3327 SvPOK(sref) ? SvPVX_const(sref) : NULL,
3328 SvPOK(sref) ? SvCUR(sref) : 0);
b8473700 3329 }
b8473700
NC
3330 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3331 GvASSUMECV_on(dstr);
3332 PL_sub_generation++;
3333 }
2440974c 3334 *location = sref;
3386d083
NC
3335 if (import_flag && !(GvFLAGS(dstr) & import_flag)
3336 && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) {
3337 GvFLAGS(dstr) |= import_flag;
b8473700
NC
3338 }
3339 break;
3340 }
b37c2d43 3341 SvREFCNT_dec(dref);
b8473700
NC
3342 if (SvTAINTED(sstr))
3343 SvTAINT(dstr);
3344 return;
3345}
3346
8d6d96c1
HS
3347void
3348Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
3349{
97aff369 3350 dVAR;
8990e307
LW
3351 register U32 sflags;
3352 register int dtype;
3353 register int stype;
463ee0b2 3354
79072805
LW
3355 if (sstr == dstr)
3356 return;
765f542d 3357 SV_CHECK_THINKFIRST_COW_DROP(dstr);
79072805 3358 if (!sstr)
3280af22 3359 sstr = &PL_sv_undef;
8990e307
LW
3360 stype = SvTYPE(sstr);
3361 dtype = SvTYPE(dstr);
79072805 3362
a0d0e21e 3363 SvAMAGIC_off(dstr);
7a5fa8a2 3364 if ( SvVOK(dstr) )
ece467f9
JP
3365 {
3366 /* need to nuke the magic */
3367 mg_free(dstr);
3368 SvRMAGICAL_off(dstr);
3369 }
9e7bc3e8 3370
463ee0b2 3371 /* There's a lot of redundancy below but we're going for speed here */
79072805 3372
8990e307 3373 switch (stype) {
79072805 3374 case SVt_NULL:
aece5585 3375 undef_sstr:
20408e3c
GS
3376 if (dtype != SVt_PVGV) {
3377 (void)SvOK_off(dstr);
3378 return;
3379 }
3380 break;
463ee0b2 3381 case SVt_IV:
aece5585
GA
3382 if (SvIOK(sstr)) {
3383 switch (dtype) {
3384 case SVt_NULL:
8990e307 3385 sv_upgrade(dstr, SVt_IV);
aece5585
GA
3386 break;
3387 case SVt_NV:
aece5585
GA
3388 case SVt_RV:
3389 case SVt_PV:
a0d0e21e 3390 sv_upgrade(dstr, SVt_PVIV);
aece5585
GA
3391 break;
3392 }
3393 (void)SvIOK_only(dstr);
45977657 3394 SvIV_set(dstr, SvIVX(sstr));
25da4f38
IZ
3395 if (SvIsUV(sstr))
3396 SvIsUV_on(dstr);
37c25af0
NC
3397 /* SvTAINTED can only be true if the SV has taint magic, which in
3398 turn means that the SV type is PVMG (or greater). This is the
3399 case statement for SVt_IV, so this cannot be true (whatever gcov
3400 may say). */
3401 assert(!SvTAINTED(sstr));
aece5585 3402 return;
8990e307 3403 }
aece5585
GA
3404 goto undef_sstr;
3405
463ee0b2 3406 case SVt_NV:
aece5585
GA
3407 if (SvNOK(sstr)) {
3408 switch (dtype) {
3409 case SVt_NULL:
3410 case SVt_IV:
8990e307 3411 sv_upgrade(dstr, SVt_NV);
aece5585
GA
3412 break;
3413 case SVt_RV:
3414 case SVt_PV:
3415 case SVt_PVIV:
a0d0e21e 3416 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
3417 break;
3418 }
9d6ce603 3419 SvNV_set(dstr, SvNVX(sstr));
aece5585 3420 (void)SvNOK_only(dstr);
37c25af0
NC
3421 /* SvTAINTED can only be true if the SV has taint magic, which in
3422 turn means that the SV type is PVMG (or greater). This is the
3423 case statement for SVt_NV, so this cannot be true (whatever gcov
3424 may say). */
3425 assert(!SvTAINTED(sstr));
aece5585 3426 return;
8990e307 3427 }
aece5585
GA
3428 goto undef_sstr;
3429
ed6116ce 3430 case SVt_RV:
8990e307 3431 if (dtype < SVt_RV)
ed6116ce 3432 sv_upgrade(dstr, SVt_RV);
ed6116ce 3433 break;
fc36a67e 3434 case SVt_PVFM:
f8c7b90f 3435#ifdef PERL_OLD_COPY_ON_WRITE
d89fc664
NC
3436 if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) {
3437 if (dtype < SVt_PVIV)
3438 sv_upgrade(dstr, SVt_PVIV);
3439 break;
3440 }
3441 /* Fall through */
3442#endif
3443 case SVt_PV:
8990e307 3444 if (dtype < SVt_PV)
463ee0b2 3445 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
3446 break;
3447 case SVt_PVIV:
8990e307 3448 if (dtype < SVt_PVIV)
463ee0b2 3449 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
3450 break;
3451 case SVt_PVNV:
8990e307 3452 if (dtype < SVt_PVNV)
463ee0b2 3453 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 3454 break;
489f7bfe 3455 default:
a3b680e6
AL
3456 {
3457 const char * const type = sv_reftype(sstr,0);
533c011a 3458 if (PL_op)
a3b680e6 3459 Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_NAME(PL_op));
4633a7c4 3460 else
a3b680e6
AL
3461 Perl_croak(aTHX_ "Bizarre copy of %s", type);
3462 }
4633a7c4
LW
3463 break;
3464
79072805 3465 case SVt_PVGV:
8990e307 3466 if (dtype <= SVt_PVGV) {
d4c19fe8 3467 glob_assign_glob(dstr, sstr, dtype);
b8c701c1 3468 return;
79072805 3469 }
5f66b61c 3470 /*FALLTHROUGH*/
79072805 3471
489f7bfe
NC
3472 case SVt_PVMG:
3473 case SVt_PVLV:
3474 case SVt_PVBM:
8d6d96c1 3475 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
973f89ab 3476 mg_get(sstr);
eb160463 3477 if ((int)SvTYPE(sstr) != stype) {
973f89ab 3478 stype = SvTYPE(sstr);
b8c701c1 3479 if (stype == SVt_PVGV && dtype <= SVt_PVGV) {
d4c19fe8 3480 glob_assign_glob(dstr, sstr, dtype);
b8c701c1
NC
3481 return;
3482 }
973f89ab
CS
3483 }
3484 }
ded42b9f 3485 if (stype == SVt_PVLV)
862a34c6 3486 SvUPGRADE(dstr, SVt_PVNV);
ded42b9f 3487 else
862a34c6 3488 SvUPGRADE(dstr, (U32)stype);
79072805
LW
3489 }
3490
ff920335
NC
3491 /* dstr may have been upgraded. */
3492 dtype = SvTYPE(dstr);
8990e307
LW
3493 sflags = SvFLAGS(sstr);
3494
3495 if (sflags & SVf_ROK) {
acaa9288
NC
3496 if (dtype == SVt_PVGV &&
3497 SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) {
3498 sstr = SvRV(sstr);
3499 if (sstr == dstr) {
3500 if (GvIMPORTED(dstr) != GVf_IMPORTED
3501 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3502 {
3503 GvIMPORTED_on(dstr);
3504 }
3505 GvMULTI_on(dstr);
3506 return;
3507 }
d4c19fe8 3508 glob_assign_glob(dstr, sstr, dtype);
acaa9288
NC
3509 return;
3510 }
3511
8990e307 3512 if (dtype >= SVt_PV) {
b8c701c1 3513 if (dtype == SVt_PVGV) {
d4c19fe8 3514 glob_assign_ref(dstr, sstr);
b8c701c1
NC
3515 return;
3516 }
3f7c398e 3517 if (SvPVX_const(dstr)) {
8bd4d4c5 3518 SvPV_free(dstr);
b162af07
SP
3519 SvLEN_set(dstr, 0);
3520 SvCUR_set(dstr, 0);
a0d0e21e 3521 }
8990e307 3522 }
a0d0e21e 3523 (void)SvOK_off(dstr);
b162af07 3524 SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
dfd48732
NC
3525 SvFLAGS(dstr) |= sflags & (SVf_ROK|SVf_AMAGIC);
3526 assert(!(sflags & SVp_NOK));
3527 assert(!(sflags & SVp_IOK));
3528 assert(!(sflags & SVf_NOK));
3529 assert(!(sflags & SVf_IOK));
ed6116ce 3530 }
c0c44674
NC
3531 else if (dtype == SVt_PVGV) {
3532 if (!(sflags & SVf_OK)) {
3533 if (ckWARN(WARN_MISC))
3534 Perl_warner(aTHX_ packWARN(WARN_MISC),
3535 "Undefined value assigned to typeglob");
3536 }
3537 else {
3538 GV *gv = gv_fetchsv(sstr, GV_ADD, SVt_PVGV);
3539 if (dstr != (SV*)gv) {
3540 if (GvGP(dstr))
3541 gp_free((GV*)dstr);
3542 GvGP(dstr) = gp_ref(GvGP(gv));
3543 }
3544 }
3545 }
8990e307 3546 else if (sflags & SVp_POK) {
765f542d 3547 bool isSwipe = 0;
79072805
LW
3548
3549 /*
3550 * Check to see if we can just swipe the string. If so, it's a
3551 * possible small lose on short strings, but a big win on long ones.
3f7c398e
SP
3552 * It might even be a win on short strings if SvPVX_const(dstr)
3553 * has to be allocated and SvPVX_const(sstr) has to be freed.
79072805
LW
3554 */
3555
120fac95
NC
3556 /* Whichever path we take through the next code, we want this true,
3557 and doing it now facilitates the COW check. */
3558 (void)SvPOK_only(dstr);
3559
765f542d 3560 if (
b8f9541a
NC
3561 /* We're not already COW */
3562 ((sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY)
f8c7b90f 3563#ifndef PERL_OLD_COPY_ON_WRITE
b8f9541a
NC
3564 /* or we are, but dstr isn't a suitable target. */
3565 || (SvFLAGS(dstr) & CAN_COW_MASK) != CAN_COW_FLAGS
3566#endif
3567 )
765f542d 3568 &&
765f542d
NC
3569 !(isSwipe =
3570 (sflags & SVs_TEMP) && /* slated for free anyway? */
3571 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
5fcdf167
NC
3572 (!(flags & SV_NOSTEAL)) &&
3573 /* and we're allowed to steal temps */
765f542d
NC
3574 SvREFCNT(sstr) == 1 && /* and no other references to it? */
3575 SvLEN(sstr) && /* and really is a string */
645c22ef 3576 /* and won't be needed again, potentially */
765f542d 3577 !(PL_op && PL_op->op_type == OP_AASSIGN))
f8c7b90f 3578#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 3579 && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS
120fac95 3580 && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS
765f542d
NC
3581 && SvTYPE(sstr) >= SVt_PVIV)
3582#endif
3583 ) {
3584 /* Failed the swipe test, and it's not a shared hash key either.
3585 Have to copy the string. */
3586 STRLEN len = SvCUR(sstr);
3587 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
3f7c398e 3588 Move(SvPVX_const(sstr),SvPVX(dstr),len,char);
765f542d
NC
3589 SvCUR_set(dstr, len);
3590 *SvEND(dstr) = '\0';
765f542d 3591 } else {
f8c7b90f 3592 /* If PERL_OLD_COPY_ON_WRITE is not defined, then isSwipe will always
765f542d 3593 be true in here. */
765f542d
NC
3594 /* Either it's a shared hash key, or it's suitable for
3595 copy-on-write or we can swipe the string. */
46187eeb 3596 if (DEBUG_C_TEST) {
ed252734 3597 PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
e419cbc5
NC
3598 sv_dump(sstr);
3599 sv_dump(dstr);
46187eeb 3600 }
f8c7b90f 3601#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
3602 if (!isSwipe) {
3603 /* I believe I should acquire a global SV mutex if
3604 it's a COW sv (not a shared hash key) to stop
3605 it going un copy-on-write.
3606 If the source SV has gone un copy on write between up there
3607 and down here, then (assert() that) it is of the correct
3608 form to make it copy on write again */
3609 if ((sflags & (SVf_FAKE | SVf_READONLY))
3610 != (SVf_FAKE | SVf_READONLY)) {
3611 SvREADONLY_on(sstr);
3612 SvFAKE_on(sstr);
3613 /* Make the source SV into a loop of 1.
3614 (about to become 2) */
a29f6d03 3615 SV_COW_NEXT_SV_SET(sstr, sstr);
765f542d
NC
3616 }
3617 }
3618#endif
3619 /* Initial code is common. */
94010e71
NC
3620 if (SvPVX_const(dstr)) { /* we know that dtype >= SVt_PV */
3621 SvPV_free(dstr);
79072805 3622 }
765f542d 3623
765f542d
NC
3624 if (!isSwipe) {
3625 /* making another shared SV. */
3626 STRLEN cur = SvCUR(sstr);
3627 STRLEN len = SvLEN(sstr);
f8c7b90f 3628#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 3629 if (len) {
b8f9541a 3630 assert (SvTYPE(dstr) >= SVt_PVIV);
765f542d
NC
3631 /* SvIsCOW_normal */
3632 /* splice us in between source and next-after-source. */
a29f6d03
NC
3633 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
3634 SV_COW_NEXT_SV_SET(sstr, dstr);
940132f3 3635 SvPV_set(dstr, SvPVX_mutable(sstr));
a604c751
NC
3636 } else
3637#endif
3638 {
765f542d 3639 /* SvIsCOW_shared_hash */
46187eeb
NC
3640 DEBUG_C(PerlIO_printf(Perl_debug_log,
3641 "Copy on write: Sharing hash\n"));
b8f9541a 3642
bdd68bc3 3643 assert (SvTYPE(dstr) >= SVt_PV);
765f542d 3644 SvPV_set(dstr,
d1db91c6 3645 HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)))));
bdd68bc3 3646 }
87a1ef3d
SP
3647 SvLEN_set(dstr, len);
3648 SvCUR_set(dstr, cur);
765f542d
NC
3649 SvREADONLY_on(dstr);
3650 SvFAKE_on(dstr);
3651 /* Relesase a global SV mutex. */
3652 }
3653 else
765f542d 3654 { /* Passes the swipe test. */
78d1e721 3655 SvPV_set(dstr, SvPVX_mutable(sstr));
765f542d
NC
3656 SvLEN_set(dstr, SvLEN(sstr));
3657 SvCUR_set(dstr, SvCUR(sstr));
3658
3659 SvTEMP_off(dstr);
3660 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
6136c704 3661 SvPV_set(sstr, NULL);
765f542d
NC
3662 SvLEN_set(sstr, 0);
3663 SvCUR_set(sstr, 0);
3664 SvTEMP_off(sstr);
3665 }
3666 }
8990e307 3667 if (sflags & SVp_NOK) {
9d6ce603 3668 SvNV_set(dstr, SvNVX(sstr));
79072805 3669 }
8990e307 3670 if (sflags & SVp_IOK) {
23525414
NC
3671 SvRELEASE_IVX(dstr);
3672 SvIV_set(dstr, SvIVX(sstr));
3673 /* Must do this otherwise some other overloaded use of 0x80000000
3674 gets confused. I guess SVpbm_VALID */
2b1c7e3e 3675 if (sflags & SVf_IVisUV)
25da4f38 3676 SvIsUV_on(dstr);
79072805 3677 }
dd2eae66
NC
3678 SvFLAGS(dstr) |= sflags & (SVf_IOK|SVp_IOK|SVf_NOK|SVp_NOK|SVf_UTF8
3679 |SVf_AMAGIC);
4f2da183
NC
3680 {
3681 const MAGIC * const smg = SvVOK(sstr);
3682 if (smg) {
3683 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
3684 smg->mg_ptr, smg->mg_len);
3685 SvRMAGICAL_on(dstr);
3686 }
7a5fa8a2 3687 }
79072805 3688 }
5d581361 3689 else if (sflags & (SVp_IOK|SVp_NOK)) {
c2468cc7 3690 (void)SvOK_off(dstr);
dd2eae66
NC
3691 SvFLAGS(dstr) |= sflags & (SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_NOK|SVp_NOK
3692 |SVf_AMAGIC);
5d581361
NC
3693 if (sflags & SVp_IOK) {
3694 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
3695 SvIV_set(dstr, SvIVX(sstr));
3696 }
3332b3c1 3697 if (sflags & SVp_NOK) {
9d6ce603 3698 SvNV_set(dstr, SvNVX(sstr));
3332b3c1
JH
3699 }
3700 }
79072805 3701 else {
f7877b28 3702 if (isGV_with_GP(sstr)) {
180488f8
NC
3703 /* This stringification rule for globs is spread in 3 places.
3704 This feels bad. FIXME. */
3705 const U32 wasfake = sflags & SVf_FAKE;
3706
3707 /* FAKE globs can get coerced, so need to turn this off
3708 temporarily if it is on. */
3709 SvFAKE_off(sstr);
3710 gv_efullname3(dstr, (GV *)sstr, "*");
3711 SvFLAGS(sstr) |= wasfake;
dd2eae66 3712 SvFLAGS(dstr) |= sflags & SVf_AMAGIC;
180488f8 3713 }
20408e3c
GS
3714 else
3715 (void)SvOK_off(dstr);
a0d0e21e 3716 }
27c9684d
AP
3717 if (SvTAINTED(sstr))
3718 SvTAINT(dstr);
79072805
LW
3719}
3720
954c1994
GS
3721/*
3722=for apidoc sv_setsv_mg
3723
3724Like C<sv_setsv>, but also handles 'set' magic.
3725
3726=cut
3727*/
3728
79072805 3729void
864dbfa3 3730Perl_sv_setsv_mg(pTHX_ SV *dstr, register