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