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