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