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