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