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