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