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