This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Replace all the S_more_* functions with a single function.
[perl5.git] / sv.c
CommitLineData
a0d0e21e 1/* sv.c
79072805 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
241d1a3b 4 * 2000, 2001, 2002, 2003, 2004, 2005, 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
JH
32#ifdef PERL_UTF8_CACHE_ASSERT
33/* The cache element 0 is the Unicode offset;
34 * the cache element 1 is the byte offset of the element 0;
35 * the cache element 2 is the Unicode length of the substring;
36 * the cache element 3 is the byte length of the substring;
37 * The checking of the substring side would be good
38 * but substr() has enough code paths to make my head spin;
39 * if adding more checks watch out for the following tests:
40 * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
41 * lib/utf8.t lib/Unicode/Collate/t/index.t
42 * --jhi
43 */
44#define ASSERT_UTF8_CACHE(cache) \
45 STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); } } STMT_END
46#else
47#define ASSERT_UTF8_CACHE(cache) NOOP
48#endif
49
f8c7b90f 50#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 51#define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
607fa7f2 52#define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next))
b5ccf5f2 53/* This is a pessimistic view. Scalar must be purely a read-write PV to copy-
765f542d 54 on-write. */
765f542d 55#endif
645c22ef
DM
56
57/* ============================================================================
58
59=head1 Allocation and deallocation of SVs.
60
5e045b90
AMS
61An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv,
62av, hv...) contains type and reference count information, as well as a
63pointer to the body (struct xrv, xpv, xpviv...), which contains fields
64specific to each type.
65
4977e971
NC
66Normally, this allocation is done using arenas, which by default are
67approximately 4K chunks of memory parcelled up into N heads or bodies. The
68first slot in each arena is reserved, and is used to hold a link to the next
69arena. In the case of heads, the unused first slot also contains some flags
70and a note of the number of slots. Snaked through each arena chain is a
5e045b90 71linked list of free items; when this becomes empty, an extra arena is
4977e971 72allocated and divided up into N items which are threaded into the free list.
645c22ef
DM
73
74The following global variables are associated with arenas:
75
76 PL_sv_arenaroot pointer to list of SV arenas
77 PL_sv_root pointer to list of free SV structures
78
79 PL_foo_arenaroot pointer to list of foo arenas,
80 PL_foo_root pointer to list of free foo bodies
81 ... for foo in xiv, xnv, xrv, xpv etc.
82
83Note that some of the larger and more rarely used body types (eg xpvio)
84are not allocated using arenas, but are instead just malloc()/free()ed as
85required. Also, if PURIFY is defined, arenas are abandoned altogether,
86with all items individually malloc()ed. In addition, a few SV heads are
87not allocated from an arena, but are instead directly created as static
4977e971
NC
88or auto variables, eg PL_sv_undef. The size of arenas can be changed from
89the default by setting PERL_ARENA_SIZE appropriately at compile time.
645c22ef
DM
90
91The SV arena serves the secondary purpose of allowing still-live SVs
92to be located and destroyed during final cleanup.
93
94At the lowest level, the macros new_SV() and del_SV() grab and free
95an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
96to return the SV to the free list with error checking.) new_SV() calls
97more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
98SVs in the free list have their SvTYPE field set to all ones.
99
100Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
101that allocate and return individual body types. Normally these are mapped
ff276b08
RG
102to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be
103instead mapped directly to malloc()/free() if PURIFY is defined. The
645c22ef
DM
104new/del functions remove from, or add to, the appropriate PL_foo_root
105list, and call more_xiv() etc to add a new arena if the list is empty.
106
ff276b08 107At the time of very final cleanup, sv_free_arenas() is called from
645c22ef
DM
108perl_destruct() to physically free all the arenas allocated since the
109start of the interpreter. Note that this also clears PL_he_arenaroot,
110which is otherwise dealt with in hv.c.
111
112Manipulation of any of the PL_*root pointers is protected by enclosing
113LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
114if threads are enabled.
115
116The function visit() scans the SV arenas list, and calls a specified
117function for each SV it finds which is still live - ie which has an SvTYPE
118other than all 1's, and a non-zero SvREFCNT. visit() is used by the
119following functions (specified as [function that calls visit()] / [function
120called by visit() for each SV]):
121
122 sv_report_used() / do_report_used()
123 dump all remaining SVs (debugging aid)
124
125 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
126 Attempt to free all objects pointed to by RVs,
127 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
128 try to do the same for all objects indirectly
129 referenced by typeglobs too. Called once from
130 perl_destruct(), prior to calling sv_clean_all()
131 below.
132
133 sv_clean_all() / do_clean_all()
134 SvREFCNT_dec(sv) each remaining SV, possibly
135 triggering an sv_free(). It also sets the
136 SVf_BREAK flag on the SV to indicate that the
137 refcnt has been artificially lowered, and thus
138 stopping sv_free() from giving spurious warnings
139 about SVs which unexpectedly have a refcnt
140 of zero. called repeatedly from perl_destruct()
141 until there are no SVs left.
142
143=head2 Summary
144
145Private API to rest of sv.c
146
147 new_SV(), del_SV(),
148
149 new_XIV(), del_XIV(),
150 new_XNV(), del_XNV(),
151 etc
152
153Public API:
154
8cf8f3d1 155 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
645c22ef
DM
156
157
158=cut
159
160============================================================================ */
161
162
51371543 163
4561caa4
CS
164/*
165 * "A time to plant, and a time to uproot what was planted..."
166 */
167
cac9b346 168
fd0854ff
DM
169#ifdef DEBUG_LEAKING_SCALARS
170# ifdef NETWARE
171# define FREE_SV_DEBUG_FILE(sv) PerlMemfree((sv)->sv_debug_file)
172# else
173# define FREE_SV_DEBUG_FILE(sv) PerlMemShared_free((sv)->sv_debug_file)
174# endif
175#else
176# define FREE_SV_DEBUG_FILE(sv)
177#endif
178
053fc874
GS
179#define plant_SV(p) \
180 STMT_START { \
fd0854ff 181 FREE_SV_DEBUG_FILE(p); \
053fc874
GS
182 SvANY(p) = (void *)PL_sv_root; \
183 SvFLAGS(p) = SVTYPEMASK; \
184 PL_sv_root = (p); \
185 --PL_sv_count; \
186 } STMT_END
a0d0e21e 187
fba3b22e 188/* sv_mutex must be held while calling uproot_SV() */
053fc874
GS
189#define uproot_SV(p) \
190 STMT_START { \
191 (p) = PL_sv_root; \
192 PL_sv_root = (SV*)SvANY(p); \
193 ++PL_sv_count; \
194 } STMT_END
195
645c22ef 196
cac9b346
NC
197/* make some more SVs by adding another arena */
198
199/* sv_mutex must be held while calling more_sv() */
200STATIC SV*
201S_more_sv(pTHX)
202{
203 SV* sv;
204
205 if (PL_nice_chunk) {
206 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
207 PL_nice_chunk = Nullch;
208 PL_nice_chunk_size = 0;
209 }
210 else {
211 char *chunk; /* must use New here to match call to */
2e7ed132
NC
212 New(704,chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
213 sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
cac9b346
NC
214 }
215 uproot_SV(sv);
216 return sv;
217}
218
645c22ef
DM
219/* new_SV(): return a new, empty SV head */
220
eba0f806
DM
221#ifdef DEBUG_LEAKING_SCALARS
222/* provide a real function for a debugger to play with */
223STATIC SV*
224S_new_SV(pTHX)
225{
226 SV* sv;
227
228 LOCK_SV_MUTEX;
229 if (PL_sv_root)
230 uproot_SV(sv);
231 else
cac9b346 232 sv = S_more_sv(aTHX);
eba0f806
DM
233 UNLOCK_SV_MUTEX;
234 SvANY(sv) = 0;
235 SvREFCNT(sv) = 1;
236 SvFLAGS(sv) = 0;
fd0854ff
DM
237 sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
238 sv->sv_debug_line = (U16) ((PL_copline == NOLINE) ?
239 (PL_curcop ? CopLINE(PL_curcop) : 0) : PL_copline);
240 sv->sv_debug_inpad = 0;
241 sv->sv_debug_cloned = 0;
242# ifdef NETWARE
243 sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL;
244# else
245 sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL;
246# endif
247
eba0f806
DM
248 return sv;
249}
250# define new_SV(p) (p)=S_new_SV(aTHX)
251
252#else
253# define new_SV(p) \
053fc874
GS
254 STMT_START { \
255 LOCK_SV_MUTEX; \
256 if (PL_sv_root) \
257 uproot_SV(p); \
258 else \
cac9b346 259 (p) = S_more_sv(aTHX); \
053fc874
GS
260 UNLOCK_SV_MUTEX; \
261 SvANY(p) = 0; \
262 SvREFCNT(p) = 1; \
263 SvFLAGS(p) = 0; \
264 } STMT_END
eba0f806 265#endif
463ee0b2 266
645c22ef
DM
267
268/* del_SV(): return an empty SV head to the free list */
269
a0d0e21e 270#ifdef DEBUGGING
4561caa4 271
053fc874
GS
272#define del_SV(p) \
273 STMT_START { \
274 LOCK_SV_MUTEX; \
aea4f609 275 if (DEBUG_D_TEST) \
053fc874
GS
276 del_sv(p); \
277 else \
278 plant_SV(p); \
279 UNLOCK_SV_MUTEX; \
280 } STMT_END
a0d0e21e 281
76e3520e 282STATIC void
cea2e8a9 283S_del_sv(pTHX_ SV *p)
463ee0b2 284{
aea4f609 285 if (DEBUG_D_TEST) {
4633a7c4 286 SV* sva;
a3b680e6 287 bool ok = 0;
3280af22 288 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
a3b680e6
AL
289 SV *sv = sva + 1;
290 SV *svend = &sva[SvREFCNT(sva)];
c0ff570e 291 if (p >= sv && p < svend) {
a0d0e21e 292 ok = 1;
c0ff570e
NC
293 break;
294 }
a0d0e21e
LW
295 }
296 if (!ok) {
0453d815 297 if (ckWARN_d(WARN_INTERNAL))
9014280d 298 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
472d47bc
SB
299 "Attempt to free non-arena SV: 0x%"UVxf
300 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
a0d0e21e
LW
301 return;
302 }
303 }
4561caa4 304 plant_SV(p);
463ee0b2 305}
a0d0e21e 306
4561caa4
CS
307#else /* ! DEBUGGING */
308
309#define del_SV(p) plant_SV(p)
310
311#endif /* DEBUGGING */
463ee0b2 312
645c22ef
DM
313
314/*
ccfc67b7
JH
315=head1 SV Manipulation Functions
316
645c22ef
DM
317=for apidoc sv_add_arena
318
319Given a chunk of memory, link it to the head of the list of arenas,
320and split it into a list of free SVs.
321
322=cut
323*/
324
4633a7c4 325void
864dbfa3 326Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
463ee0b2 327{
4633a7c4 328 SV* sva = (SV*)ptr;
463ee0b2
LW
329 register SV* sv;
330 register SV* svend;
4633a7c4
LW
331
332 /* The first SV in an arena isn't an SV. */
3280af22 333 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
4633a7c4
LW
334 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
335 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
336
3280af22
NIS
337 PL_sv_arenaroot = sva;
338 PL_sv_root = sva + 1;
4633a7c4
LW
339
340 svend = &sva[SvREFCNT(sva) - 1];
341 sv = sva + 1;
463ee0b2 342 while (sv < svend) {
a0d0e21e 343 SvANY(sv) = (void *)(SV*)(sv + 1);
03e36789 344#ifdef DEBUGGING
978b032e 345 SvREFCNT(sv) = 0;
03e36789
NC
346#endif
347 /* Must always set typemask because it's awlays checked in on cleanup
348 when the arenas are walked looking for objects. */
8990e307 349 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
350 sv++;
351 }
352 SvANY(sv) = 0;
03e36789
NC
353#ifdef DEBUGGING
354 SvREFCNT(sv) = 0;
355#endif
4633a7c4
LW
356 SvFLAGS(sv) = SVTYPEMASK;
357}
358
055972dc
DM
359/* visit(): call the named function for each non-free SV in the arenas
360 * whose flags field matches the flags/mask args. */
645c22ef 361
5226ed68 362STATIC I32
055972dc 363S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
8990e307 364{
4633a7c4 365 SV* sva;
5226ed68 366 I32 visited = 0;
8990e307 367
3280af22 368 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
a3b680e6
AL
369 register SV * const svend = &sva[SvREFCNT(sva)];
370 register SV* sv;
4561caa4 371 for (sv = sva + 1; sv < svend; ++sv) {
055972dc
DM
372 if (SvTYPE(sv) != SVTYPEMASK
373 && (sv->sv_flags & mask) == flags
374 && SvREFCNT(sv))
375 {
acfe0abc 376 (FCALL)(aTHX_ sv);
5226ed68
JH
377 ++visited;
378 }
8990e307
LW
379 }
380 }
5226ed68 381 return visited;
8990e307
LW
382}
383
758a08c3
JH
384#ifdef DEBUGGING
385
645c22ef
DM
386/* called by sv_report_used() for each live SV */
387
388static void
acfe0abc 389do_report_used(pTHX_ SV *sv)
645c22ef
DM
390{
391 if (SvTYPE(sv) != SVTYPEMASK) {
392 PerlIO_printf(Perl_debug_log, "****\n");
393 sv_dump(sv);
394 }
395}
758a08c3 396#endif
645c22ef
DM
397
398/*
399=for apidoc sv_report_used
400
401Dump the contents of all SVs not yet freed. (Debugging aid).
402
403=cut
404*/
405
8990e307 406void
864dbfa3 407Perl_sv_report_used(pTHX)
4561caa4 408{
ff270d3a 409#ifdef DEBUGGING
055972dc 410 visit(do_report_used, 0, 0);
ff270d3a 411#endif
4561caa4
CS
412}
413
645c22ef
DM
414/* called by sv_clean_objs() for each live SV */
415
416static void
acfe0abc 417do_clean_objs(pTHX_ SV *sv)
645c22ef
DM
418{
419 SV* rv;
420
421 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
422 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv)));
423 if (SvWEAKREF(sv)) {
424 sv_del_backref(sv);
425 SvWEAKREF_off(sv);
b162af07 426 SvRV_set(sv, NULL);
645c22ef
DM
427 } else {
428 SvROK_off(sv);
b162af07 429 SvRV_set(sv, NULL);
645c22ef
DM
430 SvREFCNT_dec(rv);
431 }
432 }
433
434 /* XXX Might want to check arrays, etc. */
435}
436
437/* called by sv_clean_objs() for each live SV */
438
439#ifndef DISABLE_DESTRUCTOR_KLUDGE
440static void
acfe0abc 441do_clean_named_objs(pTHX_ SV *sv)
645c22ef
DM
442{
443 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
444 if ( SvOBJECT(GvSV(sv)) ||
445 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
446 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
447 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
448 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
449 {
450 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
ec5f3c78 451 SvFLAGS(sv) |= SVf_BREAK;
645c22ef
DM
452 SvREFCNT_dec(sv);
453 }
454 }
455}
456#endif
457
458/*
459=for apidoc sv_clean_objs
460
461Attempt to destroy all objects not yet freed
462
463=cut
464*/
465
4561caa4 466void
864dbfa3 467Perl_sv_clean_objs(pTHX)
4561caa4 468{
3280af22 469 PL_in_clean_objs = TRUE;
055972dc 470 visit(do_clean_objs, SVf_ROK, SVf_ROK);
4561caa4 471#ifndef DISABLE_DESTRUCTOR_KLUDGE
2d0f3c12 472 /* some barnacles may yet remain, clinging to typeglobs */
055972dc 473 visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
4561caa4 474#endif
3280af22 475 PL_in_clean_objs = FALSE;
4561caa4
CS
476}
477
645c22ef
DM
478/* called by sv_clean_all() for each live SV */
479
480static void
acfe0abc 481do_clean_all(pTHX_ SV *sv)
645c22ef
DM
482{
483 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
484 SvFLAGS(sv) |= SVf_BREAK;
0e705b3b
DM
485 if (PL_comppad == (AV*)sv) {
486 PL_comppad = Nullav;
487 PL_curpad = Null(SV**);
488 }
645c22ef
DM
489 SvREFCNT_dec(sv);
490}
491
492/*
493=for apidoc sv_clean_all
494
495Decrement the refcnt of each remaining SV, possibly triggering a
496cleanup. This function may have to be called multiple times to free
ff276b08 497SVs which are in complex self-referential hierarchies.
645c22ef
DM
498
499=cut
500*/
501
5226ed68 502I32
864dbfa3 503Perl_sv_clean_all(pTHX)
8990e307 504{
5226ed68 505 I32 cleaned;
3280af22 506 PL_in_clean_all = TRUE;
055972dc 507 cleaned = visit(do_clean_all, 0,0);
3280af22 508 PL_in_clean_all = FALSE;
5226ed68 509 return cleaned;
8990e307 510}
463ee0b2 511
645c22ef
DM
512/*
513=for apidoc sv_free_arenas
514
515Deallocate the memory used by all arenas. Note that all the individual SV
516heads and bodies within the arenas must already have been freed.
517
518=cut
519*/
520
4633a7c4 521void
864dbfa3 522Perl_sv_free_arenas(pTHX)
4633a7c4
LW
523{
524 SV* sva;
525 SV* svanext;
7b2c381c 526 void *arena, *arenanext;
4633a7c4
LW
527
528 /* Free arenas here, but be careful about fake ones. (We assume
529 contiguity of the fake ones with the corresponding real ones.) */
530
3280af22 531 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
4633a7c4
LW
532 svanext = (SV*) SvANY(sva);
533 while (svanext && SvFAKE(svanext))
534 svanext = (SV*) SvANY(svanext);
535
536 if (!SvFAKE(sva))
1df70142 537 Safefree(sva);
4633a7c4 538 }
5f05dabc 539
612f20c3 540 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
7b2c381c 541 arenanext = *(void **)arena;
612f20c3
GS
542 Safefree(arena);
543 }
544 PL_xnv_arenaroot = 0;
bf9cdc68 545 PL_xnv_root = 0;
612f20c3 546
612f20c3 547 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
7b2c381c 548 arenanext = *(void **)arena;
612f20c3
GS
549 Safefree(arena);
550 }
551 PL_xpv_arenaroot = 0;
bf9cdc68 552 PL_xpv_root = 0;
612f20c3 553
7b2c381c
NC
554 for (arena = PL_xpviv_arenaroot; arena; arena = arenanext) {
555 arenanext = *(void **)arena;
612f20c3
GS
556 Safefree(arena);
557 }
558 PL_xpviv_arenaroot = 0;
bf9cdc68 559 PL_xpviv_root = 0;
612f20c3 560
7b2c381c
NC
561 for (arena = PL_xpvnv_arenaroot; arena; arena = arenanext) {
562 arenanext = *(void **)arena;
612f20c3
GS
563 Safefree(arena);
564 }
565 PL_xpvnv_arenaroot = 0;
bf9cdc68 566 PL_xpvnv_root = 0;
612f20c3 567
7b2c381c
NC
568 for (arena = PL_xpvcv_arenaroot; arena; arena = arenanext) {
569 arenanext = *(void **)arena;
612f20c3
GS
570 Safefree(arena);
571 }
572 PL_xpvcv_arenaroot = 0;
bf9cdc68 573 PL_xpvcv_root = 0;
612f20c3 574
7b2c381c
NC
575 for (arena = PL_xpvav_arenaroot; arena; arena = arenanext) {
576 arenanext = *(void **)arena;
612f20c3
GS
577 Safefree(arena);
578 }
579 PL_xpvav_arenaroot = 0;
bf9cdc68 580 PL_xpvav_root = 0;
612f20c3 581
7b2c381c
NC
582 for (arena = PL_xpvhv_arenaroot; arena; arena = arenanext) {
583 arenanext = *(void **)arena;
612f20c3
GS
584 Safefree(arena);
585 }
586 PL_xpvhv_arenaroot = 0;
bf9cdc68 587 PL_xpvhv_root = 0;
612f20c3 588
7b2c381c
NC
589 for (arena = PL_xpvmg_arenaroot; arena; arena = arenanext) {
590 arenanext = *(void **)arena;
612f20c3
GS
591 Safefree(arena);
592 }
593 PL_xpvmg_arenaroot = 0;
bf9cdc68 594 PL_xpvmg_root = 0;
612f20c3 595
7b2c381c
NC
596 for (arena = PL_xpvgv_arenaroot; arena; arena = arenanext) {
597 arenanext = *(void **)arena;
727879eb
NC
598 Safefree(arena);
599 }
600 PL_xpvgv_arenaroot = 0;
601 PL_xpvgv_root = 0;
602
7b2c381c
NC
603 for (arena = PL_xpvlv_arenaroot; arena; arena = arenanext) {
604 arenanext = *(void **)arena;
612f20c3
GS
605 Safefree(arena);
606 }
607 PL_xpvlv_arenaroot = 0;
bf9cdc68 608 PL_xpvlv_root = 0;
612f20c3 609
7b2c381c
NC
610 for (arena = PL_xpvbm_arenaroot; arena; arena = arenanext) {
611 arenanext = *(void **)arena;
612f20c3
GS
612 Safefree(arena);
613 }
614 PL_xpvbm_arenaroot = 0;
bf9cdc68 615 PL_xpvbm_root = 0;
612f20c3 616
b1135e3d
NC
617 {
618 HE *he;
619 HE *he_next;
620 for (he = PL_he_arenaroot; he; he = he_next) {
621 he_next = HeNEXT(he);
622 Safefree(he);
623 }
612f20c3
GS
624 }
625 PL_he_arenaroot = 0;
bf9cdc68 626 PL_he_root = 0;
612f20c3 627
892b45be 628#if defined(USE_ITHREADS)
b1135e3d
NC
629 {
630 struct ptr_tbl_ent *pte;
631 struct ptr_tbl_ent *pte_next;
632 for (pte = PL_pte_arenaroot; pte; pte = pte_next) {
633 pte_next = pte->next;
634 Safefree(pte);
635 }
32e691d0
NC
636 }
637 PL_pte_arenaroot = 0;
638 PL_pte_root = 0;
892b45be 639#endif
32e691d0 640
3280af22
NIS
641 if (PL_nice_chunk)
642 Safefree(PL_nice_chunk);
643 PL_nice_chunk = Nullch;
644 PL_nice_chunk_size = 0;
645 PL_sv_arenaroot = 0;
646 PL_sv_root = 0;
4633a7c4
LW
647}
648
29489e7c
DM
649/* ---------------------------------------------------------------------
650 *
651 * support functions for report_uninit()
652 */
653
654/* the maxiumum size of array or hash where we will scan looking
655 * for the undefined element that triggered the warning */
656
657#define FUV_MAX_SEARCH_SIZE 1000
658
659/* Look for an entry in the hash whose value has the same SV as val;
660 * If so, return a mortal copy of the key. */
661
662STATIC SV*
663S_find_hash_subscript(pTHX_ HV *hv, SV* val)
664{
27da23d5 665 dVAR;
29489e7c 666 register HE **array;
29489e7c
DM
667 I32 i;
668
669 if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
670 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
671 return Nullsv;
672
673 array = HvARRAY(hv);
674
675 for (i=HvMAX(hv); i>0; i--) {
f54cb97a 676 register HE *entry;
29489e7c
DM
677 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
678 if (HeVAL(entry) != val)
679 continue;
680 if ( HeVAL(entry) == &PL_sv_undef ||
681 HeVAL(entry) == &PL_sv_placeholder)
682 continue;
683 if (!HeKEY(entry))
684 return Nullsv;
685 if (HeKLEN(entry) == HEf_SVKEY)
686 return sv_mortalcopy(HeKEY_sv(entry));
687 return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry)));
688 }
689 }
690 return Nullsv;
691}
692
693/* Look for an entry in the array whose value has the same SV as val;
694 * If so, return the index, otherwise return -1. */
695
696STATIC I32
697S_find_array_subscript(pTHX_ AV *av, SV* val)
698{
699 SV** svp;
700 I32 i;
701 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
702 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
703 return -1;
704
705 svp = AvARRAY(av);
706 for (i=AvFILLp(av); i>=0; i--) {
707 if (svp[i] == val && svp[i] != &PL_sv_undef)
708 return i;
709 }
710 return -1;
711}
712
713/* S_varname(): return the name of a variable, optionally with a subscript.
714 * If gv is non-zero, use the name of that global, along with gvtype (one
715 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
716 * targ. Depending on the value of the subscript_type flag, return:
717 */
718
719#define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
720#define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
721#define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
722#define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
723
724STATIC SV*
bfed75c6 725S_varname(pTHX_ GV *gv, const char *gvtype, PADOFFSET targ,
29489e7c
DM
726 SV* keyname, I32 aindex, int subscript_type)
727{
728 AV *av;
a3b680e6 729 SV *sv;
29489e7c 730
a3b680e6 731 SV * const name = sv_newmortal();
29489e7c
DM
732 if (gv) {
733
734 /* simulate gv_fullname4(), but add literal '^' for $^FOO names
735 * XXX get rid of all this if gv_fullnameX() ever supports this
736 * directly */
737
bfed75c6 738 const char *p;
29489e7c
DM
739 HV *hv = GvSTASH(gv);
740 sv_setpv(name, gvtype);
741 if (!hv)
742 p = "???";
bfcb3514 743 else if (!(p=HvNAME_get(hv)))
29489e7c 744 p = "__ANON__";
29489e7c
DM
745 if (strNE(p, "main")) {
746 sv_catpv(name,p);
747 sv_catpvn(name,"::", 2);
748 }
749 if (GvNAMELEN(gv)>= 1 &&
750 ((unsigned int)*GvNAME(gv)) <= 26)
751 { /* handle $^FOO */
752 Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1);
753 sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1);
754 }
755 else
756 sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv));
757 }
758 else {
759 U32 u;
760 CV *cv = find_runcv(&u);
761 if (!cv || !CvPADLIST(cv))
762 return Nullsv;;
763 av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE));
764 sv = *av_fetch(av, targ, FALSE);
765 /* SvLEN in a pad name is not to be trusted */
f9926b10 766 sv_setpv(name, SvPV_nolen_const(sv));
29489e7c
DM
767 }
768
769 if (subscript_type == FUV_SUBSCRIPT_HASH) {
770 *SvPVX(name) = '$';
771 sv = NEWSV(0,0);
772 Perl_sv_catpvf(aTHX_ name, "{%s}",
3f7c398e 773 pv_display(sv,SvPVX_const(keyname), SvCUR(keyname), 0, 32));
29489e7c
DM
774 SvREFCNT_dec(sv);
775 }
776 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
777 *SvPVX(name) = '$';
265a12b8 778 Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
29489e7c
DM
779 }
780 else if (subscript_type == FUV_SUBSCRIPT_WITHIN)
781 sv_insert(name, 0, 0, "within ", 7);
782
783 return name;
784}
785
786
787/*
788=for apidoc find_uninit_var
789
790Find the name of the undefined variable (if any) that caused the operator o
791to issue a "Use of uninitialized value" warning.
792If match is true, only return a name if it's value matches uninit_sv.
793So roughly speaking, if a unary operator (such as OP_COS) generates a
794warning, then following the direct child of the op may yield an
795OP_PADSV or OP_GV that gives the name of the undefined variable. On the
796other hand, with OP_ADD there are two branches to follow, so we only print
797the variable name if we get an exact match.
798
799The name is returned as a mortal SV.
800
801Assumes that PL_op is the op that originally triggered the error, and that
802PL_comppad/PL_curpad points to the currently executing pad.
803
804=cut
805*/
806
807STATIC SV *
808S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match)
809{
27da23d5 810 dVAR;
29489e7c
DM
811 SV *sv;
812 AV *av;
813 SV **svp;
814 GV *gv;
815 OP *o, *o2, *kid;
816
817 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
818 uninit_sv == &PL_sv_placeholder)))
819 return Nullsv;
820
821 switch (obase->op_type) {
822
823 case OP_RV2AV:
824 case OP_RV2HV:
825 case OP_PADAV:
826 case OP_PADHV:
827 {
f54cb97a
AL
828 const bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
829 const bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV);
112dcc46
RGS
830 I32 index = 0;
831 SV *keysv = Nullsv;
29489e7c
DM
832 int subscript_type = FUV_SUBSCRIPT_WITHIN;
833
834 if (pad) { /* @lex, %lex */
835 sv = PAD_SVl(obase->op_targ);
836 gv = Nullgv;
837 }
838 else {
839 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
840 /* @global, %global */
841 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
842 if (!gv)
843 break;
844 sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv);
845 }
846 else /* @{expr}, %{expr} */
847 return find_uninit_var(cUNOPx(obase)->op_first,
848 uninit_sv, match);
849 }
850
851 /* attempt to find a match within the aggregate */
852 if (hash) {
853 keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
854 if (keysv)
855 subscript_type = FUV_SUBSCRIPT_HASH;
856 }
857 else {
858 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
859 if (index >= 0)
860 subscript_type = FUV_SUBSCRIPT_ARRAY;
861 }
862
863 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
864 break;
865
866 return S_varname(aTHX_ gv, hash ? "%" : "@", obase->op_targ,
867 keysv, index, subscript_type);
868 }
869
870 case OP_PADSV:
871 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
872 break;
873 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
874 Nullsv, 0, FUV_SUBSCRIPT_NONE);
875
876 case OP_GVSV:
877 gv = cGVOPx_gv(obase);
878 if (!gv || (match && GvSV(gv) != uninit_sv))
879 break;
880 return S_varname(aTHX_ gv, "$", 0, Nullsv, 0, FUV_SUBSCRIPT_NONE);
881
882 case OP_AELEMFAST:
883 if (obase->op_flags & OPf_SPECIAL) { /* lexical array */
884 if (match) {
885 av = (AV*)PAD_SV(obase->op_targ);
886 if (!av || SvRMAGICAL(av))
887 break;
888 svp = av_fetch(av, (I32)obase->op_private, FALSE);
889 if (!svp || *svp != uninit_sv)
890 break;
891 }
892 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
893 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
894 }
895 else {
896 gv = cGVOPx_gv(obase);
897 if (!gv)
898 break;
899 if (match) {
900 av = GvAV(gv);
901 if (!av || SvRMAGICAL(av))
902 break;
903 svp = av_fetch(av, (I32)obase->op_private, FALSE);
904 if (!svp || *svp != uninit_sv)
905 break;
906 }
907 return S_varname(aTHX_ gv, "$", 0,
908 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
909 }
910 break;
911
912 case OP_EXISTS:
913 o = cUNOPx(obase)->op_first;
914 if (!o || o->op_type != OP_NULL ||
915 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
916 break;
917 return find_uninit_var(cBINOPo->op_last, uninit_sv, match);
918
919 case OP_AELEM:
920 case OP_HELEM:
921 if (PL_op == obase)
922 /* $a[uninit_expr] or $h{uninit_expr} */
923 return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match);
924
925 gv = Nullgv;
926 o = cBINOPx(obase)->op_first;
927 kid = cBINOPx(obase)->op_last;
928
929 /* get the av or hv, and optionally the gv */
930 sv = Nullsv;
931 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
932 sv = PAD_SV(o->op_targ);
933 }
934 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
935 && cUNOPo->op_first->op_type == OP_GV)
936 {
937 gv = cGVOPx_gv(cUNOPo->op_first);
938 if (!gv)
939 break;
940 sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv);
941 }
942 if (!sv)
943 break;
944
945 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
946 /* index is constant */
947 if (match) {
948 if (SvMAGICAL(sv))
949 break;
950 if (obase->op_type == OP_HELEM) {
951 HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0);
952 if (!he || HeVAL(he) != uninit_sv)
953 break;
954 }
955 else {
956 svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE);
957 if (!svp || *svp != uninit_sv)
958 break;
959 }
960 }
961 if (obase->op_type == OP_HELEM)
962 return S_varname(aTHX_ gv, "%", o->op_targ,
963 cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH);
964 else
965 return S_varname(aTHX_ gv, "@", o->op_targ, Nullsv,
966 SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY);
967 ;
968 }
969 else {
970 /* index is an expression;
971 * attempt to find a match within the aggregate */
972 if (obase->op_type == OP_HELEM) {
973 SV *keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
974 if (keysv)
975 return S_varname(aTHX_ gv, "%", o->op_targ,
976 keysv, 0, FUV_SUBSCRIPT_HASH);
977 }
978 else {
f54cb97a 979 const I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
29489e7c 980 if (index >= 0)
f54cb97a 981 return S_varname(aTHX_ gv, "@", o->op_targ,
29489e7c
DM
982 Nullsv, index, FUV_SUBSCRIPT_ARRAY);
983 }
984 if (match)
985 break;
986 return S_varname(aTHX_ gv,
987 (o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
988 ? "@" : "%",
989 o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN);
990 }
991
992 break;
993
994 case OP_AASSIGN:
995 /* only examine RHS */
996 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match);
997
998 case OP_OPEN:
999 o = cUNOPx(obase)->op_first;
1000 if (o->op_type == OP_PUSHMARK)
1001 o = o->op_sibling;
1002
1003 if (!o->op_sibling) {
1004 /* one-arg version of open is highly magical */
1005
1006 if (o->op_type == OP_GV) { /* open FOO; */
1007 gv = cGVOPx_gv(o);
1008 if (match && GvSV(gv) != uninit_sv)
1009 break;
7a5fa8a2 1010 return S_varname(aTHX_ gv, "$", 0,
29489e7c
DM
1011 Nullsv, 0, FUV_SUBSCRIPT_NONE);
1012 }
1013 /* other possibilities not handled are:
1014 * open $x; or open my $x; should return '${*$x}'
1015 * open expr; should return '$'.expr ideally
1016 */
1017 break;
1018 }
1019 goto do_op;
1020
1021 /* ops where $_ may be an implicit arg */
1022 case OP_TRANS:
1023 case OP_SUBST:
1024 case OP_MATCH:
1025 if ( !(obase->op_flags & OPf_STACKED)) {
1026 if (uninit_sv == ((obase->op_private & OPpTARGET_MY)
1027 ? PAD_SVl(obase->op_targ)
1028 : DEFSV))
1029 {
1030 sv = sv_newmortal();
616d8c9c 1031 sv_setpvn(sv, "$_", 2);
29489e7c
DM
1032 return sv;
1033 }
1034 }
1035 goto do_op;
1036
1037 case OP_PRTF:
1038 case OP_PRINT:
1039 /* skip filehandle as it can't produce 'undef' warning */
1040 o = cUNOPx(obase)->op_first;
1041 if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK)
1042 o = o->op_sibling->op_sibling;
1043 goto do_op2;
1044
1045
e21bd382 1046 case OP_RV2SV:
29489e7c
DM
1047 case OP_CUSTOM:
1048 case OP_ENTERSUB:
1049 match = 1; /* XS or custom code could trigger random warnings */
1050 goto do_op;
1051
1052 case OP_SCHOMP:
1053 case OP_CHOMP:
1054 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
1055 return sv_2mortal(newSVpv("${$/}", 0));
1056 /* FALL THROUGH */
1057
1058 default:
1059 do_op:
1060 if (!(obase->op_flags & OPf_KIDS))
1061 break;
1062 o = cUNOPx(obase)->op_first;
1063
1064 do_op2:
1065 if (!o)
1066 break;
1067
1068 /* if all except one arg are constant, or have no side-effects,
1069 * or are optimized away, then it's unambiguous */
1070 o2 = Nullop;
1071 for (kid=o; kid; kid = kid->op_sibling) {
1072 if (kid &&
1073 ( (kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid)))
1074 || (kid->op_type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
1075 || (kid->op_type == OP_PUSHMARK)
1076 )
1077 )
1078 continue;
1079 if (o2) { /* more than one found */
1080 o2 = Nullop;
1081 break;
1082 }
1083 o2 = kid;
1084 }
1085 if (o2)
1086 return find_uninit_var(o2, uninit_sv, match);
1087
1088 /* scan all args */
1089 while (o) {
1090 sv = find_uninit_var(o, uninit_sv, 1);
1091 if (sv)
1092 return sv;
1093 o = o->op_sibling;
1094 }
1095 break;
1096 }
1097 return Nullsv;
1098}
1099
1100
645c22ef
DM
1101/*
1102=for apidoc report_uninit
1103
1104Print appropriate "Use of uninitialized variable" warning
1105
1106=cut
1107*/
1108
1d7c1841 1109void
29489e7c
DM
1110Perl_report_uninit(pTHX_ SV* uninit_sv)
1111{
1112 if (PL_op) {
112dcc46 1113 SV* varname = Nullsv;
29489e7c
DM
1114 if (uninit_sv) {
1115 varname = find_uninit_var(PL_op, uninit_sv,0);
1116 if (varname)
1117 sv_insert(varname, 0, 0, " ", 1);
1118 }
9014280d 1119 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
93524f2b 1120 varname ? SvPV_nolen_const(varname) : "",
29489e7c
DM
1121 " in ", OP_DESC(PL_op));
1122 }
1d7c1841 1123 else
29489e7c
DM
1124 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1125 "", "", "");
1d7c1841
GS
1126}
1127
cac9b346 1128STATIC void
e3bbdc67 1129S_more_bodies (pTHX_ void **arena_root, void **root, size_t size)
cac9b346 1130{
e3bbdc67
NC
1131 char *start;
1132 const char *end;
1133 size_t count = PERL_ARENA_SIZE/size;
1134 New(0, start, count*size, char);
1135 *((void **) start) = *arena_root;
1136 *arena_root = (void *)start;
cac9b346 1137
e3bbdc67 1138 end = start + (count-1) * size;
cac9b346 1139
e3bbdc67
NC
1140 /* The initial slot is used to link the arenas together, so it isn't to be
1141 linked into the list of ready-to-use bodies. */
cac9b346 1142
e3bbdc67 1143 start += size;
cac9b346 1144
e3bbdc67 1145 *root = (void *)start;
cac9b346 1146
e3bbdc67
NC
1147 while (start < end) {
1148 char *next = start + size;
1149 *(void**) start = (void *)next;
1150 start = next;
cac9b346 1151 }
e3bbdc67 1152 *(void **)start = 0;
cac9b346
NC
1153}
1154
e3bbdc67
NC
1155#define more_thingy(TYPE,lctype) \
1156 S_more_bodies(aTHX_ (void**)&PL_## lctype ## _arenaroot, \
1157 (void**)&PL_ ## lctype ## _root, \
1158 sizeof(TYPE))
cac9b346 1159
e3bbdc67
NC
1160#define more_thingy_allocated(lctype) \
1161 S_more_bodies(aTHX_ (void**)&PL_## lctype ## _arenaroot, \
1162 (void**)&PL_ ## lctype ## _root, \
1163 sizeof(lctype ## _allocated))
cac9b346 1164
cac9b346 1165
e3bbdc67
NC
1166#define more_xnv() more_thingy(NV, xnv)
1167#define more_xpv() more_thingy_allocated(xpv)
1168#define more_xpviv() more_thingy_allocated(xpviv)
1169#define more_xpvnv() more_thingy(XPVNV, xpvnv)
1170#define more_xpvcv() more_thingy(XPVCV, xpvcv)
1171#define more_xpvav() more_thingy_allocated(xpvav)
1172#define more_xpvhv() more_thingy_allocated(xpvhv)
1173#define more_xpvgv() more_thingy(XPVGV, xpvgv)
1174#define more_xpvmg() more_thingy(XPVMG, xpvmg)
1175#define more_xpvbm() more_thingy(XPVBM, xpvbm)
1176#define more_xpvlv() more_thingy(XPVLV, xpvlv)
cac9b346 1177
950dc694 1178
cac9b346
NC
1179/* grab a new NV body from the free list, allocating more if necessary */
1180
1181STATIC XPVNV*
1182S_new_xnv(pTHX)
1183{
1184 NV* xnv;
1185 LOCK_SV_MUTEX;
1186 if (!PL_xnv_root)
e3bbdc67 1187 more_xnv();
cac9b346
NC
1188 xnv = PL_xnv_root;
1189 PL_xnv_root = *(NV**)xnv;
1190 UNLOCK_SV_MUTEX;
1191 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
1192}
1193
1194/* return an NV body to the free list */
1195
1196STATIC void
1197S_del_xnv(pTHX_ XPVNV *p)
1198{
1199 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
1200 LOCK_SV_MUTEX;
1201 *(NV**)xnv = PL_xnv_root;
1202 PL_xnv_root = xnv;
1203 UNLOCK_SV_MUTEX;
ed6116ce
LW
1204}
1205
645c22ef
DM
1206/* grab a new struct xpv from the free list, allocating more if necessary */
1207
76e3520e 1208STATIC XPV*
cea2e8a9 1209S_new_xpv(pTHX)
463ee0b2 1210{
59813432 1211 xpv_allocated* xpv;
cbe51380
GS
1212 LOCK_SV_MUTEX;
1213 if (!PL_xpv_root)
e3bbdc67 1214 more_xpv();
cbe51380 1215 xpv = PL_xpv_root;
59813432 1216 PL_xpv_root = *(xpv_allocated**)xpv;
cbe51380 1217 UNLOCK_SV_MUTEX;
59813432
NC
1218 /* If xpv_allocated is the same structure as XPV then the two OFFSETs
1219 sum to zero, and the pointer is unchanged. If the allocated structure
1220 is smaller (no initial IV actually allocated) then the net effect is
1221 to subtract the size of the IV from the pointer, to return a new pointer
1222 as if an initial IV were actually allocated. */
1223 return (XPV*)((char*)xpv - STRUCT_OFFSET(XPV, xpv_cur)
1224 + STRUCT_OFFSET(xpv_allocated, xpv_cur));
463ee0b2
LW
1225}
1226
645c22ef
DM
1227/* return a struct xpv to the free list */
1228
76e3520e 1229STATIC void
cea2e8a9 1230S_del_xpv(pTHX_ XPV *p)
463ee0b2 1231{
59813432
NC
1232 xpv_allocated* xpv
1233 = (xpv_allocated*)((char*)(p) + STRUCT_OFFSET(XPV, xpv_cur)
1234 - STRUCT_OFFSET(xpv_allocated, xpv_cur));
cbe51380 1235 LOCK_SV_MUTEX;
59813432
NC
1236 *(xpv_allocated**)xpv = PL_xpv_root;
1237 PL_xpv_root = xpv;
cbe51380 1238 UNLOCK_SV_MUTEX;
463ee0b2
LW
1239}
1240
645c22ef
DM
1241/* grab a new struct xpviv from the free list, allocating more if necessary */
1242
932e9ff9
VB
1243STATIC XPVIV*
1244S_new_xpviv(pTHX)
1245{
311a25d9 1246 xpviv_allocated* xpviv;
932e9ff9
VB
1247 LOCK_SV_MUTEX;
1248 if (!PL_xpviv_root)
e3bbdc67 1249 more_xpviv();
932e9ff9 1250 xpviv = PL_xpviv_root;
311a25d9 1251 PL_xpviv_root = *(xpviv_allocated**)xpviv;
932e9ff9 1252 UNLOCK_SV_MUTEX;
311a25d9
NC
1253 /* If xpviv_allocated is the same structure as XPVIV then the two OFFSETs
1254 sum to zero, and the pointer is unchanged. If the allocated structure
1255 is smaller (no initial IV actually allocated) then the net effect is
1256 to subtract the size of the IV from the pointer, to return a new pointer
1257 as if an initial IV were actually allocated. */
1258 return (XPVIV*)((char*)xpviv - STRUCT_OFFSET(XPVIV, xpv_cur)
1259 + STRUCT_OFFSET(xpviv_allocated, xpv_cur));
932e9ff9
VB
1260}
1261
645c22ef
DM
1262/* return a struct xpviv to the free list */
1263
932e9ff9
VB
1264STATIC void
1265S_del_xpviv(pTHX_ XPVIV *p)
1266{
311a25d9
NC
1267 xpviv_allocated* xpviv
1268 = (xpviv_allocated*)((char*)(p) + STRUCT_OFFSET(XPVIV, xpv_cur)
1269 - STRUCT_OFFSET(xpviv_allocated, xpv_cur));
932e9ff9 1270 LOCK_SV_MUTEX;
311a25d9
NC
1271 *(xpviv_allocated**)xpviv = PL_xpviv_root;
1272 PL_xpviv_root = xpviv;
932e9ff9
VB
1273 UNLOCK_SV_MUTEX;
1274}
1275
645c22ef
DM
1276/* grab a new struct xpvnv from the free list, allocating more if necessary */
1277
932e9ff9
VB
1278STATIC XPVNV*
1279S_new_xpvnv(pTHX)
1280{
1281 XPVNV* xpvnv;
1282 LOCK_SV_MUTEX;
1283 if (!PL_xpvnv_root)
e3bbdc67 1284 more_xpvnv();
932e9ff9 1285 xpvnv = PL_xpvnv_root;
7b2c381c 1286 PL_xpvnv_root = *(XPVNV**)xpvnv;
932e9ff9
VB
1287 UNLOCK_SV_MUTEX;
1288 return xpvnv;
1289}
1290
645c22ef
DM
1291/* return a struct xpvnv to the free list */
1292
932e9ff9
VB
1293STATIC void
1294S_del_xpvnv(pTHX_ XPVNV *p)
1295{
1296 LOCK_SV_MUTEX;
7b2c381c 1297 *(XPVNV**)p = PL_xpvnv_root;
932e9ff9
VB
1298 PL_xpvnv_root = p;
1299 UNLOCK_SV_MUTEX;
1300}
1301
645c22ef
DM
1302/* grab a new struct xpvcv from the free list, allocating more if necessary */
1303
932e9ff9
VB
1304STATIC XPVCV*
1305S_new_xpvcv(pTHX)
1306{
1307 XPVCV* xpvcv;
1308 LOCK_SV_MUTEX;
1309 if (!PL_xpvcv_root)
e3bbdc67 1310 more_xpvcv();
932e9ff9 1311 xpvcv = PL_xpvcv_root;
7b2c381c 1312 PL_xpvcv_root = *(XPVCV**)xpvcv;
932e9ff9
VB
1313 UNLOCK_SV_MUTEX;
1314 return xpvcv;
1315}
1316
645c22ef
DM
1317/* return a struct xpvcv to the free list */
1318
932e9ff9
VB
1319STATIC void
1320S_del_xpvcv(pTHX_ XPVCV *p)
1321{
1322 LOCK_SV_MUTEX;
7b2c381c 1323 *(XPVCV**)p = PL_xpvcv_root;
932e9ff9
VB
1324 PL_xpvcv_root = p;
1325 UNLOCK_SV_MUTEX;
1326}
1327
645c22ef
DM
1328/* grab a new struct xpvav from the free list, allocating more if necessary */
1329
932e9ff9
VB
1330STATIC XPVAV*
1331S_new_xpvav(pTHX)
1332{
59813432 1333 xpvav_allocated* xpvav;
932e9ff9
VB
1334 LOCK_SV_MUTEX;
1335 if (!PL_xpvav_root)
e3bbdc67 1336 more_xpvav();
932e9ff9 1337 xpvav = PL_xpvav_root;
59813432 1338 PL_xpvav_root = *(xpvav_allocated**)xpvav;
932e9ff9 1339 UNLOCK_SV_MUTEX;
59813432
NC
1340 return (XPVAV*)((char*)xpvav - STRUCT_OFFSET(XPVAV, xav_fill)
1341 + STRUCT_OFFSET(xpvav_allocated, xav_fill));
932e9ff9
VB
1342}
1343
645c22ef
DM
1344/* return a struct xpvav to the free list */
1345
932e9ff9
VB
1346STATIC void
1347S_del_xpvav(pTHX_ XPVAV *p)
1348{
59813432
NC
1349 xpvav_allocated* xpvav
1350 = (xpvav_allocated*)((char*)(p) + STRUCT_OFFSET(XPVAV, xav_fill)
1351 - STRUCT_OFFSET(xpvav_allocated, xav_fill));
932e9ff9 1352 LOCK_SV_MUTEX;
59813432
NC
1353 *(xpvav_allocated**)xpvav = PL_xpvav_root;
1354 PL_xpvav_root = xpvav;
932e9ff9
VB
1355 UNLOCK_SV_MUTEX;
1356}
1357
645c22ef
DM
1358/* grab a new struct xpvhv from the free list, allocating more if necessary */
1359
932e9ff9
VB
1360STATIC XPVHV*
1361S_new_xpvhv(pTHX)
1362{
59813432 1363 xpvhv_allocated* xpvhv;
932e9ff9
VB
1364 LOCK_SV_MUTEX;
1365 if (!PL_xpvhv_root)
e3bbdc67 1366 more_xpvhv();
932e9ff9 1367 xpvhv = PL_xpvhv_root;
59813432 1368 PL_xpvhv_root = *(xpvhv_allocated**)xpvhv;
932e9ff9 1369 UNLOCK_SV_MUTEX;
59813432
NC
1370 return (XPVHV*)((char*)xpvhv - STRUCT_OFFSET(XPVHV, xhv_fill)
1371 + STRUCT_OFFSET(xpvhv_allocated, xhv_fill));
932e9ff9
VB
1372}
1373
645c22ef
DM
1374/* return a struct xpvhv to the free list */
1375
932e9ff9
VB
1376STATIC void
1377S_del_xpvhv(pTHX_ XPVHV *p)
1378{
59813432
NC
1379 xpvhv_allocated* xpvhv
1380 = (xpvhv_allocated*)((char*)(p) + STRUCT_OFFSET(XPVHV, xhv_fill)
1381 - STRUCT_OFFSET(xpvhv_allocated, xhv_fill));
932e9ff9 1382 LOCK_SV_MUTEX;
59813432
NC
1383 *(xpvhv_allocated**)xpvhv = PL_xpvhv_root;
1384 PL_xpvhv_root = xpvhv;
932e9ff9
VB
1385 UNLOCK_SV_MUTEX;
1386}
1387
645c22ef
DM
1388/* grab a new struct xpvmg from the free list, allocating more if necessary */
1389
932e9ff9
VB
1390STATIC XPVMG*
1391S_new_xpvmg(pTHX)
1392{
1393 XPVMG* xpvmg;
1394 LOCK_SV_MUTEX;
1395 if (!PL_xpvmg_root)
e3bbdc67 1396 more_xpvmg();
932e9ff9 1397 xpvmg = PL_xpvmg_root;
7b2c381c 1398 PL_xpvmg_root = *(XPVMG**)xpvmg;
932e9ff9
VB
1399 UNLOCK_SV_MUTEX;
1400 return xpvmg;
1401}
1402
645c22ef
DM
1403/* return a struct xpvmg to the free list */
1404
932e9ff9
VB
1405STATIC void
1406S_del_xpvmg(pTHX_ XPVMG *p)
1407{
1408 LOCK_SV_MUTEX;
7b2c381c 1409 *(XPVMG**)p = PL_xpvmg_root;
932e9ff9
VB
1410 PL_xpvmg_root = p;
1411 UNLOCK_SV_MUTEX;
1412}
1413
727879eb
NC
1414/* grab a new struct xpvgv from the free list, allocating more if necessary */
1415
1416STATIC XPVGV*
1417S_new_xpvgv(pTHX)
1418{
1419 XPVGV* xpvgv;
1420 LOCK_SV_MUTEX;
1421 if (!PL_xpvgv_root)
e3bbdc67 1422 more_xpvgv();
727879eb 1423 xpvgv = PL_xpvgv_root;
7b2c381c 1424 PL_xpvgv_root = *(XPVGV**)xpvgv;
727879eb
NC
1425 UNLOCK_SV_MUTEX;
1426 return xpvgv;
1427}
1428
1429/* return a struct xpvgv to the free list */
1430
1431STATIC void
1432S_del_xpvgv(pTHX_ XPVGV *p)
1433{
1434 LOCK_SV_MUTEX;
7b2c381c 1435 *(XPVGV**)p = PL_xpvgv_root;
727879eb
NC
1436 PL_xpvgv_root = p;
1437 UNLOCK_SV_MUTEX;
1438}
1439
645c22ef
DM
1440/* grab a new struct xpvlv from the free list, allocating more if necessary */
1441
932e9ff9
VB
1442STATIC XPVLV*
1443S_new_xpvlv(pTHX)
1444{
1445 XPVLV* xpvlv;
1446 LOCK_SV_MUTEX;
1447 if (!PL_xpvlv_root)
e3bbdc67 1448 more_xpvlv();
932e9ff9 1449 xpvlv = PL_xpvlv_root;
7b2c381c 1450 PL_xpvlv_root = *(XPVLV**)xpvlv;
932e9ff9
VB
1451 UNLOCK_SV_MUTEX;
1452 return xpvlv;
1453}
1454
645c22ef
DM
1455/* return a struct xpvlv to the free list */
1456
932e9ff9
VB
1457STATIC void
1458S_del_xpvlv(pTHX_ XPVLV *p)
1459{
1460 LOCK_SV_MUTEX;
7b2c381c 1461 *(XPVLV**)p = PL_xpvlv_root;
932e9ff9
VB
1462 PL_xpvlv_root = p;
1463 UNLOCK_SV_MUTEX;
1464}
1465
645c22ef
DM
1466/* grab a new struct xpvbm from the free list, allocating more if necessary */
1467
932e9ff9
VB
1468STATIC XPVBM*
1469S_new_xpvbm(pTHX)
1470{
1471 XPVBM* xpvbm;
1472 LOCK_SV_MUTEX;
1473 if (!PL_xpvbm_root)
e3bbdc67 1474 more_xpvbm();
932e9ff9 1475 xpvbm = PL_xpvbm_root;
7b2c381c 1476 PL_xpvbm_root = *(XPVBM**)xpvbm;
932e9ff9
VB
1477 UNLOCK_SV_MUTEX;
1478 return xpvbm;
1479}
1480
645c22ef
DM
1481/* return a struct xpvbm to the free list */
1482
932e9ff9
VB
1483STATIC void
1484S_del_xpvbm(pTHX_ XPVBM *p)
1485{
1486 LOCK_SV_MUTEX;
7b2c381c 1487 *(XPVBM**)p = PL_xpvbm_root;
932e9ff9
VB
1488 PL_xpvbm_root = p;
1489 UNLOCK_SV_MUTEX;
1490}
1491
7bab3ede
MB
1492#define my_safemalloc(s) (void*)safemalloc(s)
1493#define my_safefree(p) safefree((char*)p)
463ee0b2 1494
d33b2eba 1495#ifdef PURIFY
463ee0b2 1496
d33b2eba
GS
1497#define new_XNV() my_safemalloc(sizeof(XPVNV))
1498#define del_XNV(p) my_safefree(p)
463ee0b2 1499
d33b2eba
GS
1500#define new_XPV() my_safemalloc(sizeof(XPV))
1501#define del_XPV(p) my_safefree(p)
9b94d1dd 1502
d33b2eba
GS
1503#define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1504#define del_XPVIV(p) my_safefree(p)
932e9ff9 1505
d33b2eba
GS
1506#define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1507#define del_XPVNV(p) my_safefree(p)
932e9ff9 1508
d33b2eba
GS
1509#define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1510#define del_XPVCV(p) my_safefree(p)
932e9ff9 1511
d33b2eba
GS
1512#define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1513#define del_XPVAV(p) my_safefree(p)
1514
1515#define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1516#define del_XPVHV(p) my_safefree(p)
1c846c1f 1517
d33b2eba
GS
1518#define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1519#define del_XPVMG(p) my_safefree(p)
1520
727879eb
NC
1521#define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1522#define del_XPVGV(p) my_safefree(p)
1523
d33b2eba
GS
1524#define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1525#define del_XPVLV(p) my_safefree(p)
1526
1527#define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1528#define del_XPVBM(p) my_safefree(p)
1529
1530#else /* !PURIFY */
1531
d33b2eba
GS
1532#define new_XNV() (void*)new_xnv()
1533#define del_XNV(p) del_xnv((XPVNV*) p)
9b94d1dd 1534
d33b2eba
GS
1535#define new_XPV() (void*)new_xpv()
1536#define del_XPV(p) del_xpv((XPV *)p)
1537
1538#define new_XPVIV() (void*)new_xpviv()
1539#define del_XPVIV(p) del_xpviv((XPVIV *)p)
1540
1541#define new_XPVNV() (void*)new_xpvnv()
1542#define del_XPVNV(p) del_xpvnv((XPVNV *)p)
1543
1544#define new_XPVCV() (void*)new_xpvcv()
1545#define del_XPVCV(p) del_xpvcv((XPVCV *)p)
1546
1547#define new_XPVAV() (void*)new_xpvav()
1548#define del_XPVAV(p) del_xpvav((XPVAV *)p)
1549
1550#define new_XPVHV() (void*)new_xpvhv()
1551#define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1c846c1f 1552
d33b2eba
GS
1553#define new_XPVMG() (void*)new_xpvmg()
1554#define del_XPVMG(p) del_xpvmg((XPVMG *)p)
1555
727879eb
NC
1556#define new_XPVGV() (void*)new_xpvgv()
1557#define del_XPVGV(p) del_xpvgv((XPVGV *)p)
1558
d33b2eba
GS
1559#define new_XPVLV() (void*)new_xpvlv()
1560#define del_XPVLV(p) del_xpvlv((XPVLV *)p)
1561
1562#define new_XPVBM() (void*)new_xpvbm()
1563#define del_XPVBM(p) del_xpvbm((XPVBM *)p)
1564
1565#endif /* PURIFY */
9b94d1dd 1566
d33b2eba
GS
1567#define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1568#define del_XPVFM(p) my_safefree(p)
1c846c1f 1569
d33b2eba
GS
1570#define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1571#define del_XPVIO(p) my_safefree(p)
8990e307 1572
954c1994
GS
1573/*
1574=for apidoc sv_upgrade
1575
ff276b08 1576Upgrade an SV to a more complex form. Generally adds a new body type to the
645c22ef 1577SV, then copies across as much information as possible from the old body.
ff276b08 1578You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
954c1994
GS
1579
1580=cut
1581*/
1582
63f97190 1583void
864dbfa3 1584Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
79072805 1585{
e763e3dc 1586
d2e56290
NC
1587 char* pv;
1588 U32 cur;
1589 U32 len;
1590 IV iv;
1591 NV nv;
1592 MAGIC* magic;
1593 HV* stash;
79072805 1594
765f542d
NC
1595 if (mt != SVt_PV && SvIsCOW(sv)) {
1596 sv_force_normal_flags(sv, 0);
f130fd45
NIS
1597 }
1598
79072805 1599 if (SvTYPE(sv) == mt)
63f97190 1600 return;
79072805 1601
d2e56290
NC
1602 pv = NULL;
1603 cur = 0;
1604 len = 0;
1605 iv = 0;
1606 nv = 0.0;
1607 magic = NULL;
1608 stash = Nullhv;
1609
79072805
LW
1610 switch (SvTYPE(sv)) {
1611 case SVt_NULL:
79072805 1612 break;
79072805 1613 case SVt_IV:
463ee0b2 1614 iv = SvIVX(sv);
ed6116ce 1615 if (mt == SVt_NV)
463ee0b2 1616 mt = SVt_PVNV;
ed6116ce
LW
1617 else if (mt < SVt_PVIV)
1618 mt = SVt_PVIV;
79072805
LW
1619 break;
1620 case SVt_NV:
463ee0b2 1621 nv = SvNVX(sv);
79072805 1622 del_XNV(SvANY(sv));
ed6116ce 1623 if (mt < SVt_PVNV)
79072805
LW
1624 mt = SVt_PVNV;
1625 break;
ed6116ce
LW
1626 case SVt_RV:
1627 pv = (char*)SvRV(sv);
ed6116ce 1628 break;
79072805 1629 case SVt_PV:
4d84ee25 1630 pv = SvPVX_mutable(sv);
79072805
LW
1631 cur = SvCUR(sv);
1632 len = SvLEN(sv);
79072805 1633 del_XPV(SvANY(sv));
748a9306
LW
1634 if (mt <= SVt_IV)
1635 mt = SVt_PVIV;
1636 else if (mt == SVt_NV)
1637 mt = SVt_PVNV;
79072805
LW
1638 break;
1639 case SVt_PVIV:
4d84ee25 1640 pv = SvPVX_mutable(sv);
79072805
LW
1641 cur = SvCUR(sv);
1642 len = SvLEN(sv);
463ee0b2 1643 iv = SvIVX(sv);
79072805
LW
1644 del_XPVIV(SvANY(sv));
1645 break;
1646 case SVt_PVNV:
4d84ee25 1647 pv = SvPVX_mutable(sv);
79072805
LW
1648 cur = SvCUR(sv);
1649 len = SvLEN(sv);
463ee0b2
LW
1650 iv = SvIVX(sv);
1651 nv = SvNVX(sv);
79072805
LW
1652 del_XPVNV(SvANY(sv));
1653 break;
1654 case SVt_PVMG:
0ec50a73
NC
1655 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1656 there's no way that it can be safely upgraded, because perl.c
1657 expects to Safefree(SvANY(PL_mess_sv)) */
1658 assert(sv != PL_mess_sv);
bce8f412
NC
1659 /* This flag bit is used to mean other things in other scalar types.
1660 Given that it only has meaning inside the pad, it shouldn't be set
1661 on anything that can get upgraded. */
1662 assert((SvFLAGS(sv) & SVpad_TYPED) == 0);
4d84ee25 1663 pv = SvPVX_mutable(sv);
79072805
LW
1664 cur = SvCUR(sv);
1665 len = SvLEN(sv);
463ee0b2
LW
1666 iv = SvIVX(sv);
1667 nv = SvNVX(sv);
79072805
LW
1668 magic = SvMAGIC(sv);
1669 stash = SvSTASH(sv);
1670 del_XPVMG(SvANY(sv));
1671 break;
1672 default:
cea2e8a9 1673 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
79072805
LW
1674 }
1675
ffb05e06
NC
1676 SvFLAGS(sv) &= ~SVTYPEMASK;
1677 SvFLAGS(sv) |= mt;
1678
79072805
LW
1679 switch (mt) {
1680 case SVt_NULL:
cea2e8a9 1681 Perl_croak(aTHX_ "Can't upgrade to undef");
79072805 1682 case SVt_IV:
339049b0 1683 SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
45977657 1684 SvIV_set(sv, iv);
79072805
LW
1685 break;
1686 case SVt_NV:
1687 SvANY(sv) = new_XNV();
9d6ce603 1688 SvNV_set(sv, nv);
79072805 1689 break;
ed6116ce 1690 case SVt_RV:
339049b0 1691 SvANY(sv) = &sv->sv_u.svu_rv;
b162af07 1692 SvRV_set(sv, (SV*)pv);
ed6116ce 1693 break;
79072805
LW
1694 case SVt_PVHV:
1695 SvANY(sv) = new_XPVHV();
463ee0b2
LW
1696 HvFILL(sv) = 0;
1697 HvMAX(sv) = 0;
8aacddc1 1698 HvTOTALKEYS(sv) = 0;
bd4b1eb5
NC
1699
1700 /* Fall through... */
1701 if (0) {
1702 case SVt_PVAV:
1703 SvANY(sv) = new_XPVAV();
1704 AvMAX(sv) = -1;
1705 AvFILLp(sv) = -1;
1706 AvALLOC(sv) = 0;
11ca45c0 1707 AvREAL_only(sv);
bd4b1eb5
NC
1708 }
1709 /* to here. */
c2bfdfaf
NC
1710 /* XXX? Only SVt_NULL is ever upgraded to AV or HV? */
1711 assert(!pv);
8bd4d4c5
NC
1712 /* FIXME. Should be able to remove all this if()... if the above
1713 assertion is genuinely always true. */
1714 if(SvOOK(sv)) {
1715 pv -= iv;
1716 SvFLAGS(sv) &= ~SVf_OOK;
1717 }
1718 Safefree(pv);
bd4b1eb5 1719 SvPV_set(sv, (char*)0);
b162af07
SP
1720 SvMAGIC_set(sv, magic);
1721 SvSTASH_set(sv, stash);
79072805 1722 break;
bd4b1eb5
NC
1723
1724 case SVt_PVIO:
1725 SvANY(sv) = new_XPVIO();
1726 Zero(SvANY(sv), 1, XPVIO);
1727 IoPAGE_LEN(sv) = 60;
1728 goto set_magic_common;
1729 case SVt_PVFM:
1730 SvANY(sv) = new_XPVFM();
1731 Zero(SvANY(sv), 1, XPVFM);
1732 goto set_magic_common;
1733 case SVt_PVBM:
1734 SvANY(sv) = new_XPVBM();
1735 BmRARE(sv) = 0;
1736 BmUSEFUL(sv) = 0;
1737 BmPREVIOUS(sv) = 0;
1738 goto set_magic_common;
1739 case SVt_PVGV:
1740 SvANY(sv) = new_XPVGV();
1741 GvGP(sv) = 0;
1742 GvNAME(sv) = 0;
1743 GvNAMELEN(sv) = 0;
1744 GvSTASH(sv) = 0;
1745 GvFLAGS(sv) = 0;
1746 goto set_magic_common;
79072805
LW
1747 case SVt_PVCV:
1748 SvANY(sv) = new_XPVCV();
748a9306 1749 Zero(SvANY(sv), 1, XPVCV);
bd4b1eb5
NC
1750 goto set_magic_common;
1751 case SVt_PVLV:
1752 SvANY(sv) = new_XPVLV();
1753 LvTARGOFF(sv) = 0;
1754 LvTARGLEN(sv) = 0;
1755 LvTARG(sv) = 0;
1756 LvTYPE(sv) = 0;
93a17b20 1757 GvGP(sv) = 0;
79072805
LW
1758 GvNAME(sv) = 0;
1759 GvNAMELEN(sv) = 0;
1760 GvSTASH(sv) = 0;
a5f75d66 1761 GvFLAGS(sv) = 0;
bd4b1eb5
NC
1762 /* Fall through. */
1763 if (0) {
1764 case SVt_PVMG:
1765 SvANY(sv) = new_XPVMG();
1766 }
1767 set_magic_common:
b162af07
SP
1768 SvMAGIC_set(sv, magic);
1769 SvSTASH_set(sv, stash);
bd4b1eb5
NC
1770 /* Fall through. */
1771 if (0) {
1772 case SVt_PVNV:
1773 SvANY(sv) = new_XPVNV();
1774 }
9d6ce603 1775 SvNV_set(sv, nv);
bd4b1eb5
NC
1776 /* Fall through. */
1777 if (0) {
1778 case SVt_PVIV:
1779 SvANY(sv) = new_XPVIV();
1780 if (SvNIOK(sv))
1781 (void)SvIOK_on(sv);
1782 SvNOK_off(sv);
1783 }
1784 SvIV_set(sv, iv);
1785 /* Fall through. */
1786 if (0) {
1787 case SVt_PV:
1788 SvANY(sv) = new_XPV();
1789 }
f880fe2f 1790 SvPV_set(sv, pv);
b162af07
SP
1791 SvCUR_set(sv, cur);
1792 SvLEN_set(sv, len);
8990e307
LW
1793 break;
1794 }
79072805
LW
1795}
1796
645c22ef
DM
1797/*
1798=for apidoc sv_backoff
1799
1800Remove any string offset. You should normally use the C<SvOOK_off> macro
1801wrapper instead.
1802
1803=cut
1804*/
1805
79072805 1806int
864dbfa3 1807Perl_sv_backoff(pTHX_ register SV *sv)
79072805
LW
1808{
1809 assert(SvOOK(sv));
b79f7545
NC
1810 assert(SvTYPE(sv) != SVt_PVHV);
1811 assert(SvTYPE(sv) != SVt_PVAV);
463ee0b2 1812 if (SvIVX(sv)) {
3f7c398e 1813 const char *s = SvPVX_const(sv);
b162af07 1814 SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
f880fe2f 1815 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
79072805 1816 SvIV_set(sv, 0);
463ee0b2 1817 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
1818 }
1819 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 1820 return 0;
79072805
LW
1821}
1822
954c1994
GS
1823/*
1824=for apidoc sv_grow
1825
645c22ef
DM
1826Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1827upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1828Use the C<SvGROW> wrapper instead.
954c1994
GS
1829
1830=cut
1831*/
1832
79072805 1833char *
864dbfa3 1834Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
79072805
LW
1835{
1836 register char *s;
1837
55497cff 1838#ifdef HAS_64K_LIMIT
79072805 1839 if (newlen >= 0x10000) {
1d7c1841
GS
1840 PerlIO_printf(Perl_debug_log,
1841 "Allocation too large: %"UVxf"\n", (UV)newlen);
79072805
LW
1842 my_exit(1);
1843 }
55497cff 1844#endif /* HAS_64K_LIMIT */
a0d0e21e
LW
1845 if (SvROK(sv))
1846 sv_unref(sv);
79072805
LW
1847 if (SvTYPE(sv) < SVt_PV) {
1848 sv_upgrade(sv, SVt_PV);
93524f2b 1849 s = SvPVX_mutable(sv);
79072805
LW
1850 }
1851 else if (SvOOK(sv)) { /* pv is offset? */
1852 sv_backoff(sv);
93524f2b 1853 s = SvPVX_mutable(sv);
79072805
LW
1854 if (newlen > SvLEN(sv))
1855 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
c6f8c383
GA
1856#ifdef HAS_64K_LIMIT
1857 if (newlen >= 0x10000)
1858 newlen = 0xFFFF;
1859#endif
79072805 1860 }
bc44a8a2 1861 else
4d84ee25 1862 s = SvPVX_mutable(sv);
54f0641b 1863
79072805 1864 if (newlen > SvLEN(sv)) { /* need more room? */
7a9b70e9 1865 newlen = PERL_STRLEN_ROUNDUP(newlen);
8d6dde3e 1866 if (SvLEN(sv) && s) {
7bab3ede 1867#ifdef MYMALLOC
93524f2b 1868 const STRLEN l = malloced_size((void*)SvPVX_const(sv));
8d6dde3e
IZ
1869 if (newlen <= l) {
1870 SvLEN_set(sv, l);
1871 return s;
1872 } else
c70c8a0a 1873#endif
1936d2a7 1874 s = saferealloc(s, newlen);
8d6dde3e 1875 }
bfed75c6 1876 else {
1936d2a7 1877 s = safemalloc(newlen);
3f7c398e
SP
1878 if (SvPVX_const(sv) && SvCUR(sv)) {
1879 Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
40565179 1880 }
4e83176d 1881 }
79072805 1882 SvPV_set(sv, s);
e1ec3a88 1883 SvLEN_set(sv, newlen);
79072805
LW
1884 }
1885 return s;
1886}
1887
954c1994
GS
1888/*
1889=for apidoc sv_setiv
1890
645c22ef
DM
1891Copies an integer into the given SV, upgrading first if necessary.
1892Does not handle 'set' magic. See also C<sv_setiv_mg>.
954c1994
GS
1893
1894=cut
1895*/
1896
79072805 1897void
864dbfa3 1898Perl_sv_setiv(pTHX_ register SV *sv, IV i)
79072805 1899{
765f542d 1900 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2
LW
1901 switch (SvTYPE(sv)) {
1902 case SVt_NULL:
79072805 1903 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
1904 break;
1905 case SVt_NV:
1906 sv_upgrade(sv, SVt_PVNV);
1907 break;
ed6116ce 1908 case SVt_RV:
463ee0b2 1909 case SVt_PV:
79072805 1910 sv_upgrade(sv, SVt_PVIV);
463ee0b2 1911 break;
a0d0e21e
LW
1912
1913 case SVt_PVGV:
a0d0e21e
LW
1914 case SVt_PVAV:
1915 case SVt_PVHV:
1916 case SVt_PVCV:
1917 case SVt_PVFM:
1918 case SVt_PVIO:
411caa50 1919 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
53e06cf0 1920 OP_DESC(PL_op));
463ee0b2 1921 }
a0d0e21e 1922 (void)SvIOK_only(sv); /* validate number */
45977657 1923 SvIV_set(sv, i);
463ee0b2 1924 SvTAINT(sv);
79072805
LW
1925}
1926
954c1994
GS
1927/*
1928=for apidoc sv_setiv_mg
1929
1930Like C<sv_setiv>, but also handles 'set' magic.
1931
1932=cut
1933*/
1934
79072805 1935void
864dbfa3 1936Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
ef50df4b
GS
1937{
1938 sv_setiv(sv,i);
1939 SvSETMAGIC(sv);
1940}
1941
954c1994
GS
1942/*
1943=for apidoc sv_setuv
1944
645c22ef
DM
1945Copies an unsigned integer into the given SV, upgrading first if necessary.
1946Does not handle 'set' magic. See also C<sv_setuv_mg>.
954c1994
GS
1947
1948=cut
1949*/
1950
ef50df4b 1951void
864dbfa3 1952Perl_sv_setuv(pTHX_ register SV *sv, UV u)
55497cff 1953{
55ada374
NC
1954 /* With these two if statements:
1955 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 1956
55ada374
NC
1957 without
1958 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 1959
55ada374
NC
1960 If you wish to remove them, please benchmark to see what the effect is
1961 */
28e5dec8
JH
1962 if (u <= (UV)IV_MAX) {
1963 sv_setiv(sv, (IV)u);
1964 return;
1965 }
25da4f38
IZ
1966 sv_setiv(sv, 0);
1967 SvIsUV_on(sv);
607fa7f2 1968 SvUV_set(sv, u);
55497cff
PP
1969}
1970
954c1994
GS
1971/*
1972=for apidoc sv_setuv_mg
1973
1974Like C<sv_setuv>, but also handles 'set' magic.
1975
1976=cut
1977*/
1978
55497cff 1979void
864dbfa3 1980Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
ef50df4b 1981{
55ada374
NC
1982 /* With these two if statements:
1983 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 1984
55ada374
NC
1985 without
1986 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 1987
55ada374
NC
1988 If you wish to remove them, please benchmark to see what the effect is
1989 */
28e5dec8
JH
1990 if (u <= (UV)IV_MAX) {
1991 sv_setiv(sv, (IV)u);
1992 } else {
1993 sv_setiv(sv, 0);
1994 SvIsUV_on(sv);
1995 sv_setuv(sv,u);
1996 }
ef50df4b
GS
1997 SvSETMAGIC(sv);
1998}
1999
954c1994
GS
2000/*
2001=for apidoc sv_setnv
2002
645c22ef
DM
2003Copies a double into the given SV, upgrading first if necessary.
2004Does not handle 'set' magic. See also C<sv_setnv_mg>.
954c1994
GS
2005
2006=cut
2007*/
2008
ef50df4b 2009void
65202027 2010Perl_sv_setnv(pTHX_ register SV *sv, NV num)
79072805 2011{
765f542d 2012 SV_CHECK_THINKFIRST_COW_DROP(sv);
a0d0e21e
LW
2013 switch (SvTYPE(sv)) {
2014 case SVt_NULL:
2015 case SVt_IV:
79072805 2016 sv_upgrade(sv, SVt_NV);
a0d0e21e 2017 break;
a0d0e21e
LW
2018 case SVt_RV:
2019 case SVt_PV:
2020 case SVt_PVIV:
79072805 2021 sv_upgrade(sv, SVt_PVNV);
a0d0e21e 2022 break;
827b7e14 2023
a0d0e21e 2024 case SVt_PVGV:
a0d0e21e
LW
2025 case SVt_PVAV:
2026 case SVt_PVHV:
2027 case SVt_PVCV:
2028 case SVt_PVFM:
2029 case SVt_PVIO:
411caa50 2030 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
53e06cf0 2031 OP_NAME(PL_op));
79072805 2032 }
9d6ce603 2033 SvNV_set(sv, num);
a0d0e21e 2034 (void)SvNOK_only(sv); /* validate number */
463ee0b2 2035 SvTAINT(sv);
79072805
LW
2036}
2037
954c1994
GS
2038/*
2039=for apidoc sv_setnv_mg
2040
2041Like C<sv_setnv>, but also handles 'set' magic.
2042
2043=cut
2044*/
2045
ef50df4b 2046void
65202027 2047Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
ef50df4b
GS
2048{
2049 sv_setnv(sv,num);
2050 SvSETMAGIC(sv);
2051}
2052
645c22ef
DM
2053/* Print an "isn't numeric" warning, using a cleaned-up,
2054 * printable version of the offending string
2055 */
2056
76e3520e 2057STATIC void
cea2e8a9 2058S_not_a_number(pTHX_ SV *sv)
a0d0e21e 2059{
94463019
JH
2060 SV *dsv;
2061 char tmpbuf[64];
2062 char *pv;
2063
2064 if (DO_UTF8(sv)) {
2065 dsv = sv_2mortal(newSVpv("", 0));
2066 pv = sv_uni_display(dsv, sv, 10, 0);
2067 } else {
2068 char *d = tmpbuf;
2069 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
2070 /* each *s can expand to 4 chars + "...\0",
2071 i.e. need room for 8 chars */
ecdeb87c 2072
e62f0680
NC
2073 const char *s, *end;
2074 for (s = SvPVX_const(sv), end = s + SvCUR(sv); s < end && d < limit;
2075 s++) {
94463019
JH
2076 int ch = *s & 0xFF;
2077 if (ch & 128 && !isPRINT_LC(ch)) {
2078 *d++ = 'M';
2079 *d++ = '-';
2080 ch &= 127;
2081 }
2082 if (ch == '\n') {
2083 *d++ = '\\';
2084 *d++ = 'n';
2085 }
2086 else if (ch == '\r') {
2087 *d++ = '\\';
2088 *d++ = 'r';
2089 }
2090 else if (ch == '\f') {
2091 *d++ = '\\';
2092 *d++ = 'f';
2093 }
2094 else if (ch == '\\') {
2095 *d++ = '\\';
2096 *d++ = '\\';
2097 }
2098 else if (ch == '\0') {
2099 *d++ = '\\';
2100 *d++ = '0';
2101 }
2102 else if (isPRINT_LC(ch))
2103 *d++ = ch;
2104 else {
2105 *d++ = '^';
2106 *d++ = toCTRL(ch);
2107 }
2108 }
2109 if (s < end) {
2110 *d++ = '.';
2111 *d++ = '.';
2112 *d++ = '.';
2113 }
2114 *d = '\0';
2115 pv = tmpbuf;
a0d0e21e 2116 }
a0d0e21e 2117
533c011a 2118 if (PL_op)
9014280d 2119 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019
JH
2120 "Argument \"%s\" isn't numeric in %s", pv,
2121 OP_DESC(PL_op));
a0d0e21e 2122 else
9014280d 2123 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019 2124 "Argument \"%s\" isn't numeric", pv);
a0d0e21e
LW
2125}
2126
c2988b20
NC
2127/*
2128=for apidoc looks_like_number
2129
645c22ef
DM
2130Test if the content of an SV looks like a number (or is a number).
2131C<Inf> and C<Infinity> are treated as numbers (so will not issue a
2132non-numeric warning), even if your atof() doesn't grok them.
c2988b20
NC
2133
2134=cut
2135*/
2136
2137I32
2138Perl_looks_like_number(pTHX_ SV *sv)
2139{
a3b680e6 2140 register const char *sbegin;
c2988b20
NC
2141 STRLEN len;
2142
2143 if (SvPOK(sv)) {
3f7c398e 2144 sbegin = SvPVX_const(sv);
c2988b20
NC
2145 len = SvCUR(sv);
2146 }
2147 else if (SvPOKp(sv))
83003860 2148 sbegin = SvPV_const(sv, len);
c2988b20 2149 else
e0ab1c0e 2150 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
c2988b20
NC
2151 return grok_number(sbegin, len, NULL);
2152}
25da4f38
IZ
2153
2154/* Actually, ISO C leaves conversion of UV to IV undefined, but
2155 until proven guilty, assume that things are not that bad... */
2156
645c22ef
DM
2157/*
2158 NV_PRESERVES_UV:
2159
2160 As 64 bit platforms often have an NV that doesn't preserve all bits of
28e5dec8
JH
2161 an IV (an assumption perl has been based on to date) it becomes necessary
2162 to remove the assumption that the NV always carries enough precision to
2163 recreate the IV whenever needed, and that the NV is the canonical form.
2164 Instead, IV/UV and NV need to be given equal rights. So as to not lose
645c22ef 2165 precision as a side effect of conversion (which would lead to insanity
28e5dec8
JH
2166 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
2167 1) to distinguish between IV/UV/NV slots that have cached a valid
2168 conversion where precision was lost and IV/UV/NV slots that have a
2169 valid conversion which has lost no precision
645c22ef 2170 2) to ensure that if a numeric conversion to one form is requested that
28e5dec8
JH
2171 would lose precision, the precise conversion (or differently
2172 imprecise conversion) is also performed and cached, to prevent
2173 requests for different numeric formats on the same SV causing
2174 lossy conversion chains. (lossless conversion chains are perfectly
2175 acceptable (still))
2176
2177
2178 flags are used:
2179 SvIOKp is true if the IV slot contains a valid value
2180 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
2181 SvNOKp is true if the NV slot contains a valid value
2182 SvNOK is true only if the NV value is accurate
2183
2184 so
645c22ef 2185 while converting from PV to NV, check to see if converting that NV to an
28e5dec8
JH
2186 IV(or UV) would lose accuracy over a direct conversion from PV to
2187 IV(or UV). If it would, cache both conversions, return NV, but mark
2188 SV as IOK NOKp (ie not NOK).
2189
645c22ef 2190 While converting from PV to IV, check to see if converting that IV to an
28e5dec8
JH
2191 NV would lose accuracy over a direct conversion from PV to NV. If it
2192 would, cache both conversions, flag similarly.
2193
2194 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
2195 correctly because if IV & NV were set NV *always* overruled.
645c22ef
DM
2196 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
2197 changes - now IV and NV together means that the two are interchangeable:
28e5dec8 2198 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
d460ef45 2199
645c22ef
DM
2200 The benefit of this is that operations such as pp_add know that if
2201 SvIOK is true for both left and right operands, then integer addition
2202 can be used instead of floating point (for cases where the result won't
2203 overflow). Before, floating point was always used, which could lead to
28e5dec8
JH
2204 loss of precision compared with integer addition.
2205
2206 * making IV and NV equal status should make maths accurate on 64 bit
2207 platforms
2208 * may speed up maths somewhat if pp_add and friends start to use
645c22ef 2209 integers when possible instead of fp. (Hopefully the overhead in
28e5dec8
JH
2210 looking for SvIOK and checking for overflow will not outweigh the
2211 fp to integer speedup)
2212 * will slow down integer operations (callers of SvIV) on "inaccurate"
2213 values, as the change from SvIOK to SvIOKp will cause a call into
2214 sv_2iv each time rather than a macro access direct to the IV slot
2215 * should speed up number->string conversion on integers as IV is
645c22ef 2216 favoured when IV and NV are equally accurate
28e5dec8
JH
2217
2218 ####################################################################
645c22ef
DM
2219 You had better be using SvIOK_notUV if you want an IV for arithmetic:
2220 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
2221 On the other hand, SvUOK is true iff UV.
28e5dec8
JH
2222 ####################################################################
2223
645c22ef 2224 Your mileage will vary depending your CPU's relative fp to integer
28e5dec8
JH
2225 performance ratio.
2226*/
2227
2228#ifndef NV_PRESERVES_UV
645c22ef
DM
2229# define IS_NUMBER_UNDERFLOW_IV 1
2230# define IS_NUMBER_UNDERFLOW_UV 2
2231# define IS_NUMBER_IV_AND_UV 2
2232# define IS_NUMBER_OVERFLOW_IV 4
2233# define IS_NUMBER_OVERFLOW_UV 5
2234
2235/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
28e5dec8
JH
2236
2237/* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2238STATIC int
645c22ef 2239S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
28e5dec8 2240{
3f7c398e 2241 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
2242 if (SvNVX(sv) < (NV)IV_MIN) {
2243 (void)SvIOKp_on(sv);
2244 (void)SvNOK_on(sv);
45977657 2245 SvIV_set(sv, IV_MIN);
28e5dec8
JH
2246 return IS_NUMBER_UNDERFLOW_IV;
2247 }
2248 if (SvNVX(sv) > (NV)UV_MAX) {
2249 (void)SvIOKp_on(sv);
2250 (void)SvNOK_on(sv);
2251 SvIsUV_on(sv);
607fa7f2 2252 SvUV_set(sv, UV_MAX);
28e5dec8
JH
2253 return IS_NUMBER_OVERFLOW_UV;
2254 }
c2988b20
NC
2255 (void)SvIOKp_on(sv);
2256 (void)SvNOK_on(sv);
2257 /* Can't use strtol etc to convert this string. (See truth table in
2258 sv_2iv */
2259 if (SvNVX(sv) <= (UV)IV_MAX) {
45977657 2260 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2261 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2262 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2263 } else {
2264 /* Integer is imprecise. NOK, IOKp */
2265 }
2266 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2267 }
2268 SvIsUV_on(sv);
607fa7f2 2269 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2270 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2271 if (SvUVX(sv) == UV_MAX) {
2272 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2273 possibly be preserved by NV. Hence, it must be overflow.
2274 NOK, IOKp */
2275 return IS_NUMBER_OVERFLOW_UV;
2276 }
2277 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2278 } else {
2279 /* Integer is imprecise. NOK, IOKp */
28e5dec8 2280 }
c2988b20 2281 return IS_NUMBER_OVERFLOW_IV;
28e5dec8 2282}
645c22ef
DM
2283#endif /* !NV_PRESERVES_UV*/
2284
891f9566
YST
2285/* sv_2iv() is now a macro using Perl_sv_2iv_flags();
2286 * this function provided for binary compatibility only
2287 */
2288
2289IV
2290Perl_sv_2iv(pTHX_ register SV *sv)
2291{
2292 return sv_2iv_flags(sv, SV_GMAGIC);
2293}
2294
645c22ef 2295/*
891f9566 2296=for apidoc sv_2iv_flags
645c22ef 2297
891f9566
YST
2298Return the integer value of an SV, doing any necessary string
2299conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2300Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
645c22ef
DM
2301
2302=cut
2303*/
28e5dec8 2304
a0d0e21e 2305IV
891f9566 2306Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags)
79072805
LW
2307{
2308 if (!sv)
2309 return 0;
8990e307 2310 if (SvGMAGICAL(sv)) {
891f9566
YST
2311 if (flags & SV_GMAGIC)
2312 mg_get(sv);
463ee0b2
LW
2313 if (SvIOKp(sv))
2314 return SvIVX(sv);
748a9306 2315 if (SvNOKp(sv)) {
25da4f38 2316 return I_V(SvNVX(sv));
748a9306 2317 }
36477c24
PP
2318 if (SvPOKp(sv) && SvLEN(sv))
2319 return asIV(sv);
3fe9a6f1 2320 if (!SvROK(sv)) {
d008e5eb 2321 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2322 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2323 report_uninit(sv);
c6ee37c5 2324 }
36477c24 2325 return 0;
3fe9a6f1 2326 }
463ee0b2 2327 }
ed6116ce 2328 if (SvTHINKFIRST(sv)) {
a0d0e21e 2329 if (SvROK(sv)) {
a0d0e21e 2330 SV* tmpstr;
1554e226 2331 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2332 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2333 return SvIV(tmpstr);
56431972 2334 return PTR2IV(SvRV(sv));
a0d0e21e 2335 }
765f542d
NC
2336 if (SvIsCOW(sv)) {
2337 sv_force_normal_flags(sv, 0);
47deb5e7 2338 }
0336b60e 2339 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2340 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2341 report_uninit(sv);
ed6116ce
LW
2342 return 0;
2343 }
79072805 2344 }
25da4f38
IZ
2345 if (SvIOKp(sv)) {
2346 if (SvIsUV(sv)) {
2347 return (IV)(SvUVX(sv));
2348 }
2349 else {
2350 return SvIVX(sv);
2351 }
463ee0b2 2352 }
748a9306 2353 if (SvNOKp(sv)) {
28e5dec8
JH
2354 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2355 * without also getting a cached IV/UV from it at the same time
2356 * (ie PV->NV conversion should detect loss of accuracy and cache
2357 * IV or UV at same time to avoid this. NWC */
25da4f38
IZ
2358
2359 if (SvTYPE(sv) == SVt_NV)
2360 sv_upgrade(sv, SVt_PVNV);
2361
28e5dec8
JH
2362 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2363 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2364 certainly cast into the IV range at IV_MAX, whereas the correct
2365 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2366 cases go to UV */
2367 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2368 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2369 if (SvNVX(sv) == (NV) SvIVX(sv)
2370#ifndef NV_PRESERVES_UV
2371 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2372 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2373 /* Don't flag it as "accurately an integer" if the number
2374 came from a (by definition imprecise) NV operation, and
2375 we're outside the range of NV integer precision */
2376#endif
2377 ) {
2378 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2379 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2380 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2381 PTR2UV(sv),
2382 SvNVX(sv),
2383 SvIVX(sv)));
2384
2385 } else {
2386 /* IV not precise. No need to convert from PV, as NV
2387 conversion would already have cached IV if it detected
2388 that PV->IV would be better than PV->NV->IV
2389 flags already correct - don't set public IOK. */
2390 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2391 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2392 PTR2UV(sv),
2393 SvNVX(sv),
2394 SvIVX(sv)));
2395 }
2396 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2397 but the cast (NV)IV_MIN rounds to a the value less (more
2398 negative) than IV_MIN which happens to be equal to SvNVX ??
2399 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2400 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2401 (NV)UVX == NVX are both true, but the values differ. :-(
2402 Hopefully for 2s complement IV_MIN is something like
2403 0x8000000000000000 which will be exact. NWC */
d460ef45 2404 }
25da4f38 2405 else {
607fa7f2 2406 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2407 if (
2408 (SvNVX(sv) == (NV) SvUVX(sv))
2409#ifndef NV_PRESERVES_UV
2410 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2411 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2412 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2413 /* Don't flag it as "accurately an integer" if the number
2414 came from a (by definition imprecise) NV operation, and
2415 we're outside the range of NV integer precision */
2416#endif
2417 )
2418 SvIOK_on(sv);
25da4f38
IZ
2419 SvIsUV_on(sv);
2420 ret_iv_max:
1c846c1f 2421 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 2422 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 2423 PTR2UV(sv),
57def98f
JH
2424 SvUVX(sv),
2425 SvUVX(sv)));
25da4f38
IZ
2426 return (IV)SvUVX(sv);
2427 }
748a9306
LW
2428 }
2429 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2430 UV value;
504618e9 2431 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
25da4f38
IZ
2432 /* We want to avoid a possible problem when we cache an IV which
2433 may be later translated to an NV, and the resulting NV is not
c2988b20
NC
2434 the same as the direct translation of the initial string
2435 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2436 be careful to ensure that the value with the .456 is around if the
2437 NV value is requested in the future).
1c846c1f 2438
25da4f38
IZ
2439 This means that if we cache such an IV, we need to cache the
2440 NV as well. Moreover, we trade speed for space, and do not
28e5dec8 2441 cache the NV if we are sure it's not needed.
25da4f38 2442 */
16b7a9a4 2443
c2988b20
NC
2444 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2445 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2446 == IS_NUMBER_IN_UV) {
5e045b90 2447 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8
JH
2448 if (SvTYPE(sv) < SVt_PVIV)
2449 sv_upgrade(sv, SVt_PVIV);
f7bbb42a 2450 (void)SvIOK_on(sv);
c2988b20
NC
2451 } else if (SvTYPE(sv) < SVt_PVNV)
2452 sv_upgrade(sv, SVt_PVNV);
28e5dec8 2453
c2988b20
NC
2454 /* If NV preserves UV then we only use the UV value if we know that
2455 we aren't going to call atof() below. If NVs don't preserve UVs
2456 then the value returned may have more precision than atof() will
2457 return, even though value isn't perfectly accurate. */
2458 if ((numtype & (IS_NUMBER_IN_UV
2459#ifdef NV_PRESERVES_UV
2460 | IS_NUMBER_NOT_INT
2461#endif
2462 )) == IS_NUMBER_IN_UV) {
2463 /* This won't turn off the public IOK flag if it was set above */
2464 (void)SvIOKp_on(sv);
2465
2466 if (!(numtype & IS_NUMBER_NEG)) {
2467 /* positive */;
2468 if (value <= (UV)IV_MAX) {
45977657 2469 SvIV_set(sv, (IV)value);
c2988b20 2470 } else {
607fa7f2 2471 SvUV_set(sv, value);
c2988b20
NC
2472 SvIsUV_on(sv);
2473 }
2474 } else {
2475 /* 2s complement assumption */
2476 if (value <= (UV)IV_MIN) {
45977657 2477 SvIV_set(sv, -(IV)value);
c2988b20
NC
2478 } else {
2479 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2480 I'm assuming it will be rare. */
c2988b20
NC
2481 if (SvTYPE(sv) < SVt_PVNV)
2482 sv_upgrade(sv, SVt_PVNV);
2483 SvNOK_on(sv);
2484 SvIOK_off(sv);
2485 SvIOKp_on(sv);
9d6ce603 2486 SvNV_set(sv, -(NV)value);
45977657 2487 SvIV_set(sv, IV_MIN);
c2988b20
NC
2488 }
2489 }
2490 }
2491 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2492 will be in the previous block to set the IV slot, and the next
2493 block to set the NV slot. So no else here. */
2494
2495 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2496 != IS_NUMBER_IN_UV) {
2497 /* It wasn't an (integer that doesn't overflow the UV). */
3f7c398e 2498 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8 2499
c2988b20
NC
2500 if (! numtype && ckWARN(WARN_NUMERIC))
2501 not_a_number(sv);
28e5dec8 2502
65202027 2503#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2504 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2505 PTR2UV(sv), SvNVX(sv)));
65202027 2506#else
1779d84d 2507 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
c2988b20 2508 PTR2UV(sv), SvNVX(sv)));
65202027 2509#endif
28e5dec8
JH
2510
2511
2512#ifdef NV_PRESERVES_UV
c2988b20
NC
2513 (void)SvIOKp_on(sv);
2514 (void)SvNOK_on(sv);
2515 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2516 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2517 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2518 SvIOK_on(sv);
28e5dec8 2519 } else {
c2988b20
NC
2520 /* Integer is imprecise. NOK, IOKp */
2521 }
2522 /* UV will not work better than IV */
2523 } else {
2524 if (SvNVX(sv) > (NV)UV_MAX) {
2525 SvIsUV_on(sv);
2526 /* Integer is inaccurate. NOK, IOKp, is UV */
607fa7f2 2527 SvUV_set(sv, UV_MAX);
c2988b20
NC
2528 SvIsUV_on(sv);
2529 } else {
607fa7f2 2530 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2531 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2532 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2533 SvIOK_on(sv);
28e5dec8
JH
2534 SvIsUV_on(sv);
2535 } else {
c2988b20
NC
2536 /* Integer is imprecise. NOK, IOKp, is UV */
2537 SvIsUV_on(sv);
28e5dec8 2538 }
28e5dec8 2539 }
c2988b20
NC
2540 goto ret_iv_max;
2541 }
28e5dec8 2542#else /* NV_PRESERVES_UV */
c2988b20
NC
2543 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2544 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2545 /* The IV slot will have been set from value returned by
2546 grok_number above. The NV slot has just been set using
2547 Atof. */
560b0c46 2548 SvNOK_on(sv);
c2988b20
NC
2549 assert (SvIOKp(sv));
2550 } else {
2551 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2552 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2553 /* Small enough to preserve all bits. */
2554 (void)SvIOKp_on(sv);
2555 SvNOK_on(sv);
45977657 2556 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2557 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2558 SvIOK_on(sv);
2559 /* Assumption: first non-preserved integer is < IV_MAX,
2560 this NV is in the preserved range, therefore: */
2561 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2562 < (UV)IV_MAX)) {
32fdb065 2563 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
2564 }
2565 } else {
2566 /* IN_UV NOT_INT
2567 0 0 already failed to read UV.
2568 0 1 already failed to read UV.
2569 1 0 you won't get here in this case. IV/UV
2570 slot set, public IOK, Atof() unneeded.
2571 1 1 already read UV.
2572 so there's no point in sv_2iuv_non_preserve() attempting
2573 to use atol, strtol, strtoul etc. */
2574 if (sv_2iuv_non_preserve (sv, numtype)
2575 >= IS_NUMBER_OVERFLOW_IV)
2576 goto ret_iv_max;
2577 }
2578 }
28e5dec8 2579#endif /* NV_PRESERVES_UV */
25da4f38 2580 }
28e5dec8 2581 } else {
599cee73 2582 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 2583 report_uninit(sv);
25da4f38
IZ
2584 if (SvTYPE(sv) < SVt_IV)
2585 /* Typically the caller expects that sv_any is not NULL now. */
2586 sv_upgrade(sv, SVt_IV);
a0d0e21e 2587 return 0;
79072805 2588 }
1d7c1841
GS
2589 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2590 PTR2UV(sv),SvIVX(sv)));
25da4f38 2591 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
2592}
2593
891f9566
YST
2594/* sv_2uv() is now a macro using Perl_sv_2uv_flags();
2595 * this function provided for binary compatibility only
2596 */
2597
2598UV
2599Perl_sv_2uv(pTHX_ register SV *sv)
2600{
2601 return sv_2uv_flags(sv, SV_GMAGIC);
2602}
2603
645c22ef 2604/*
891f9566 2605=for apidoc sv_2uv_flags
645c22ef
DM
2606
2607Return the unsigned integer value of an SV, doing any necessary string
891f9566
YST
2608conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2609Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
645c22ef
DM
2610
2611=cut
2612*/
2613
ff68c719 2614UV
891f9566 2615Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags)
ff68c719
PP
2616{
2617 if (!sv)
2618 return 0;
2619 if (SvGMAGICAL(sv)) {
891f9566
YST
2620 if (flags & SV_GMAGIC)
2621 mg_get(sv);
ff68c719
PP
2622 if (SvIOKp(sv))
2623 return SvUVX(sv);
2624 if (SvNOKp(sv))
2625 return U_V(SvNVX(sv));
36477c24
PP
2626 if (SvPOKp(sv) && SvLEN(sv))
2627 return asUV(sv);
3fe9a6f1 2628 if (!SvROK(sv)) {
d008e5eb 2629 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2630 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2631 report_uninit(sv);
c6ee37c5 2632 }
36477c24 2633 return 0;
3fe9a6f1 2634 }
ff68c719
PP
2635 }
2636 if (SvTHINKFIRST(sv)) {
2637 if (SvROK(sv)) {
ff68c719 2638 SV* tmpstr;
1554e226 2639 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2640 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2641 return SvUV(tmpstr);
56431972 2642 return PTR2UV(SvRV(sv));
ff68c719 2643 }
765f542d
NC
2644 if (SvIsCOW(sv)) {
2645 sv_force_normal_flags(sv, 0);
8a818333 2646 }
0336b60e 2647 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2648 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2649 report_uninit(sv);
ff68c719
PP
2650 return 0;
2651 }
2652 }
25da4f38
IZ
2653 if (SvIOKp(sv)) {
2654 if (SvIsUV(sv)) {
2655 return SvUVX(sv);
2656 }
2657 else {
2658 return (UV)SvIVX(sv);
2659 }
ff68c719
PP
2660 }
2661 if (SvNOKp(sv)) {
28e5dec8
JH
2662 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2663 * without also getting a cached IV/UV from it at the same time
2664 * (ie PV->NV conversion should detect loss of accuracy and cache
2665 * IV or UV at same time to avoid this. */
2666 /* IV-over-UV optimisation - choose to cache IV if possible */
2667
25da4f38
IZ
2668 if (SvTYPE(sv) == SVt_NV)
2669 sv_upgrade(sv, SVt_PVNV);
28e5dec8
JH
2670
2671 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2672 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2673 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2674 if (SvNVX(sv) == (NV) SvIVX(sv)
2675#ifndef NV_PRESERVES_UV
2676 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2677 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2678 /* Don't flag it as "accurately an integer" if the number
2679 came from a (by definition imprecise) NV operation, and
2680 we're outside the range of NV integer precision */
2681#endif
2682 ) {
2683 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2684 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2685 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2686 PTR2UV(sv),
2687 SvNVX(sv),
2688 SvIVX(sv)));
2689
2690 } else {
2691 /* IV not precise. No need to convert from PV, as NV
2692 conversion would already have cached IV if it detected
2693 that PV->IV would be better than PV->NV->IV
2694 flags already correct - don't set public IOK. */
2695 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2696 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2697 PTR2UV(sv),
2698 SvNVX(sv),
2699 SvIVX(sv)));
2700 }
2701 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2702 but the cast (NV)IV_MIN rounds to a the value less (more
2703 negative) than IV_MIN which happens to be equal to SvNVX ??
2704 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2705 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2706 (NV)UVX == NVX are both true, but the values differ. :-(
2707 Hopefully for 2s complement IV_MIN is something like
2708 0x8000000000000000 which will be exact. NWC */
d460ef45 2709 }
28e5dec8 2710 else {
607fa7f2 2711 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2712 if (
2713 (SvNVX(sv) == (NV) SvUVX(sv))
2714#ifndef NV_PRESERVES_UV
2715 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2716 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2717 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2718 /* Don't flag it as "accurately an integer" if the number
2719 came from a (by definition imprecise) NV operation, and
2720 we're outside the range of NV integer precision */
2721#endif
2722 )
2723 SvIOK_on(sv);
2724 SvIsUV_on(sv);
1c846c1f 2725 DEBUG_c(PerlIO_printf(Perl_debug_log,
28e5dec8 2726 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
57def98f 2727 PTR2UV(sv),
28e5dec8
JH
2728 SvUVX(sv),
2729 SvUVX(sv)));
25da4f38 2730 }
ff68c719
PP
2731 }
2732 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2733 UV value;
504618e9 2734 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
25da4f38
IZ
2735
2736 /* We want to avoid a possible problem when we cache a UV which
2737 may be later translated to an NV, and the resulting NV is not
2738 the translation of the initial data.
1c846c1f 2739
25da4f38
IZ
2740 This means that if we cache such a UV, we need to cache the
2741 NV as well. Moreover, we trade speed for space, and do not
2742 cache the NV if not needed.
2743 */
16b7a9a4 2744
c2988b20
NC
2745 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2746 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2747 == IS_NUMBER_IN_UV) {
5e045b90 2748 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8 2749 if (SvTYPE(sv) < SVt_PVIV)
f7bbb42a
JH
2750 sv_upgrade(sv, SVt_PVIV);
2751 (void)SvIOK_on(sv);
c2988b20
NC
2752 } else if (SvTYPE(sv) < SVt_PVNV)
2753 sv_upgrade(sv, SVt_PVNV);
d460ef45 2754
c2988b20
NC
2755 /* If NV preserves UV then we only use the UV value if we know that
2756 we aren't going to call atof() below. If NVs don't preserve UVs
2757 then the value returned may have more precision than atof() will
2758 return, even though it isn't accurate. */
2759 if ((numtype & (IS_NUMBER_IN_UV
2760#ifdef NV_PRESERVES_UV
2761 | IS_NUMBER_NOT_INT
2762#endif
2763 )) == IS_NUMBER_IN_UV) {
2764 /* This won't turn off the public IOK flag if it was set above */
2765 (void)SvIOKp_on(sv);
2766
2767 if (!(numtype & IS_NUMBER_NEG)) {
2768 /* positive */;
2769 if (value <= (UV)IV_MAX) {
45977657 2770 SvIV_set(sv, (IV)value);
28e5dec8
JH
2771 } else {
2772 /* it didn't overflow, and it was positive. */
607fa7f2 2773 SvUV_set(sv, value);
28e5dec8
JH
2774 SvIsUV_on(sv);
2775 }
c2988b20
NC
2776 } else {
2777 /* 2s complement assumption */
2778 if (value <= (UV)IV_MIN) {
45977657 2779 SvIV_set(sv, -(IV)value);
c2988b20
NC
2780 } else {
2781 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2782 I'm assuming it will be rare. */
c2988b20
NC
2783 if (SvTYPE(sv) < SVt_PVNV)
2784 sv_upgrade(sv, SVt_PVNV);
2785 SvNOK_on(sv);
2786 SvIOK_off(sv);
2787 SvIOKp_on(sv);
9d6ce603 2788 SvNV_set(sv, -(NV)value);
45977657 2789 SvIV_set(sv, IV_MIN);
c2988b20
NC
2790 }
2791 }
2792 }
2793
2794 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2795 != IS_NUMBER_IN_UV) {
2796 /* It wasn't an integer, or it overflowed the UV. */
3f7c398e 2797 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8 2798
c2988b20 2799 if (! numtype && ckWARN(WARN_NUMERIC))
28e5dec8
JH
2800 not_a_number(sv);
2801
2802#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2803 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
2804 PTR2UV(sv), SvNVX(sv)));
28e5dec8 2805#else
1779d84d 2806 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
c2988b20 2807 PTR2UV(sv), SvNVX(sv)));
28e5dec8
JH
2808#endif
2809
2810#ifdef NV_PRESERVES_UV
c2988b20
NC
2811 (void)SvIOKp_on(sv);
2812 (void)SvNOK_on(sv);
2813 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2814 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2815 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2816 SvIOK_on(sv);
2817 } else {
2818 /* Integer is imprecise. NOK, IOKp */
2819 }
2820 /* UV will not work better than IV */
2821 } else {
2822 if (SvNVX(sv) > (NV)UV_MAX) {
2823 SvIsUV_on(sv);
2824 /* Integer is inaccurate. NOK, IOKp, is UV */
607fa7f2 2825 SvUV_set(sv, UV_MAX);
c2988b20
NC
2826 SvIsUV_on(sv);
2827 } else {
607fa7f2 2828 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2829 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2830 NV preservse UV so can do correct comparison. */
2831 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2832 SvIOK_on(sv);
2833 SvIsUV_on(sv);
2834 } else {
2835 /* Integer is imprecise. NOK, IOKp, is UV */
2836 SvIsUV_on(sv);
2837 }
2838 }
2839 }
28e5dec8 2840#else /* NV_PRESERVES_UV */
c2988b20
NC
2841 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2842 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2843 /* The UV slot will have been set from value returned by
2844 grok_number above. The NV slot has just been set using
2845 Atof. */
560b0c46 2846 SvNOK_on(sv);
c2988b20
NC
2847 assert (SvIOKp(sv));
2848 } else {
2849 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2850 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2851 /* Small enough to preserve all bits. */
2852 (void)SvIOKp_on(sv);
2853 SvNOK_on(sv);
45977657 2854 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2855 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2856 SvIOK_on(sv);
2857 /* Assumption: first non-preserved integer is < IV_MAX,
2858 this NV is in the preserved range, therefore: */
2859 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2860 < (UV)IV_MAX)) {
32fdb065 2861 Perl_croak(aTHX_ "sv_2uv 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
2862 }
2863 } else
2864 sv_2iuv_non_preserve (sv, numtype);
2865 }
28e5dec8 2866#endif /* NV_PRESERVES_UV */
f7bbb42a 2867 }
ff68c719
PP
2868 }
2869 else {
d008e5eb 2870 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2871 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2872 report_uninit(sv);
c6ee37c5 2873 }
25da4f38
IZ
2874 if (SvTYPE(sv) < SVt_IV)
2875 /* Typically the caller expects that sv_any is not NULL now. */
2876 sv_upgrade(sv, SVt_IV);
ff68c719
PP
2877 return 0;
2878 }
25da4f38 2879
1d7c1841
GS
2880 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2881 PTR2UV(sv),SvUVX(sv)));
25da4f38 2882 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719
PP
2883}
2884
645c22ef
DM
2885/*
2886=for apidoc sv_2nv
2887
2888Return the num value of an SV, doing any necessary string or integer
2889conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
2890macros.
2891
2892=cut
2893*/
2894
65202027 2895NV
864dbfa3 2896Perl_sv_2nv(pTHX_ register SV *sv)
79072805
LW
2897{
2898 if (!sv)
2899 return 0.0;
8990e307 2900 if (SvGMAGICAL(sv)) {
463ee0b2
LW
2901 mg_get(sv);
2902 if (SvNOKp(sv))
2903 return SvNVX(sv);
a0d0e21e 2904 if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2905 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
504618e9 2906 !grok_number(SvPVX_const(sv), SvCUR(sv), NULL))
a0d0e21e 2907 not_a_number(sv);
3f7c398e 2908 return Atof(SvPVX_const(sv));
a0d0e21e 2909 }
25da4f38 2910 if (SvIOKp(sv)) {
1c846c1f 2911 if (SvIsUV(sv))
65202027 2912 return (NV)SvUVX(sv);
25da4f38 2913 else
65202027 2914 return (NV)SvIVX(sv);
25da4f38 2915 }
16d20bd9 2916 if (!SvROK(sv)) {
d008e5eb 2917 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2918 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2919 report_uninit(sv);
c6ee37c5 2920 }
66a1b24b 2921 return (NV)0;
16d20bd9 2922 }
463ee0b2 2923 }
ed6116ce 2924 if (SvTHINKFIRST(sv)) {
a0d0e21e 2925 if (SvROK(sv)) {
a0d0e21e 2926 SV* tmpstr;
1554e226 2927 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2928 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2929 return SvNV(tmpstr);
56431972 2930 return PTR2NV(SvRV(sv));
a0d0e21e 2931 }
765f542d
NC
2932 if (SvIsCOW(sv)) {
2933 sv_force_normal_flags(sv, 0);
8a818333 2934 }
0336b60e 2935 if (SvREADONLY(sv) && !SvOK(sv)) {
599cee73 2936 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2937 report_uninit(sv);
ed6116ce
LW
2938 return 0.0;
2939 }
79072805
LW
2940 }
2941 if (SvTYPE(sv) < SVt_NV) {
463ee0b2
LW
2942 if (SvTYPE(sv) == SVt_IV)
2943 sv_upgrade(sv, SVt_PVNV);
2944 else
2945 sv_upgrade(sv, SVt_NV);
906f284f 2946#ifdef USE_LONG_DOUBLE
097ee67d 2947 DEBUG_c({
f93f4e46 2948 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2949 PerlIO_printf(Perl_debug_log,
2950 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2951 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2952 RESTORE_NUMERIC_LOCAL();
2953 });
65202027 2954#else
572bbb43 2955 DEBUG_c({
f93f4e46 2956 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2957 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
1d7c1841 2958 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2959 RESTORE_NUMERIC_LOCAL();
2960 });
572bbb43 2961#endif
79072805
LW
2962 }
2963 else if (SvTYPE(sv) < SVt_PVNV)
2964 sv_upgrade(sv, SVt_PVNV);
59d8ce62
NC
2965 if (SvNOKp(sv)) {
2966 return SvNVX(sv);
61604483 2967 }
59d8ce62 2968 if (SvIOKp(sv)) {
9d6ce603 2969 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
28e5dec8
JH
2970#ifdef NV_PRESERVES_UV
2971 SvNOK_on(sv);
2972#else
2973 /* Only set the public NV OK flag if this NV preserves the IV */
2974 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2975 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2976 : (SvIVX(sv) == I_V(SvNVX(sv))))
2977 SvNOK_on(sv);
2978 else
2979 SvNOKp_on(sv);
2980#endif
93a17b20 2981 }
748a9306 2982 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2983 UV value;
3f7c398e 2984 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
c2988b20 2985 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
a0d0e21e 2986 not_a_number(sv);
28e5dec8 2987#ifdef NV_PRESERVES_UV
c2988b20
NC
2988 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2989 == IS_NUMBER_IN_UV) {
5e045b90 2990 /* It's definitely an integer */
9d6ce603 2991 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
c2988b20 2992 } else
3f7c398e 2993 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
2994 SvNOK_on(sv);
2995#else
3f7c398e 2996 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
2997 /* Only set the public NV OK flag if this NV preserves the value in
2998 the PV at least as well as an IV/UV would.
2999 Not sure how to do this 100% reliably. */
3000 /* if that shift count is out of range then Configure's test is
3001 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
3002 UV_BITS */
3003 if (((UV)1 << NV_PRESERVES_UV_BITS) >
c2988b20 3004 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
28e5dec8 3005 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
c2988b20
NC
3006 } else if (!(numtype & IS_NUMBER_IN_UV)) {
3007 /* Can't use strtol etc to convert this string, so don't try.
3008 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
3009 SvNOK_on(sv);
3010 } else {
3011 /* value has been set. It may not be precise. */
3012 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
3013 /* 2s complement assumption for (UV)IV_MIN */
3014 SvNOK_on(sv); /* Integer is too negative. */
3015 } else {
3016 SvNOKp_on(sv);
3017 SvIOKp_on(sv);
6fa402ec 3018
c2988b20 3019 if (numtype & IS_NUMBER_NEG) {
45977657 3020 SvIV_set(sv, -(IV)value);
c2988b20 3021 } else if (value <= (UV)IV_MAX) {
45977657 3022 SvIV_set(sv, (IV)value);
c2988b20 3023 } else {
607fa7f2 3024 SvUV_set(sv, value);
c2988b20
NC
3025 SvIsUV_on(sv);
3026 }
3027
3028 if (numtype & IS_NUMBER_NOT_INT) {
3029 /* I believe that even if the original PV had decimals,
3030 they are lost beyond the limit of the FP precision.
3031 However, neither is canonical, so both only get p
3032 flags. NWC, 2000/11/25 */
3033 /* Both already have p flags, so do nothing */
3034 } else {
66a1b24b 3035 const NV nv = SvNVX(sv);
c2988b20
NC
3036 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
3037 if (SvIVX(sv) == I_V(nv)) {
3038 SvNOK_on(sv);
3039 SvIOK_on(sv);
3040 } else {
3041 SvIOK_on(sv);
3042 /* It had no "." so it must be integer. */
3043 }
3044 } else {
3045 /* between IV_MAX and NV(UV_MAX).
3046 Could be slightly > UV_MAX */
6fa402ec 3047
c2988b20
NC
3048 if (numtype & IS_NUMBER_NOT_INT) {
3049 /* UV and NV both imprecise. */
3050 } else {
66a1b24b 3051 const UV nv_as_uv = U_V(nv);
c2988b20
NC
3052
3053 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
3054 SvNOK_on(sv);
3055 SvIOK_on(sv);
3056 } else {
3057 SvIOK_on(sv);
3058 }
3059 }
3060 }
3061 }
3062 }
3063 }
28e5dec8 3064#endif /* NV_PRESERVES_UV */
93a17b20 3065 }
79072805 3066 else {
599cee73 3067 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 3068 report_uninit(sv);
25da4f38
IZ
3069 if (SvTYPE(sv) < SVt_NV)
3070 /* Typically the caller expects that sv_any is not NULL now. */
28e5dec8
JH
3071 /* XXX Ilya implies that this is a bug in callers that assume this
3072 and ideally should be fixed. */
25da4f38 3073 sv_upgrade(sv, SVt_NV);
a0d0e21e 3074 return 0.0;
79072805 3075 }
572bbb43 3076#if defined(USE_LONG_DOUBLE)
097ee67d 3077 DEBUG_c({
f93f4e46 3078 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
3079 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
3080 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
3081 RESTORE_NUMERIC_LOCAL();
3082 });
65202027 3083#else
572bbb43 3084 DEBUG_c({
f93f4e46 3085 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 3086 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
1d7c1841 3087 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
3088 RESTORE_NUMERIC_LOCAL();
3089 });
572bbb43 3090#endif
463ee0b2 3091 return SvNVX(sv);
79072805
LW
3092}
3093
645c22ef
DM
3094/* asIV(): extract an integer from the string value of an SV.
3095 * Caller must validate PVX */
3096
76e3520e 3097STATIC IV
cea2e8a9 3098S_asIV(pTHX_ SV *sv)
36477c24 3099{
c2988b20 3100 UV value;
66a1b24b 3101 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
c2988b20
NC
3102
3103 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3104 == IS_NUMBER_IN_UV) {
645c22ef 3105 /* It's definitely an integer */
c2988b20
NC
3106 if (numtype & IS_NUMBER_NEG) {
3107 if (value < (UV)IV_MIN)
3108 return -(IV)value;
3109 } else {
3110 if (value < (UV)IV_MAX)
3111 return (IV)value;
3112 }
3113 }
d008e5eb 3114 if (!numtype) {
d008e5eb
GS
3115 if (ckWARN(WARN_NUMERIC))
3116 not_a_number(sv);
3117 }
3f7c398e 3118 return I_V(Atof(SvPVX_const(sv)));
36477c24
PP
3119}
3120
645c22ef
DM
3121/* asUV(): extract an unsigned integer from the string value of an SV
3122 * Caller must validate PVX */
3123
76e3520e 3124STATIC UV
cea2e8a9 3125S_asUV(pTHX_ SV *sv)
36477c24 3126{
c2988b20 3127 UV value;
504618e9 3128 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
36477c24 3129
c2988b20
NC
3130 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3131 == IS_NUMBER_IN_UV) {
645c22ef 3132 /* It's definitely an integer */
6fa402ec 3133 if (!(numtype & IS_NUMBER_NEG))
c2988b20
NC
3134 return value;
3135 }
d008e5eb 3136 if (!numtype) {
d008e5eb
GS
3137 if (ckWARN(WARN_NUMERIC))
3138 not_a_number(sv);
3139 }
3f7c398e 3140 return U_V(Atof(SvPVX_const(sv)));
36477c24
PP
3141}
3142
645c22ef
DM
3143/*
3144=for apidoc sv_2pv_nolen
3145
3146Like C<sv_2pv()>, but doesn't return the length too. You should usually
3147use the macro wrapper C<SvPV_nolen(sv)> instead.
3148=cut
3149*/
3150
79072805 3151char *
864dbfa3 3152Perl_sv_2pv_nolen(pTHX_ register SV *sv)
1fa8b10d 3153{
dafda6d1 3154 return sv_2pv(sv, 0);
1fa8b10d
JD
3155}
3156
645c22ef
DM
3157/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
3158 * UV as a string towards the end of buf, and return pointers to start and
3159 * end of it.
3160 *
3161 * We assume that buf is at least TYPE_CHARS(UV) long.
3162 */
3163
864dbfa3 3164static char *
25da4f38
IZ
3165uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
3166{
25da4f38
IZ
3167 char *ptr = buf + TYPE_CHARS(UV);
3168 char *ebuf = ptr;
3169 int sign;
25da4f38
IZ
3170
3171 if (is_uv)
3172 sign = 0;
3173 else if (iv >= 0) {
3174 uv = iv;
3175 sign = 0;
3176 } else {
3177 uv = -iv;
3178 sign = 1;
3179 }
3180 do {
eb160463 3181 *--ptr = '0' + (char)(uv % 10);
25da4f38
IZ
3182 } while (uv /= 10);
3183 if (sign)
3184 *--ptr = '-';
3185 *peob = ebuf;
3186 return ptr;
3187}
3188
09540bc3
JH
3189/* sv_2pv() is now a macro using Perl_sv_2pv_flags();
3190 * this function provided for binary compatibility only
3191 */
3192
3193char *
3194Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
3195{
3196 return sv_2pv_flags(sv, lp, SV_GMAGIC);
3197}
3198
645c22ef
DM
3199/*
3200=for apidoc sv_2pv_flags
3201
ff276b08 3202Returns a pointer to the string value of an SV, and sets *lp to its length.
645c22ef
DM
3203If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
3204if necessary.
3205Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
3206usually end up here too.
3207
3208=cut
3209*/
3210
8d6d96c1
HS
3211char *
3212Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
3213{
79072805
LW
3214 register char *s;
3215 int olderrno;
cb50f42d 3216 SV *tsv, *origsv;
25da4f38
IZ
3217 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
3218 char *tmpbuf = tbuf;
79072805 3219
463ee0b2 3220 if (!sv) {
cdb061a3
NC
3221 if (lp)
3222 *lp = 0;
73d840c0 3223 return (char *)"";
463ee0b2 3224 }
8990e307 3225 if (SvGMAGICAL(sv)) {
8d6d96c1
HS
3226 if (flags & SV_GMAGIC)
3227 mg_get(sv);
463ee0b2 3228 if (SvPOKp(sv)) {
cdb061a3
NC
3229 if (lp)
3230 *lp = SvCUR(sv);
10516c54
NC
3231 if (flags & SV_MUTABLE_RETURN)
3232 return SvPVX_mutable(sv);
4d84ee25
NC
3233 if (flags & SV_CONST_RETURN)
3234 return (char *)SvPVX_const(sv);
463ee0b2
LW
3235 return SvPVX(sv);
3236 }
cf2093f6 3237 if (SvIOKp(sv)) {
1c846c1f 3238 if (SvIsUV(sv))
57def98f 3239 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
cf2093f6 3240 else
57def98f 3241 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
46fc3d4c 3242 tsv = Nullsv;
a0d0e21e 3243 goto tokensave;
463ee0b2
LW
3244 }
3245 if (SvNOKp(sv)) {
2d4389e4 3246 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
46fc3d4c 3247 tsv = Nullsv;
a0d0e21e 3248 goto tokensave;
463ee0b2 3249 }
16d20bd9 3250 if (!SvROK(sv)) {
d008e5eb 3251 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 3252 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 3253 report_uninit(sv);
c6ee37c5 3254 }
cdb061a3
NC
3255 if (lp)
3256 *lp = 0;
73d840c0 3257 return (char *)"";
16d20bd9 3258 }
463ee0b2 3259 }
ed6116ce
LW
3260 if (SvTHINKFIRST(sv)) {
3261 if (SvROK(sv)) {
a0d0e21e 3262 SV* tmpstr;
e1ec3a88 3263 register const char *typestr;
1554e226 3264 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
b4b9a328 3265 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
50adf7d2
NC
3266 /* Unwrap this: */
3267 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr); */
3268
3269 char *pv;
3270 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
3271 if (flags & SV_CONST_RETURN) {
3272 pv = (char *) SvPVX_const(tmpstr);
3273 } else {
3274 pv = (flags & SV_MUTABLE_RETURN)
3275 ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
3276 }
3277 if (lp)
3278 *lp = SvCUR(tmpstr);
3279 } else {
3280 pv = sv_2pv_flags(tmpstr, lp, flags);
3281 }
446eaa42
YST
3282 if (SvUTF8(tmpstr))
3283 SvUTF8_on(sv);
3284 else
3285 SvUTF8_off(sv);
3286 return pv;
3287 }
cb50f42d 3288 origsv = sv;
ed6116ce
LW
3289 sv = (SV*)SvRV(sv);
3290 if (!sv)
e1ec3a88 3291 typestr = "NULLREF";
ed6116ce 3292 else {
f9277f47
IZ
3293 MAGIC *mg;
3294
ed6116ce 3295 switch (SvTYPE(sv)) {
f9277f47
IZ
3296 case SVt_PVMG:
3297 if ( ((SvFLAGS(sv) &
1c846c1f 3298 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
faf82a0b 3299 == (SVs_OBJECT|SVs_SMG))
14befaf4 3300 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
e1ec3a88 3301 const regexp *re = (regexp *)mg->mg_obj;
1bd3ad17 3302
2cd61cdb 3303 if (!mg->mg_ptr) {
e1ec3a88 3304 const char *fptr = "msix";
8782bef2
GB
3305 char reflags[6];
3306 char ch;
3307 int left = 0;
3308 int right = 4;
ff385a1b 3309 char need_newline = 0;
eb160463 3310 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
8782bef2 3311
155aba94 3312 while((ch = *fptr++)) {
8782bef2
GB
3313 if(reganch & 1) {
3314 reflags[left++] = ch;
3315 }
3316 else {
3317 reflags[right--] = ch;
3318 }
3319 reganch >>= 1;
3320 }
3321 if(left != 4) {
3322 reflags[left] = '-';
3323 left = 5;
3324 }
3325
3326 mg->mg_len = re->prelen + 4 + left;
ff385a1b
JF
3327 /*
3328 * If /x was used, we have to worry about a regex
3329 * ending with a comment later being embedded
3330 * within another regex. If so, we don't want this
3331 * regex's "commentization" to leak out to the
3332 * right part of the enclosing regex, we must cap
3333 * it with a newline.
3334 *
3335 * So, if /x was used, we scan backwards from the
3336 * end of the regex. If we find a '#' before we
3337 * find a newline, we need to add a newline
3338 * ourself. If we find a '\n' first (or if we
3339 * don't find '#' or '\n'), we don't need to add
3340 * anything. -jfriedl
3341 */
3342 if (PMf_EXTENDED & re->reganch)
3343 {
e1ec3a88 3344 const char *endptr = re->precomp + re->prelen;
ff385a1b
JF
3345 while (endptr >= re->precomp)
3346 {
e1ec3a88 3347 const char c = *(endptr--);
ff385a1b
JF
3348 if (c == '\n')
3349 break; /* don't need another */
3350 if (c == '#') {
3351 /* we end while in a comment, so we
3352 need a newline */
3353 mg->mg_len++; /* save space for it */
3354 need_newline = 1; /* note to add it */
ab01544f 3355 break;
ff385a1b
JF
3356 }
3357 }
3358 }
3359
8782bef2
GB
3360 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3361 Copy("(?", mg->mg_ptr, 2, char);
3362 Copy(reflags, mg->mg_ptr+2, left, char);
3363 Copy(":", mg->mg_ptr+left+2, 1, char);
3364 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
ff385a1b
JF
3365 if (need_newline)
3366 mg->mg_ptr[mg->mg_len - 2] = '\n';
1bd3ad17
IZ
3367 mg->mg_ptr[mg->mg_len - 1] = ')';
3368 mg->mg_ptr[mg->mg_len] = 0;
3369 }
3280af22 3370 PL_reginterp_cnt += re->program[0].next_off;
cb50f42d
YST
3371
3372 if (re->reganch & ROPT_UTF8)
3373 SvUTF8_on(origsv);
3374 else
3375 SvUTF8_off(origsv);
cdb061a3
NC
3376 if (lp)
3377 *lp = mg->mg_len;
1bd3ad17 3378 return mg->mg_ptr;
f9277f47
IZ
3379 }
3380 /* Fall through */
ed6116ce
LW
3381 case SVt_NULL:
3382 case SVt_IV:
3383 case SVt_NV:
3384 case SVt_RV:
3385 case SVt_PV:
3386 case SVt_PVIV:
3387 case SVt_PVNV:
e1ec3a88
AL
3388 case SVt_PVBM: typestr = SvROK(sv) ? "REF" : "SCALAR"; break;
3389 case SVt_PVLV: typestr = SvROK(sv) ? "REF"
be65207d
DM
3390 /* tied lvalues should appear to be
3391 * scalars for backwards compatitbility */
3392 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
3393 ? "SCALAR" : "LVALUE"; break;
e1ec3a88
AL
3394 case SVt_PVAV: typestr = "ARRAY"; break;
3395 case SVt_PVHV: typestr = "HASH"; break;
3396 case SVt_PVCV: typestr = "CODE"; break;
3397 case SVt_PVGV: typestr = "GLOB"; break;
3398 case SVt_PVFM: typestr = "FORMAT"; break;
3399 case SVt_PVIO: typestr = "IO"; break;
3400 default: typestr = "UNKNOWN"; break;
ed6116ce 3401 }
46fc3d4c 3402 tsv = NEWSV(0,0);
a5cb6b62 3403 if (SvOBJECT(sv)) {
bfcb3514 3404 const char *name = HvNAME_get(SvSTASH(sv));
a5cb6b62 3405 Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
e1ec3a88 3406 name ? name : "__ANON__" , typestr, PTR2UV(sv));
a5cb6b62 3407 }
ed6116ce 3408 else
e1ec3a88 3409 Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv));
a0d0e21e 3410 goto tokensaveref;
463ee0b2 3411 }
cdb061a3
NC
3412 if (lp)
3413 *lp = strlen(typestr);
73d840c0 3414 return (char *)typestr;
79072805 3415 }
0336b60e 3416 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 3417 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 3418 report_uninit(sv);
cdb061a3
NC
3419 if (lp)
3420 *lp = 0;
73d840c0 3421 return (char *)"";
79072805 3422 }
79072805 3423 }
28e5dec8
JH
3424 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3425 /* I'm assuming that if both IV and NV are equally valid then
3426 converting the IV is going to be more efficient */
e1ec3a88
AL
3427 const U32 isIOK = SvIOK(sv);
3428 const U32 isUIOK = SvIsUV(sv);
28e5dec8
JH
3429 char buf[TYPE_CHARS(UV)];
3430 char *ebuf, *ptr;
3431
3432 if (SvTYPE(sv) < SVt_PVIV)
3433 sv_upgrade(sv, SVt_PVIV);
3434 if (isUIOK)
3435 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3436 else
3437 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
5902b6a9
NC
3438 /* inlined from sv_setpvn */
3439 SvGROW_mutable(sv, (STRLEN)(ebuf - ptr + 1));
4d84ee25 3440 Move(ptr,SvPVX_mutable(sv),ebuf - ptr,char);
28e5dec8
JH
3441 SvCUR_set(sv, ebuf - ptr);
3442 s = SvEND(sv);
3443 *s = '\0';
3444 if (isIOK)
3445 SvIOK_on(sv);
3446 else
3447 SvIOKp_on(sv);
3448 if (isUIOK)
3449 SvIsUV_on(sv);
3450 }
3451 else if (SvNOKp(sv)) {
79072805
LW
3452 if (SvTYPE(sv) < SVt_PVNV)
3453 sv_upgrade(sv, SVt_PVNV);
1c846c1f 3454 /* The +20 is pure guesswork. Configure test needed. --jhi */
5902b6a9 3455 s = SvGROW_mutable(sv, NV_DIG + 20);
79072805 3456 olderrno = errno; /* some Xenix systems wipe out errno here */
79072805 3457#ifdef apollo
463ee0b2 3458 if (SvNVX(sv) == 0.0)
79072805
LW
3459 (void)strcpy(s,"0");
3460 else
3461#endif /*apollo*/
bbce6d69 3462 {
2d4389e4 3463 Gconvert(SvNVX(sv), NV_DIG, 0, s);
bbce6d69 3464 }
79072805 3465 errno = olderrno;
a0d0e21e
LW
3466#ifdef FIXNEGATIVEZERO
3467 if (*s == '-' && s[1] == '0' && !s[2])
3468 strcpy(s,"0");
3469#endif
79072805
LW
3470 while (*s) s++;
3471#ifdef hcx
3472 if (s[-1] == '.')
46fc3d4c 3473 *--s = '\0';
79072805
LW
3474#endif
3475 }
79072805 3476 else {
0336b60e
IZ
3477 if (ckWARN(WARN_UNINITIALIZED)
3478 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 3479 report_uninit(sv);
cdb061a3 3480 if (lp)
a0d0e21e 3481 *lp = 0;
25da4f38
IZ
3482 if (SvTYPE(sv) < SVt_PV)
3483 /* Typically the caller expects that sv_any is not NULL now. */
3484 sv_upgrade(sv, SVt_PV);
73d840c0 3485 return (char *)"";
79072805 3486 }
cdb061a3
NC
3487 {
3488 STRLEN len = s - SvPVX_const(sv);
3489 if (lp)
3490 *lp = len;
3491 SvCUR_set(sv, len);
3492 }
79072805 3493 SvPOK_on(sv);
1d7c1841 3494 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3f7c398e 3495 PTR2UV(sv),SvPVX_const(sv)));
4d84ee25
NC
3496 if (flags & SV_CONST_RETURN)
3497 return (char *)SvPVX_const(sv);
10516c54
NC
3498 if (flags & SV_MUTABLE_RETURN)
3499 return SvPVX_mutable(sv);
463ee0b2 3500 return SvPVX(sv);
a0d0e21e
LW
3501
3502 tokensave:
3503 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3504 /* Sneaky stuff here */
3505
3506 tokensaveref:
46fc3d4c 3507 if (!tsv)
96827780 3508 tsv = newSVpv(tmpbuf, 0);
46fc3d4c 3509 sv_2mortal(tsv);
cdb061a3
NC
3510 if (lp)
3511 *lp = SvCUR(tsv);
46fc3d4c 3512 return SvPVX(tsv);
a0d0e21e
LW
3513 }
3514 else {
27da23d5 3515 dVAR;
a0d0e21e 3516 STRLEN len;
73d840c0 3517 const char *t;
46fc3d4c
PP
3518
3519 if (tsv) {
3520 sv_2mortal(tsv);
3f7c398e 3521 t = SvPVX_const(tsv);
46fc3d4c
PP
3522 len = SvCUR(tsv);
3523 }
3524 else {
96827780
MB
3525 t = tmpbuf;
3526 len = strlen(tmpbuf);
46fc3d4c 3527 }
a0d0e21e 3528#ifdef FIXNEGATIVEZERO
46fc3d4c
PP
3529 if (len == 2 && t[0] == '-' && t[1] == '0') {
3530 t = "0";
3531 len = 1;
3532 }
a0d0e21e 3533#endif
862a34c6 3534 SvUPGRADE(sv, SVt_PV);
cdb061a3
NC
3535 if (lp)
3536 *lp = len;
5902b6a9 3537 s = SvGROW_mutable(sv, len + 1);
a0d0e21e 3538 SvCUR_set(sv, len);
6bf554b4 3539 SvPOKp_on(sv);
e90e2364 3540 return strcpy(s, t);
a0d0e21e 3541 }
463ee0b2
LW
3542}
3543
645c22ef 3544/*
6050d10e
JP
3545=for apidoc sv_copypv
3546
3547Copies a stringified representation of the source SV into the
3548destination SV. Automatically performs any necessary mg_get and
54f0641b 3549coercion of numeric values into strings. Guaranteed to preserve
6050d10e 3550UTF-8 flag even from overloaded objects. Similar in nature to
54f0641b
NIS
3551sv_2pv[_flags] but operates directly on an SV instead of just the
3552string. Mostly uses sv_2pv_flags to do its work, except when that
6050d10e
JP
3553would lose the UTF-8'ness of the PV.
3554
3555=cut
3556*/
3557
3558void
3559Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3560{
446eaa42 3561 STRLEN len;
4d84ee25
NC
3562 const char *s;
3563 s = SvPV_const(ssv,len);
cb50f42d 3564 sv_setpvn(dsv,s,len);
446eaa42 3565 if (SvUTF8(ssv))
cb50f42d 3566 SvUTF8_on(dsv);
446eaa42 3567 else
cb50f42d 3568 SvUTF8_off(dsv);
6050d10e
JP
3569}
3570
3571/*
645c22ef
DM
3572=for apidoc sv_2pvbyte_nolen
3573
3574Return a pointer to the byte-encoded representation of the SV.
1e54db1a 3575May cause the SV to be downgraded from UTF-8 as a side-effect.
645c22ef
DM
3576
3577Usually accessed via the C<SvPVbyte_nolen> macro.
3578
3579=cut
3580*/
3581
7340a771
GS
3582char *
3583Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3584{
dafda6d1 3585 return sv_2pvbyte(sv, 0);
7340a771
GS
3586}
3587
645c22ef
DM
3588/*
3589=for apidoc sv_2pvbyte
3590
3591Return a pointer to the byte-encoded representation of the SV, and set *lp
1e54db1a 3592to its length. May cause the SV to be downgraded from UTF-8 as a
645c22ef
DM
3593side-effect.
3594
3595Usually accessed via the C<SvPVbyte> macro.
3596
3597=cut
3598*/
3599
7340a771
GS
3600char *
3601Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3602{
0875d2fe 3603 sv_utf8_downgrade(sv,0);
97972285 3604 return lp ? SvPV(sv,*lp) : SvPV_nolen(sv);
7340a771
GS
3605}
3606
645c22ef
DM
3607/*
3608=for apidoc sv_2pvutf8_nolen
3609
1e54db1a
JH
3610Return a pointer to the UTF-8-encoded representation of the SV.
3611May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3612
3613Usually accessed via the C<SvPVutf8_nolen> macro.
3614
3615=cut
3616*/
3617
7340a771
GS
3618char *
3619Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3620{
dafda6d1 3621 return sv_2pvutf8(sv, 0);
7340a771
GS
3622}
3623
645c22ef
DM
3624/*
3625=for apidoc sv_2pvutf8
3626
1e54db1a
JH
3627Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3628to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3629
3630Usually accessed via the C<SvPVutf8> macro.
3631
3632=cut
3633*/
3634
7340a771
GS
3635char *
3636Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3637{
560a288e 3638 sv_utf8_upgrade(sv);
7d59b7e4 3639 return SvPV(sv,*lp);
7340a771 3640}
1c846c1f 3641
645c22ef
DM
3642/*
3643=for apidoc sv_2bool
3644
3645This function is only called on magical items, and is only used by
8cf8f3d1 3646sv_true() or its macro equivalent.
645c22ef
DM
3647
3648=cut
3649*/
3650
463ee0b2 3651bool
864dbfa3 3652Perl_sv_2bool(pTHX_ register SV *sv)
463ee0b2 3653{
8990e307 3654 if (SvGMAGICAL(sv))
463ee0b2
LW
3655 mg_get(sv);
3656
a0d0e21e
LW
3657 if (!SvOK(sv))
3658 return 0;
3659 if (SvROK(sv)) {
a0d0e21e 3660 SV* tmpsv;
1554e226 3661 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
9e3013b1 3662 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
8a31060d 3663 return (bool)SvTRUE(tmpsv);
a0d0e21e
LW
3664 return SvRV(sv) != 0;
3665 }
463ee0b2 3666 if (SvPOKp(sv)) {
11343788
MB
3667 register XPV* Xpvtmp;
3668 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
339049b0 3669 (*sv->sv_u.svu_pv > '0' ||
11343788 3670 Xpvtmp->xpv_cur > 1 ||
339049b0 3671 (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0')))
463ee0b2
LW
3672 return 1;
3673 else
3674 return 0;
3675 }
3676 else {
3677 if (SvIOKp(sv))
3678 return SvIVX(sv) != 0;
3679 else {
3680 if (SvNOKp(sv))
3681 return SvNVX(sv) != 0.0;
3682 else
3683 return FALSE;
3684 }
3685 }
79072805
LW
3686}
3687
09540bc3
JH
3688/* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3689 * this function provided for binary compatibility only
3690 */
3691
3692
3693STRLEN
3694Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3695{
3696 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3697}
3698
c461cf8f
JH
3699/*
3700=for apidoc sv_utf8_upgrade
3701
78ea37eb 3702Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3703Forces the SV to string form if it is not already.
4411f3b6
NIS
3704Always sets the SvUTF8 flag to avoid future validity checks even
3705if all the bytes have hibit clear.
c461cf8f 3706
13a6c0e0
JH
3707This is not as a general purpose byte encoding to Unicode interface:
3708use the Encode extension for that.
3709
8d6d96c1
HS
3710=for apidoc sv_utf8_upgrade_flags
3711
78ea37eb 3712Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3713Forces the SV to string form if it is not already.
8d6d96c1
HS
3714Always sets the SvUTF8 flag to avoid future validity checks even
3715if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3716will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3717C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3718
13a6c0e0
JH
3719This is not as a general purpose byte encoding to Unicode interface:
3720use the Encode extension for that.
3721
8d6d96c1
HS
3722=cut
3723*/
3724
3725STRLEN
3726Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3727{
808c356f
RGS
3728 if (sv == &PL_sv_undef)
3729 return 0;
e0e62c2a
NIS
3730 if (!SvPOK(sv)) {
3731 STRLEN len = 0;
d52b7888
NC
3732 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3733 (void) sv_2pv_flags(sv,&len, flags);
3734 if (SvUTF8(sv))
3735 return len;
3736 } else {
3737 (void) SvPV_force(sv,len);
3738 }
e0e62c2a 3739 }
4411f3b6 3740
f5cee72b 3741 if (SvUTF8(sv)) {
5fec3b1d 3742 return SvCUR(sv);
f5cee72b 3743 }
5fec3b1d 3744
765f542d
NC
3745 if (SvIsCOW(sv)) {
3746 sv_force_normal_flags(sv, 0);
db42d148
NIS
3747 }
3748
88632417 3749 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
799ef3cb 3750 sv_recode_to_utf8(sv, PL_encoding);
9f4817db 3751 else { /* Assume Latin-1/EBCDIC */
c4e7c712
NC
3752 /* This function could be much more efficient if we
3753 * had a FLAG in SVs to signal if there are any hibit
3754 * chars in the PV. Given that there isn't such a flag
3755 * make the loop as fast as possible. */
93524f2b
NC
3756 const U8 *s = (U8 *) SvPVX_const(sv);
3757 const U8 *e = (U8 *) SvEND(sv);
3758 const U8 *t = s;
c4e7c712
NC
3759 int hibit = 0;
3760
3761 while (t < e) {
3762 U8 ch = *t++;
3763 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
3764 break;
3765 }
3766 if (hibit) {
3767 STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */
1e2ebb21 3768 U8 *recoded = bytes_to_utf8((U8*)s, &len);
c4e7c712
NC
3769
3770 SvPV_free(sv); /* No longer using what was there before. */
3771
1e2ebb21 3772 SvPV_set(sv, (char*)recoded);
c4e7c712
NC
3773 SvCUR_set(sv, len - 1);
3774 SvLEN_set(sv, len); /* No longer know the real size. */
3775 }
3776 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3777 SvUTF8_on(sv);
560a288e 3778 }
4411f3b6 3779 return SvCUR(sv);
560a288e
GS
3780}
3781
c461cf8f
JH
3782/*
3783=for apidoc sv_utf8_downgrade
3784
78ea37eb
ST
3785Attempts to convert the PV of an SV from characters to bytes.
3786If the PV contains a character beyond byte, this conversion will fail;
3787in this case, either returns false or, if C<fail_ok> is not
c461cf8f
JH
3788true, croaks.
3789