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