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