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