This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Skip no-common-vars optimisation for aliases
[perl5.git] / sv.c
CommitLineData
a0d0e21e 1/* sv.c
79072805 2 *
1129b882 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
83706693
RGS
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall
5 * and others
79072805
LW
6 *
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
9 *
4ac71550
TC
10 */
11
12/*
13 * 'I wonder what the Entish is for "yes" and "no",' he thought.
14 * --Pippin
15 *
16 * [p.480 of _The Lord of the Rings_, III/iv: "Treebeard"]
17 */
18
19/*
645c22ef
DM
20 *
21 *
5e045b90
AMS
22 * This file contains the code that creates, manipulates and destroys
23 * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the
24 * structure of an SV, so their creation and destruction is handled
25 * here; higher-level functions are in av.c, hv.c, and so on. Opcode
26 * level functions (eg. substr, split, join) for each of the types are
27 * in the pp*.c files.
79072805
LW
28 */
29
30#include "EXTERN.h"
864dbfa3 31#define PERL_IN_SV_C
79072805 32#include "perl.h"
d2f185dc 33#include "regcomp.h"
9d9a81f0
CB
34#ifdef __VMS
35# include <rms.h>
36#endif
79072805 37
2f8ed50e
OS
38#ifdef __Lynx__
39/* Missing proto on LynxOS */
40 char *gconvert(double, int, int, char *);
41#endif
42
e8c6a474
YO
43#ifdef PERL_NEW_COPY_ON_WRITE
44# ifndef SV_COW_THRESHOLD
45# define SV_COW_THRESHOLD 0 /* COW iff len > K */
46# endif
47# ifndef SV_COWBUF_THRESHOLD
48# define SV_COWBUF_THRESHOLD 1250 /* COW iff len > K */
49# endif
50# ifndef SV_COW_MAX_WASTE_THRESHOLD
51# define SV_COW_MAX_WASTE_THRESHOLD 80 /* COW iff (len - cur) < K */
52# endif
53# ifndef SV_COWBUF_WASTE_THRESHOLD
54# define SV_COWBUF_WASTE_THRESHOLD 80 /* COW iff (len - cur) < K */
55# endif
56# ifndef SV_COW_MAX_WASTE_FACTOR_THRESHOLD
57# define SV_COW_MAX_WASTE_FACTOR_THRESHOLD 2 /* COW iff len < (cur * K) */
58# endif
59# ifndef SV_COWBUF_WASTE_FACTOR_THRESHOLD
60# define SV_COWBUF_WASTE_FACTOR_THRESHOLD 2 /* COW iff len < (cur * K) */
61# endif
62#endif
63/* Work around compiler warnings about unsigned >= THRESHOLD when thres-
64 hold is 0. */
65#if SV_COW_THRESHOLD
66# define GE_COW_THRESHOLD(cur) ((cur) >= SV_COW_THRESHOLD)
67#else
68# define GE_COW_THRESHOLD(cur) 1
69#endif
70#if SV_COWBUF_THRESHOLD
71# define GE_COWBUF_THRESHOLD(cur) ((cur) >= SV_COWBUF_THRESHOLD)
72#else
73# define GE_COWBUF_THRESHOLD(cur) 1
74#endif
75#if SV_COW_MAX_WASTE_THRESHOLD
76# define GE_COW_MAX_WASTE_THRESHOLD(cur,len) (((len)-(cur)) < SV_COW_MAX_WASTE_THRESHOLD)
77#else
78# define GE_COW_MAX_WASTE_THRESHOLD(cur,len) 1
79#endif
80#if SV_COWBUF_WASTE_THRESHOLD
81# define GE_COWBUF_WASTE_THRESHOLD(cur,len) (((len)-(cur)) < SV_COWBUF_WASTE_THRESHOLD)
82#else
83# define GE_COWBUF_WASTE_THRESHOLD(cur,len) 1
84#endif
85#if SV_COW_MAX_WASTE_FACTOR_THRESHOLD
86# define GE_COW_MAX_WASTE_FACTOR_THRESHOLD(cur,len) ((len) < SV_COW_MAX_WASTE_FACTOR_THRESHOLD * (cur))
87#else
88# define GE_COW_MAX_WASTE_FACTOR_THRESHOLD(cur,len) 1
89#endif
90#if SV_COWBUF_WASTE_FACTOR_THRESHOLD
91# define GE_COWBUF_WASTE_FACTOR_THRESHOLD(cur,len) ((len) < SV_COWBUF_WASTE_FACTOR_THRESHOLD * (cur))
92#else
93# define GE_COWBUF_WASTE_FACTOR_THRESHOLD(cur,len) 1
94#endif
95
96#define CHECK_COW_THRESHOLD(cur,len) (\
97 GE_COW_THRESHOLD((cur)) && \
98 GE_COW_MAX_WASTE_THRESHOLD((cur),(len)) && \
99 GE_COW_MAX_WASTE_FACTOR_THRESHOLD((cur),(len)) \
100)
101#define CHECK_COWBUF_THRESHOLD(cur,len) (\
102 GE_COWBUF_THRESHOLD((cur)) && \
103 GE_COWBUF_WASTE_THRESHOLD((cur),(len)) && \
104 GE_COWBUF_WASTE_FACTOR_THRESHOLD((cur),(len)) \
105)
cca0492e 106
e23c8137 107#ifdef PERL_UTF8_CACHE_ASSERT
ab455f60 108/* if adding more checks watch out for the following tests:
e23c8137
JH
109 * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
110 * lib/utf8.t lib/Unicode/Collate/t/index.t
111 * --jhi
112 */
6f207bd3 113# define ASSERT_UTF8_CACHE(cache) \
ab455f60
NC
114 STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); \
115 assert((cache)[2] <= (cache)[3]); \
116 assert((cache)[3] <= (cache)[1]);} \
117 } STMT_END
e23c8137 118#else
6f207bd3 119# define ASSERT_UTF8_CACHE(cache) NOOP
e23c8137
JH
120#endif
121
f8c7b90f 122#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 123#define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
607fa7f2 124#define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next))
765f542d 125#endif
645c22ef
DM
126
127/* ============================================================================
128
129=head1 Allocation and deallocation of SVs.
d2a0f284
JC
130An SV (or AV, HV, etc.) is allocated in two parts: the head (struct
131sv, av, hv...) contains type and reference count information, and for
132many types, a pointer to the body (struct xrv, xpv, xpviv...), which
133contains fields specific to each type. Some types store all they need
134in the head, so don't have a body.
135
486ec47a 136In all but the most memory-paranoid configurations (ex: PURIFY), heads
d2a0f284
JC
137and bodies are allocated out of arenas, which by default are
138approximately 4K chunks of memory parcelled up into N heads or bodies.
93e68bfb
JC
139Sv-bodies are allocated by their sv-type, guaranteeing size
140consistency needed to allocate safely from arrays.
141
d2a0f284
JC
142For SV-heads, the first slot in each arena is reserved, and holds a
143link to the next arena, some flags, and a note of the number of slots.
144Snaked through each arena chain is a linked list of free items; when
145this becomes empty, an extra arena is allocated and divided up into N
146items which are threaded into the free list.
147
148SV-bodies are similar, but they use arena-sets by default, which
149separate the link and info from the arena itself, and reclaim the 1st
150slot in the arena. SV-bodies are further described later.
645c22ef
DM
151
152The following global variables are associated with arenas:
153
7fefc6c1
KW
154 PL_sv_arenaroot pointer to list of SV arenas
155 PL_sv_root pointer to list of free SV structures
645c22ef 156
7fefc6c1
KW
157 PL_body_arenas head of linked-list of body arenas
158 PL_body_roots[] array of pointers to list of free bodies of svtype
159 arrays are indexed by the svtype needed
93e68bfb 160
d2a0f284
JC
161A few special SV heads are not allocated from an arena, but are
162instead directly created in the interpreter structure, eg PL_sv_undef.
93e68bfb
JC
163The size of arenas can be changed from the default by setting
164PERL_ARENA_SIZE appropriately at compile time.
645c22ef
DM
165
166The SV arena serves the secondary purpose of allowing still-live SVs
167to be located and destroyed during final cleanup.
168
169At the lowest level, the macros new_SV() and del_SV() grab and free
170an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
171to return the SV to the free list with error checking.) new_SV() calls
172more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
173SVs in the free list have their SvTYPE field set to all ones.
174
ff276b08 175At the time of very final cleanup, sv_free_arenas() is called from
645c22ef 176perl_destruct() to physically free all the arenas allocated since the
6a93a7e5 177start of the interpreter.
645c22ef 178
645c22ef
DM
179The function visit() scans the SV arenas list, and calls a specified
180function for each SV it finds which is still live - ie which has an SvTYPE
181other than all 1's, and a non-zero SvREFCNT. visit() is used by the
182following functions (specified as [function that calls visit()] / [function
183called by visit() for each SV]):
184
185 sv_report_used() / do_report_used()
f2524eef 186 dump all remaining SVs (debugging aid)
645c22ef 187
e4487e9b 188 sv_clean_objs() / do_clean_objs(),do_clean_named_objs(),
e76981f9 189 do_clean_named_io_objs(),do_curse()
645c22ef 190 Attempt to free all objects pointed to by RVs,
e76981f9
FC
191 try to do the same for all objects indir-
192 ectly referenced by typeglobs too, and
193 then do a final sweep, cursing any
194 objects that remain. Called once from
645c22ef
DM
195 perl_destruct(), prior to calling sv_clean_all()
196 below.
197
198 sv_clean_all() / do_clean_all()
199 SvREFCNT_dec(sv) each remaining SV, possibly
200 triggering an sv_free(). It also sets the
201 SVf_BREAK flag on the SV to indicate that the
202 refcnt has been artificially lowered, and thus
203 stopping sv_free() from giving spurious warnings
204 about SVs which unexpectedly have a refcnt
205 of zero. called repeatedly from perl_destruct()
206 until there are no SVs left.
207
93e68bfb 208=head2 Arena allocator API Summary
645c22ef
DM
209
210Private API to rest of sv.c
211
212 new_SV(), del_SV(),
213
df0f0429 214 new_XPVNV(), del_XPVGV(),
645c22ef
DM
215 etc
216
217Public API:
218
8cf8f3d1 219 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
645c22ef 220
645c22ef
DM
221=cut
222
3e8320cc 223 * ========================================================================= */
645c22ef 224
4561caa4
CS
225/*
226 * "A time to plant, and a time to uproot what was planted..."
227 */
228
d7a2c63c
MHM
229#ifdef PERL_MEM_LOG
230# define MEM_LOG_NEW_SV(sv, file, line, func) \
231 Perl_mem_log_new_sv(sv, file, line, func)
232# define MEM_LOG_DEL_SV(sv, file, line, func) \
233 Perl_mem_log_del_sv(sv, file, line, func)
234#else
235# define MEM_LOG_NEW_SV(sv, file, line, func) NOOP
236# define MEM_LOG_DEL_SV(sv, file, line, func) NOOP
237#endif
238
fd0854ff 239#ifdef DEBUG_LEAKING_SCALARS
484e6108
FC
240# define FREE_SV_DEBUG_FILE(sv) STMT_START { \
241 if ((sv)->sv_debug_file) PerlMemShared_free((sv)->sv_debug_file); \
242 } STMT_END
d7a2c63c
MHM
243# define DEBUG_SV_SERIAL(sv) \
244 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) del_SV\n", \
245 PTR2UV(sv), (long)(sv)->sv_debug_serial))
fd0854ff
DM
246#else
247# define FREE_SV_DEBUG_FILE(sv)
d7a2c63c 248# define DEBUG_SV_SERIAL(sv) NOOP
fd0854ff
DM
249#endif
250
48614a46
NC
251#ifdef PERL_POISON
252# define SvARENA_CHAIN(sv) ((sv)->sv_u.svu_rv)
daba3364 253# define SvARENA_CHAIN_SET(sv,val) (sv)->sv_u.svu_rv = MUTABLE_SV((val))
48614a46
NC
254/* Whilst I'd love to do this, it seems that things like to check on
255 unreferenced scalars
7e337ee0 256# define POSION_SV_HEAD(sv) PoisonNew(sv, 1, struct STRUCT_SV)
48614a46 257*/
7e337ee0
JH
258# define POSION_SV_HEAD(sv) PoisonNew(&SvANY(sv), 1, void *), \
259 PoisonNew(&SvREFCNT(sv), 1, U32)
48614a46
NC
260#else
261# define SvARENA_CHAIN(sv) SvANY(sv)
3eef1deb 262# define SvARENA_CHAIN_SET(sv,val) SvANY(sv) = (void *)(val)
48614a46
NC
263# define POSION_SV_HEAD(sv)
264#endif
265
990198f0
DM
266/* Mark an SV head as unused, and add to free list.
267 *
268 * If SVf_BREAK is set, skip adding it to the free list, as this SV had
269 * its refcount artificially decremented during global destruction, so
270 * there may be dangling pointers to it. The last thing we want in that
271 * case is for it to be reused. */
272
053fc874
GS
273#define plant_SV(p) \
274 STMT_START { \
990198f0 275 const U32 old_flags = SvFLAGS(p); \
d7a2c63c
MHM
276 MEM_LOG_DEL_SV(p, __FILE__, __LINE__, FUNCTION__); \
277 DEBUG_SV_SERIAL(p); \
fd0854ff 278 FREE_SV_DEBUG_FILE(p); \
48614a46 279 POSION_SV_HEAD(p); \
053fc874 280 SvFLAGS(p) = SVTYPEMASK; \
990198f0 281 if (!(old_flags & SVf_BREAK)) { \
3eef1deb 282 SvARENA_CHAIN_SET(p, PL_sv_root); \
990198f0
DM
283 PL_sv_root = (p); \
284 } \
053fc874
GS
285 --PL_sv_count; \
286 } STMT_END
a0d0e21e 287
053fc874
GS
288#define uproot_SV(p) \
289 STMT_START { \
290 (p) = PL_sv_root; \
daba3364 291 PL_sv_root = MUTABLE_SV(SvARENA_CHAIN(p)); \
053fc874
GS
292 ++PL_sv_count; \
293 } STMT_END
294
645c22ef 295
cac9b346
NC
296/* make some more SVs by adding another arena */
297
cac9b346
NC
298STATIC SV*
299S_more_sv(pTHX)
300{
301 SV* sv;
9a87bd09
NC
302 char *chunk; /* must use New here to match call to */
303 Newx(chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
304 sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
cac9b346
NC
305 uproot_SV(sv);
306 return sv;
307}
308
645c22ef
DM
309/* new_SV(): return a new, empty SV head */
310
eba0f806
DM
311#ifdef DEBUG_LEAKING_SCALARS
312/* provide a real function for a debugger to play with */
313STATIC SV*
d7a2c63c 314S_new_SV(pTHX_ const char *file, int line, const char *func)
eba0f806
DM
315{
316 SV* sv;
317
eba0f806
DM
318 if (PL_sv_root)
319 uproot_SV(sv);
320 else
cac9b346 321 sv = S_more_sv(aTHX);
eba0f806
DM
322 SvANY(sv) = 0;
323 SvREFCNT(sv) = 1;
324 SvFLAGS(sv) = 0;
fd0854ff 325 sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
e385c3bf
DM
326 sv->sv_debug_line = (U16) (PL_parser && PL_parser->copline != NOLINE
327 ? PL_parser->copline
328 : PL_curcop
f24aceb1
DM
329 ? CopLINE(PL_curcop)
330 : 0
e385c3bf 331 );
fd0854ff 332 sv->sv_debug_inpad = 0;
cd676548 333 sv->sv_debug_parent = NULL;
484e6108 334 sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL;
d7a2c63c
MHM
335
336 sv->sv_debug_serial = PL_sv_serial++;
337
338 MEM_LOG_NEW_SV(sv, file, line, func);
339 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) new_SV (from %s:%d [%s])\n",
340 PTR2UV(sv), (long)sv->sv_debug_serial, file, line, func));
341
eba0f806
DM
342 return sv;
343}
d7a2c63c 344# define new_SV(p) (p)=S_new_SV(aTHX_ __FILE__, __LINE__, FUNCTION__)
eba0f806
DM
345
346#else
347# define new_SV(p) \
053fc874 348 STMT_START { \
053fc874
GS
349 if (PL_sv_root) \
350 uproot_SV(p); \
351 else \
cac9b346 352 (p) = S_more_sv(aTHX); \
053fc874
GS
353 SvANY(p) = 0; \
354 SvREFCNT(p) = 1; \
355 SvFLAGS(p) = 0; \
d7a2c63c 356 MEM_LOG_NEW_SV(p, __FILE__, __LINE__, FUNCTION__); \
053fc874 357 } STMT_END
eba0f806 358#endif
463ee0b2 359
645c22ef
DM
360
361/* del_SV(): return an empty SV head to the free list */
362
a0d0e21e 363#ifdef DEBUGGING
4561caa4 364
053fc874
GS
365#define del_SV(p) \
366 STMT_START { \
aea4f609 367 if (DEBUG_D_TEST) \
053fc874
GS
368 del_sv(p); \
369 else \
370 plant_SV(p); \
053fc874 371 } STMT_END
a0d0e21e 372
76e3520e 373STATIC void
cea2e8a9 374S_del_sv(pTHX_ SV *p)
463ee0b2 375{
7918f24d
NC
376 PERL_ARGS_ASSERT_DEL_SV;
377
aea4f609 378 if (DEBUG_D_TEST) {
4633a7c4 379 SV* sva;
a3b680e6 380 bool ok = 0;
daba3364 381 for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
53c1dcc0
AL
382 const SV * const sv = sva + 1;
383 const SV * const svend = &sva[SvREFCNT(sva)];
c0ff570e 384 if (p >= sv && p < svend) {
a0d0e21e 385 ok = 1;
c0ff570e
NC
386 break;
387 }
a0d0e21e
LW
388 }
389 if (!ok) {
9b387841
NC
390 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
391 "Attempt to free non-arena SV: 0x%"UVxf
392 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
a0d0e21e
LW
393 return;
394 }
395 }
4561caa4 396 plant_SV(p);
463ee0b2 397}
a0d0e21e 398
4561caa4
CS
399#else /* ! DEBUGGING */
400
401#define del_SV(p) plant_SV(p)
402
403#endif /* DEBUGGING */
463ee0b2 404
645c22ef
DM
405
406/*
ccfc67b7
JH
407=head1 SV Manipulation Functions
408
645c22ef
DM
409=for apidoc sv_add_arena
410
411Given a chunk of memory, link it to the head of the list of arenas,
412and split it into a list of free SVs.
413
414=cut
415*/
416
d2bd4e7f
NC
417static void
418S_sv_add_arena(pTHX_ char *const ptr, const U32 size, const U32 flags)
463ee0b2 419{
daba3364 420 SV *const sva = MUTABLE_SV(ptr);
eb578fdb
KW
421 SV* sv;
422 SV* svend;
4633a7c4 423
7918f24d
NC
424 PERL_ARGS_ASSERT_SV_ADD_ARENA;
425
4633a7c4 426 /* The first SV in an arena isn't an SV. */
3280af22 427 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
4633a7c4
LW
428 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
429 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
430
3280af22
NIS
431 PL_sv_arenaroot = sva;
432 PL_sv_root = sva + 1;
4633a7c4
LW
433
434 svend = &sva[SvREFCNT(sva) - 1];
435 sv = sva + 1;
463ee0b2 436 while (sv < svend) {
3eef1deb 437 SvARENA_CHAIN_SET(sv, (sv + 1));
03e36789 438#ifdef DEBUGGING
978b032e 439 SvREFCNT(sv) = 0;
03e36789 440#endif
4b69cbe3 441 /* Must always set typemask because it's always checked in on cleanup
03e36789 442 when the arenas are walked looking for objects. */
8990e307 443 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
444 sv++;
445 }
3eef1deb 446 SvARENA_CHAIN_SET(sv, 0);
03e36789
NC
447#ifdef DEBUGGING
448 SvREFCNT(sv) = 0;
449#endif
4633a7c4
LW
450 SvFLAGS(sv) = SVTYPEMASK;
451}
452
055972dc
DM
453/* visit(): call the named function for each non-free SV in the arenas
454 * whose flags field matches the flags/mask args. */
645c22ef 455
5226ed68 456STATIC I32
de37a194 457S_visit(pTHX_ SVFUNC_t f, const U32 flags, const U32 mask)
8990e307 458{
4633a7c4 459 SV* sva;
5226ed68 460 I32 visited = 0;
8990e307 461
7918f24d
NC
462 PERL_ARGS_ASSERT_VISIT;
463
daba3364 464 for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
eb578fdb
KW
465 const SV * const svend = &sva[SvREFCNT(sva)];
466 SV* sv;
4561caa4 467 for (sv = sva + 1; sv < svend; ++sv) {
e4787c0c 468 if (SvTYPE(sv) != (svtype)SVTYPEMASK
055972dc
DM
469 && (sv->sv_flags & mask) == flags
470 && SvREFCNT(sv))
471 {
942481a7 472 (*f)(aTHX_ sv);
5226ed68
JH
473 ++visited;
474 }
8990e307
LW
475 }
476 }
5226ed68 477 return visited;
8990e307
LW
478}
479
758a08c3
JH
480#ifdef DEBUGGING
481
645c22ef
DM
482/* called by sv_report_used() for each live SV */
483
484static void
5fa45a31 485do_report_used(pTHX_ SV *const sv)
645c22ef 486{
e4787c0c 487 if (SvTYPE(sv) != (svtype)SVTYPEMASK) {
645c22ef
DM
488 PerlIO_printf(Perl_debug_log, "****\n");
489 sv_dump(sv);
490 }
491}
758a08c3 492#endif
645c22ef
DM
493
494/*
495=for apidoc sv_report_used
496
fde67290 497Dump the contents of all SVs not yet freed (debugging aid).
645c22ef
DM
498
499=cut
500*/
501
8990e307 502void
864dbfa3 503Perl_sv_report_used(pTHX)
4561caa4 504{
ff270d3a 505#ifdef DEBUGGING
055972dc 506 visit(do_report_used, 0, 0);
96a5add6
AL
507#else
508 PERL_UNUSED_CONTEXT;
ff270d3a 509#endif
4561caa4
CS
510}
511
645c22ef
DM
512/* called by sv_clean_objs() for each live SV */
513
514static void
de37a194 515do_clean_objs(pTHX_ SV *const ref)
645c22ef 516{
ea724faa
NC
517 assert (SvROK(ref));
518 {
823a54a3
AL
519 SV * const target = SvRV(ref);
520 if (SvOBJECT(target)) {
521 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(ref)));
522 if (SvWEAKREF(ref)) {
523 sv_del_backref(target, ref);
524 SvWEAKREF_off(ref);
525 SvRV_set(ref, NULL);
526 } else {
527 SvROK_off(ref);
528 SvRV_set(ref, NULL);
fc2b2dca 529 SvREFCNT_dec_NN(target);
823a54a3 530 }
645c22ef
DM
531 }
532 }
645c22ef
DM
533}
534
645c22ef 535
e4487e9b
DM
536/* clear any slots in a GV which hold objects - except IO;
537 * called by sv_clean_objs() for each live GV */
538
645c22ef 539static void
f30de749 540do_clean_named_objs(pTHX_ SV *const sv)
645c22ef 541{
57ef47cc 542 SV *obj;
ea724faa 543 assert(SvTYPE(sv) == SVt_PVGV);
d011219a 544 assert(isGV_with_GP(sv));
57ef47cc
DM
545 if (!GvGP(sv))
546 return;
547
548 /* freeing GP entries may indirectly free the current GV;
549 * hold onto it while we mess with the GP slots */
550 SvREFCNT_inc(sv);
551
552 if ( ((obj = GvSV(sv) )) && SvOBJECT(obj)) {
553 DEBUG_D((PerlIO_printf(Perl_debug_log,
554 "Cleaning named glob SV object:\n "), sv_dump(obj)));
555 GvSV(sv) = NULL;
fc2b2dca 556 SvREFCNT_dec_NN(obj);
57ef47cc
DM
557 }
558 if ( ((obj = MUTABLE_SV(GvAV(sv)) )) && SvOBJECT(obj)) {
559 DEBUG_D((PerlIO_printf(Perl_debug_log,
560 "Cleaning named glob AV object:\n "), sv_dump(obj)));
561 GvAV(sv) = NULL;
fc2b2dca 562 SvREFCNT_dec_NN(obj);
57ef47cc
DM
563 }
564 if ( ((obj = MUTABLE_SV(GvHV(sv)) )) && SvOBJECT(obj)) {
565 DEBUG_D((PerlIO_printf(Perl_debug_log,
566 "Cleaning named glob HV object:\n "), sv_dump(obj)));
567 GvHV(sv) = NULL;
fc2b2dca 568 SvREFCNT_dec_NN(obj);
57ef47cc
DM
569 }
570 if ( ((obj = MUTABLE_SV(GvCV(sv)) )) && SvOBJECT(obj)) {
571 DEBUG_D((PerlIO_printf(Perl_debug_log,
572 "Cleaning named glob CV object:\n "), sv_dump(obj)));
c43ae56f 573 GvCV_set(sv, NULL);
fc2b2dca 574 SvREFCNT_dec_NN(obj);
57ef47cc 575 }
fc2b2dca 576 SvREFCNT_dec_NN(sv); /* undo the inc above */
e4487e9b
DM
577}
578
68b590d9 579/* clear any IO slots in a GV which hold objects (except stderr, defout);
e4487e9b
DM
580 * called by sv_clean_objs() for each live GV */
581
582static void
583do_clean_named_io_objs(pTHX_ SV *const sv)
584{
e4487e9b
DM
585 SV *obj;
586 assert(SvTYPE(sv) == SVt_PVGV);
587 assert(isGV_with_GP(sv));
68b590d9 588 if (!GvGP(sv) || sv == (SV*)PL_stderrgv || sv == (SV*)PL_defoutgv)
e4487e9b
DM
589 return;
590
591 SvREFCNT_inc(sv);
57ef47cc
DM
592 if ( ((obj = MUTABLE_SV(GvIO(sv)) )) && SvOBJECT(obj)) {
593 DEBUG_D((PerlIO_printf(Perl_debug_log,
594 "Cleaning named glob IO object:\n "), sv_dump(obj)));
595 GvIOp(sv) = NULL;
fc2b2dca 596 SvREFCNT_dec_NN(obj);
645c22ef 597 }
fc2b2dca 598 SvREFCNT_dec_NN(sv); /* undo the inc above */
645c22ef 599}
645c22ef 600
4155e4fe
FC
601/* Void wrapper to pass to visit() */
602static void
603do_curse(pTHX_ SV * const sv) {
c2910e6c
FC
604 if ((PL_stderrgv && GvGP(PL_stderrgv) && (SV*)GvIO(PL_stderrgv) == sv)
605 || (PL_defoutgv && GvGP(PL_defoutgv) && (SV*)GvIO(PL_defoutgv) == sv))
4155e4fe
FC
606 return;
607 (void)curse(sv, 0);
608}
609
645c22ef
DM
610/*
611=for apidoc sv_clean_objs
612
fde67290 613Attempt to destroy all objects not yet freed.
645c22ef
DM
614
615=cut
616*/
617
4561caa4 618void
864dbfa3 619Perl_sv_clean_objs(pTHX)
4561caa4 620{
68b590d9 621 GV *olddef, *olderr;
3280af22 622 PL_in_clean_objs = TRUE;
055972dc 623 visit(do_clean_objs, SVf_ROK, SVf_ROK);
e4487e9b
DM
624 /* Some barnacles may yet remain, clinging to typeglobs.
625 * Run the non-IO destructors first: they may want to output
626 * error messages, close files etc */
d011219a 627 visit(do_clean_named_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
e4487e9b 628 visit(do_clean_named_io_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
4155e4fe
FC
629 /* And if there are some very tenacious barnacles clinging to arrays,
630 closures, or what have you.... */
631 visit(do_curse, SVs_OBJECT, SVs_OBJECT);
68b590d9
DM
632 olddef = PL_defoutgv;
633 PL_defoutgv = NULL; /* disable skip of PL_defoutgv */
634 if (olddef && isGV_with_GP(olddef))
635 do_clean_named_io_objs(aTHX_ MUTABLE_SV(olddef));
636 olderr = PL_stderrgv;
637 PL_stderrgv = NULL; /* disable skip of PL_stderrgv */
638 if (olderr && isGV_with_GP(olderr))
639 do_clean_named_io_objs(aTHX_ MUTABLE_SV(olderr));
640 SvREFCNT_dec(olddef);
3280af22 641 PL_in_clean_objs = FALSE;
4561caa4
CS
642}
643
645c22ef
DM
644/* called by sv_clean_all() for each live SV */
645
646static void
de37a194 647do_clean_all(pTHX_ SV *const sv)
645c22ef 648{
daba3364 649 if (sv == (const SV *) PL_fdpid || sv == (const SV *)PL_strtab) {
cddfcddc 650 /* don't clean pid table and strtab */
d17ea597 651 return;
cddfcddc 652 }
645c22ef
DM
653 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
654 SvFLAGS(sv) |= SVf_BREAK;
fc2b2dca 655 SvREFCNT_dec_NN(sv);
645c22ef
DM
656}
657
658/*
659=for apidoc sv_clean_all
660
661Decrement the refcnt of each remaining SV, possibly triggering a
fde67290 662cleanup. This function may have to be called multiple times to free
ff276b08 663SVs which are in complex self-referential hierarchies.
645c22ef
DM
664
665=cut
666*/
667
5226ed68 668I32
864dbfa3 669Perl_sv_clean_all(pTHX)
8990e307 670{
5226ed68 671 I32 cleaned;
3280af22 672 PL_in_clean_all = TRUE;
055972dc 673 cleaned = visit(do_clean_all, 0,0);
5226ed68 674 return cleaned;
8990e307 675}
463ee0b2 676
5e258f8c
JC
677/*
678 ARENASETS: a meta-arena implementation which separates arena-info
679 into struct arena_set, which contains an array of struct
680 arena_descs, each holding info for a single arena. By separating
681 the meta-info from the arena, we recover the 1st slot, formerly
682 borrowed for list management. The arena_set is about the size of an
39244528 683 arena, avoiding the needless malloc overhead of a naive linked-list.
5e258f8c
JC
684
685 The cost is 1 arena-set malloc per ~320 arena-mallocs, + the unused
686 memory in the last arena-set (1/2 on average). In trade, we get
687 back the 1st slot in each arena (ie 1.7% of a CV-arena, less for
d2a0f284 688 smaller types). The recovery of the wasted space allows use of
e15dad31
JC
689 small arenas for large, rare body types, by changing array* fields
690 in body_details_by_type[] below.
5e258f8c 691*/
5e258f8c 692struct arena_desc {
398c677b
NC
693 char *arena; /* the raw storage, allocated aligned */
694 size_t size; /* its size ~4k typ */
e5973ed5 695 svtype utype; /* bodytype stored in arena */
5e258f8c
JC
696};
697
e6148039
NC
698struct arena_set;
699
700/* Get the maximum number of elements in set[] such that struct arena_set
e15dad31 701 will fit within PERL_ARENA_SIZE, which is probably just under 4K, and
e6148039
NC
702 therefore likely to be 1 aligned memory page. */
703
704#define ARENAS_PER_SET ((PERL_ARENA_SIZE - sizeof(struct arena_set*) \
705 - 2 * sizeof(int)) / sizeof (struct arena_desc))
5e258f8c
JC
706
707struct arena_set {
708 struct arena_set* next;
0a848332
NC
709 unsigned int set_size; /* ie ARENAS_PER_SET */
710 unsigned int curr; /* index of next available arena-desc */
5e258f8c
JC
711 struct arena_desc set[ARENAS_PER_SET];
712};
713
645c22ef
DM
714/*
715=for apidoc sv_free_arenas
716
fde67290 717Deallocate the memory used by all arenas. Note that all the individual SV
645c22ef
DM
718heads and bodies within the arenas must already have been freed.
719
720=cut
7fefc6c1 721
645c22ef 722*/
4633a7c4 723void
864dbfa3 724Perl_sv_free_arenas(pTHX)
4633a7c4
LW
725{
726 SV* sva;
727 SV* svanext;
0a848332 728 unsigned int i;
4633a7c4
LW
729
730 /* Free arenas here, but be careful about fake ones. (We assume
731 contiguity of the fake ones with the corresponding real ones.) */
732
3280af22 733 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
daba3364 734 svanext = MUTABLE_SV(SvANY(sva));
4633a7c4 735 while (svanext && SvFAKE(svanext))
daba3364 736 svanext = MUTABLE_SV(SvANY(svanext));
4633a7c4
LW
737
738 if (!SvFAKE(sva))
1df70142 739 Safefree(sva);
4633a7c4 740 }
93e68bfb 741
5e258f8c 742 {
0a848332
NC
743 struct arena_set *aroot = (struct arena_set*) PL_body_arenas;
744
745 while (aroot) {
746 struct arena_set *current = aroot;
747 i = aroot->curr;
748 while (i--) {
5e258f8c
JC
749 assert(aroot->set[i].arena);
750 Safefree(aroot->set[i].arena);
751 }
0a848332
NC
752 aroot = aroot->next;
753 Safefree(current);
5e258f8c
JC
754 }
755 }
dc8220bf 756 PL_body_arenas = 0;
fdda85ca 757
0a848332
NC
758 i = PERL_ARENA_ROOTS_SIZE;
759 while (i--)
93e68bfb 760 PL_body_roots[i] = 0;
93e68bfb 761
3280af22
NIS
762 PL_sv_arenaroot = 0;
763 PL_sv_root = 0;
4633a7c4
LW
764}
765
bd81e77b
NC
766/*
767 Here are mid-level routines that manage the allocation of bodies out
768 of the various arenas. There are 5 kinds of arenas:
29489e7c 769
bd81e77b
NC
770 1. SV-head arenas, which are discussed and handled above
771 2. regular body arenas
772 3. arenas for reduced-size bodies
773 4. Hash-Entry arenas
29489e7c 774
bd81e77b
NC
775 Arena types 2 & 3 are chained by body-type off an array of
776 arena-root pointers, which is indexed by svtype. Some of the
777 larger/less used body types are malloced singly, since a large
778 unused block of them is wasteful. Also, several svtypes dont have
779 bodies; the data fits into the sv-head itself. The arena-root
780 pointer thus has a few unused root-pointers (which may be hijacked
781 later for arena types 4,5)
29489e7c 782
bd81e77b
NC
783 3 differs from 2 as an optimization; some body types have several
784 unused fields in the front of the structure (which are kept in-place
785 for consistency). These bodies can be allocated in smaller chunks,
786 because the leading fields arent accessed. Pointers to such bodies
787 are decremented to point at the unused 'ghost' memory, knowing that
788 the pointers are used with offsets to the real memory.
29489e7c 789
d2a0f284
JC
790
791=head1 SV-Body Allocation
792
f554dfc5
MH
793=cut
794
d2a0f284
JC
795Allocation of SV-bodies is similar to SV-heads, differing as follows;
796the allocation mechanism is used for many body types, so is somewhat
797more complicated, it uses arena-sets, and has no need for still-live
798SV detection.
799
800At the outermost level, (new|del)_X*V macros return bodies of the
801appropriate type. These macros call either (new|del)_body_type or
802(new|del)_body_allocated macro pairs, depending on specifics of the
803type. Most body types use the former pair, the latter pair is used to
804allocate body types with "ghost fields".
805
806"ghost fields" are fields that are unused in certain types, and
69ba284b 807consequently don't need to actually exist. They are declared because
d2a0f284
JC
808they're part of a "base type", which allows use of functions as
809methods. The simplest examples are AVs and HVs, 2 aggregate types
810which don't use the fields which support SCALAR semantics.
811
69ba284b 812For these types, the arenas are carved up into appropriately sized
d2a0f284
JC
813chunks, we thus avoid wasted memory for those unaccessed members.
814When bodies are allocated, we adjust the pointer back in memory by the
69ba284b 815size of the part not allocated, so it's as if we allocated the full
d2a0f284
JC
816structure. (But things will all go boom if you write to the part that
817is "not there", because you'll be overwriting the last members of the
818preceding structure in memory.)
819
69ba284b 820We calculate the correction using the STRUCT_OFFSET macro on the first
a05ea1cf 821member present. If the allocated structure is smaller (no initial NV
69ba284b
NC
822actually allocated) then the net effect is to subtract the size of the NV
823from the pointer, to return a new pointer as if an initial NV were actually
a05ea1cf 824allocated. (We were using structures named *_allocated for this, but
69ba284b
NC
825this turned out to be a subtle bug, because a structure without an NV
826could have a lower alignment constraint, but the compiler is allowed to
827optimised accesses based on the alignment constraint of the actual pointer
828to the full structure, for example, using a single 64 bit load instruction
829because it "knows" that two adjacent 32 bit members will be 8-byte aligned.)
d2a0f284 830
a05ea1cf 831This is the same trick as was used for NV and IV bodies. Ironically it
d2a0f284 832doesn't need to be used for NV bodies any more, because NV is now at
a05ea1cf 833the start of the structure. IV bodies don't need it either, because
d2a0f284
JC
834they are no longer allocated.
835
836In turn, the new_body_* allocators call S_new_body(), which invokes
837new_body_inline macro, which takes a lock, and takes a body off the
1e30fcd5 838linked list at PL_body_roots[sv_type], calling Perl_more_bodies() if
d2a0f284
JC
839necessary to refresh an empty list. Then the lock is released, and
840the body is returned.
841
99816f8d 842Perl_more_bodies allocates a new arena, and carves it up into an array of N
d2a0f284
JC
843bodies, which it strings into a linked list. It looks up arena-size
844and body-size from the body_details table described below, thus
845supporting the multiple body-types.
846
847If PURIFY is defined, or PERL_ARENA_SIZE=0, arenas are not used, and
848the (new|del)_X*V macros are mapped directly to malloc/free.
849
d2a0f284
JC
850For each sv-type, struct body_details bodies_by_type[] carries
851parameters which control these aspects of SV handling:
852
853Arena_size determines whether arenas are used for this body type, and if
854so, how big they are. PURIFY or PERL_ARENA_SIZE=0 set this field to
855zero, forcing individual mallocs and frees.
856
857Body_size determines how big a body is, and therefore how many fit into
858each arena. Offset carries the body-pointer adjustment needed for
69ba284b 859"ghost fields", and is used in *_allocated macros.
d2a0f284
JC
860
861But its main purpose is to parameterize info needed in
862Perl_sv_upgrade(). The info here dramatically simplifies the function
69ba284b 863vs the implementation in 5.8.8, making it table-driven. All fields
d2a0f284
JC
864are used for this, except for arena_size.
865
866For the sv-types that have no bodies, arenas are not used, so those
867PL_body_roots[sv_type] are unused, and can be overloaded. In
868something of a special case, SVt_NULL is borrowed for HE arenas;
c6f8b1d0 869PL_body_roots[HE_SVSLOT=SVt_NULL] is filled by S_more_he, but the
d2a0f284 870bodies_by_type[SVt_NULL] slot is not used, as the table is not
c6f8b1d0 871available in hv.c.
d2a0f284 872
29489e7c
DM
873*/
874
bd81e77b 875struct body_details {
0fb58b32 876 U8 body_size; /* Size to allocate */
10666ae3 877 U8 copy; /* Size of structure to copy (may be shorter) */
0fb58b32 878 U8 offset;
10666ae3
NC
879 unsigned int type : 4; /* We have space for a sanity check. */
880 unsigned int cant_upgrade : 1; /* Cannot upgrade this type */
881 unsigned int zero_nv : 1; /* zero the NV when upgrading from this */
882 unsigned int arena : 1; /* Allocated from an arena */
883 size_t arena_size; /* Size of arena to allocate */
bd81e77b 884};
29489e7c 885
bd81e77b
NC
886#define HADNV FALSE
887#define NONV TRUE
29489e7c 888
d2a0f284 889
bd81e77b
NC
890#ifdef PURIFY
891/* With -DPURFIY we allocate everything directly, and don't use arenas.
892 This seems a rather elegant way to simplify some of the code below. */
893#define HASARENA FALSE
894#else
895#define HASARENA TRUE
896#endif
897#define NOARENA FALSE
29489e7c 898
d2a0f284
JC
899/* Size the arenas to exactly fit a given number of bodies. A count
900 of 0 fits the max number bodies into a PERL_ARENA_SIZE.block,
901 simplifying the default. If count > 0, the arena is sized to fit
902 only that many bodies, allowing arenas to be used for large, rare
903 bodies (XPVFM, XPVIO) without undue waste. The arena size is
904 limited by PERL_ARENA_SIZE, so we can safely oversize the
905 declarations.
906 */
95db5f15
MB
907#define FIT_ARENA0(body_size) \
908 ((size_t)(PERL_ARENA_SIZE / body_size) * body_size)
909#define FIT_ARENAn(count,body_size) \
910 ( count * body_size <= PERL_ARENA_SIZE) \
911 ? count * body_size \
912 : FIT_ARENA0 (body_size)
913#define FIT_ARENA(count,body_size) \
914 count \
915 ? FIT_ARENAn (count, body_size) \
916 : FIT_ARENA0 (body_size)
d2a0f284 917
bd81e77b
NC
918/* Calculate the length to copy. Specifically work out the length less any
919 final padding the compiler needed to add. See the comment in sv_upgrade
920 for why copying the padding proved to be a bug. */
29489e7c 921
bd81e77b
NC
922#define copy_length(type, last_member) \
923 STRUCT_OFFSET(type, last_member) \
daba3364 924 + sizeof (((type*)SvANY((const SV *)0))->last_member)
29489e7c 925
bd81e77b 926static const struct body_details bodies_by_type[] = {
829cd18a
NC
927 /* HEs use this offset for their arena. */
928 { 0, 0, 0, SVt_NULL, FALSE, NONV, NOARENA, 0 },
d2a0f284 929
db93c0c4
NC
930 /* IVs are in the head, so the allocation size is 0. */
931 { 0,
d2a0f284 932 sizeof(IV), /* This is used to copy out the IV body. */
10666ae3 933 STRUCT_OFFSET(XPVIV, xiv_iv), SVt_IV, FALSE, NONV,
db93c0c4 934 NOARENA /* IVS don't need an arena */, 0
d2a0f284
JC
935 },
936
6e128786
NC
937 { sizeof(NV), sizeof(NV),
938 STRUCT_OFFSET(XPVNV, xnv_u),
939 SVt_NV, FALSE, HADNV, HASARENA, FIT_ARENA(0, sizeof(NV)) },
d2a0f284 940
bc337e5c 941 { sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur),
889d28b2
NC
942 copy_length(XPV, xpv_len) - STRUCT_OFFSET(XPV, xpv_cur),
943 + STRUCT_OFFSET(XPV, xpv_cur),
69ba284b 944 SVt_PV, FALSE, NONV, HASARENA,
889d28b2 945 FIT_ARENA(0, sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur)) },
d2a0f284 946
d361b004
KW
947 { sizeof(XINVLIST) - STRUCT_OFFSET(XPV, xpv_cur),
948 copy_length(XINVLIST, is_offset) - STRUCT_OFFSET(XPV, xpv_cur),
949 + STRUCT_OFFSET(XPV, xpv_cur),
950 SVt_INVLIST, TRUE, NONV, HASARENA,
951 FIT_ARENA(0, sizeof(XINVLIST) - STRUCT_OFFSET(XPV, xpv_cur)) },
e94d9b54 952
bc337e5c 953 { sizeof(XPVIV) - STRUCT_OFFSET(XPV, xpv_cur),
889d28b2
NC
954 copy_length(XPVIV, xiv_u) - STRUCT_OFFSET(XPV, xpv_cur),
955 + STRUCT_OFFSET(XPV, xpv_cur),
956 SVt_PVIV, FALSE, NONV, HASARENA,
957 FIT_ARENA(0, sizeof(XPVIV) - STRUCT_OFFSET(XPV, xpv_cur)) },
d2a0f284 958
bc337e5c 959 { sizeof(XPVNV) - STRUCT_OFFSET(XPV, xpv_cur),
889d28b2
NC
960 copy_length(XPVNV, xnv_u) - STRUCT_OFFSET(XPV, xpv_cur),
961 + STRUCT_OFFSET(XPV, xpv_cur),
962 SVt_PVNV, FALSE, HADNV, HASARENA,
963 FIT_ARENA(0, sizeof(XPVNV) - STRUCT_OFFSET(XPV, xpv_cur)) },
d2a0f284 964
6e128786 965 { sizeof(XPVMG), copy_length(XPVMG, xnv_u), 0, SVt_PVMG, FALSE, HADNV,
d2a0f284 966 HASARENA, FIT_ARENA(0, sizeof(XPVMG)) },
4df7f6af 967
601dfd0a
NC
968 { sizeof(regexp),
969 sizeof(regexp),
970 0,
ecff11eb 971 SVt_REGEXP, TRUE, NONV, HASARENA,
eaeb1e7f 972 FIT_ARENA(0, sizeof(regexp))
5c35adbb 973 },
4df7f6af 974
10666ae3 975 { sizeof(XPVGV), sizeof(XPVGV), 0, SVt_PVGV, TRUE, HADNV,
d2a0f284
JC
976 HASARENA, FIT_ARENA(0, sizeof(XPVGV)) },
977
10666ae3 978 { sizeof(XPVLV), sizeof(XPVLV), 0, SVt_PVLV, TRUE, HADNV,
d2a0f284
JC
979 HASARENA, FIT_ARENA(0, sizeof(XPVLV)) },
980
601dfd0a 981 { sizeof(XPVAV),
4f7003f5 982 copy_length(XPVAV, xav_alloc),
601dfd0a 983 0,
69ba284b 984 SVt_PVAV, TRUE, NONV, HASARENA,
601dfd0a 985 FIT_ARENA(0, sizeof(XPVAV)) },
d2a0f284 986
601dfd0a 987 { sizeof(XPVHV),
359164a0 988 copy_length(XPVHV, xhv_max),
601dfd0a 989 0,
69ba284b 990 SVt_PVHV, TRUE, NONV, HASARENA,
601dfd0a 991 FIT_ARENA(0, sizeof(XPVHV)) },
d2a0f284 992
601dfd0a
NC
993 { sizeof(XPVCV),
994 sizeof(XPVCV),
995 0,
69ba284b 996 SVt_PVCV, TRUE, NONV, HASARENA,
601dfd0a 997 FIT_ARENA(0, sizeof(XPVCV)) },
69ba284b 998
601dfd0a
NC
999 { sizeof(XPVFM),
1000 sizeof(XPVFM),
1001 0,
69ba284b 1002 SVt_PVFM, TRUE, NONV, NOARENA,
601dfd0a 1003 FIT_ARENA(20, sizeof(XPVFM)) },
d2a0f284 1004
601dfd0a
NC
1005 { sizeof(XPVIO),
1006 sizeof(XPVIO),
1007 0,
b6f60916 1008 SVt_PVIO, TRUE, NONV, HASARENA,
601dfd0a 1009 FIT_ARENA(24, sizeof(XPVIO)) },
bd81e77b 1010};
29489e7c 1011
bd81e77b 1012#define new_body_allocated(sv_type) \
d2a0f284 1013 (void *)((char *)S_new_body(aTHX_ sv_type) \
bd81e77b 1014 - bodies_by_type[sv_type].offset)
29489e7c 1015
26359cfa
NC
1016/* return a thing to the free list */
1017
1018#define del_body(thing, root) \
1019 STMT_START { \
1020 void ** const thing_copy = (void **)thing; \
1021 *thing_copy = *root; \
1022 *root = (void*)thing_copy; \
1023 } STMT_END
29489e7c 1024
bd81e77b 1025#ifdef PURIFY
29489e7c 1026
beeec492
NC
1027#define new_XNV() safemalloc(sizeof(XPVNV))
1028#define new_XPVNV() safemalloc(sizeof(XPVNV))
1029#define new_XPVMG() safemalloc(sizeof(XPVMG))
29489e7c 1030
beeec492 1031#define del_XPVGV(p) safefree(p)
29489e7c 1032
bd81e77b 1033#else /* !PURIFY */
29489e7c 1034
65ac1738 1035#define new_XNV() new_body_allocated(SVt_NV)
65ac1738 1036#define new_XPVNV() new_body_allocated(SVt_PVNV)
65ac1738 1037#define new_XPVMG() new_body_allocated(SVt_PVMG)
645c22ef 1038
26359cfa
NC
1039#define del_XPVGV(p) del_body(p + bodies_by_type[SVt_PVGV].offset, \
1040 &PL_body_roots[SVt_PVGV])
1d7c1841 1041
bd81e77b 1042#endif /* PURIFY */
93e68bfb 1043
bd81e77b 1044/* no arena for you! */
93e68bfb 1045
bd81e77b 1046#define new_NOARENA(details) \
beeec492 1047 safemalloc((details)->body_size + (details)->offset)
bd81e77b 1048#define new_NOARENAZ(details) \
beeec492 1049 safecalloc((details)->body_size + (details)->offset, 1)
d2a0f284 1050
1e30fcd5
NC
1051void *
1052Perl_more_bodies (pTHX_ const svtype sv_type, const size_t body_size,
1053 const size_t arena_size)
d2a0f284 1054{
d2a0f284 1055 void ** const root = &PL_body_roots[sv_type];
99816f8d
NC
1056 struct arena_desc *adesc;
1057 struct arena_set *aroot = (struct arena_set *) PL_body_arenas;
1058 unsigned int curr;
d2a0f284
JC
1059 char *start;
1060 const char *end;
02982131 1061 const size_t good_arena_size = Perl_malloc_good_size(arena_size);
20b7effb
JH
1062#if defined(DEBUGGING) && defined(PERL_GLOBAL_STRUCT)
1063 dVAR;
1064#endif
0b2d3faa 1065#if defined(DEBUGGING) && !defined(PERL_GLOBAL_STRUCT_PRIVATE)
23e9d66c
NC
1066 static bool done_sanity_check;
1067
0b2d3faa
JH
1068 /* PERL_GLOBAL_STRUCT_PRIVATE cannot coexist with global
1069 * variables like done_sanity_check. */
10666ae3 1070 if (!done_sanity_check) {
ea471437 1071 unsigned int i = SVt_LAST;
10666ae3
NC
1072
1073 done_sanity_check = TRUE;
1074
1075 while (i--)
1076 assert (bodies_by_type[i].type == i);
1077 }
1078#endif
1079
02982131 1080 assert(arena_size);
23e9d66c 1081
99816f8d
NC
1082 /* may need new arena-set to hold new arena */
1083 if (!aroot || aroot->curr >= aroot->set_size) {
1084 struct arena_set *newroot;
1085 Newxz(newroot, 1, struct arena_set);
1086 newroot->set_size = ARENAS_PER_SET;
1087 newroot->next = aroot;
1088 aroot = newroot;
1089 PL_body_arenas = (void *) newroot;
1090 DEBUG_m(PerlIO_printf(Perl_debug_log, "new arenaset %p\n", (void*)aroot));
1091 }
1092
1093 /* ok, now have arena-set with at least 1 empty/available arena-desc */
1094 curr = aroot->curr++;
1095 adesc = &(aroot->set[curr]);
1096 assert(!adesc->arena);
1097
1098 Newx(adesc->arena, good_arena_size, char);
1099 adesc->size = good_arena_size;
1100 adesc->utype = sv_type;
1101 DEBUG_m(PerlIO_printf(Perl_debug_log, "arena %d added: %p size %"UVuf"\n",
1102 curr, (void*)adesc->arena, (UV)good_arena_size));
1103
1104 start = (char *) adesc->arena;
d2a0f284 1105
29657bb6
NC
1106 /* Get the address of the byte after the end of the last body we can fit.
1107 Remember, this is integer division: */
02982131 1108 end = start + good_arena_size / body_size * body_size;
d2a0f284 1109
486ec47a 1110 /* computed count doesn't reflect the 1st slot reservation */
d8fca402
NC
1111#if defined(MYMALLOC) || defined(HAS_MALLOC_GOOD_SIZE)
1112 DEBUG_m(PerlIO_printf(Perl_debug_log,
1113 "arena %p end %p arena-size %d (from %d) type %d "
1114 "size %d ct %d\n",
02982131
NC
1115 (void*)start, (void*)end, (int)good_arena_size,
1116 (int)arena_size, sv_type, (int)body_size,
1117 (int)good_arena_size / (int)body_size));
d8fca402 1118#else
d2a0f284
JC
1119 DEBUG_m(PerlIO_printf(Perl_debug_log,
1120 "arena %p end %p arena-size %d type %d size %d ct %d\n",
6c9570dc 1121 (void*)start, (void*)end,
02982131
NC
1122 (int)arena_size, sv_type, (int)body_size,
1123 (int)good_arena_size / (int)body_size));
d8fca402 1124#endif
d2a0f284
JC
1125 *root = (void *)start;
1126
29657bb6
NC
1127 while (1) {
1128 /* Where the next body would start: */
d2a0f284 1129 char * const next = start + body_size;
29657bb6
NC
1130
1131 if (next >= end) {
1132 /* This is the last body: */
1133 assert(next == end);
1134
1135 *(void **)start = 0;
1136 return *root;
1137 }
1138
d2a0f284
JC
1139 *(void**) start = (void *)next;
1140 start = next;
1141 }
d2a0f284
JC
1142}
1143
1144/* grab a new thing from the free list, allocating more if necessary.
1145 The inline version is used for speed in hot routines, and the
1146 function using it serves the rest (unless PURIFY).
1147*/
1148#define new_body_inline(xpv, sv_type) \
1149 STMT_START { \
1150 void ** const r3wt = &PL_body_roots[sv_type]; \
11b79775 1151 xpv = (PTR_TBL_ENT_t*) (*((void **)(r3wt)) \
1e30fcd5 1152 ? *((void **)(r3wt)) : Perl_more_bodies(aTHX_ sv_type, \
02982131
NC
1153 bodies_by_type[sv_type].body_size,\
1154 bodies_by_type[sv_type].arena_size)); \
d2a0f284 1155 *(r3wt) = *(void**)(xpv); \
d2a0f284
JC
1156 } STMT_END
1157
1158#ifndef PURIFY
1159
1160STATIC void *
de37a194 1161S_new_body(pTHX_ const svtype sv_type)
d2a0f284 1162{
d2a0f284
JC
1163 void *xpv;
1164 new_body_inline(xpv, sv_type);
1165 return xpv;
1166}
1167
1168#endif
93e68bfb 1169
238b27b3
NC
1170static const struct body_details fake_rv =
1171 { 0, 0, 0, SVt_IV, FALSE, NONV, NOARENA, 0 };
1172
bd81e77b
NC
1173/*
1174=for apidoc sv_upgrade
93e68bfb 1175
bd81e77b
NC
1176Upgrade an SV to a more complex form. Generally adds a new body type to the
1177SV, then copies across as much information as possible from the old body.
9521ca61
FC
1178It croaks if the SV is already in a more complex form than requested. You
1179generally want to use the C<SvUPGRADE> macro wrapper, which checks the type
1180before calling C<sv_upgrade>, and hence does not croak. See also
1181C<svtype>.
93e68bfb 1182
bd81e77b 1183=cut
93e68bfb 1184*/
93e68bfb 1185
bd81e77b 1186void
5aaab254 1187Perl_sv_upgrade(pTHX_ SV *const sv, svtype new_type)
cac9b346 1188{
bd81e77b
NC
1189 void* old_body;
1190 void* new_body;
42d0e0b7 1191 const svtype old_type = SvTYPE(sv);
d2a0f284 1192 const struct body_details *new_type_details;
238b27b3 1193 const struct body_details *old_type_details
bd81e77b 1194 = bodies_by_type + old_type;
4df7f6af 1195 SV *referant = NULL;
cac9b346 1196
7918f24d
NC
1197 PERL_ARGS_ASSERT_SV_UPGRADE;
1198
1776cbe8
NC
1199 if (old_type == new_type)
1200 return;
1201
1202 /* This clause was purposefully added ahead of the early return above to
1203 the shared string hackery for (sort {$a <=> $b} keys %hash), with the
1204 inference by Nick I-S that it would fix other troublesome cases. See
1205 changes 7162, 7163 (f130fd4589cf5fbb24149cd4db4137c8326f49c1 and parent)
1206
1207 Given that shared hash key scalars are no longer PVIV, but PV, there is
1208 no longer need to unshare so as to free up the IVX slot for its proper
1209 purpose. So it's safe to move the early return earlier. */
1210
093085a8 1211 if (new_type > SVt_PVMG && SvIsCOW(sv)) {
bd81e77b
NC
1212 sv_force_normal_flags(sv, 0);
1213 }
cac9b346 1214
bd81e77b 1215 old_body = SvANY(sv);
de042e1d 1216
bd81e77b
NC
1217 /* Copying structures onto other structures that have been neatly zeroed
1218 has a subtle gotcha. Consider XPVMG
cac9b346 1219
bd81e77b
NC
1220 +------+------+------+------+------+-------+-------+
1221 | NV | CUR | LEN | IV | MAGIC | STASH |
1222 +------+------+------+------+------+-------+-------+
1223 0 4 8 12 16 20 24 28
645c22ef 1224
bd81e77b
NC
1225 where NVs are aligned to 8 bytes, so that sizeof that structure is
1226 actually 32 bytes long, with 4 bytes of padding at the end:
08742458 1227
bd81e77b
NC
1228 +------+------+------+------+------+-------+-------+------+
1229 | NV | CUR | LEN | IV | MAGIC | STASH | ??? |
1230 +------+------+------+------+------+-------+-------+------+
1231 0 4 8 12 16 20 24 28 32
08742458 1232
bd81e77b 1233 so what happens if you allocate memory for this structure:
30f9da9e 1234
bd81e77b
NC
1235 +------+------+------+------+------+-------+-------+------+------+...
1236 | NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME |
1237 +------+------+------+------+------+-------+-------+------+------+...
1238 0 4 8 12 16 20 24 28 32 36
bfc44f79 1239
bd81e77b
NC
1240 zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
1241 expect, because you copy the area marked ??? onto GP. Now, ??? may have
1242 started out as zero once, but it's quite possible that it isn't. So now,
1243 rather than a nicely zeroed GP, you have it pointing somewhere random.
1244 Bugs ensue.
bfc44f79 1245
bd81e77b
NC
1246 (In fact, GP ends up pointing at a previous GP structure, because the
1247 principle cause of the padding in XPVMG getting garbage is a copy of
6c9e42f7
NC
1248 sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob. Right now
1249 this happens to be moot because XPVGV has been re-ordered, with GP
1250 no longer after STASH)
30f9da9e 1251
bd81e77b
NC
1252 So we are careful and work out the size of used parts of all the
1253 structures. */
bfc44f79 1254
bd81e77b
NC
1255 switch (old_type) {
1256 case SVt_NULL:
1257 break;
1258 case SVt_IV:
4df7f6af
NC
1259 if (SvROK(sv)) {
1260 referant = SvRV(sv);
238b27b3
NC
1261 old_type_details = &fake_rv;
1262 if (new_type == SVt_NV)
1263 new_type = SVt_PVNV;
4df7f6af
NC
1264 } else {
1265 if (new_type < SVt_PVIV) {
1266 new_type = (new_type == SVt_NV)
1267 ? SVt_PVNV : SVt_PVIV;
1268 }
bd81e77b
NC
1269 }
1270 break;
1271 case SVt_NV:
1272 if (new_type < SVt_PVNV) {
1273 new_type = SVt_PVNV;
bd81e77b
NC
1274 }
1275 break;
bd81e77b
NC
1276 case SVt_PV:
1277 assert(new_type > SVt_PV);
1278 assert(SVt_IV < SVt_PV);
1279 assert(SVt_NV < SVt_PV);
1280 break;
1281 case SVt_PVIV:
1282 break;
1283 case SVt_PVNV:
1284 break;
1285 case SVt_PVMG:
1286 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1287 there's no way that it can be safely upgraded, because perl.c
1288 expects to Safefree(SvANY(PL_mess_sv)) */
1289 assert(sv != PL_mess_sv);
1290 /* This flag bit is used to mean other things in other scalar types.
1291 Given that it only has meaning inside the pad, it shouldn't be set
1292 on anything that can get upgraded. */
00b1698f 1293 assert(!SvPAD_TYPED(sv));
bd81e77b
NC
1294 break;
1295 default:
2439e033 1296 if (UNLIKELY(old_type_details->cant_upgrade))
c81225bc
NC
1297 Perl_croak(aTHX_ "Can't upgrade %s (%" UVuf ") to %" UVuf,
1298 sv_reftype(sv, 0), (UV) old_type, (UV) new_type);
bd81e77b 1299 }
3376de98 1300
2439e033 1301 if (UNLIKELY(old_type > new_type))
3376de98
NC
1302 Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
1303 (int)old_type, (int)new_type);
1304
2fa1109b 1305 new_type_details = bodies_by_type + new_type;
645c22ef 1306
bd81e77b
NC
1307 SvFLAGS(sv) &= ~SVTYPEMASK;
1308 SvFLAGS(sv) |= new_type;
932e9ff9 1309
ab4416c0
NC
1310 /* This can't happen, as SVt_NULL is <= all values of new_type, so one of
1311 the return statements above will have triggered. */
1312 assert (new_type != SVt_NULL);
bd81e77b 1313 switch (new_type) {
bd81e77b
NC
1314 case SVt_IV:
1315 assert(old_type == SVt_NULL);
1316 SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
1317 SvIV_set(sv, 0);
1318 return;
1319 case SVt_NV:
1320 assert(old_type == SVt_NULL);
1321 SvANY(sv) = new_XNV();
1322 SvNV_set(sv, 0);
1323 return;
bd81e77b 1324 case SVt_PVHV:
bd81e77b 1325 case SVt_PVAV:
d2a0f284 1326 assert(new_type_details->body_size);
c1ae03ae
NC
1327
1328#ifndef PURIFY
1329 assert(new_type_details->arena);
d2a0f284 1330 assert(new_type_details->arena_size);
c1ae03ae 1331 /* This points to the start of the allocated area. */
d2a0f284
JC
1332 new_body_inline(new_body, new_type);
1333 Zero(new_body, new_type_details->body_size, char);
c1ae03ae
NC
1334 new_body = ((char *)new_body) - new_type_details->offset;
1335#else
1336 /* We always allocated the full length item with PURIFY. To do this
1337 we fake things so that arena is false for all 16 types.. */
1338 new_body = new_NOARENAZ(new_type_details);
1339#endif
1340 SvANY(sv) = new_body;
1341 if (new_type == SVt_PVAV) {
1342 AvMAX(sv) = -1;
1343 AvFILLp(sv) = -1;
1344 AvREAL_only(sv);
64484faa 1345 if (old_type_details->body_size) {
ac572bf4
NC
1346 AvALLOC(sv) = 0;
1347 } else {
1348 /* It will have been zeroed when the new body was allocated.
1349 Lets not write to it, in case it confuses a write-back
1350 cache. */
1351 }
78ac7dd9
NC
1352 } else {
1353 assert(!SvOK(sv));
1354 SvOK_off(sv);
1355#ifndef NODEFAULT_SHAREKEYS
1356 HvSHAREKEYS_on(sv); /* key-sharing on by default */
1357#endif
586fc6a3
SM
1358 /* start with PERL_HASH_DEFAULT_HvMAX+1 buckets: */
1359 HvMAX(sv) = PERL_HASH_DEFAULT_HvMAX;
c1ae03ae 1360 }
aeb18a1e 1361
bd81e77b
NC
1362 /* SVt_NULL isn't the only thing upgraded to AV or HV.
1363 The target created by newSVrv also is, and it can have magic.
1364 However, it never has SvPVX set.
1365 */
4df7f6af
NC
1366 if (old_type == SVt_IV) {
1367 assert(!SvROK(sv));
1368 } else if (old_type >= SVt_PV) {
bd81e77b
NC
1369 assert(SvPVX_const(sv) == 0);
1370 }
aeb18a1e 1371
bd81e77b 1372 if (old_type >= SVt_PVMG) {
e736a858 1373 SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_u.xmg_magic);
bd81e77b 1374 SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
797c7171
NC
1375 } else {
1376 sv->sv_u.svu_array = NULL; /* or svu_hash */
bd81e77b
NC
1377 }
1378 break;
93e68bfb 1379
bd81e77b
NC
1380 case SVt_PVIV:
1381 /* XXX Is this still needed? Was it ever needed? Surely as there is
1382 no route from NV to PVIV, NOK can never be true */
1383 assert(!SvNOKp(sv));
1384 assert(!SvNOK(sv));
1385 case SVt_PVIO:
1386 case SVt_PVFM:
bd81e77b
NC
1387 case SVt_PVGV:
1388 case SVt_PVCV:
1389 case SVt_PVLV:
d361b004 1390 case SVt_INVLIST:
12c45b25 1391 case SVt_REGEXP:
bd81e77b
NC
1392 case SVt_PVMG:
1393 case SVt_PVNV:
1394 case SVt_PV:
93e68bfb 1395
d2a0f284 1396 assert(new_type_details->body_size);
bd81e77b
NC
1397 /* We always allocated the full length item with PURIFY. To do this
1398 we fake things so that arena is false for all 16 types.. */
1399 if(new_type_details->arena) {
1400 /* This points to the start of the allocated area. */
d2a0f284
JC
1401 new_body_inline(new_body, new_type);
1402 Zero(new_body, new_type_details->body_size, char);
bd81e77b
NC
1403 new_body = ((char *)new_body) - new_type_details->offset;
1404 } else {
1405 new_body = new_NOARENAZ(new_type_details);
1406 }
1407 SvANY(sv) = new_body;
5e2fc214 1408
bd81e77b 1409 if (old_type_details->copy) {
f9ba3d20
NC
1410 /* There is now the potential for an upgrade from something without
1411 an offset (PVNV or PVMG) to something with one (PVCV, PVFM) */
1412 int offset = old_type_details->offset;
1413 int length = old_type_details->copy;
1414
1415 if (new_type_details->offset > old_type_details->offset) {
d4c19fe8 1416 const int difference
f9ba3d20
NC
1417 = new_type_details->offset - old_type_details->offset;
1418 offset += difference;
1419 length -= difference;
1420 }
1421 assert (length >= 0);
1422
1423 Copy((char *)old_body + offset, (char *)new_body + offset, length,
1424 char);
bd81e77b
NC
1425 }
1426
1427#ifndef NV_ZERO_IS_ALLBITS_ZERO
f2524eef 1428 /* If NV 0.0 is stores as all bits 0 then Zero() already creates a
e5ce394c
NC
1429 * correct 0.0 for us. Otherwise, if the old body didn't have an
1430 * NV slot, but the new one does, then we need to initialise the
1431 * freshly created NV slot with whatever the correct bit pattern is
1432 * for 0.0 */
e22a937e
NC
1433 if (old_type_details->zero_nv && !new_type_details->zero_nv
1434 && !isGV_with_GP(sv))
bd81e77b 1435 SvNV_set(sv, 0);
82048762 1436#endif
5e2fc214 1437
2439e033 1438 if (UNLIKELY(new_type == SVt_PVIO)) {
85dca89a 1439 IO * const io = MUTABLE_IO(sv);
d963bf01 1440 GV *iogv = gv_fetchpvs("IO::File::", GV_ADD, SVt_PVHV);
85dca89a
NC
1441
1442 SvOBJECT_on(io);
1443 /* Clear the stashcache because a new IO could overrule a package
1444 name */
103f5a36 1445 DEBUG_o(Perl_deb(aTHX_ "sv_upgrade clearing PL_stashcache\n"));
85dca89a
NC
1446 hv_clear(PL_stashcache);
1447
85dca89a 1448 SvSTASH_set(io, MUTABLE_HV(SvREFCNT_inc(GvHV(iogv))));
f2524eef 1449 IoPAGE_LEN(sv) = 60;
85dca89a 1450 }
2439e033 1451 if (UNLIKELY(new_type == SVt_REGEXP))
8d919b0a
FC
1452 sv->sv_u.svu_rx = (regexp *)new_body;
1453 else if (old_type < SVt_PV) {
4df7f6af
NC
1454 /* referant will be NULL unless the old type was SVt_IV emulating
1455 SVt_RV */
1456 sv->sv_u.svu_rv = referant;
1457 }
bd81e77b
NC
1458 break;
1459 default:
afd78fd5
JH
1460 Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu",
1461 (unsigned long)new_type);
bd81e77b 1462 }
73171d91 1463
db93c0c4 1464 if (old_type > SVt_IV) {
bd81e77b 1465#ifdef PURIFY
beeec492 1466 safefree(old_body);
bd81e77b 1467#else
bc786448
GG
1468 /* Note that there is an assumption that all bodies of types that
1469 can be upgraded came from arenas. Only the more complex non-
1470 upgradable types are allowed to be directly malloc()ed. */
1471 assert(old_type_details->arena);
bd81e77b
NC
1472 del_body((void*)((char*)old_body + old_type_details->offset),
1473 &PL_body_roots[old_type]);
1474#endif
1475 }
1476}
73171d91 1477
bd81e77b
NC
1478/*
1479=for apidoc sv_backoff
73171d91 1480
fde67290 1481Remove any string offset. You should normally use the C<SvOOK_off> macro
bd81e77b 1482wrapper instead.
73171d91 1483
bd81e77b 1484=cut
73171d91
NC
1485*/
1486
bd81e77b 1487int
ddeaf645 1488Perl_sv_backoff(SV *const sv)
bd81e77b 1489{
69240efd 1490 STRLEN delta;
7a4bba22 1491 const char * const s = SvPVX_const(sv);
7918f24d
NC
1492
1493 PERL_ARGS_ASSERT_SV_BACKOFF;
7918f24d 1494
bd81e77b
NC
1495 assert(SvOOK(sv));
1496 assert(SvTYPE(sv) != SVt_PVHV);
1497 assert(SvTYPE(sv) != SVt_PVAV);
7a4bba22 1498
69240efd
NC
1499 SvOOK_offset(sv, delta);
1500
7a4bba22
NC
1501 SvLEN_set(sv, SvLEN(sv) + delta);
1502 SvPV_set(sv, SvPVX(sv) - delta);
1503 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
bd81e77b
NC
1504 SvFLAGS(sv) &= ~SVf_OOK;
1505 return 0;
1506}
73171d91 1507
bd81e77b
NC
1508/*
1509=for apidoc sv_grow
73171d91 1510
bd81e77b
NC
1511Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1512upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1513Use the C<SvGROW> wrapper instead.
93e68bfb 1514
bd81e77b
NC
1515=cut
1516*/
93e68bfb 1517
e0060e30
FC
1518static void S_sv_uncow(pTHX_ SV * const sv, const U32 flags);
1519
bd81e77b 1520char *
5aaab254 1521Perl_sv_grow(pTHX_ SV *const sv, STRLEN newlen)
bd81e77b 1522{
eb578fdb 1523 char *s;
93e68bfb 1524
7918f24d
NC
1525 PERL_ARGS_ASSERT_SV_GROW;
1526
bd81e77b
NC
1527 if (SvROK(sv))
1528 sv_unref(sv);
1529 if (SvTYPE(sv) < SVt_PV) {
1530 sv_upgrade(sv, SVt_PV);
1531 s = SvPVX_mutable(sv);
1532 }
1533 else if (SvOOK(sv)) { /* pv is offset? */
1534 sv_backoff(sv);
1535 s = SvPVX_mutable(sv);
1536 if (newlen > SvLEN(sv))
1537 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
bd81e77b
NC
1538 }
1539 else
db2c6cb3 1540 {
e0060e30 1541 if (SvIsCOW(sv)) S_sv_uncow(aTHX_ sv, 0);
bd81e77b 1542 s = SvPVX_mutable(sv);
db2c6cb3 1543 }
aeb18a1e 1544
cbcb2a16
DM
1545#ifdef PERL_NEW_COPY_ON_WRITE
1546 /* the new COW scheme uses SvPVX(sv)[SvLEN(sv)-1] (if spare)
3c239bea 1547 * to store the COW count. So in general, allocate one more byte than
cbcb2a16
DM
1548 * asked for, to make it likely this byte is always spare: and thus
1549 * make more strings COW-able.
1550 * If the new size is a big power of two, don't bother: we assume the
1551 * caller wanted a nice 2^N sized block and will be annoyed at getting
1552 * 2^N+1 */
1553 if (newlen & 0xff)
1554 newlen++;
1555#endif
1556
ce861ea7
YO
1557#if defined(PERL_USE_MALLOC_SIZE) && defined(Perl_safesysmalloc_size)
1558#define PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1559#endif
1560
bd81e77b 1561 if (newlen > SvLEN(sv)) { /* need more room? */
f1200559 1562 STRLEN minlen = SvCUR(sv);
3c239bea 1563 minlen += (minlen >> PERL_STRLEN_EXPAND_SHIFT) + 10;
f1200559
WH
1564 if (newlen < minlen)
1565 newlen = minlen;
ce861ea7 1566#ifndef PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
7c641603
KW
1567
1568 /* Don't round up on the first allocation, as odds are pretty good that
1569 * the initial request is accurate as to what is really needed */
ce861ea7 1570 if (SvLEN(sv)) {
e8c6a474 1571 newlen = PERL_STRLEN_ROUNDUP(newlen);
ce861ea7 1572 }
bd81e77b 1573#endif
98653f18 1574 if (SvLEN(sv) && s) {
10edeb5d 1575 s = (char*)saferealloc(s, newlen);
bd81e77b
NC
1576 }
1577 else {
10edeb5d 1578 s = (char*)safemalloc(newlen);
bd81e77b
NC
1579 if (SvPVX_const(sv) && SvCUR(sv)) {
1580 Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
1581 }
1582 }
1583 SvPV_set(sv, s);
ce861ea7 1584#ifdef PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
98653f18
NC
1585 /* Do this here, do it once, do it right, and then we will never get
1586 called back into sv_grow() unless there really is some growing
1587 needed. */
ca7c1a29 1588 SvLEN_set(sv, Perl_safesysmalloc_size(s));
98653f18 1589#else
bd81e77b 1590 SvLEN_set(sv, newlen);
98653f18 1591#endif
bd81e77b
NC
1592 }
1593 return s;
1594}
aeb18a1e 1595
bd81e77b
NC
1596/*
1597=for apidoc sv_setiv
932e9ff9 1598
bd81e77b
NC
1599Copies an integer into the given SV, upgrading first if necessary.
1600Does not handle 'set' magic. See also C<sv_setiv_mg>.
463ee0b2 1601
bd81e77b
NC
1602=cut
1603*/
463ee0b2 1604
bd81e77b 1605void
5aaab254 1606Perl_sv_setiv(pTHX_ SV *const sv, const IV i)
bd81e77b 1607{
7918f24d
NC
1608 PERL_ARGS_ASSERT_SV_SETIV;
1609
bd81e77b
NC
1610 SV_CHECK_THINKFIRST_COW_DROP(sv);
1611 switch (SvTYPE(sv)) {
1612 case SVt_NULL:
bd81e77b 1613 case SVt_NV:
3376de98 1614 sv_upgrade(sv, SVt_IV);
bd81e77b 1615 break;
bd81e77b
NC
1616 case SVt_PV:
1617 sv_upgrade(sv, SVt_PVIV);
1618 break;
463ee0b2 1619
bd81e77b 1620 case SVt_PVGV:
6e592b3a
BM
1621 if (!isGV_with_GP(sv))
1622 break;
bd81e77b
NC
1623 case SVt_PVAV:
1624 case SVt_PVHV:
1625 case SVt_PVCV:
1626 case SVt_PVFM:
1627 case SVt_PVIO:
22e74366 1628 /* diag_listed_as: Can't coerce %s to %s in %s */
bd81e77b
NC
1629 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1630 OP_DESC(PL_op));
42d0e0b7 1631 default: NOOP;
bd81e77b
NC
1632 }
1633 (void)SvIOK_only(sv); /* validate number */
1634 SvIV_set(sv, i);
1635 SvTAINT(sv);
1636}
932e9ff9 1637
bd81e77b
NC
1638/*
1639=for apidoc sv_setiv_mg
d33b2eba 1640
bd81e77b 1641Like C<sv_setiv>, but also handles 'set' magic.
1c846c1f 1642
bd81e77b
NC
1643=cut
1644*/
d33b2eba 1645
bd81e77b 1646void
5aaab254 1647Perl_sv_setiv_mg(pTHX_ SV *const sv, const IV i)
bd81e77b 1648{
7918f24d
NC
1649 PERL_ARGS_ASSERT_SV_SETIV_MG;
1650
bd81e77b
NC
1651 sv_setiv(sv,i);
1652 SvSETMAGIC(sv);
1653}
727879eb 1654
bd81e77b
NC
1655/*
1656=for apidoc sv_setuv
d33b2eba 1657
bd81e77b
NC
1658Copies an unsigned integer into the given SV, upgrading first if necessary.
1659Does not handle 'set' magic. See also C<sv_setuv_mg>.
9b94d1dd 1660
bd81e77b
NC
1661=cut
1662*/
d33b2eba 1663
bd81e77b 1664void
5aaab254 1665Perl_sv_setuv(pTHX_ SV *const sv, const UV u)
bd81e77b 1666{
7918f24d
NC
1667 PERL_ARGS_ASSERT_SV_SETUV;
1668
013abb9b
NC
1669 /* With the if statement to ensure that integers are stored as IVs whenever
1670 possible:
bd81e77b 1671 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d33b2eba 1672
bd81e77b
NC
1673 without
1674 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1c846c1f 1675
013abb9b
NC
1676 If you wish to remove the following if statement, so that this routine
1677 (and its callers) always return UVs, please benchmark to see what the
1678 effect is. Modern CPUs may be different. Or may not :-)
bd81e77b
NC
1679 */
1680 if (u <= (UV)IV_MAX) {
1681 sv_setiv(sv, (IV)u);
1682 return;
1683 }
1684 sv_setiv(sv, 0);
1685 SvIsUV_on(sv);
1686 SvUV_set(sv, u);
1687}
d33b2eba 1688
bd81e77b
NC
1689/*
1690=for apidoc sv_setuv_mg
727879eb 1691
bd81e77b 1692Like C<sv_setuv>, but also handles 'set' magic.
9b94d1dd 1693
bd81e77b
NC
1694=cut
1695*/
5e2fc214 1696
bd81e77b 1697void
5aaab254 1698Perl_sv_setuv_mg(pTHX_ SV *const sv, const UV u)
bd81e77b 1699{
7918f24d
NC
1700 PERL_ARGS_ASSERT_SV_SETUV_MG;
1701
bd81e77b
NC
1702 sv_setuv(sv,u);
1703 SvSETMAGIC(sv);
1704}
5e2fc214 1705
954c1994 1706/*
bd81e77b 1707=for apidoc sv_setnv
954c1994 1708
bd81e77b
NC
1709Copies a double into the given SV, upgrading first if necessary.
1710Does not handle 'set' magic. See also C<sv_setnv_mg>.
954c1994
GS
1711
1712=cut
1713*/
1714
63f97190 1715void
5aaab254 1716Perl_sv_setnv(pTHX_ SV *const sv, const NV num)
79072805 1717{
7918f24d
NC
1718 PERL_ARGS_ASSERT_SV_SETNV;
1719
bd81e77b
NC
1720 SV_CHECK_THINKFIRST_COW_DROP(sv);
1721 switch (SvTYPE(sv)) {
79072805 1722 case SVt_NULL:
79072805 1723 case SVt_IV:
bd81e77b 1724 sv_upgrade(sv, SVt_NV);
79072805
LW
1725 break;
1726 case SVt_PV:
79072805 1727 case SVt_PVIV:
bd81e77b 1728 sv_upgrade(sv, SVt_PVNV);
79072805 1729 break;
bd4b1eb5 1730
bd4b1eb5 1731 case SVt_PVGV:
6e592b3a
BM
1732 if (!isGV_with_GP(sv))
1733 break;
bd81e77b
NC
1734 case SVt_PVAV:
1735 case SVt_PVHV:
79072805 1736 case SVt_PVCV:
bd81e77b
NC
1737 case SVt_PVFM:
1738 case SVt_PVIO:
22e74366 1739 /* diag_listed_as: Can't coerce %s to %s in %s */
bd81e77b 1740 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
94bbb3f4 1741 OP_DESC(PL_op));
42d0e0b7 1742 default: NOOP;
2068cd4d 1743 }
bd81e77b
NC
1744 SvNV_set(sv, num);
1745 (void)SvNOK_only(sv); /* validate number */
1746 SvTAINT(sv);
79072805
LW
1747}
1748
645c22ef 1749/*
bd81e77b 1750=for apidoc sv_setnv_mg
645c22ef 1751
bd81e77b 1752Like C<sv_setnv>, but also handles 'set' magic.
645c22ef
DM
1753
1754=cut
1755*/
1756
bd81e77b 1757void
5aaab254 1758Perl_sv_setnv_mg(pTHX_ SV *const sv, const NV num)
79072805 1759{
7918f24d
NC
1760 PERL_ARGS_ASSERT_SV_SETNV_MG;
1761
bd81e77b
NC
1762 sv_setnv(sv,num);
1763 SvSETMAGIC(sv);
79072805
LW
1764}
1765
3f7602fa
TC
1766/* Return a cleaned-up, printable version of sv, for non-numeric, or
1767 * not incrementable warning display.
1768 * Originally part of S_not_a_number().
1769 * The return value may be != tmpbuf.
bd81e77b 1770 */
954c1994 1771
3f7602fa
TC
1772STATIC const char *
1773S_sv_display(pTHX_ SV *const sv, char *tmpbuf, STRLEN tmpbuf_size) {
1774 const char *pv;
94463019 1775
3f7602fa 1776 PERL_ARGS_ASSERT_SV_DISPLAY;
7918f24d 1777
94463019 1778 if (DO_UTF8(sv)) {
3f7602fa 1779 SV *dsv = newSVpvs_flags("", SVs_TEMP);
472394e4 1780 pv = sv_uni_display(dsv, sv, 10, UNI_DISPLAY_ISPRINT);
94463019
JH
1781 } else {
1782 char *d = tmpbuf;
3f7602fa 1783 const char * const limit = tmpbuf + tmpbuf_size - 8;
94463019
JH
1784 /* each *s can expand to 4 chars + "...\0",
1785 i.e. need room for 8 chars */
ecdeb87c 1786
00b6aa41
AL
1787 const char *s = SvPVX_const(sv);
1788 const char * const end = s + SvCUR(sv);
1789 for ( ; s < end && d < limit; s++ ) {
94463019 1790 int ch = *s & 0xFF;
bd27cf70 1791 if (! isASCII(ch) && !isPRINT_LC(ch)) {
94463019
JH
1792 *d++ = 'M';
1793 *d++ = '-';
bd27cf70
KW
1794
1795 /* Map to ASCII "equivalent" of Latin1 */
1796 ch = LATIN1_TO_NATIVE(NATIVE_TO_LATIN1(ch) & 127);
94463019
JH
1797 }
1798 if (ch == '\n') {
1799 *d++ = '\\';
1800 *d++ = 'n';
1801 }
1802 else if (ch == '\r') {
1803 *d++ = '\\';
1804 *d++ = 'r';
1805 }
1806 else if (ch == '\f') {
1807 *d++ = '\\';
1808 *d++ = 'f';
1809 }
1810 else if (ch == '\\') {
1811 *d++ = '\\';
1812 *d++ = '\\';
1813 }
1814 else if (ch == '\0') {
1815 *d++ = '\\';
1816 *d++ = '0';
1817 }
1818 else if (isPRINT_LC(ch))
1819 *d++ = ch;
1820 else {
1821 *d++ = '^';
1822 *d++ = toCTRL(ch);
1823 }
1824 }
1825 if (s < end) {
1826 *d++ = '.';
1827 *d++ = '.';
1828 *d++ = '.';
1829 }
1830 *d = '\0';
1831 pv = tmpbuf;
a0d0e21e 1832 }
a0d0e21e 1833
3f7602fa
TC
1834 return pv;
1835}
1836
1837/* Print an "isn't numeric" warning, using a cleaned-up,
1838 * printable version of the offending string
1839 */
1840
1841STATIC void
1842S_not_a_number(pTHX_ SV *const sv)
1843{
3f7602fa
TC
1844 char tmpbuf[64];
1845 const char *pv;
1846
1847 PERL_ARGS_ASSERT_NOT_A_NUMBER;
1848
1849 pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
1850
533c011a 1851 if (PL_op)
9014280d 1852 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
734856a2 1853 /* diag_listed_as: Argument "%s" isn't numeric%s */
94463019
JH
1854 "Argument \"%s\" isn't numeric in %s", pv,
1855 OP_DESC(PL_op));
a0d0e21e 1856 else
9014280d 1857 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
734856a2 1858 /* diag_listed_as: Argument "%s" isn't numeric%s */
94463019 1859 "Argument \"%s\" isn't numeric", pv);
a0d0e21e
LW
1860}
1861
3f7602fa
TC
1862STATIC void
1863S_not_incrementable(pTHX_ SV *const sv) {
3f7602fa
TC
1864 char tmpbuf[64];
1865 const char *pv;
1866
1867 PERL_ARGS_ASSERT_NOT_INCREMENTABLE;
1868
1869 pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
1870
1871 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1872 "Argument \"%s\" treated as 0 in increment (++)", pv);
1873}
1874
c2988b20
NC
1875/*
1876=for apidoc looks_like_number
1877
645c22ef
DM
1878Test if the content of an SV looks like a number (or is a number).
1879C<Inf> and C<Infinity> are treated as numbers (so will not issue a
f52e41ad
FC
1880non-numeric warning), even if your atof() doesn't grok them. Get-magic is
1881ignored.
c2988b20
NC
1882
1883=cut
1884*/
1885
1886I32
aad570aa 1887Perl_looks_like_number(pTHX_ SV *const sv)
c2988b20 1888{
eb578fdb 1889 const char *sbegin;
c2988b20
NC
1890 STRLEN len;
1891
7918f24d
NC
1892 PERL_ARGS_ASSERT_LOOKS_LIKE_NUMBER;
1893
f52e41ad
FC
1894 if (SvPOK(sv) || SvPOKp(sv)) {
1895 sbegin = SvPV_nomg_const(sv, len);
c2988b20 1896 }
c2988b20 1897 else
e0ab1c0e 1898 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
c2988b20
NC
1899 return grok_number(sbegin, len, NULL);
1900}
25da4f38 1901
19f6321d
NC
1902STATIC bool
1903S_glob_2number(pTHX_ GV * const gv)
180488f8 1904{
7918f24d
NC
1905 PERL_ARGS_ASSERT_GLOB_2NUMBER;
1906
675c862f
AL
1907 /* We know that all GVs stringify to something that is not-a-number,
1908 so no need to test that. */
1909 if (ckWARN(WARN_NUMERIC))
8e629ff4
FC
1910 {
1911 SV *const buffer = sv_newmortal();
1912 gv_efullname3(buffer, gv, "*");
675c862f 1913 not_a_number(buffer);
8e629ff4 1914 }
675c862f
AL
1915 /* We just want something true to return, so that S_sv_2iuv_common
1916 can tail call us and return true. */
19f6321d 1917 return TRUE;
675c862f
AL
1918}
1919
25da4f38
IZ
1920/* Actually, ISO C leaves conversion of UV to IV undefined, but
1921 until proven guilty, assume that things are not that bad... */
1922
645c22ef
DM
1923/*
1924 NV_PRESERVES_UV:
1925
1926 As 64 bit platforms often have an NV that doesn't preserve all bits of
28e5dec8
JH
1927 an IV (an assumption perl has been based on to date) it becomes necessary
1928 to remove the assumption that the NV always carries enough precision to
1929 recreate the IV whenever needed, and that the NV is the canonical form.
1930 Instead, IV/UV and NV need to be given equal rights. So as to not lose
645c22ef 1931 precision as a side effect of conversion (which would lead to insanity
28e5dec8 1932 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
8a4a3196
KW
1933 1) to distinguish between IV/UV/NV slots that have a valid conversion cached
1934 where precision was lost, and IV/UV/NV slots that have a valid conversion
1935 which has lost no precision
645c22ef 1936 2) to ensure that if a numeric conversion to one form is requested that
28e5dec8
JH
1937 would lose precision, the precise conversion (or differently
1938 imprecise conversion) is also performed and cached, to prevent
1939 requests for different numeric formats on the same SV causing
1940 lossy conversion chains. (lossless conversion chains are perfectly
1941 acceptable (still))
1942
1943
1944 flags are used:
1945 SvIOKp is true if the IV slot contains a valid value
1946 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1947 SvNOKp is true if the NV slot contains a valid value
1948 SvNOK is true only if the NV value is accurate
1949
1950 so
645c22ef 1951 while converting from PV to NV, check to see if converting that NV to an
28e5dec8
JH
1952 IV(or UV) would lose accuracy over a direct conversion from PV to
1953 IV(or UV). If it would, cache both conversions, return NV, but mark
1954 SV as IOK NOKp (ie not NOK).
1955
645c22ef 1956 While converting from PV to IV, check to see if converting that IV to an
28e5dec8
JH
1957 NV would lose accuracy over a direct conversion from PV to NV. If it
1958 would, cache both conversions, flag similarly.
1959
1960 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1961 correctly because if IV & NV were set NV *always* overruled.
645c22ef
DM
1962 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1963 changes - now IV and NV together means that the two are interchangeable:
28e5dec8 1964 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
d460ef45 1965
645c22ef
DM
1966 The benefit of this is that operations such as pp_add know that if
1967 SvIOK is true for both left and right operands, then integer addition
1968 can be used instead of floating point (for cases where the result won't
1969 overflow). Before, floating point was always used, which could lead to
28e5dec8
JH
1970 loss of precision compared with integer addition.
1971
1972 * making IV and NV equal status should make maths accurate on 64 bit
1973 platforms
1974 * may speed up maths somewhat if pp_add and friends start to use
645c22ef 1975 integers when possible instead of fp. (Hopefully the overhead in
28e5dec8
JH
1976 looking for SvIOK and checking for overflow will not outweigh the
1977 fp to integer speedup)
1978 * will slow down integer operations (callers of SvIV) on "inaccurate"
1979 values, as the change from SvIOK to SvIOKp will cause a call into
1980 sv_2iv each time rather than a macro access direct to the IV slot
1981 * should speed up number->string conversion on integers as IV is
645c22ef 1982 favoured when IV and NV are equally accurate
28e5dec8
JH
1983
1984 ####################################################################
645c22ef
DM
1985 You had better be using SvIOK_notUV if you want an IV for arithmetic:
1986 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1987 On the other hand, SvUOK is true iff UV.
28e5dec8
JH
1988 ####################################################################
1989
645c22ef 1990 Your mileage will vary depending your CPU's relative fp to integer
28e5dec8
JH
1991 performance ratio.
1992*/
1993
1994#ifndef NV_PRESERVES_UV
645c22ef
DM
1995# define IS_NUMBER_UNDERFLOW_IV 1
1996# define IS_NUMBER_UNDERFLOW_UV 2
1997# define IS_NUMBER_IV_AND_UV 2
1998# define IS_NUMBER_OVERFLOW_IV 4
1999# define IS_NUMBER_OVERFLOW_UV 5
2000
2001/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
28e5dec8
JH
2002
2003/* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2004STATIC int
5aaab254 2005S_sv_2iuv_non_preserve(pTHX_ SV *const sv
47031da6
NC
2006# ifdef DEBUGGING
2007 , I32 numtype
2008# endif
2009 )
28e5dec8 2010{
7918f24d 2011 PERL_ARGS_ASSERT_SV_2IUV_NON_PRESERVE;
23491f1d 2012 PERL_UNUSED_CONTEXT;
7918f24d 2013
3f7c398e 2014 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
2015 if (SvNVX(sv) < (NV)IV_MIN) {
2016 (void)SvIOKp_on(sv);
2017 (void)SvNOK_on(sv);
45977657 2018 SvIV_set(sv, IV_MIN);
28e5dec8
JH
2019 return IS_NUMBER_UNDERFLOW_IV;
2020 }
2021 if (SvNVX(sv) > (NV)UV_MAX) {
2022 (void)SvIOKp_on(sv);
2023 (void)SvNOK_on(sv);
2024 SvIsUV_on(sv);
607fa7f2 2025 SvUV_set(sv, UV_MAX);
28e5dec8
JH
2026 return IS_NUMBER_OVERFLOW_UV;
2027 }
c2988b20
NC
2028 (void)SvIOKp_on(sv);
2029 (void)SvNOK_on(sv);
2030 /* Can't use strtol etc to convert this string. (See truth table in
2031 sv_2iv */
2032 if (SvNVX(sv) <= (UV)IV_MAX) {
45977657 2033 SvIV_set(sv, I_V(SvNVX(sv)));
659c4b96 2034 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
c2988b20
NC
2035 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2036 } else {
2037 /* Integer is imprecise. NOK, IOKp */
2038 }
2039 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2040 }
2041 SvIsUV_on(sv);
607fa7f2 2042 SvUV_set(sv, U_V(SvNVX(sv)));
659c4b96 2043 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
c2988b20
NC
2044 if (SvUVX(sv) == UV_MAX) {
2045 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2046 possibly be preserved by NV. Hence, it must be overflow.
2047 NOK, IOKp */
2048 return IS_NUMBER_OVERFLOW_UV;
2049 }
2050 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2051 } else {
2052 /* Integer is imprecise. NOK, IOKp */
28e5dec8 2053 }
c2988b20 2054 return IS_NUMBER_OVERFLOW_IV;
28e5dec8 2055}
645c22ef
DM
2056#endif /* !NV_PRESERVES_UV*/
2057
af359546 2058STATIC bool
7918f24d
NC
2059S_sv_2iuv_common(pTHX_ SV *const sv)
2060{
7918f24d
NC
2061 PERL_ARGS_ASSERT_SV_2IUV_COMMON;
2062
af359546 2063 if (SvNOKp(sv)) {
28e5dec8
JH
2064 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2065 * without also getting a cached IV/UV from it at the same time
2066 * (ie PV->NV conversion should detect loss of accuracy and cache
af359546
NC
2067 * IV or UV at same time to avoid this. */
2068 /* IV-over-UV optimisation - choose to cache IV if possible */
25da4f38
IZ
2069
2070 if (SvTYPE(sv) == SVt_NV)
2071 sv_upgrade(sv, SVt_PVNV);
2072
28e5dec8
JH
2073 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2074 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2075 certainly cast into the IV range at IV_MAX, whereas the correct
2076 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2077 cases go to UV */
cab190d4
JD
2078#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2079 if (Perl_isnan(SvNVX(sv))) {
2080 SvUV_set(sv, 0);
2081 SvIsUV_on(sv);
fdbe6d7c 2082 return FALSE;
cab190d4 2083 }
cab190d4 2084#endif
28e5dec8 2085 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2086 SvIV_set(sv, I_V(SvNVX(sv)));
659c4b96 2087 if (SvNVX(sv) == (NV) SvIVX(sv)
28e5dec8
JH
2088#ifndef NV_PRESERVES_UV
2089 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2090 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2091 /* Don't flag it as "accurately an integer" if the number
2092 came from a (by definition imprecise) NV operation, and
2093 we're outside the range of NV integer precision */
2094#endif
2095 ) {
a43d94f2
NC
2096 if (SvNOK(sv))
2097 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2098 else {
2099 /* scalar has trailing garbage, eg "42a" */
2100 }
28e5dec8 2101 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2102 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2103 PTR2UV(sv),
2104 SvNVX(sv),
2105 SvIVX(sv)));
2106
2107 } else {
2108 /* IV not precise. No need to convert from PV, as NV
2109 conversion would already have cached IV if it detected
2110 that PV->IV would be better than PV->NV->IV
2111 flags already correct - don't set public IOK. */
2112 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2113 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2114 PTR2UV(sv),
2115 SvNVX(sv),
2116 SvIVX(sv)));
2117 }
2118 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2119 but the cast (NV)IV_MIN rounds to a the value less (more
2120 negative) than IV_MIN which happens to be equal to SvNVX ??
2121 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2122 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2123 (NV)UVX == NVX are both true, but the values differ. :-(
2124 Hopefully for 2s complement IV_MIN is something like
2125 0x8000000000000000 which will be exact. NWC */
d460ef45 2126 }
25da4f38 2127 else {
607fa7f2 2128 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8 2129 if (
659c4b96 2130 (SvNVX(sv) == (NV) SvUVX(sv))
28e5dec8
JH
2131#ifndef NV_PRESERVES_UV
2132 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2133 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2134 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2135 /* Don't flag it as "accurately an integer" if the number
2136 came from a (by definition imprecise) NV operation, and
2137 we're outside the range of NV integer precision */
2138#endif
a43d94f2 2139 && SvNOK(sv)
28e5dec8
JH
2140 )
2141 SvIOK_on(sv);
25da4f38 2142 SvIsUV_on(sv);
1c846c1f 2143 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 2144 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 2145 PTR2UV(sv),
57def98f
JH
2146 SvUVX(sv),
2147 SvUVX(sv)));
25da4f38 2148 }
748a9306 2149 }
cd84013a 2150 else if (SvPOKp(sv)) {
c2988b20 2151 UV value;
504618e9 2152 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
af359546 2153 /* We want to avoid a possible problem when we cache an IV/ a UV which
25da4f38 2154 may be later translated to an NV, and the resulting NV is not
c2988b20
NC
2155 the same as the direct translation of the initial string
2156 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2157 be careful to ensure that the value with the .456 is around if the
2158 NV value is requested in the future).
1c846c1f 2159
af359546 2160 This means that if we cache such an IV/a UV, we need to cache the
25da4f38 2161 NV as well. Moreover, we trade speed for space, and do not
28e5dec8 2162 cache the NV if we are sure it's not needed.
25da4f38 2163 */
16b7a9a4 2164
c2988b20
NC
2165 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2166 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2167 == IS_NUMBER_IN_UV) {
5e045b90 2168 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8
JH
2169 if (SvTYPE(sv) < SVt_PVIV)
2170 sv_upgrade(sv, SVt_PVIV);
f7bbb42a 2171 (void)SvIOK_on(sv);
c2988b20
NC
2172 } else if (SvTYPE(sv) < SVt_PVNV)
2173 sv_upgrade(sv, SVt_PVNV);
28e5dec8 2174
f2524eef 2175 /* If NVs preserve UVs then we only use the UV value if we know that
c2988b20
NC
2176 we aren't going to call atof() below. If NVs don't preserve UVs
2177 then the value returned may have more precision than atof() will
2178 return, even though value isn't perfectly accurate. */
2179 if ((numtype & (IS_NUMBER_IN_UV
2180#ifdef NV_PRESERVES_UV
2181 | IS_NUMBER_NOT_INT
2182#endif
2183 )) == IS_NUMBER_IN_UV) {
2184 /* This won't turn off the public IOK flag if it was set above */
2185 (void)SvIOKp_on(sv);
2186
2187 if (!(numtype & IS_NUMBER_NEG)) {
2188 /* positive */;
2189 if (value <= (UV)IV_MAX) {
45977657 2190 SvIV_set(sv, (IV)value);
c2988b20 2191 } else {
af359546 2192 /* it didn't overflow, and it was positive. */
607fa7f2 2193 SvUV_set(sv, value);
c2988b20
NC
2194 SvIsUV_on(sv);
2195 }
2196 } else {
2197 /* 2s complement assumption */
2198 if (value <= (UV)IV_MIN) {
45977657 2199 SvIV_set(sv, -(IV)value);
c2988b20
NC
2200 } else {
2201 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2202 I'm assuming it will be rare. */
c2988b20
NC
2203 if (SvTYPE(sv) < SVt_PVNV)
2204 sv_upgrade(sv, SVt_PVNV);
2205 SvNOK_on(sv);
2206 SvIOK_off(sv);
2207 SvIOKp_on(sv);
9d6ce603 2208 SvNV_set(sv, -(NV)value);
45977657 2209 SvIV_set(sv, IV_MIN);
c2988b20
NC
2210 }
2211 }
2212 }
2213 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2214 will be in the previous block to set the IV slot, and the next
2215 block to set the NV slot. So no else here. */
2216
2217 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2218 != IS_NUMBER_IN_UV) {
2219 /* It wasn't an (integer that doesn't overflow the UV). */
3f7c398e 2220 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8 2221
c2988b20
NC
2222 if (! numtype && ckWARN(WARN_NUMERIC))
2223 not_a_number(sv);
28e5dec8 2224
88cb8500 2225 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" NVgf ")\n",
c2988b20 2226 PTR2UV(sv), SvNVX(sv)));
28e5dec8 2227
28e5dec8 2228#ifdef NV_PRESERVES_UV
af359546
NC
2229 (void)SvIOKp_on(sv);
2230 (void)SvNOK_on(sv);
74c8813d
JH
2231#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2232 if (Perl_isnan(SvNVX(sv))) {
2233 SvUV_set(sv, 0);
2234 SvIsUV_on(sv);
2235 return FALSE;
2236 }
2237#endif
af359546
NC
2238 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2239 SvIV_set(sv, I_V(SvNVX(sv)));
2240 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2241 SvIOK_on(sv);
2242 } else {
6f207bd3 2243 NOOP; /* Integer is imprecise. NOK, IOKp */
af359546
NC
2244 }
2245 /* UV will not work better than IV */
2246 } else {
2247 if (SvNVX(sv) > (NV)UV_MAX) {
2248 SvIsUV_on(sv);
2249 /* Integer is inaccurate. NOK, IOKp, is UV */
2250 SvUV_set(sv, UV_MAX);
af359546
NC
2251 } else {
2252 SvUV_set(sv, U_V(SvNVX(sv)));
2253 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2254 NV preservse UV so can do correct comparison. */
2255 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2256 SvIOK_on(sv);
af359546 2257 } else {
6f207bd3 2258 NOOP; /* Integer is imprecise. NOK, IOKp, is UV */
af359546
NC
2259 }
2260 }
4b0c9573 2261 SvIsUV_on(sv);
af359546 2262 }
28e5dec8 2263#else /* NV_PRESERVES_UV */
c2988b20
NC
2264 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2265 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
af359546 2266 /* The IV/UV slot will have been set from value returned by
c2988b20
NC
2267 grok_number above. The NV slot has just been set using
2268 Atof. */
560b0c46 2269 SvNOK_on(sv);
c2988b20
NC
2270 assert (SvIOKp(sv));
2271 } else {
2272 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2273 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2274 /* Small enough to preserve all bits. */
2275 (void)SvIOKp_on(sv);
2276 SvNOK_on(sv);
45977657 2277 SvIV_set(sv, I_V(SvNVX(sv)));
659c4b96 2278 if ((NV)(SvIVX(sv)) == SvNVX(sv))
c2988b20
NC
2279 SvIOK_on(sv);
2280 /* Assumption: first non-preserved integer is < IV_MAX,
2281 this NV is in the preserved range, therefore: */
2282 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2283 < (UV)IV_MAX)) {
32fdb065 2284 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
2285 }
2286 } else {
2287 /* IN_UV NOT_INT
2288 0 0 already failed to read UV.
2289 0 1 already failed to read UV.
2290 1 0 you won't get here in this case. IV/UV
2291 slot set, public IOK, Atof() unneeded.
2292 1 1 already read UV.
2293 so there's no point in sv_2iuv_non_preserve() attempting
2294 to use atol, strtol, strtoul etc. */
47031da6 2295# ifdef DEBUGGING
40a17c4c 2296 sv_2iuv_non_preserve (sv, numtype);
47031da6
NC
2297# else
2298 sv_2iuv_non_preserve (sv);
2299# endif
c2988b20
NC
2300 }
2301 }
28e5dec8 2302#endif /* NV_PRESERVES_UV */
a43d94f2
NC
2303 /* It might be more code efficient to go through the entire logic above
2304 and conditionally set with SvIOKp_on() rather than SvIOK(), but it
2305 gets complex and potentially buggy, so more programmer efficient
2306 to do it this way, by turning off the public flags: */
2307 if (!numtype)
2308 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
25da4f38 2309 }
af359546
NC
2310 }
2311 else {
675c862f 2312 if (isGV_with_GP(sv))
159b6efe 2313 return glob_2number(MUTABLE_GV(sv));
180488f8 2314
4f62cd62 2315 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
af359546 2316 report_uninit(sv);
25da4f38
IZ
2317 if (SvTYPE(sv) < SVt_IV)
2318 /* Typically the caller expects that sv_any is not NULL now. */
2319 sv_upgrade(sv, SVt_IV);
af359546
NC
2320 /* Return 0 from the caller. */
2321 return TRUE;
2322 }
2323 return FALSE;
2324}
2325
2326/*
2327=for apidoc sv_2iv_flags
2328
2329Return the integer value of an SV, doing any necessary string
2330conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2331Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2332
2333=cut
2334*/
2335
2336IV
5aaab254 2337Perl_sv_2iv_flags(pTHX_ SV *const sv, const I32 flags)
af359546 2338{
1061065f 2339 PERL_ARGS_ASSERT_SV_2IV_FLAGS;
4bac9ae4 2340
217f6fa3
FC
2341 assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2342 && SvTYPE(sv) != SVt_PVFM);
2343
4bac9ae4
CS
2344 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2345 mg_get(sv);
2346
2347 if (SvROK(sv)) {
2348 if (SvAMAGIC(sv)) {
2349 SV * tmpstr;
2350 if (flags & SV_SKIP_OVERLOAD)
2351 return 0;
2352 tmpstr = AMG_CALLunary(sv, numer_amg);
2353 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2354 return SvIV(tmpstr);
2355 }
2356 }
2357 return PTR2IV(SvRV(sv));
2358 }
2359
8d919b0a 2360 if (SvVALID(sv) || isREGEXP(sv)) {
2b2b6d6d
NC
2361 /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2362 the same flag bit as SVf_IVisUV, so must not let them cache IVs.
2363 In practice they are extremely unlikely to actually get anywhere
2364 accessible by user Perl code - the only way that I'm aware of is when
2365 a constant subroutine which is used as the second argument to index.
cd84013a
FC
2366
2367 Regexps have no SvIVX and SvNVX fields.
2b2b6d6d 2368 */
8d919b0a 2369 assert(isREGEXP(sv) || SvPOKp(sv));
e20b6c3b 2370 {
71c558c3 2371 UV value;
8d919b0a
FC
2372 const char * const ptr =
2373 isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
71c558c3 2374 const int numtype
8d919b0a 2375 = grok_number(ptr, SvCUR(sv), &value);
71c558c3
NC
2376
2377 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2378 == IS_NUMBER_IN_UV) {
2379 /* It's definitely an integer */
2380 if (numtype & IS_NUMBER_NEG) {
2381 if (value < (UV)IV_MIN)
2382 return -(IV)value;
2383 } else {
2384 if (value < (UV)IV_MAX)
2385 return (IV)value;
2386 }
2387 }
058b8ae2 2388
5336f891 2389 /* Quite wrong but no good choices. */
058b8ae2
JH
2390 if ((numtype & IS_NUMBER_INFINITY)) {
2391 return (numtype & IS_NUMBER_NEG) ? IV_MIN : IV_MAX;
2392 } else if ((numtype & IS_NUMBER_NAN)) {
2393 return 0; /* So wrong. */
2394 }
2395
71c558c3
NC
2396 if (!numtype) {
2397 if (ckWARN(WARN_NUMERIC))
2398 not_a_number(sv);
2399 }
8d919b0a 2400 return I_V(Atof(ptr));
e20b6c3b 2401 }
4bac9ae4
CS
2402 }
2403
2404 if (SvTHINKFIRST(sv)) {
73381561 2405#ifdef PERL_OLD_COPY_ON_WRITE
af359546
NC
2406 if (SvIsCOW(sv)) {
2407 sv_force_normal_flags(sv, 0);
2408 }
73381561 2409#endif
af359546
NC
2410 if (SvREADONLY(sv) && !SvOK(sv)) {
2411 if (ckWARN(WARN_UNINITIALIZED))
2412 report_uninit(sv);
2413 return 0;
2414 }
2415 }
4bac9ae4 2416
af359546
NC
2417 if (!SvIOKp(sv)) {
2418 if (S_sv_2iuv_common(aTHX_ sv))
2419 return 0;
79072805 2420 }
4bac9ae4 2421
1d7c1841
GS
2422 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2423 PTR2UV(sv),SvIVX(sv)));
25da4f38 2424 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
2425}
2426
645c22ef 2427/*
891f9566 2428=for apidoc sv_2uv_flags
645c22ef
DM
2429
2430Return the unsigned integer value of an SV, doing any necessary string
891f9566
YST
2431conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2432Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
645c22ef
DM
2433
2434=cut
2435*/
2436
ff68c719 2437UV
5aaab254 2438Perl_sv_2uv_flags(pTHX_ SV *const sv, const I32 flags)
ff68c719 2439{
1061065f 2440 PERL_ARGS_ASSERT_SV_2UV_FLAGS;
4bac9ae4
CS
2441
2442 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2443 mg_get(sv);
2444
2445 if (SvROK(sv)) {
2446 if (SvAMAGIC(sv)) {
2447 SV *tmpstr;
2448 if (flags & SV_SKIP_OVERLOAD)
2449 return 0;
2450 tmpstr = AMG_CALLunary(sv, numer_amg);
2451 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2452 return SvUV(tmpstr);
2453 }
2454 }
2455 return PTR2UV(SvRV(sv));
2456 }
2457
8d919b0a 2458 if (SvVALID(sv) || isREGEXP(sv)) {
2b2b6d6d 2459 /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
cd84013a
FC
2460 the same flag bit as SVf_IVisUV, so must not let them cache IVs.
2461 Regexps have no SvIVX and SvNVX fields. */
8d919b0a 2462 assert(isREGEXP(sv) || SvPOKp(sv));
e20b6c3b 2463 {
71c558c3 2464 UV value;
8d919b0a
FC
2465 const char * const ptr =
2466 isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
71c558c3 2467 const int numtype
8d919b0a 2468 = grok_number(ptr, SvCUR(sv), &value);
71c558c3
NC
2469
2470 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2471 == IS_NUMBER_IN_UV) {
2472 /* It's definitely an integer */
2473 if (!(numtype & IS_NUMBER_NEG))
2474 return value;
2475 }
058b8ae2 2476
5336f891 2477 /* Quite wrong but no good choices. */
058b8ae2
JH
2478 if ((numtype & IS_NUMBER_INFINITY)) {
2479 return UV_MAX; /* So wrong. */
2480 } else if ((numtype & IS_NUMBER_NAN)) {
2481 return 0; /* So wrong. */
2482 }
2483
71c558c3
NC
2484 if (!numtype) {
2485 if (ckWARN(WARN_NUMERIC))
2486 not_a_number(sv);
2487 }
8d919b0a 2488 return U_V(Atof(ptr));
e20b6c3b 2489 }
4bac9ae4
CS
2490 }
2491
2492 if (SvTHINKFIRST(sv)) {
73381561 2493#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
2494 if (SvIsCOW(sv)) {
2495 sv_force_normal_flags(sv, 0);
8a818333 2496 }
73381561 2497#endif
0336b60e 2498 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2499 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2500 report_uninit(sv);
ff68c719
PP
2501 return 0;
2502 }
2503 }
4bac9ae4 2504
af359546
NC
2505 if (!SvIOKp(sv)) {
2506 if (S_sv_2iuv_common(aTHX_ sv))
2507 return 0;
ff68c719 2508 }
25da4f38 2509
1d7c1841
GS
2510 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2511 PTR2UV(sv),SvUVX(sv)));
25da4f38 2512 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719
PP
2513}
2514
645c22ef 2515/*
196007d1 2516=for apidoc sv_2nv_flags
645c22ef
DM
2517
2518Return the num value of an SV, doing any necessary string or integer
fde67290 2519conversion. If flags includes SV_GMAGIC, does an mg_get() first.
39d5de13 2520Normally used via the C<SvNV(sv)> and C<SvNVx(sv)> macros.
645c22ef
DM
2521
2522=cut
2523*/
2524
65202027 2525NV
5aaab254 2526Perl_sv_2nv_flags(pTHX_ SV *const sv, const I32 flags)
79072805 2527{
1061065f
DD
2528 PERL_ARGS_ASSERT_SV_2NV_FLAGS;
2529
217f6fa3
FC
2530 assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2531 && SvTYPE(sv) != SVt_PVFM);
8d919b0a 2532 if (SvGMAGICAL(sv) || SvVALID(sv) || isREGEXP(sv)) {
2b2b6d6d 2533 /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
cd84013a
FC
2534 the same flag bit as SVf_IVisUV, so must not let them cache NVs.
2535 Regexps have no SvIVX and SvNVX fields. */
8d919b0a 2536 const char *ptr;
39d5de13
DM
2537 if (flags & SV_GMAGIC)
2538 mg_get(sv);
463ee0b2
LW
2539 if (SvNOKp(sv))
2540 return SvNVX(sv);
cd84013a 2541 if (SvPOKp(sv) && !SvIOKp(sv)) {
8d919b0a
FC
2542 ptr = SvPVX_const(sv);
2543 grokpv:
041457d9 2544 if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) &&
8d919b0a 2545 !grok_number(ptr, SvCUR(sv), NULL))
a0d0e21e 2546 not_a_number(sv);
8d919b0a 2547 return Atof(ptr);
a0d0e21e 2548 }
25da4f38 2549 if (SvIOKp(sv)) {
1c846c1f 2550 if (SvIsUV(sv))
65202027 2551 return (NV)SvUVX(sv);
25da4f38 2552 else
65202027 2553 return (NV)SvIVX(sv);
47a72cb8
NC
2554 }
2555 if (SvROK(sv)) {
2556 goto return_rok;
2557 }
8d919b0a
FC
2558 if (isREGEXP(sv)) {
2559 ptr = RX_WRAPPED((REGEXP *)sv);
2560 goto grokpv;
2561 }
47a72cb8
NC
2562 assert(SvTYPE(sv) >= SVt_PVMG);
2563 /* This falls through to the report_uninit near the end of the
2564 function. */
2565 } else if (SvTHINKFIRST(sv)) {
a0d0e21e 2566 if (SvROK(sv)) {
47a72cb8 2567 return_rok:
deb46114 2568 if (SvAMAGIC(sv)) {
aee036bb
DM
2569 SV *tmpstr;
2570 if (flags & SV_SKIP_OVERLOAD)
2571 return 0;
31d632c3 2572 tmpstr = AMG_CALLunary(sv, numer_amg);
deb46114
NC
2573 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2574 return SvNV(tmpstr);
2575 }
2576 }
2577 return PTR2NV(SvRV(sv));
a0d0e21e 2578 }
73381561 2579#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
2580 if (SvIsCOW(sv)) {
2581 sv_force_normal_flags(sv, 0);
8a818333 2582 }
73381561 2583#endif
0336b60e 2584 if (SvREADONLY(sv) && !SvOK(sv)) {
599cee73 2585 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2586 report_uninit(sv);
ed6116ce
LW
2587 return 0.0;
2588 }
79072805
LW
2589 }
2590 if (SvTYPE(sv) < SVt_NV) {
7e25a7e9
NC
2591 /* The logic to use SVt_PVNV if necessary is in sv_upgrade. */
2592 sv_upgrade(sv, SVt_NV);
097ee67d 2593 DEBUG_c({
f93f4e46 2594 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841 2595 PerlIO_printf(Perl_debug_log,
88cb8500 2596 "0x%"UVxf" num(%" NVgf ")\n",
1d7c1841 2597 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2598 RESTORE_NUMERIC_LOCAL();
2599 });
79072805
LW
2600 }
2601 else if (SvTYPE(sv) < SVt_PVNV)
2602 sv_upgrade(sv, SVt_PVNV);
59d8ce62
NC
2603 if (SvNOKp(sv)) {
2604 return SvNVX(sv);
61604483 2605 }
59d8ce62 2606 if (SvIOKp(sv)) {
9d6ce603 2607 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
28e5dec8 2608#ifdef NV_PRESERVES_UV
a43d94f2
NC
2609 if (SvIOK(sv))
2610 SvNOK_on(sv);
2611 else
2612 SvNOKp_on(sv);
28e5dec8
JH
2613#else
2614 /* Only set the public NV OK flag if this NV preserves the IV */
2615 /* Check it's not 0xFFFFFFFFFFFFFFFF */
a43d94f2
NC
2616 if (SvIOK(sv) &&
2617 SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
28e5dec8
JH
2618 : (SvIVX(sv) == I_V(SvNVX(sv))))
2619 SvNOK_on(sv);
2620 else
2621 SvNOKp_on(sv);
2622#endif
93a17b20 2623 }
cd84013a 2624 else if (SvPOKp(sv)) {
c2988b20 2625 UV value;
3f7c398e 2626 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
041457d9 2627 if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC))
a0d0e21e 2628 not_a_number(sv);
28e5dec8 2629#ifdef NV_PRESERVES_UV
c2988b20
NC
2630 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2631 == IS_NUMBER_IN_UV) {
5e045b90 2632 /* It's definitely an integer */
9d6ce603 2633 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
66d83377
JH
2634 } else {
2635 if ((numtype & IS_NUMBER_INFINITY)) {
2636 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -NV_INF : NV_INF);
2637 } else if ((numtype & IS_NUMBER_NAN)) {
2638 SvNV_set(sv, NV_NAN);
2639 } else
2640 SvNV_set(sv, Atof(SvPVX_const(sv)));
2641 }
a43d94f2
NC
2642 if (numtype)
2643 SvNOK_on(sv);
2644 else
2645 SvNOKp_on(sv);
28e5dec8 2646#else
3f7c398e 2647 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
2648 /* Only set the public NV OK flag if this NV preserves the value in
2649 the PV at least as well as an IV/UV would.
2650 Not sure how to do this 100% reliably. */
2651 /* if that shift count is out of range then Configure's test is
2652 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2653 UV_BITS */
2654 if (((UV)1 << NV_PRESERVES_UV_BITS) >
c2988b20 2655 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
28e5dec8 2656 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
c2988b20
NC
2657 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2658 /* Can't use strtol etc to convert this string, so don't try.
2659 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2660 SvNOK_on(sv);
2661 } else {
2662 /* value has been set. It may not be precise. */
2663 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
2664 /* 2s complement assumption for (UV)IV_MIN */
2665 SvNOK_on(sv); /* Integer is too negative. */
2666 } else {
2667 SvNOKp_on(sv);
2668 SvIOKp_on(sv);
6fa402ec 2669
c2988b20 2670 if (numtype & IS_NUMBER_NEG) {
45977657 2671 SvIV_set(sv, -(IV)value);
c2988b20 2672 } else if (value <= (UV)IV_MAX) {
45977657 2673 SvIV_set(sv, (IV)value);
c2988b20 2674 } else {
607fa7f2 2675 SvUV_set(sv, value);
c2988b20
NC
2676 SvIsUV_on(sv);
2677 }
2678
2679 if (numtype & IS_NUMBER_NOT_INT) {
2680 /* I believe that even if the original PV had decimals,
2681 they are lost beyond the limit of the FP precision.
2682 However, neither is canonical, so both only get p
2683 flags. NWC, 2000/11/25 */
2684 /* Both already have p flags, so do nothing */
2685 } else {
66a1b24b 2686 const NV nv = SvNVX(sv);
74c8813d 2687 /* XXX should this spot have NAN_COMPARE_BROKEN, too? */
c2988b20
NC
2688 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2689 if (SvIVX(sv) == I_V(nv)) {
2690 SvNOK_on(sv);
c2988b20 2691 } else {
c2988b20
NC
2692 /* It had no "." so it must be integer. */
2693 }
00b6aa41 2694 SvIOK_on(sv);
c2988b20
NC
2695 } else {
2696 /* between IV_MAX and NV(UV_MAX).
2697 Could be slightly > UV_MAX */
6fa402ec 2698
c2988b20
NC
2699 if (numtype & IS_NUMBER_NOT_INT) {
2700 /* UV and NV both imprecise. */
2701 } else {
66a1b24b 2702 const UV nv_as_uv = U_V(nv);
c2988b20
NC
2703
2704 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2705 SvNOK_on(sv);
c2988b20 2706 }
00b6aa41 2707 SvIOK_on(sv);
c2988b20
NC
2708 }
2709 }
2710 }
2711 }
2712 }
a43d94f2
NC
2713 /* It might be more code efficient to go through the entire logic above
2714 and conditionally set with SvNOKp_on() rather than SvNOK(), but it
2715 gets complex and potentially buggy, so more programmer efficient
2716 to do it this way, by turning off the public flags: */
2717 if (!numtype)
2718 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
28e5dec8 2719#endif /* NV_PRESERVES_UV */
93a17b20 2720 }
79072805 2721 else {
f7877b28 2722 if (isGV_with_GP(sv)) {
159b6efe 2723 glob_2number(MUTABLE_GV(sv));
180488f8
NC
2724 return 0.0;
2725 }
2726
4f62cd62 2727 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
29489e7c 2728 report_uninit(sv);
7e25a7e9
NC
2729 assert (SvTYPE(sv) >= SVt_NV);
2730 /* Typically the caller expects that sv_any is not NULL now. */
2731 /* XXX Ilya implies that this is a bug in callers that assume this
2732 and ideally should be fixed. */
a0d0e21e 2733 return 0.0;
79072805 2734 }
097ee67d 2735 DEBUG_c({
f93f4e46 2736 STORE_NUMERIC_LOCAL_SET_STANDARD();
88cb8500 2737 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" NVgf ")\n",
1d7c1841 2738 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2739 RESTORE_NUMERIC_LOCAL();
2740 });
463ee0b2 2741 return SvNVX(sv);
79072805
LW
2742}
2743
800401ee
JH
2744/*
2745=for apidoc sv_2num
2746
2747Return an SV with the numeric value of the source SV, doing any necessary
a196a5fa
JH
2748reference or overload conversion. You must use the C<SvNUM(sv)> macro to
2749access this function.
800401ee
JH
2750
2751=cut
2752*/
2753
2754SV *
5aaab254 2755Perl_sv_2num(pTHX_ SV *const sv)
800401ee 2756{
7918f24d
NC
2757 PERL_ARGS_ASSERT_SV_2NUM;
2758
b9ee0594
RGS
2759 if (!SvROK(sv))
2760 return sv;
800401ee 2761 if (SvAMAGIC(sv)) {
31d632c3 2762 SV * const tmpsv = AMG_CALLunary(sv, numer_amg);
a02ec77a 2763 TAINT_IF(tmpsv && SvTAINTED(tmpsv));
800401ee
JH
2764 if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
2765 return sv_2num(tmpsv);
2766 }
2767 return sv_2mortal(newSVuv(PTR2UV(SvRV(sv))));
2768}
2769
645c22ef
DM
2770/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2771 * UV as a string towards the end of buf, and return pointers to start and
2772 * end of it.
2773 *
2774 * We assume that buf is at least TYPE_CHARS(UV) long.
2775 */
2776
864dbfa3 2777static char *
5de3775c 2778S_uiv_2buf(char *const buf, const IV iv, UV uv, const int is_uv, char **const peob)
25da4f38 2779{
25da4f38 2780 char *ptr = buf + TYPE_CHARS(UV);
823a54a3 2781 char * const ebuf = ptr;
25da4f38 2782 int sign;
25da4f38 2783
7918f24d
NC
2784 PERL_ARGS_ASSERT_UIV_2BUF;
2785
25da4f38
IZ
2786 if (is_uv)
2787 sign = 0;
2788 else if (iv >= 0) {
2789 uv = iv;
2790 sign = 0;
2791 } else {
2792 uv = -iv;
2793 sign = 1;
2794 }
2795 do {
eb160463 2796 *--ptr = '0' + (char)(uv % 10);
25da4f38
IZ
2797 } while (uv /= 10);
2798 if (sign)
2799 *--ptr = '-';
2800 *peob = ebuf;
2801 return ptr;
2802}
2803
0c7e610f 2804/* Helper for sv_2pv_flags and sv_vcatpvfn_flags. If the NV is an
fde71869
JH
2805 * infinity or a not-a-number, writes the appropriate strings to the
2806 * buffer, including a zero byte. On success returns the written length,
5e85836e
JH
2807 * excluding the zero byte, on failure (not an infinity, not a nan, or the
2808 * maxlen too small) returns zero. */
0c7e610f 2809STATIC size_t
5e85836e 2810S_infnan_2pv(NV nv, char* buffer, size_t maxlen) {
128eeacb 2811 /* XXX this should be an assert */
5e85836e 2812 if (maxlen < 4) /* "Inf\0", "NaN\0" */
0c7e610f
JH
2813 return 0;
2814 else {
2815 char* s = buffer;
128eeacb
DD
2816 /* isnan must be first due to NAN_COMPARE_BROKEN builds, since NAN might
2817 use the broken for NAN >/< ops in the inf check, and then the inf
2818 check returns true for NAN on NAN_COMPARE_BROKEN compilers */
2819 if (Perl_isnan(nv)) {
0c7e610f
JH
2820 *s++ = 'N';
2821 *s++ = 'a';
2822 *s++ = 'N';
5e85836e
JH
2823 /* XXX optionally output the payload mantissa bits as
2824 * "(unsigned)" (to match the nan("...") C99 function,
2825 * or maybe as "(0xhhh...)" would make more sense...
2826 * provide a format string so that the user can decide?
2827 * NOTE: would affect the maxlen and assert() logic.*/
0c7e610f 2828 }
128eeacb
DD
2829 else if (Perl_isinf(nv)) {
2830 if (nv < 0) {
2831 if (maxlen < 5) /* "-Inf\0" */
2832 return 0;
2833 *s++ = '-';
2834 }
2835 *s++ = 'I';
2836 *s++ = 'n';
2837 *s++ = 'f';
2838 }
2839
0c7e610f
JH
2840 else
2841 return 0;
5e85836e 2842 assert((s == buffer + 3) || (s == buffer + 4));
0c7e610f 2843 *s++ = 0;
5e85836e 2844 return s - buffer - 1; /* -1: excluding the zero byte */
0c7e610f
JH
2845 }
2846}
2847
645c22ef
DM
2848/*
2849=for apidoc sv_2pv_flags
2850
ff276b08 2851Returns a pointer to the string value of an SV, and sets *lp to its length.
fde67290
FC
2852If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a
2853string if necessary. Normally invoked via the C<SvPV_flags> macro.
2854C<sv_2pv()> and C<sv_2pv_nomg> usually end up here too.
645c22ef
DM
2855
2856=cut
2857*/
2858
8d6d96c1 2859char *
5aaab254 2860Perl_sv_2pv_flags(pTHX_ SV *const sv, STRLEN *const lp, const I32 flags)
8d6d96c1 2861{
eb578fdb 2862 char *s;
79072805 2863
1061065f
DD
2864 PERL_ARGS_ASSERT_SV_2PV_FLAGS;
2865
217f6fa3
FC
2866 assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2867 && SvTYPE(sv) != SVt_PVFM);
4bac9ae4
CS
2868 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2869 mg_get(sv);
2870 if (SvROK(sv)) {
2871 if (SvAMAGIC(sv)) {
2872 SV *tmpstr;
2873 if (flags & SV_SKIP_OVERLOAD)
2874 return NULL;
2875 tmpstr = AMG_CALLunary(sv, string_amg);
2876 TAINT_IF(tmpstr && SvTAINTED(tmpstr));
2877 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2878 /* Unwrap this: */
2879 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr);
2880 */
75dfc8ec 2881
4bac9ae4
CS
2882 char *pv;
2883 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
2884 if (flags & SV_CONST_RETURN) {
2885 pv = (char *) SvPVX_const(tmpstr);
50adf7d2 2886 } else {
4bac9ae4
CS
2887 pv = (flags & SV_MUTABLE_RETURN)
2888 ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
50adf7d2 2889 }
4bac9ae4
CS
2890 if (lp)
2891 *lp = SvCUR(tmpstr);
2892 } else {
2893 pv = sv_2pv_flags(tmpstr, lp, flags);
50adf7d2 2894 }
4bac9ae4
CS
2895 if (SvUTF8(tmpstr))
2896 SvUTF8_on(sv);
2897 else
2898 SvUTF8_off(sv);
2899 return pv;
deb46114 2900 }
4bac9ae4
CS
2901 }
2902 {
2903 STRLEN len;
2904 char *retval;
2905 char *buffer;
2906 SV *const referent = SvRV(sv);
2907
2908 if (!referent) {
2909 len = 7;
2910 retval = buffer = savepvn("NULLREF", len);
2911 } else if (SvTYPE(referent) == SVt_REGEXP &&
2912 (!(PL_curcop->cop_hints & HINT_NO_AMAGIC) ||
2913 amagic_is_enabled(string_amg))) {
2914 REGEXP * const re = (REGEXP *)MUTABLE_PTR(referent);
2915
2916 assert(re);
67d2d14d 2917
4bac9ae4
CS
2918 /* If the regex is UTF-8 we want the containing scalar to
2919 have an UTF-8 flag too */
2920 if (RX_UTF8(re))
2921 SvUTF8_on(sv);
2922 else
2923 SvUTF8_off(sv);
67d2d14d 2924
4bac9ae4
CS
2925 if (lp)
2926 *lp = RX_WRAPLEN(re);
67d2d14d 2927
4bac9ae4
CS
2928 return RX_WRAPPED(re);
2929 } else {
2930 const char *const typestr = sv_reftype(referent, 0);
2931 const STRLEN typelen = strlen(typestr);
2932 UV addr = PTR2UV(referent);
2933 const char *stashname = NULL;
2934 STRLEN stashnamelen = 0; /* hush, gcc */
2935 const char *buffer_end;
2936
2937 if (SvOBJECT(referent)) {
2938 const HEK *const name = HvNAME_HEK(SvSTASH(referent));
2939
2940 if (name) {
2941 stashname = HEK_KEY(name);
2942 stashnamelen = HEK_LEN(name);
2943
2944 if (HEK_UTF8(name)) {
2945 SvUTF8_on(sv);
fafee734 2946 } else {
4bac9ae4 2947 SvUTF8_off(sv);
fafee734 2948 }
fafee734 2949 } else {
4bac9ae4
CS
2950 stashname = "__ANON__";
2951 stashnamelen = 8;
fafee734 2952 }
4bac9ae4
CS
2953 len = stashnamelen + 1 /* = */ + typelen + 3 /* (0x */
2954 + 2 * sizeof(UV) + 2 /* )\0 */;
2955 } else {
2956 len = typelen + 3 /* (0x */
2957 + 2 * sizeof(UV) + 2 /* )\0 */;
2958 }
fafee734 2959
4bac9ae4
CS
2960 Newx(buffer, len, char);
2961 buffer_end = retval = buffer + len;
2962
2963 /* Working backwards */
2964 *--retval = '\0';
2965 *--retval = ')';
2966 do {
2967 *--retval = PL_hexdigit[addr & 15];
2968 } while (addr >>= 4);
2969 *--retval = 'x';
2970 *--retval = '0';
2971 *--retval = '(';
2972
2973 retval -= typelen;
2974 memcpy(retval, typestr, typelen);
2975
2976 if (stashname) {
2977 *--retval = '=';
2978 retval -= stashnamelen;
2979 memcpy(retval, stashname, stashnamelen);
c080367d 2980 }
4bac9ae4
CS
2981 /* retval may not necessarily have reached the start of the
2982 buffer here. */
2983 assert (retval >= buffer);
2984
2985 len = buffer_end - retval - 1; /* -1 for that \0 */
463ee0b2 2986 }
cdb061a3 2987 if (lp)
4bac9ae4
CS
2988 *lp = len;
2989 SAVEFREEPV(buffer);
2990 return retval;
79072805 2991 }
79072805 2992 }
4bac9ae4
CS
2993
2994 if (SvPOKp(sv)) {
2995 if (lp)
2996 *lp = SvCUR(sv);
2997 if (flags & SV_MUTABLE_RETURN)
2998 return SvPVX_mutable(sv);
2999 if (flags & SV_CONST_RETURN)
3000 return (char *)SvPVX_const(sv);
3001 return SvPVX(sv);
3002 }
3003
3004 if (SvIOK(sv)) {
28e5dec8
JH
3005 /* I'm assuming that if both IV and NV are equally valid then
3006 converting the IV is going to be more efficient */
e1ec3a88 3007 const U32 isUIOK = SvIsUV(sv);
28e5dec8
JH
3008 char buf[TYPE_CHARS(UV)];
3009 char *ebuf, *ptr;
97a130b8 3010 STRLEN len;
28e5dec8
JH
3011
3012 if (SvTYPE(sv) < SVt_PVIV)
3013 sv_upgrade(sv, SVt_PVIV);
4ea1d550 3014 ptr = uiv_2buf(buf, SvIVX(sv), SvUVX(sv), isUIOK, &ebuf);
97a130b8 3015 len = ebuf - ptr;
5902b6a9 3016 /* inlined from sv_setpvn */
97a130b8
NC
3017 s = SvGROW_mutable(sv, len + 1);
3018 Move(ptr, s, len, char);
3019 s += len;
28e5dec8 3020 *s = '\0';
b127e37e 3021 SvPOK_on(sv);
28e5dec8 3022 }
4bac9ae4 3023 else if (SvNOK(sv)) {
79072805
LW
3024 if (SvTYPE(sv) < SVt_PVNV)
3025 sv_upgrade(sv, SVt_PVNV);
128eeacb
DD
3026 if (SvNVX(sv) == 0.0
3027#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
3028 && !Perl_isnan(SvNVX(sv))
3029#endif
3030 ) {
29912d93
NC
3031 s = SvGROW_mutable(sv, 2);
3032 *s++ = '0';
3033 *s = '\0';
3034 } else {
29912d93 3035 /* The +20 is pure guesswork. Configure test needed. --jhi */
5e85836e
JH
3036 STRLEN size = NV_DIG + 20;
3037 STRLEN len;
3038 s = SvGROW_mutable(sv, size);
0c7e610f 3039
5e85836e 3040 len = S_infnan_2pv(SvNVX(sv), s, size);
0c7e610f
JH
3041 if (len > 0)
3042 s += len;
3043 else {
3044 dSAVE_ERRNO;
3045 /* some Xenix systems wipe out errno here */
b127e37e 3046
b127e37e 3047#ifndef USE_LOCALE_NUMERIC
ffea512f 3048 PERL_UNUSED_RESULT(Gconvert(SvNVX(sv), NV_DIG, 0, s));
0c7e610f
JH
3049 SvPOK_on(sv);
3050#else
28acfe03 3051 {
0c7e610f
JH
3052 DECLARE_STORE_LC_NUMERIC_SET_TO_NEEDED();
3053 PERL_UNUSED_RESULT(Gconvert(SvNVX(sv), NV_DIG, 0, s));
3054
3055 /* If the radix character is UTF-8, and actually is in the
3056 * output, turn on the UTF-8 flag for the scalar */
3057 if (PL_numeric_local
3058 && PL_numeric_radix_sv && SvUTF8(PL_numeric_radix_sv)
3059 && instr(s, SvPVX_const(PL_numeric_radix_sv)))
3060 {
3061 SvUTF8_on(sv);
3062 }
3063 RESTORE_LC_NUMERIC();
28acfe03 3064 }
68e8f474 3065
0c7e610f
JH
3066 /* We don't call SvPOK_on(), because it may come to
3067 * pass that the locale changes so that the
3068 * stringification we just did is no longer correct. We
3069 * will have to re-stringify every time it is needed */
b127e37e 3070#endif
0c7e610f
JH
3071 RESTORE_ERRNO;
3072 }
3073 while (*s) s++;
bbce6d69 3074 }
79072805 3075 }
4bac9ae4
CS
3076 else if (isGV_with_GP(sv)) {
3077 GV *const gv = MUTABLE_GV(sv);
3078 SV *const buffer = sv_newmortal();
8d1c3e26 3079
4bac9ae4 3080 gv_efullname3(buffer, gv, "*");
180488f8 3081
4bac9ae4
CS
3082 assert(SvPOK(buffer));
3083 if (SvUTF8(buffer))
3084 SvUTF8_on(sv);
3085 if (lp)
3086 *lp = SvCUR(buffer);
3087 return SvPVX(buffer);
3088 }
8d919b0a
FC
3089 else if (isREGEXP(sv)) {
3090 if (lp) *lp = RX_WRAPLEN((REGEXP *)sv);
3091 return RX_WRAPPED((REGEXP *)sv);
3092 }
4bac9ae4 3093 else {
cdb061a3 3094 if (lp)
00b6aa41 3095 *lp = 0;
9f621bb0
NC
3096 if (flags & SV_UNDEF_RETURNS_NULL)
3097 return NULL;
4f62cd62 3098 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
9f621bb0 3099 report_uninit(sv);
4bac9ae4
CS
3100 /* Typically the caller expects that sv_any is not NULL now. */
3101 if (!SvREADONLY(sv) && SvTYPE(sv) < SVt_PV)
25da4f38 3102 sv_upgrade(sv, SVt_PV);
73d840c0 3103 return (char *)"";
79072805 3104 }
4bac9ae4 3105
cdb061a3 3106 {
823a54a3 3107 const STRLEN len = s - SvPVX_const(sv);
cdb061a3
NC
3108 if (lp)
3109 *lp = len;
3110 SvCUR_set(sv, len);
3111 }
1d7c1841 3112 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3f7c398e 3113 PTR2UV(sv),SvPVX_const(sv)));
4d84ee25
NC
3114 if (flags & SV_CONST_RETURN)
3115 return (char *)SvPVX_const(sv);
10516c54
NC
3116 if (flags & SV_MUTABLE_RETURN)
3117 return SvPVX_mutable(sv);
463ee0b2
LW
3118 return SvPVX(sv);
3119}
3120
645c22ef 3121/*
6050d10e
JP
3122=for apidoc sv_copypv
3123
3124Copies a stringified representation of the source SV into the
3125destination SV. Automatically performs any necessary mg_get and
54f0641b 3126coercion of numeric values into strings. Guaranteed to preserve
2575c402 3127UTF8 flag even from overloaded objects. Similar in nature to
54f0641b
NIS
3128sv_2pv[_flags] but operates directly on an SV instead of just the
3129string. Mostly uses sv_2pv_flags to do its work, except when that
6050d10e
JP
3130would lose the UTF-8'ness of the PV.
3131
4bac9ae4
CS
3132=for apidoc sv_copypv_nomg
3133
3134Like sv_copypv, but doesn't invoke get magic first.
3135
3136=for apidoc sv_copypv_flags
3137
3138Implementation of sv_copypv and sv_copypv_nomg. Calls get magic iff flags
3139include SV_GMAGIC.
3140
6050d10e
JP
3141=cut
3142*/
3143
3144void
5aaab254 3145Perl_sv_copypv(pTHX_ SV *const dsv, SV *const ssv)
6050d10e 3146{
4bac9ae4
CS
3147 PERL_ARGS_ASSERT_SV_COPYPV;
3148
3149 sv_copypv_flags(dsv, ssv, 0);
3150}
3151
3152void
5aaab254 3153Perl_sv_copypv_flags(pTHX_ SV *const dsv, SV *const ssv, const I32 flags)
4bac9ae4 3154{
446eaa42 3155 STRLEN len;
4bac9ae4 3156 const char *s;
7918f24d 3157
4bac9ae4 3158 PERL_ARGS_ASSERT_SV_COPYPV_FLAGS;
7918f24d 3159
75f9f0f4 3160 s = SvPV_flags_const(ssv,len,flags & SV_GMAGIC);
cb50f42d 3161 sv_setpvn(dsv,s,len);
446eaa42 3162 if (SvUTF8(ssv))
cb50f42d 3163 SvUTF8_on(dsv);
446eaa42 3164 else
cb50f42d 3165 SvUTF8_off(dsv);
6050d10e
JP
3166}
3167
3168/*
645c22ef
DM
3169=for apidoc sv_2pvbyte
3170
3171Return a pointer to the byte-encoded representation of the SV, and set *lp
1e54db1a 3172to its length. May cause the SV to be downgraded from UTF-8 as a
645c22ef
DM
3173side-effect.
3174
3175Usually accessed via the C<SvPVbyte> macro.
3176
3177=cut
3178*/
3179
7340a771 3180char *
5aaab254 3181Perl_sv_2pvbyte(pTHX_ SV *sv, STRLEN *const lp)
7340a771 3182{
7918f24d
NC
3183 PERL_ARGS_ASSERT_SV_2PVBYTE;
3184
48120f8f 3185 SvGETMAGIC(sv);
4499db73
FC
3186 if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
3187 || isGV_with_GP(sv) || SvROK(sv)) {
a901b181 3188 SV *sv2 = sv_newmortal();
48120f8f 3189 sv_copypv_nomg(sv2,sv);
a901b181
FC
3190 sv = sv2;
3191 }
0875d2fe 3192 sv_utf8_downgrade(sv,0);
71eb6d8c 3193 return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
7340a771
GS
3194}
3195
645c22ef 3196/*
035cbb0e
RGS
3197=for apidoc sv_2pvutf8
3198
3199Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3200to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
3201
3202Usually accessed via the C<SvPVutf8> macro.
3203
3204=cut
3205*/
645c22ef 3206
7340a771 3207char *
5aaab254 3208Perl_sv_2pvutf8(pTHX_ SV *sv, STRLEN *const lp)
7340a771 3209{
7918f24d
NC
3210 PERL_ARGS_ASSERT_SV_2PVUTF8;
3211
4499db73
FC
3212 if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
3213 || isGV_with_GP(sv) || SvROK(sv))
fe46cbda 3214 sv = sv_mortalcopy(sv);
4bac9ae4
CS
3215 else
3216 SvGETMAGIC(sv);
3217 sv_utf8_upgrade_nomg(sv);
c3ec315f 3218 return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
7340a771 3219}
1c846c1f 3220
7ee2227d 3221
645c22ef
DM
3222/*
3223=for apidoc sv_2bool
3224
06c841cf
FC
3225This macro is only used by sv_true() or its macro equivalent, and only if
3226the latter's argument is neither SvPOK, SvIOK nor SvNOK.
3227It calls sv_2bool_flags with the SV_GMAGIC flag.
3228
3229=for apidoc sv_2bool_flags
3230
3231This function is only used by sv_true() and friends, and only if
fde67290 3232the latter's argument is neither SvPOK, SvIOK nor SvNOK. If the flags
06c841cf
FC
3233contain SV_GMAGIC, then it does an mg_get() first.
3234
645c22ef
DM
3235
3236=cut
3237*/
3238
463ee0b2 3239bool
9d176cd8 3240Perl_sv_2bool_flags(pTHX_ SV *sv, I32 flags)
463ee0b2 3241{
06c841cf 3242 PERL_ARGS_ASSERT_SV_2BOOL_FLAGS;
7918f24d 3243
9d176cd8 3244 restart:
06c841cf 3245 if(flags & SV_GMAGIC) SvGETMAGIC(sv);
463ee0b2 3246
a0d0e21e
LW
3247 if (!SvOK(sv))
3248 return 0;
3249 if (SvROK(sv)) {
fabdb6c0 3250 if (SvAMAGIC(sv)) {
31d632c3 3251 SV * const tmpsv = AMG_CALLunary(sv, bool__amg);
9d176cd8
DD
3252 if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) {
3253 bool svb;
3254 sv = tmpsv;
3255 if(SvGMAGICAL(sv)) {
3256 flags = SV_GMAGIC;
3257 goto restart; /* call sv_2bool */
3258 }
3259 /* expanded SvTRUE_common(sv, (flags = 0, goto restart)) */
3260 else if(!SvOK(sv)) {
3261 svb = 0;
3262 }
3263 else if(SvPOK(sv)) {
3264 svb = SvPVXtrue(sv);
3265 }
3266 else if((SvFLAGS(sv) & (SVf_IOK|SVf_NOK))) {
3267 svb = (SvIOK(sv) && SvIVX(sv) != 0)
659c4b96 3268 || (SvNOK(sv) && SvNVX(sv) != 0.0);
9d176cd8
DD
3269 }
3270 else {
3271 flags = 0;
3272 goto restart; /* call sv_2bool_nomg */
3273 }
3274 return cBOOL(svb);
3275 }
fabdb6c0
AL
3276 }
3277 return SvRV(sv) != 0;
a0d0e21e 3278 }
85b7d9b3
FC
3279 if (isREGEXP(sv))
3280 return
3281 RX_WRAPLEN(sv) > 1 || (RX_WRAPLEN(sv) && *RX_WRAPPED(sv) != '0');
4bac9ae4 3282 return SvTRUE_common(sv, isGV_with_GP(sv) ? 1 : 0);
79072805
LW
3283}
3284
c461cf8f
JH
3285/*
3286=for apidoc sv_utf8_upgrade
3287
78ea37eb 3288Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3289Forces the SV to string form if it is not already.
2bbc8d55 3290Will C<mg_get> on C<sv> if appropriate.
4411f3b6 3291Always sets the SvUTF8 flag to avoid future validity checks even
2bbc8d55
SP
3292if the whole string is the same in UTF-8 as not.
3293Returns the number of bytes in the converted string
c461cf8f 3294
0efd0472 3295This is not a general purpose byte encoding to Unicode interface:
13a6c0e0
JH
3296use the Encode extension for that.
3297
fe749c9a
KW
3298=for apidoc sv_utf8_upgrade_nomg
3299
fde67290 3300Like sv_utf8_upgrade, but doesn't do magic on C<sv>.
fe749c9a 3301
8d6d96c1
HS
3302=for apidoc sv_utf8_upgrade_flags
3303
78ea37eb 3304Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3305Forces the SV to string form if it is not already.
8d6d96c1 3306Always sets the SvUTF8 flag to avoid future validity checks even
960b0271
FC
3307if all the bytes are invariant in UTF-8.
3308If C<flags> has C<SV_GMAGIC> bit set,
2bbc8d55 3309will C<mg_get> on C<sv> if appropriate, else not.
2a590426
KW
3310
3311If C<flags> has SV_FORCE_UTF8_UPGRADE set, this function assumes that the PV
3312will expand when converted to UTF-8, and skips the extra work of checking for
3313that. Typically this flag is used by a routine that has already parsed the
3314string and found such characters, and passes this information on so that the
3315work doesn't have to be repeated.
3316
3317Returns the number of bytes in the converted string.
8d6d96c1 3318
0efd0472 3319This is not a general purpose byte encoding to Unicode interface:
13a6c0e0
JH
3320use the Encode extension for that.
3321
2a590426 3322=for apidoc sv_utf8_upgrade_flags_grow
b3ab6785 3323
2a590426
KW
3324Like sv_utf8_upgrade_flags, but has an additional parameter C<extra>, which is
3325the number of unused bytes the string of 'sv' is guaranteed to have free after
3326it upon return. This allows the caller to reserve extra space that it intends
3327to fill, to avoid extra grows.
b3ab6785 3328
2a590426
KW
3329C<sv_utf8_upgrade>, C<sv_utf8_upgrade_nomg>, and C<sv_utf8_upgrade_flags>
3330are implemented in terms of this function.
3331
3332Returns the number of bytes in the converted string (not including the spares).
3333
3334=cut
b3ab6785
KW
3335
3336(One might think that the calling routine could pass in the position of the
2a590426
KW
3337first variant character when it has set SV_FORCE_UTF8_UPGRADE, so it wouldn't
3338have to be found again. But that is not the case, because typically when the
3339caller is likely to use this flag, it won't be calling this routine unless it
3340finds something that won't fit into a byte. Otherwise it tries to not upgrade
3341and just use bytes. But some things that do fit into a byte are variants in
3342utf8, and the caller may not have been keeping track of these.)
b3ab6785 3343
6602b933
KW
3344If the routine itself changes the string, it adds a trailing C<NUL>. Such a
3345C<NUL> isn't guaranteed due to having other routines do the work in some input
3346cases, or if the input is already flagged as being in utf8.
b3ab6785
KW
3347
3348The speed of this could perhaps be improved for many cases if someone wanted to
3349write a fast function that counts the number of variant characters in a string,
3350especially if it could return the position of the first one.
3351
8d6d96c1
HS
3352*/
3353
3354STRLEN
5aaab254 3355Perl_sv_utf8_upgrade_flags_grow(pTHX_ SV *const sv, const I32 flags, STRLEN extra)
8d6d96c1 3356{
b3ab6785 3357 PERL_ARGS_ASSERT_SV_UTF8_UPGRADE_FLAGS_GROW;
7918f24d 3358
808c356f
RGS
3359 if (sv == &PL_sv_undef)
3360 return 0;
892f9127 3361 if (!SvPOK_nog(sv)) {
e0e62c2a 3362 STRLEN len = 0;
d52b7888
NC
3363 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3364 (void) sv_2pv_flags(sv,&len, flags);
b3ab6785
KW
3365 if (SvUTF8(sv)) {
3366 if (extra) SvGROW(sv, SvCUR(sv) + extra);
d52b7888 3367 return len;
b3ab6785 3368 }
d52b7888 3369 } else {
33fb6f35 3370 (void) SvPV_force_flags(sv,len,flags & SV_GMAGIC);
d52b7888 3371 }
e0e62c2a 3372 }
4411f3b6 3373
f5cee72b 3374 if (SvUTF8(sv)) {
b3ab6785 3375 if (extra) SvGROW(sv, SvCUR(sv) + extra);
5fec3b1d 3376 return SvCUR(sv);
f5cee72b 3377 }
5fec3b1d 3378
765f542d 3379 if (SvIsCOW(sv)) {
c56ed9f6 3380 S_sv_uncow(aTHX_ sv, 0);
db42d148
NIS
3381 }
3382
b3ab6785 3383 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING)) {
799ef3cb 3384 sv_recode_to_utf8(sv, PL_encoding);
b3ab6785
KW
3385 if (extra) SvGROW(sv, SvCUR(sv) + extra);
3386 return SvCUR(sv);
3387 }
3388
4e93345f
KW
3389 if (SvCUR(sv) == 0) {
3390 if (extra) SvGROW(sv, extra);
3391 } else { /* Assume Latin-1/EBCDIC */
c4e7c712 3392 /* This function could be much more efficient if we
2bbc8d55 3393 * had a FLAG in SVs to signal if there are any variant
c4e7c712 3394 * chars in the PV. Given that there isn't such a flag
b3ab6785
KW
3395 * make the loop as fast as possible (although there are certainly ways
3396 * to speed this up, eg. through vectorization) */
3397 U8 * s = (U8 *) SvPVX_const(sv);
3398 U8 * e = (U8 *) SvEND(sv);
3399 U8 *t = s;
3400 STRLEN two_byte_count = 0;
c4e7c712 3401
b3ab6785
KW
3402 if (flags & SV_FORCE_UTF8_UPGRADE) goto must_be_utf8;
3403
3404 /* See if really will need to convert to utf8. We mustn't rely on our
3405 * incoming SV being well formed and having a trailing '\0', as certain
3406 * code in pp_formline can send us partially built SVs. */
3407
c4e7c712 3408 while (t < e) {
53c1dcc0 3409 const U8 ch = *t++;
6f2d5cbc 3410 if (NATIVE_BYTE_IS_INVARIANT(ch)) continue;
b3ab6785
KW
3411
3412 t--; /* t already incremented; re-point to first variant */
3413 two_byte_count = 1;
3414 goto must_be_utf8;
c4e7c712 3415 }
b3ab6785
KW
3416
3417 /* utf8 conversion not needed because all are invariants. Mark as
3418 * UTF-8 even if no variant - saves scanning loop */
c4e7c712 3419 SvUTF8_on(sv);
7f0bfbea 3420 if (extra) SvGROW(sv, SvCUR(sv) + extra);
b3ab6785
KW
3421 return SvCUR(sv);
3422
3423must_be_utf8:
3424
3425 /* Here, the string should be converted to utf8, either because of an
3426 * input flag (two_byte_count = 0), or because a character that
3427 * requires 2 bytes was found (two_byte_count = 1). t points either to
3428 * the beginning of the string (if we didn't examine anything), or to
3429 * the first variant. In either case, everything from s to t - 1 will
3430 * occupy only 1 byte each on output.
3431 *
3432 * There are two main ways to convert. One is to create a new string
3433 * and go through the input starting from the beginning, appending each
3434 * converted value onto the new string as we go along. It's probably
3435 * best to allocate enough space in the string for the worst possible
3436 * case rather than possibly running out of space and having to
3437 * reallocate and then copy what we've done so far. Since everything
3438 * from s to t - 1 is invariant, the destination can be initialized
3439 * with these using a fast memory copy
3440 *
3441 * The other way is to figure out exactly how big the string should be
3442 * by parsing the entire input. Then you don't have to make it big
3443 * enough to handle the worst possible case, and more importantly, if
3444 * the string you already have is large enough, you don't have to
3445 * allocate a new string, you can copy the last character in the input
3446 * string to the final position(s) that will be occupied by the
3447 * converted string and go backwards, stopping at t, since everything
3448 * before that is invariant.
3449 *
3450 * There are advantages and disadvantages to each method.
3451 *
3452 * In the first method, we can allocate a new string, do the memory
3453 * copy from the s to t - 1, and then proceed through the rest of the
3454 * string byte-by-byte.
3455 *
3456 * In the second method, we proceed through the rest of the input
3457 * string just calculating how big the converted string will be. Then
3458 * there are two cases:
3459 * 1) if the string has enough extra space to handle the converted
3460 * value. We go backwards through the string, converting until we
3461 * get to the position we are at now, and then stop. If this
3462 * position is far enough along in the string, this method is
3463 * faster than the other method. If the memory copy were the same
3464 * speed as the byte-by-byte loop, that position would be about
3465 * half-way, as at the half-way mark, parsing to the end and back
3466 * is one complete string's parse, the same amount as starting
3467 * over and going all the way through. Actually, it would be
3468 * somewhat less than half-way, as it's faster to just count bytes
3469 * than to also copy, and we don't have the overhead of allocating
3470 * a new string, changing the scalar to use it, and freeing the
3471 * existing one. But if the memory copy is fast, the break-even
3472 * point is somewhere after half way. The counting loop could be
3473 * sped up by vectorization, etc, to move the break-even point
3474 * further towards the beginning.
3475 * 2) if the string doesn't have enough space to handle the converted
3476 * value. A new string will have to be allocated, and one might
3477 * as well, given that, start from the beginning doing the first
3478 * method. We've spent extra time parsing the string and in
3479 * exchange all we've gotten is that we know precisely how big to
3480 * make the new one. Perl is more optimized for time than space,
3481 * so this case is a loser.
3482 * So what I've decided to do is not use the 2nd method unless it is
3483 * guaranteed that a new string won't have to be allocated, assuming
3484 * the worst case. I also decided not to put any more conditions on it
3485 * than this, for now. It seems likely that, since the worst case is
3486 * twice as big as the unknown portion of the string (plus 1), we won't
3487 * be guaranteed enough space, causing us to go to the first method,
3488 * unless the string is short, or the first variant character is near
3489 * the end of it. In either of these cases, it seems best to use the
3490 * 2nd method. The only circumstance I can think of where this would
3491 * be really slower is if the string had once had much more data in it
3492 * than it does now, but there is still a substantial amount in it */
3493
3494 {
3495 STRLEN invariant_head = t - s;
3496 STRLEN size = invariant_head + (e - t) * 2 + 1 + extra;
3497 if (SvLEN(sv) < size) {
3498
3499 /* Here, have decided to allocate a new string */
3500
3501 U8 *dst;
3502 U8 *d;
3503
3504 Newx(dst, size, U8);
3505
3506 /* If no known invariants at the beginning of the input string,
3507 * set so starts from there. Otherwise, can use memory copy to
3508 * get up to where we are now, and then start from here */
3509
5b26a7b3 3510 if (invariant_head == 0) {
b3ab6785
KW
3511 d = dst;
3512 } else {
3513 Copy(s, dst, invariant_head, char);
3514 d = dst + invariant_head;
3515 }
3516
3517 while (t < e) {
55d09dc8
KW
3518 append_utf8_from_native_byte(*t, &d);
3519 t++;
b3ab6785
KW
3520 }
3521 *d = '\0';
3522 SvPV_free(sv); /* No longer using pre-existing string */
3523 SvPV_set(sv, (char*)dst);
3524 SvCUR_set(sv, d - dst);
3525 SvLEN_set(sv, size);
3526 } else {
3527
3528 /* Here, have decided to get the exact size of the string.
3529 * Currently this happens only when we know that there is
3530 * guaranteed enough space to fit the converted string, so
3531 * don't have to worry about growing. If two_byte_count is 0,
3532 * then t points to the first byte of the string which hasn't
3533 * been examined yet. Otherwise two_byte_count is 1, and t
3534 * points to the first byte in the string that will expand to
3535 * two. Depending on this, start examining at t or 1 after t.
3536 * */
3537
3538 U8 *d = t + two_byte_count;
3539
3540
3541 /* Count up the remaining bytes that expand to two */
3542
3543 while (d < e) {
3544 const U8 chr = *d++;
6f2d5cbc 3545 if (! NATIVE_BYTE_IS_INVARIANT(chr)) two_byte_count++;
b3ab6785
KW
3546 }
3547
3548 /* The string will expand by just the number of bytes that
3549 * occupy two positions. But we are one afterwards because of
3550 * the increment just above. This is the place to put the
3551 * trailing NUL, and to set the length before we decrement */
3552
3553 d += two_byte_count;
3554 SvCUR_set(sv, d - s);
3555 *d-- = '\0';
3556
3557
3558 /* Having decremented d, it points to the position to put the
3559 * very last byte of the expanded string. Go backwards through
3560 * the string, copying and expanding as we go, stopping when we
3561 * get to the part that is invariant the rest of the way down */
3562
3563 e--;
3564 while (e >= t) {
6f2d5cbc 3565 if (NATIVE_BYTE_IS_INVARIANT(*e)) {
9ff651ce 3566 *d-- = *e;
b3ab6785 3567 } else {
9ff651ce
KW
3568 *d-- = UTF8_EIGHT_BIT_LO(*e);
3569 *d-- = UTF8_EIGHT_BIT_HI(*e);
b3ab6785 3570 }
9ff651ce 3571 e--;
b3ab6785
KW
3572 }
3573 }
75da9d4c
DM
3574
3575 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3576 /* Update pos. We do it at the end rather than during
3577 * the upgrade, to avoid slowing down the common case
25fdce4a
FC
3578 * (upgrade without pos).
3579 * pos can be stored as either bytes or characters. Since
3580 * this was previously a byte string we can just turn off
3581 * the bytes flag. */
75da9d4c
DM
3582 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3583 if (mg) {
25fdce4a 3584 mg->mg_flags &= ~MGf_BYTES;
75da9d4c
DM
3585 }
3586 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3587 magic_setutf8(sv,mg); /* clear UTF8 cache */
3588 }
b3ab6785 3589 }
560a288e 3590 }
b3ab6785
KW
3591
3592 /* Mark as UTF-8 even if no variant - saves scanning loop */
3593 SvUTF8_on(sv);
4411f3b6 3594 return SvCUR(sv);
560a288e
GS
3595}
3596
c461cf8f
JH
3597/*
3598=for apidoc sv_utf8_downgrade
3599
78ea37eb 3600Attempts to convert the PV of an SV from characters to bytes.
2bbc8d55
SP
3601If the PV contains a character that cannot fit
3602in a byte, this conversion will fail;
78ea37eb 3603in this case, either returns false or, if C<fail_ok> is not
c461cf8f
JH
3604true, croaks.
3605
0efd0472 3606This is not a general purpose Unicode to byte encoding interface:
13a6c0e0
JH
3607use the Encode extension for that.
3608
c461cf8f
JH
3609=cut
3610*/
3611
560a288e 3612bool
5aaab254