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