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