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