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