This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
PVFMs don't need CvDEPTH, and PVCVs don't use SvIVX, so moving
[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
fdda85ca
JC
645
646 for (i=0; i<SVt_LAST; i++)
93e68bfb 647 PL_body_roots[i] = 0;
93e68bfb 648
43c5f42d 649 Safefree(PL_nice_chunk);
bd61b366 650 PL_nice_chunk = NULL;
3280af22
NIS
651 PL_nice_chunk_size = 0;
652 PL_sv_arenaroot = 0;
653 PL_sv_root = 0;
4633a7c4
LW
654}
655
bd81e77b
NC
656/*
657 Here are mid-level routines that manage the allocation of bodies out
658 of the various arenas. There are 5 kinds of arenas:
29489e7c 659
bd81e77b
NC
660 1. SV-head arenas, which are discussed and handled above
661 2. regular body arenas
662 3. arenas for reduced-size bodies
663 4. Hash-Entry arenas
664 5. pte arenas (thread related)
29489e7c 665
bd81e77b
NC
666 Arena types 2 & 3 are chained by body-type off an array of
667 arena-root pointers, which is indexed by svtype. Some of the
668 larger/less used body types are malloced singly, since a large
669 unused block of them is wasteful. Also, several svtypes dont have
670 bodies; the data fits into the sv-head itself. The arena-root
671 pointer thus has a few unused root-pointers (which may be hijacked
672 later for arena types 4,5)
29489e7c 673
bd81e77b
NC
674 3 differs from 2 as an optimization; some body types have several
675 unused fields in the front of the structure (which are kept in-place
676 for consistency). These bodies can be allocated in smaller chunks,
677 because the leading fields arent accessed. Pointers to such bodies
678 are decremented to point at the unused 'ghost' memory, knowing that
679 the pointers are used with offsets to the real memory.
29489e7c 680
bd81e77b
NC
681 HE, HEK arenas are managed separately, with separate code, but may
682 be merge-able later..
683
684 PTE arenas are not sv-bodies, but they share these mid-level
685 mechanics, so are considered here. The new mid-level mechanics rely
686 on the sv_type of the body being allocated, so we just reserve one
687 of the unused body-slots for PTEs, then use it in those (2) PTE
688 contexts below (line ~10k)
689*/
690
5e258f8c
JC
691/* get_arena(size): when ARENASETS is enabled, this creates
692 custom-sized arenas, otherwize it uses PERL_ARENA_SIZE, as
693 previously done.
694 TBD: export properly for hv.c: S_more_he().
695*/
696void*
697Perl_get_arena(pTHX_ int arena_size)
698{
699#if !ARENASETS
700 union arena* arp;
701
702 /* allocate and attach arena */
d2a0f284 703 Newx(arp, arena_size, char);
5e258f8c
JC
704 arp->next = PL_body_arenas;
705 PL_body_arenas = arp;
706 return arp;
707
708#else
709 struct arena_desc* adesc;
476a1e16 710 struct arena_set *newroot, **aroot = (struct arena_set**) &PL_body_arenas;
5e258f8c
JC
711 int curr;
712
476a1e16
JC
713 /* shouldnt need this
714 if (!arena_size) arena_size = PERL_ARENA_SIZE;
715 */
5e258f8c
JC
716
717 /* may need new arena-set to hold new arena */
476a1e16 718 if (!*aroot || (*aroot)->curr >= (*aroot)->set_size) {
5e258f8c
JC
719 Newxz(newroot, 1, struct arena_set);
720 newroot->set_size = ARENAS_PER_SET;
476a1e16
JC
721 newroot->next = *aroot;
722 *aroot = newroot;
723 DEBUG_m(PerlIO_printf(Perl_debug_log, "new arenaset %p\n", *aroot));
5e258f8c
JC
724 }
725
726 /* ok, now have arena-set with at least 1 empty/available arena-desc */
476a1e16
JC
727 curr = (*aroot)->curr++;
728 adesc = &((*aroot)->set[curr]);
5e258f8c
JC
729 assert(!adesc->arena);
730
5e258f8c
JC
731 Newxz(adesc->arena, arena_size, char);
732 adesc->size = arena_size;
d2a0f284
JC
733 DEBUG_m(PerlIO_printf(Perl_debug_log, "arena %d added: %p size %d\n",
734 curr, adesc->arena, arena_size));
5e258f8c
JC
735
736 return adesc->arena;
737#endif
738}
739
53c1dcc0 740
bd81e77b 741/* return a thing to the free list */
29489e7c 742
bd81e77b
NC
743#define del_body(thing, root) \
744 STMT_START { \
00b6aa41 745 void ** const thing_copy = (void **)thing;\
bd81e77b
NC
746 LOCK_SV_MUTEX; \
747 *thing_copy = *root; \
748 *root = (void*)thing_copy; \
749 UNLOCK_SV_MUTEX; \
750 } STMT_END
29489e7c 751
bd81e77b 752/*
d2a0f284
JC
753
754=head1 SV-Body Allocation
755
756Allocation of SV-bodies is similar to SV-heads, differing as follows;
757the allocation mechanism is used for many body types, so is somewhat
758more complicated, it uses arena-sets, and has no need for still-live
759SV detection.
760
761At the outermost level, (new|del)_X*V macros return bodies of the
762appropriate type. These macros call either (new|del)_body_type or
763(new|del)_body_allocated macro pairs, depending on specifics of the
764type. Most body types use the former pair, the latter pair is used to
765allocate body types with "ghost fields".
766
767"ghost fields" are fields that are unused in certain types, and
768consequently dont need to actually exist. They are declared because
769they're part of a "base type", which allows use of functions as
770methods. The simplest examples are AVs and HVs, 2 aggregate types
771which don't use the fields which support SCALAR semantics.
772
773For these types, the arenas are carved up into *_allocated size
774chunks, we thus avoid wasted memory for those unaccessed members.
775When bodies are allocated, we adjust the pointer back in memory by the
776size of the bit not allocated, so it's as if we allocated the full
777structure. (But things will all go boom if you write to the part that
778is "not there", because you'll be overwriting the last members of the
779preceding structure in memory.)
780
781We calculate the correction using the STRUCT_OFFSET macro. For
782example, if xpv_allocated is the same structure as XPV then the two
783OFFSETs sum to zero, and the pointer is unchanged. If the allocated
784structure is smaller (no initial NV actually allocated) then the net
785effect is to subtract the size of the NV from the pointer, to return a
786new pointer as if an initial NV were actually allocated.
787
788This is the same trick as was used for NV and IV bodies. Ironically it
789doesn't need to be used for NV bodies any more, because NV is now at
790the start of the structure. IV bodies don't need it either, because
791they are no longer allocated.
792
793In turn, the new_body_* allocators call S_new_body(), which invokes
794new_body_inline macro, which takes a lock, and takes a body off the
795linked list at PL_body_roots[sv_type], calling S_more_bodies() if
796necessary to refresh an empty list. Then the lock is released, and
797the body is returned.
798
799S_more_bodies calls get_arena(), and carves it up into an array of N
800bodies, which it strings into a linked list. It looks up arena-size
801and body-size from the body_details table described below, thus
802supporting the multiple body-types.
803
804If PURIFY is defined, or PERL_ARENA_SIZE=0, arenas are not used, and
805the (new|del)_X*V macros are mapped directly to malloc/free.
806
807*/
808
809/*
810
811For each sv-type, struct body_details bodies_by_type[] carries
812parameters which control these aspects of SV handling:
813
814Arena_size determines whether arenas are used for this body type, and if
815so, how big they are. PURIFY or PERL_ARENA_SIZE=0 set this field to
816zero, forcing individual mallocs and frees.
817
818Body_size determines how big a body is, and therefore how many fit into
819each arena. Offset carries the body-pointer adjustment needed for
820*_allocated body types, and is used in *_allocated macros.
821
822But its main purpose is to parameterize info needed in
823Perl_sv_upgrade(). The info here dramatically simplifies the function
824vs the implementation in 5.8.7, making it table-driven. All fields
825are used for this, except for arena_size.
826
827For the sv-types that have no bodies, arenas are not used, so those
828PL_body_roots[sv_type] are unused, and can be overloaded. In
829something of a special case, SVt_NULL is borrowed for HE arenas;
830PL_body_roots[SVt_NULL] is filled by S_more_he, but the
831bodies_by_type[SVt_NULL] slot is not used, as the table is not
832available in hv.c,
833
834PTEs also use arenas, but are never seen in Perl_sv_upgrade.
835Nonetheless, they get their own slot in bodies_by_type[SVt_NULL], so
836they can just use the same allocation semantics. At first, PTEs were
837also overloaded to a non-body sv-type, but this yielded hard-to-find
838malloc bugs, so was simplified by claiming a new slot. This choice
839has no consequence at this time.
840
29489e7c
DM
841*/
842
bd81e77b 843struct body_details {
d2a0f284 844 size_t body_size; /* Size to allocate */
bd81e77b
NC
845 size_t copy; /* Size of structure to copy (may be shorter) */
846 size_t offset;
d2a0f284 847 bool cant_upgrade; /* Cannot upgrade this type */
bd81e77b
NC
848 bool zero_nv; /* zero the NV when upgrading from this */
849 bool arena; /* Allocated from an arena */
d2a0f284 850 size_t arena_size; /* Size of arena to allocate */
bd81e77b 851};
29489e7c 852
bd81e77b
NC
853#define HADNV FALSE
854#define NONV TRUE
29489e7c 855
d2a0f284 856
bd81e77b
NC
857#ifdef PURIFY
858/* With -DPURFIY we allocate everything directly, and don't use arenas.
859 This seems a rather elegant way to simplify some of the code below. */
860#define HASARENA FALSE
861#else
862#define HASARENA TRUE
863#endif
864#define NOARENA FALSE
29489e7c 865
d2a0f284
JC
866/* Size the arenas to exactly fit a given number of bodies. A count
867 of 0 fits the max number bodies into a PERL_ARENA_SIZE.block,
868 simplifying the default. If count > 0, the arena is sized to fit
869 only that many bodies, allowing arenas to be used for large, rare
870 bodies (XPVFM, XPVIO) without undue waste. The arena size is
871 limited by PERL_ARENA_SIZE, so we can safely oversize the
872 declarations.
873 */
874#define FIT_ARENA(count, body_size) \
875 (!count || count * body_size > PERL_ARENA_SIZE) \
876 ? (int)(PERL_ARENA_SIZE / body_size) * body_size : count * body_size
877
bd81e77b 878/* A macro to work out the offset needed to subtract from a pointer to (say)
29489e7c 879
bd81e77b
NC
880typedef struct {
881 STRLEN xpv_cur;
882 STRLEN xpv_len;
883} xpv_allocated;
29489e7c 884
bd81e77b 885to make its members accessible via a pointer to (say)
29489e7c 886
bd81e77b
NC
887struct xpv {
888 NV xnv_nv;
889 STRLEN xpv_cur;
890 STRLEN xpv_len;
891};
29489e7c 892
bd81e77b 893*/
29489e7c 894
bd81e77b
NC
895#define relative_STRUCT_OFFSET(longer, shorter, member) \
896 (STRUCT_OFFSET(shorter, member) - STRUCT_OFFSET(longer, member))
29489e7c 897
bd81e77b
NC
898/* Calculate the length to copy. Specifically work out the length less any
899 final padding the compiler needed to add. See the comment in sv_upgrade
900 for why copying the padding proved to be a bug. */
29489e7c 901
bd81e77b
NC
902#define copy_length(type, last_member) \
903 STRUCT_OFFSET(type, last_member) \
904 + sizeof (((type*)SvANY((SV*)0))->last_member)
29489e7c 905
bd81e77b 906static const struct body_details bodies_by_type[] = {
d2a0f284
JC
907 { sizeof(HE), 0, 0, FALSE, NONV, NOARENA, FIT_ARENA(0, sizeof(HE)) },
908
909 /* IVs are in the head, so the allocation size is 0.
910 However, the slot is overloaded for PTEs. */
911 { sizeof(struct ptr_tbl_ent), /* This is used for PTEs. */
912 sizeof(IV), /* This is used to copy out the IV body. */
913 STRUCT_OFFSET(XPVIV, xiv_iv), FALSE, NONV,
914 NOARENA /* IVS don't need an arena */,
915 /* But PTEs need to know the size of their arena */
916 FIT_ARENA(0, sizeof(struct ptr_tbl_ent))
917 },
918
bd81e77b 919 /* 8 bytes on most ILP32 with IEEE doubles */
d2a0f284
JC
920 { sizeof(NV), sizeof(NV), 0, FALSE, HADNV, HASARENA,
921 FIT_ARENA(0, sizeof(NV)) },
922
923 /* RVs are in the head now. */
924 { 0, 0, 0, FALSE, NONV, NOARENA, 0 },
925
bd81e77b 926 /* 8 bytes on most ILP32 with IEEE doubles */
d2a0f284
JC
927 { sizeof(xpv_allocated),
928 copy_length(XPV, xpv_len)
929 - relative_STRUCT_OFFSET(xpv_allocated, XPV, xpv_cur),
930 + relative_STRUCT_OFFSET(xpv_allocated, XPV, xpv_cur),
931 FALSE, NONV, HASARENA, FIT_ARENA(0, sizeof(xpv_allocated)) },
932
bd81e77b 933 /* 12 */
d2a0f284
JC
934 { sizeof(xpviv_allocated),
935 copy_length(XPVIV, xiv_u)
936 - relative_STRUCT_OFFSET(xpviv_allocated, XPVIV, xpv_cur),
937 + relative_STRUCT_OFFSET(xpviv_allocated, XPVIV, xpv_cur),
938 FALSE, NONV, HASARENA, FIT_ARENA(0, sizeof(xpviv_allocated)) },
939
bd81e77b 940 /* 20 */
d2a0f284
JC
941 { sizeof(XPVNV), copy_length(XPVNV, xiv_u), 0, FALSE, HADNV,
942 HASARENA, FIT_ARENA(0, sizeof(XPVNV)) },
943
bd81e77b 944 /* 28 */
d2a0f284
JC
945 { sizeof(XPVMG), copy_length(XPVMG, xmg_stash), 0, FALSE, HADNV,
946 HASARENA, FIT_ARENA(0, sizeof(XPVMG)) },
947
bd81e77b 948 /* 36 */
d2a0f284
JC
949 { sizeof(XPVBM), sizeof(XPVBM), 0, TRUE, HADNV,
950 HASARENA, FIT_ARENA(0, sizeof(XPVBM)) },
951
bd81e77b 952 /* 48 */
d2a0f284
JC
953 { sizeof(XPVGV), sizeof(XPVGV), 0, TRUE, HADNV,
954 HASARENA, FIT_ARENA(0, sizeof(XPVGV)) },
955
bd81e77b 956 /* 64 */
d2a0f284
JC
957 { sizeof(XPVLV), sizeof(XPVLV), 0, TRUE, HADNV,
958 HASARENA, FIT_ARENA(0, sizeof(XPVLV)) },
959
960 { sizeof(xpvav_allocated),
961 copy_length(XPVAV, xmg_stash)
962 - relative_STRUCT_OFFSET(xpvav_allocated, XPVAV, xav_fill),
963 + relative_STRUCT_OFFSET(xpvav_allocated, XPVAV, xav_fill),
964 TRUE, HADNV, HASARENA, FIT_ARENA(0, sizeof(xpvav_allocated)) },
965
966 { sizeof(xpvhv_allocated),
967 copy_length(XPVHV, xmg_stash)
968 - relative_STRUCT_OFFSET(xpvhv_allocated, XPVHV, xhv_fill),
969 + relative_STRUCT_OFFSET(xpvhv_allocated, XPVHV, xhv_fill),
970 TRUE, HADNV, HASARENA, FIT_ARENA(0, sizeof(xpvhv_allocated)) },
971
bd81e77b 972 /* 76 */
d2a0f284
JC
973 { sizeof(XPVCV), sizeof(XPVCV), 0, TRUE, HADNV,
974 HASARENA, FIT_ARENA(0, sizeof(XPVCV)) },
975
3038937b
NC
976 { sizeof(xpvfm_allocated),
977 sizeof(xpvfm_allocated)
978 - relative_STRUCT_OFFSET(xpvfm_allocated, XPVFM, xpv_cur),
979 + relative_STRUCT_OFFSET(xpvfm_allocated, XPVFM, xpv_cur),
980 TRUE, HADNV, 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
IZ
1713
1714/* Actually, ISO C leaves conversion of UV to IV undefined, but
1715 until proven guilty, assume that things are not that bad... */
1716
645c22ef
DM
1717/*
1718 NV_PRESERVES_UV:
1719
1720 As 64 bit platforms often have an NV that doesn't preserve all bits of
28e5dec8
JH
1721 an IV (an assumption perl has been based on to date) it becomes necessary
1722 to remove the assumption that the NV always carries enough precision to
1723 recreate the IV whenever needed, and that the NV is the canonical form.
1724 Instead, IV/UV and NV need to be given equal rights. So as to not lose
645c22ef 1725 precision as a side effect of conversion (which would lead to insanity
28e5dec8
JH
1726 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1727 1) to distinguish between IV/UV/NV slots that have cached a valid
1728 conversion where precision was lost and IV/UV/NV slots that have a
1729 valid conversion which has lost no precision
645c22ef 1730 2) to ensure that if a numeric conversion to one form is requested that
28e5dec8
JH
1731 would lose precision, the precise conversion (or differently
1732 imprecise conversion) is also performed and cached, to prevent
1733 requests for different numeric formats on the same SV causing
1734 lossy conversion chains. (lossless conversion chains are perfectly
1735 acceptable (still))
1736
1737
1738 flags are used:
1739 SvIOKp is true if the IV slot contains a valid value
1740 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1741 SvNOKp is true if the NV slot contains a valid value
1742 SvNOK is true only if the NV value is accurate
1743
1744 so
645c22ef 1745 while converting from PV to NV, check to see if converting that NV to an
28e5dec8
JH
1746 IV(or UV) would lose accuracy over a direct conversion from PV to
1747 IV(or UV). If it would, cache both conversions, return NV, but mark
1748 SV as IOK NOKp (ie not NOK).
1749
645c22ef 1750 While converting from PV to IV, check to see if converting that IV to an
28e5dec8
JH
1751 NV would lose accuracy over a direct conversion from PV to NV. If it
1752 would, cache both conversions, flag similarly.
1753
1754 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1755 correctly because if IV & NV were set NV *always* overruled.
645c22ef
DM
1756 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1757 changes - now IV and NV together means that the two are interchangeable:
28e5dec8 1758 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
d460ef45 1759
645c22ef
DM
1760 The benefit of this is that operations such as pp_add know that if
1761 SvIOK is true for both left and right operands, then integer addition
1762 can be used instead of floating point (for cases where the result won't
1763 overflow). Before, floating point was always used, which could lead to
28e5dec8
JH
1764 loss of precision compared with integer addition.
1765
1766 * making IV and NV equal status should make maths accurate on 64 bit
1767 platforms
1768 * may speed up maths somewhat if pp_add and friends start to use
645c22ef 1769 integers when possible instead of fp. (Hopefully the overhead in
28e5dec8
JH
1770 looking for SvIOK and checking for overflow will not outweigh the
1771 fp to integer speedup)
1772 * will slow down integer operations (callers of SvIV) on "inaccurate"
1773 values, as the change from SvIOK to SvIOKp will cause a call into
1774 sv_2iv each time rather than a macro access direct to the IV slot
1775 * should speed up number->string conversion on integers as IV is
645c22ef 1776 favoured when IV and NV are equally accurate
28e5dec8
JH
1777
1778 ####################################################################
645c22ef
DM
1779 You had better be using SvIOK_notUV if you want an IV for arithmetic:
1780 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1781 On the other hand, SvUOK is true iff UV.
28e5dec8
JH
1782 ####################################################################
1783
645c22ef 1784 Your mileage will vary depending your CPU's relative fp to integer
28e5dec8
JH
1785 performance ratio.
1786*/
1787
1788#ifndef NV_PRESERVES_UV
645c22ef
DM
1789# define IS_NUMBER_UNDERFLOW_IV 1
1790# define IS_NUMBER_UNDERFLOW_UV 2
1791# define IS_NUMBER_IV_AND_UV 2
1792# define IS_NUMBER_OVERFLOW_IV 4
1793# define IS_NUMBER_OVERFLOW_UV 5
1794
1795/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
28e5dec8
JH
1796
1797/* For sv_2nv these three cases are "SvNOK and don't bother casting" */
1798STATIC int
645c22ef 1799S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
28e5dec8 1800{
97aff369 1801 dVAR;
3f7c398e 1802 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
1803 if (SvNVX(sv) < (NV)IV_MIN) {
1804 (void)SvIOKp_on(sv);
1805 (void)SvNOK_on(sv);
45977657 1806 SvIV_set(sv, IV_MIN);
28e5dec8
JH
1807 return IS_NUMBER_UNDERFLOW_IV;
1808 }
1809 if (SvNVX(sv) > (NV)UV_MAX) {
1810 (void)SvIOKp_on(sv);
1811 (void)SvNOK_on(sv);
1812 SvIsUV_on(sv);
607fa7f2 1813 SvUV_set(sv, UV_MAX);
28e5dec8
JH
1814 return IS_NUMBER_OVERFLOW_UV;
1815 }
c2988b20
NC
1816 (void)SvIOKp_on(sv);
1817 (void)SvNOK_on(sv);
1818 /* Can't use strtol etc to convert this string. (See truth table in
1819 sv_2iv */
1820 if (SvNVX(sv) <= (UV)IV_MAX) {
45977657 1821 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
1822 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
1823 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
1824 } else {
1825 /* Integer is imprecise. NOK, IOKp */
1826 }
1827 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
1828 }
1829 SvIsUV_on(sv);
607fa7f2 1830 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
1831 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
1832 if (SvUVX(sv) == UV_MAX) {
1833 /* As we know that NVs don't preserve UVs, UV_MAX cannot
1834 possibly be preserved by NV. Hence, it must be overflow.
1835 NOK, IOKp */
1836 return IS_NUMBER_OVERFLOW_UV;
1837 }
1838 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
1839 } else {
1840 /* Integer is imprecise. NOK, IOKp */
28e5dec8 1841 }
c2988b20 1842 return IS_NUMBER_OVERFLOW_IV;
28e5dec8 1843}
645c22ef
DM
1844#endif /* !NV_PRESERVES_UV*/
1845
af359546
NC
1846STATIC bool
1847S_sv_2iuv_common(pTHX_ SV *sv) {
97aff369 1848 dVAR;
af359546 1849 if (SvNOKp(sv)) {
28e5dec8
JH
1850 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
1851 * without also getting a cached IV/UV from it at the same time
1852 * (ie PV->NV conversion should detect loss of accuracy and cache
af359546
NC
1853 * IV or UV at same time to avoid this. */
1854 /* IV-over-UV optimisation - choose to cache IV if possible */
25da4f38
IZ
1855
1856 if (SvTYPE(sv) == SVt_NV)
1857 sv_upgrade(sv, SVt_PVNV);
1858
28e5dec8
JH
1859 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
1860 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
1861 certainly cast into the IV range at IV_MAX, whereas the correct
1862 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
1863 cases go to UV */
1864 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 1865 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
1866 if (SvNVX(sv) == (NV) SvIVX(sv)
1867#ifndef NV_PRESERVES_UV
1868 && (((UV)1 << NV_PRESERVES_UV_BITS) >
1869 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
1870 /* Don't flag it as "accurately an integer" if the number
1871 came from a (by definition imprecise) NV operation, and
1872 we're outside the range of NV integer precision */
1873#endif
1874 ) {
1875 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
1876 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 1877 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
1878 PTR2UV(sv),
1879 SvNVX(sv),
1880 SvIVX(sv)));
1881
1882 } else {
1883 /* IV not precise. No need to convert from PV, as NV
1884 conversion would already have cached IV if it detected
1885 that PV->IV would be better than PV->NV->IV
1886 flags already correct - don't set public IOK. */
1887 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 1888 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
1889 PTR2UV(sv),
1890 SvNVX(sv),
1891 SvIVX(sv)));
1892 }
1893 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
1894 but the cast (NV)IV_MIN rounds to a the value less (more
1895 negative) than IV_MIN which happens to be equal to SvNVX ??
1896 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
1897 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
1898 (NV)UVX == NVX are both true, but the values differ. :-(
1899 Hopefully for 2s complement IV_MIN is something like
1900 0x8000000000000000 which will be exact. NWC */
d460ef45 1901 }
25da4f38 1902 else {
607fa7f2 1903 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
1904 if (
1905 (SvNVX(sv) == (NV) SvUVX(sv))
1906#ifndef NV_PRESERVES_UV
1907 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
1908 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
1909 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
1910 /* Don't flag it as "accurately an integer" if the number
1911 came from a (by definition imprecise) NV operation, and
1912 we're outside the range of NV integer precision */
1913#endif
1914 )
1915 SvIOK_on(sv);
25da4f38 1916 SvIsUV_on(sv);
1c846c1f 1917 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 1918 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 1919 PTR2UV(sv),
57def98f
JH
1920 SvUVX(sv),
1921 SvUVX(sv)));
25da4f38 1922 }
748a9306
LW
1923 }
1924 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 1925 UV value;
504618e9 1926 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
af359546 1927 /* We want to avoid a possible problem when we cache an IV/ a UV which
25da4f38 1928 may be later translated to an NV, and the resulting NV is not
c2988b20
NC
1929 the same as the direct translation of the initial string
1930 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
1931 be careful to ensure that the value with the .456 is around if the
1932 NV value is requested in the future).
1c846c1f 1933
af359546 1934 This means that if we cache such an IV/a UV, we need to cache the
25da4f38 1935 NV as well. Moreover, we trade speed for space, and do not
28e5dec8 1936 cache the NV if we are sure it's not needed.
25da4f38 1937 */
16b7a9a4 1938
c2988b20
NC
1939 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
1940 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
1941 == IS_NUMBER_IN_UV) {
5e045b90 1942 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8
JH
1943 if (SvTYPE(sv) < SVt_PVIV)
1944 sv_upgrade(sv, SVt_PVIV);
f7bbb42a 1945 (void)SvIOK_on(sv);
c2988b20
NC
1946 } else if (SvTYPE(sv) < SVt_PVNV)
1947 sv_upgrade(sv, SVt_PVNV);
28e5dec8 1948
f2524eef 1949 /* If NVs preserve UVs then we only use the UV value if we know that
c2988b20
NC
1950 we aren't going to call atof() below. If NVs don't preserve UVs
1951 then the value returned may have more precision than atof() will
1952 return, even though value isn't perfectly accurate. */
1953 if ((numtype & (IS_NUMBER_IN_UV
1954#ifdef NV_PRESERVES_UV
1955 | IS_NUMBER_NOT_INT
1956#endif
1957 )) == IS_NUMBER_IN_UV) {
1958 /* This won't turn off the public IOK flag if it was set above */
1959 (void)SvIOKp_on(sv);
1960
1961 if (!(numtype & IS_NUMBER_NEG)) {
1962 /* positive */;
1963 if (value <= (UV)IV_MAX) {
45977657 1964 SvIV_set(sv, (IV)value);
c2988b20 1965 } else {
af359546 1966 /* it didn't overflow, and it was positive. */
607fa7f2 1967 SvUV_set(sv, value);
c2988b20
NC
1968 SvIsUV_on(sv);
1969 }
1970 } else {
1971 /* 2s complement assumption */
1972 if (value <= (UV)IV_MIN) {
45977657 1973 SvIV_set(sv, -(IV)value);
c2988b20
NC
1974 } else {
1975 /* Too negative for an IV. This is a double upgrade, but
d1be9408 1976 I'm assuming it will be rare. */
c2988b20
NC
1977 if (SvTYPE(sv) < SVt_PVNV)
1978 sv_upgrade(sv, SVt_PVNV);
1979 SvNOK_on(sv);
1980 SvIOK_off(sv);
1981 SvIOKp_on(sv);
9d6ce603 1982 SvNV_set(sv, -(NV)value);
45977657 1983 SvIV_set(sv, IV_MIN);
c2988b20
NC
1984 }
1985 }
1986 }
1987 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
1988 will be in the previous block to set the IV slot, and the next
1989 block to set the NV slot. So no else here. */
1990
1991 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
1992 != IS_NUMBER_IN_UV) {
1993 /* It wasn't an (integer that doesn't overflow the UV). */
3f7c398e 1994 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8 1995
c2988b20
NC
1996 if (! numtype && ckWARN(WARN_NUMERIC))
1997 not_a_number(sv);
28e5dec8 1998
65202027 1999#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2000 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2001 PTR2UV(sv), SvNVX(sv)));
65202027 2002#else
1779d84d 2003 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
c2988b20 2004 PTR2UV(sv), SvNVX(sv)));
65202027 2005#endif
28e5dec8 2006
28e5dec8 2007#ifdef NV_PRESERVES_UV
af359546
NC
2008 (void)SvIOKp_on(sv);
2009 (void)SvNOK_on(sv);
2010 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2011 SvIV_set(sv, I_V(SvNVX(sv)));
2012 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2013 SvIOK_on(sv);
2014 } else {
bb263b4e 2015 /*EMPTY*/; /* Integer is imprecise. NOK, IOKp */
af359546
NC
2016 }
2017 /* UV will not work better than IV */
2018 } else {
2019 if (SvNVX(sv) > (NV)UV_MAX) {
2020 SvIsUV_on(sv);
2021 /* Integer is inaccurate. NOK, IOKp, is UV */
2022 SvUV_set(sv, UV_MAX);
af359546
NC
2023 } else {
2024 SvUV_set(sv, U_V(SvNVX(sv)));
2025 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2026 NV preservse UV so can do correct comparison. */
2027 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2028 SvIOK_on(sv);
af359546 2029 } else {
bb263b4e 2030 /*EMPTY*/; /* Integer is imprecise. NOK, IOKp, is UV */
af359546
NC
2031 }
2032 }
4b0c9573 2033 SvIsUV_on(sv);
af359546 2034 }
28e5dec8 2035#else /* NV_PRESERVES_UV */
c2988b20
NC
2036 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2037 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
af359546 2038 /* The IV/UV slot will have been set from value returned by
c2988b20
NC
2039 grok_number above. The NV slot has just been set using
2040 Atof. */
560b0c46 2041 SvNOK_on(sv);
c2988b20
NC
2042 assert (SvIOKp(sv));
2043 } else {
2044 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2045 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2046 /* Small enough to preserve all bits. */
2047 (void)SvIOKp_on(sv);
2048 SvNOK_on(sv);
45977657 2049 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2050 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2051 SvIOK_on(sv);
2052 /* Assumption: first non-preserved integer is < IV_MAX,
2053 this NV is in the preserved range, therefore: */
2054 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2055 < (UV)IV_MAX)) {
32fdb065 2056 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
2057 }
2058 } else {
2059 /* IN_UV NOT_INT
2060 0 0 already failed to read UV.
2061 0 1 already failed to read UV.
2062 1 0 you won't get here in this case. IV/UV
2063 slot set, public IOK, Atof() unneeded.
2064 1 1 already read UV.
2065 so there's no point in sv_2iuv_non_preserve() attempting
2066 to use atol, strtol, strtoul etc. */
40a17c4c 2067 sv_2iuv_non_preserve (sv, numtype);
c2988b20
NC
2068 }
2069 }
28e5dec8 2070#endif /* NV_PRESERVES_UV */
25da4f38 2071 }
af359546
NC
2072 }
2073 else {
2074 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2075 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2076 report_uninit(sv);
2077 }
25da4f38
IZ
2078 if (SvTYPE(sv) < SVt_IV)
2079 /* Typically the caller expects that sv_any is not NULL now. */
2080 sv_upgrade(sv, SVt_IV);
af359546
NC
2081 /* Return 0 from the caller. */
2082 return TRUE;
2083 }
2084 return FALSE;
2085}
2086
2087/*
2088=for apidoc sv_2iv_flags
2089
2090Return the integer value of an SV, doing any necessary string
2091conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2092Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2093
2094=cut
2095*/
2096
2097IV
2098Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags)
2099{
97aff369 2100 dVAR;
af359546 2101 if (!sv)
a0d0e21e 2102 return 0;
af359546
NC
2103 if (SvGMAGICAL(sv)) {
2104 if (flags & SV_GMAGIC)
2105 mg_get(sv);
2106 if (SvIOKp(sv))
2107 return SvIVX(sv);
2108 if (SvNOKp(sv)) {
2109 return I_V(SvNVX(sv));
2110 }
71c558c3
NC
2111 if (SvPOKp(sv) && SvLEN(sv)) {
2112 UV value;
2113 const int numtype
2114 = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2115
2116 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2117 == IS_NUMBER_IN_UV) {
2118 /* It's definitely an integer */
2119 if (numtype & IS_NUMBER_NEG) {
2120 if (value < (UV)IV_MIN)
2121 return -(IV)value;
2122 } else {
2123 if (value < (UV)IV_MAX)
2124 return (IV)value;
2125 }
2126 }
2127 if (!numtype) {
2128 if (ckWARN(WARN_NUMERIC))
2129 not_a_number(sv);
2130 }
2131 return I_V(Atof(SvPVX_const(sv)));
2132 }
1c7ff15e
NC
2133 if (SvROK(sv)) {
2134 goto return_rok;
af359546 2135 }
1c7ff15e
NC
2136 assert(SvTYPE(sv) >= SVt_PVMG);
2137 /* This falls through to the report_uninit inside S_sv_2iuv_common. */
4cb1ec55 2138 } else if (SvTHINKFIRST(sv)) {
af359546 2139 if (SvROK(sv)) {
1c7ff15e 2140 return_rok:
af359546
NC
2141 if (SvAMAGIC(sv)) {
2142 SV * const tmpstr=AMG_CALLun(sv,numer);
2143 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2144 return SvIV(tmpstr);
2145 }
2146 }
2147 return PTR2IV(SvRV(sv));
2148 }
2149 if (SvIsCOW(sv)) {
2150 sv_force_normal_flags(sv, 0);
2151 }
2152 if (SvREADONLY(sv) && !SvOK(sv)) {
2153 if (ckWARN(WARN_UNINITIALIZED))
2154 report_uninit(sv);
2155 return 0;
2156 }
2157 }
2158 if (!SvIOKp(sv)) {
2159 if (S_sv_2iuv_common(aTHX_ sv))
2160 return 0;
79072805 2161 }
1d7c1841
GS
2162 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2163 PTR2UV(sv),SvIVX(sv)));
25da4f38 2164 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
2165}
2166
645c22ef 2167/*
891f9566 2168=for apidoc sv_2uv_flags
645c22ef
DM
2169
2170Return the unsigned integer value of an SV, doing any necessary string
891f9566
YST
2171conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2172Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
645c22ef
DM
2173
2174=cut
2175*/
2176
ff68c719 2177UV
891f9566 2178Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags)
ff68c719 2179{
97aff369 2180 dVAR;
ff68c719
PP
2181 if (!sv)
2182 return 0;
2183 if (SvGMAGICAL(sv)) {
891f9566
YST
2184 if (flags & SV_GMAGIC)
2185 mg_get(sv);
ff68c719
PP
2186 if (SvIOKp(sv))
2187 return SvUVX(sv);
2188 if (SvNOKp(sv))
2189 return U_V(SvNVX(sv));
71c558c3
NC
2190 if (SvPOKp(sv) && SvLEN(sv)) {
2191 UV value;
2192 const int numtype
2193 = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2194
2195 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2196 == IS_NUMBER_IN_UV) {
2197 /* It's definitely an integer */
2198 if (!(numtype & IS_NUMBER_NEG))
2199 return value;
2200 }
2201 if (!numtype) {
2202 if (ckWARN(WARN_NUMERIC))
2203 not_a_number(sv);
2204 }
2205 return U_V(Atof(SvPVX_const(sv)));
2206 }
1c7ff15e
NC
2207 if (SvROK(sv)) {
2208 goto return_rok;
3fe9a6f1 2209 }
1c7ff15e
NC
2210 assert(SvTYPE(sv) >= SVt_PVMG);
2211 /* This falls through to the report_uninit inside S_sv_2iuv_common. */
4cb1ec55 2212 } else if (SvTHINKFIRST(sv)) {
ff68c719 2213 if (SvROK(sv)) {
1c7ff15e 2214 return_rok:
deb46114
NC
2215 if (SvAMAGIC(sv)) {
2216 SV *const tmpstr = AMG_CALLun(sv,numer);
2217 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2218 return SvUV(tmpstr);
2219 }
2220 }
2221 return PTR2UV(SvRV(sv));
ff68c719 2222 }
765f542d
NC
2223 if (SvIsCOW(sv)) {
2224 sv_force_normal_flags(sv, 0);
8a818333 2225 }
0336b60e 2226 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2227 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2228 report_uninit(sv);
ff68c719
PP
2229 return 0;
2230 }
2231 }
af359546
NC
2232 if (!SvIOKp(sv)) {
2233 if (S_sv_2iuv_common(aTHX_ sv))
2234 return 0;
ff68c719 2235 }
25da4f38 2236
1d7c1841
GS
2237 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2238 PTR2UV(sv),SvUVX(sv)));
25da4f38 2239 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719
PP
2240}
2241
645c22ef
DM
2242/*
2243=for apidoc sv_2nv
2244
2245Return the num value of an SV, doing any necessary string or integer
2246conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
2247macros.
2248
2249=cut
2250*/
2251
65202027 2252NV
864dbfa3 2253Perl_sv_2nv(pTHX_ register SV *sv)
79072805 2254{
97aff369 2255 dVAR;
79072805
LW
2256 if (!sv)
2257 return 0.0;
8990e307 2258 if (SvGMAGICAL(sv)) {
463ee0b2
LW
2259 mg_get(sv);
2260 if (SvNOKp(sv))
2261 return SvNVX(sv);
0aa395f8 2262 if ((SvPOKp(sv) && SvLEN(sv)) && !SvIOKp(sv)) {
041457d9 2263 if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) &&
504618e9 2264 !grok_number(SvPVX_const(sv), SvCUR(sv), NULL))
a0d0e21e 2265 not_a_number(sv);
3f7c398e 2266 return Atof(SvPVX_const(sv));
a0d0e21e 2267 }
25da4f38 2268 if (SvIOKp(sv)) {
1c846c1f 2269 if (SvIsUV(sv))
65202027 2270 return (NV)SvUVX(sv);
25da4f38 2271 else
65202027 2272 return (NV)SvIVX(sv);
47a72cb8
NC
2273 }
2274 if (SvROK(sv)) {
2275 goto return_rok;
2276 }
2277 assert(SvTYPE(sv) >= SVt_PVMG);
2278 /* This falls through to the report_uninit near the end of the
2279 function. */
2280 } else if (SvTHINKFIRST(sv)) {
a0d0e21e 2281 if (SvROK(sv)) {
47a72cb8 2282 return_rok:
deb46114
NC
2283 if (SvAMAGIC(sv)) {
2284 SV *const tmpstr = AMG_CALLun(sv,numer);
2285 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2286 return SvNV(tmpstr);
2287 }
2288 }
2289 return PTR2NV(SvRV(sv));
a0d0e21e 2290 }
765f542d
NC
2291 if (SvIsCOW(sv)) {
2292 sv_force_normal_flags(sv, 0);
8a818333 2293 }
0336b60e 2294 if (SvREADONLY(sv) && !SvOK(sv)) {
599cee73 2295 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2296 report_uninit(sv);
ed6116ce
LW
2297 return 0.0;
2298 }
79072805
LW
2299 }
2300 if (SvTYPE(sv) < SVt_NV) {
7e25a7e9
NC
2301 /* The logic to use SVt_PVNV if necessary is in sv_upgrade. */
2302 sv_upgrade(sv, SVt_NV);
906f284f 2303#ifdef USE_LONG_DOUBLE
097ee67d 2304 DEBUG_c({
f93f4e46 2305 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2306 PerlIO_printf(Perl_debug_log,
2307 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2308 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2309 RESTORE_NUMERIC_LOCAL();
2310 });
65202027 2311#else
572bbb43 2312 DEBUG_c({
f93f4e46 2313 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2314 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
1d7c1841 2315 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2316 RESTORE_NUMERIC_LOCAL();
2317 });
572bbb43 2318#endif
79072805
LW
2319 }
2320 else if (SvTYPE(sv) < SVt_PVNV)
2321 sv_upgrade(sv, SVt_PVNV);
59d8ce62
NC
2322 if (SvNOKp(sv)) {
2323 return SvNVX(sv);
61604483 2324 }
59d8ce62 2325 if (SvIOKp(sv)) {
9d6ce603 2326 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
28e5dec8
JH
2327#ifdef NV_PRESERVES_UV
2328 SvNOK_on(sv);
2329#else
2330 /* Only set the public NV OK flag if this NV preserves the IV */
2331 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2332 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2333 : (SvIVX(sv) == I_V(SvNVX(sv))))
2334 SvNOK_on(sv);
2335 else
2336 SvNOKp_on(sv);
2337#endif
93a17b20 2338 }
748a9306 2339 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2340 UV value;
3f7c398e 2341 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
041457d9 2342 if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC))
a0d0e21e 2343 not_a_number(sv);
28e5dec8 2344#ifdef NV_PRESERVES_UV
c2988b20
NC
2345 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2346 == IS_NUMBER_IN_UV) {
5e045b90 2347 /* It's definitely an integer */
9d6ce603 2348 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
c2988b20 2349 } else
3f7c398e 2350 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
2351 SvNOK_on(sv);
2352#else
3f7c398e 2353 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
2354 /* Only set the public NV OK flag if this NV preserves the value in
2355 the PV at least as well as an IV/UV would.
2356 Not sure how to do this 100% reliably. */
2357 /* if that shift count is out of range then Configure's test is
2358 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2359 UV_BITS */
2360 if (((UV)1 << NV_PRESERVES_UV_BITS) >
c2988b20 2361 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
28e5dec8 2362 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
c2988b20
NC
2363 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2364 /* Can't use strtol etc to convert this string, so don't try.
2365 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2366 SvNOK_on(sv);
2367 } else {
2368 /* value has been set. It may not be precise. */
2369 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
2370 /* 2s complement assumption for (UV)IV_MIN */
2371 SvNOK_on(sv); /* Integer is too negative. */
2372 } else {
2373 SvNOKp_on(sv);
2374 SvIOKp_on(sv);
6fa402ec 2375
c2988b20 2376 if (numtype & IS_NUMBER_NEG) {
45977657 2377 SvIV_set(sv, -(IV)value);
c2988b20 2378 } else if (value <= (UV)IV_MAX) {
45977657 2379 SvIV_set(sv, (IV)value);
c2988b20 2380 } else {
607fa7f2 2381 SvUV_set(sv, value);
c2988b20
NC
2382 SvIsUV_on(sv);
2383 }
2384
2385 if (numtype & IS_NUMBER_NOT_INT) {
2386 /* I believe that even if the original PV had decimals,
2387 they are lost beyond the limit of the FP precision.
2388 However, neither is canonical, so both only get p
2389 flags. NWC, 2000/11/25 */
2390 /* Both already have p flags, so do nothing */
2391 } else {
66a1b24b 2392 const NV nv = SvNVX(sv);
c2988b20
NC
2393 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2394 if (SvIVX(sv) == I_V(nv)) {
2395 SvNOK_on(sv);
c2988b20 2396 } else {
c2988b20
NC
2397 /* It had no "." so it must be integer. */
2398 }
00b6aa41 2399 SvIOK_on(sv);
c2988b20
NC
2400 } else {
2401 /* between IV_MAX and NV(UV_MAX).
2402 Could be slightly > UV_MAX */
6fa402ec 2403
c2988b20
NC
2404 if (numtype & IS_NUMBER_NOT_INT) {
2405 /* UV and NV both imprecise. */
2406 } else {
66a1b24b 2407 const UV nv_as_uv = U_V(nv);
c2988b20
NC
2408
2409 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2410 SvNOK_on(sv);
c2988b20 2411 }
00b6aa41 2412 SvIOK_on(sv);
c2988b20
NC
2413 }
2414 }
2415 }
2416 }
2417 }
28e5dec8 2418#endif /* NV_PRESERVES_UV */
93a17b20 2419 }
79072805 2420 else {
041457d9 2421 if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED))
29489e7c 2422 report_uninit(sv);
7e25a7e9
NC
2423 assert (SvTYPE(sv) >= SVt_NV);
2424 /* Typically the caller expects that sv_any is not NULL now. */
2425 /* XXX Ilya implies that this is a bug in callers that assume this
2426 and ideally should be fixed. */
a0d0e21e 2427 return 0.0;
79072805 2428 }
572bbb43 2429#if defined(USE_LONG_DOUBLE)
097ee67d 2430 DEBUG_c({
f93f4e46 2431 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2432 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2433 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2434 RESTORE_NUMERIC_LOCAL();
2435 });
65202027 2436#else
572bbb43 2437 DEBUG_c({
f93f4e46 2438 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2439 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
1d7c1841 2440 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2441 RESTORE_NUMERIC_LOCAL();
2442 });
572bbb43 2443#endif
463ee0b2 2444 return SvNVX(sv);
79072805
LW
2445}
2446
645c22ef
DM
2447/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2448 * UV as a string towards the end of buf, and return pointers to start and
2449 * end of it.
2450 *
2451 * We assume that buf is at least TYPE_CHARS(UV) long.
2452 */
2453
864dbfa3 2454static char *
aec46f14 2455S_uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
25da4f38 2456{
25da4f38 2457 char *ptr = buf + TYPE_CHARS(UV);
823a54a3 2458 char * const ebuf = ptr;
25da4f38 2459 int sign;
25da4f38
IZ
2460
2461 if (is_uv)
2462 sign = 0;
2463 else if (iv >= 0) {
2464 uv = iv;
2465 sign = 0;
2466 } else {
2467 uv = -iv;
2468 sign = 1;
2469 }
2470 do {
eb160463 2471 *--ptr = '0' + (char)(uv % 10);
25da4f38
IZ
2472 } while (uv /= 10);
2473 if (sign)
2474 *--ptr = '-';
2475 *peob = ebuf;
2476 return ptr;
2477}
2478
9af30d34
NC
2479/* stringify_regexp(): private routine for use by sv_2pv_flags(): converts
2480 * a regexp to its stringified form.
2481 */
2482
2483static char *
2484S_stringify_regexp(pTHX_ SV *sv, MAGIC *mg, STRLEN *lp) {
97aff369 2485 dVAR;
00b6aa41 2486 const regexp * const re = (regexp *)mg->mg_obj;
9af30d34
NC
2487
2488 if (!mg->mg_ptr) {
2489 const char *fptr = "msix";
2490 char reflags[6];
2491 char ch;
2492 int left = 0;
2493 int right = 4;
00b6aa41 2494 bool need_newline = 0;
9af30d34
NC
2495 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
2496
2497 while((ch = *fptr++)) {
2498 if(reganch & 1) {
2499 reflags[left++] = ch;
2500 }
2501 else {
2502 reflags[right--] = ch;
2503 }
2504 reganch >>= 1;
2505 }
2506 if(left != 4) {
2507 reflags[left] = '-';
2508 left = 5;
2509 }
2510
2511 mg->mg_len = re->prelen + 4 + left;
2512 /*
2513 * If /x was used, we have to worry about a regex ending with a
2514 * comment later being embedded within another regex. If so, we don't
2515 * want this regex's "commentization" to leak out to the right part of
2516 * the enclosing regex, we must cap it with a newline.
2517 *
2518 * So, if /x was used, we scan backwards from the end of the regex. If
2519 * we find a '#' before we find a newline, we need to add a newline
2520 * ourself. If we find a '\n' first (or if we don't find '#' or '\n'),
2521 * we don't need to add anything. -jfriedl
2522 */
2523 if (PMf_EXTENDED & re->reganch) {
2524 const char *endptr = re->precomp + re->prelen;
2525 while (endptr >= re->precomp) {
2526 const char c = *(endptr--);
2527 if (c == '\n')
2528 break; /* don't need another */
2529 if (c == '#') {
2530 /* we end while in a comment, so we need a newline */
2531 mg->mg_len++; /* save space for it */
2532 need_newline = 1; /* note to add it */
2533 break;
2534 }
2535 }
2536 }
2537
2538 Newx(mg->mg_ptr, mg->mg_len + 1 + left, char);
2539 mg->mg_ptr[0] = '(';
2540 mg->mg_ptr[1] = '?';
2541 Copy(reflags, mg->mg_ptr+2, left, char);
2542 *(mg->mg_ptr+left+2) = ':';
2543 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
2544 if (need_newline)
2545 mg->mg_ptr[mg->mg_len - 2] = '\n';
2546 mg->mg_ptr[mg->mg_len - 1] = ')';
2547 mg->mg_ptr[mg->mg_len] = 0;
2548 }
2549 PL_reginterp_cnt += re->program[0].next_off;
2550
2551 if (re->reganch & ROPT_UTF8)
2552 SvUTF8_on(sv);
2553 else
2554 SvUTF8_off(sv);
2555 if (lp)
2556 *lp = mg->mg_len;
2557 return mg->mg_ptr;
2558}
2559
645c22ef
DM
2560/*
2561=for apidoc sv_2pv_flags
2562
ff276b08 2563Returns a pointer to the string value of an SV, and sets *lp to its length.
645c22ef
DM
2564If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
2565if necessary.
2566Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
2567usually end up here too.
2568
2569=cut
2570*/
2571
8d6d96c1
HS
2572char *
2573Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
2574{
97aff369 2575 dVAR;
79072805 2576 register char *s;
79072805 2577
463ee0b2 2578 if (!sv) {
cdb061a3
NC
2579 if (lp)
2580 *lp = 0;
73d840c0 2581 return (char *)"";
463ee0b2 2582 }
8990e307 2583 if (SvGMAGICAL(sv)) {
8d6d96c1
HS
2584 if (flags & SV_GMAGIC)
2585 mg_get(sv);
463ee0b2 2586 if (SvPOKp(sv)) {
cdb061a3
NC
2587 if (lp)
2588 *lp = SvCUR(sv);
10516c54
NC
2589 if (flags & SV_MUTABLE_RETURN)
2590 return SvPVX_mutable(sv);
4d84ee25
NC
2591 if (flags & SV_CONST_RETURN)
2592 return (char *)SvPVX_const(sv);
463ee0b2
LW
2593 return SvPVX(sv);
2594 }
75dfc8ec
NC
2595 if (SvIOKp(sv) || SvNOKp(sv)) {
2596 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
75dfc8ec
NC
2597 STRLEN len;
2598
2599 if (SvIOKp(sv)) {
e8ada2d0
NC
2600 len = SvIsUV(sv) ? my_sprintf(tbuf,"%"UVuf, (UV)SvUVX(sv))
2601 : my_sprintf(tbuf,"%"IVdf, (IV)SvIVX(sv));
75dfc8ec 2602 } else {
e8ada2d0
NC
2603 Gconvert(SvNVX(sv), NV_DIG, 0, tbuf);
2604 len = strlen(tbuf);
75dfc8ec 2605 }
b5b886f0
NC
2606 assert(!SvROK(sv));
2607 {
75dfc8ec
NC
2608 dVAR;
2609
2610#ifdef FIXNEGATIVEZERO
e8ada2d0
NC
2611 if (len == 2 && tbuf[0] == '-' && tbuf[1] == '0') {
2612 tbuf[0] = '0';
2613 tbuf[1] = 0;
75dfc8ec
NC
2614 len = 1;
2615 }
2616#endif
2617 SvUPGRADE(sv, SVt_PV);
2618 if (lp)
2619 *lp = len;
2620 s = SvGROW_mutable(sv, len + 1);
2621 SvCUR_set(sv, len);
2622 SvPOKp_on(sv);
e8ada2d0 2623 return memcpy(s, tbuf, len + 1);
75dfc8ec 2624 }
463ee0b2 2625 }
1c7ff15e
NC
2626 if (SvROK(sv)) {
2627 goto return_rok;
2628 }
2629 assert(SvTYPE(sv) >= SVt_PVMG);
2630 /* This falls through to the report_uninit near the end of the
2631 function. */
2632 } else if (SvTHINKFIRST(sv)) {
ed6116ce 2633 if (SvROK(sv)) {
1c7ff15e 2634 return_rok:
deb46114
NC
2635 if (SvAMAGIC(sv)) {
2636 SV *const tmpstr = AMG_CALLun(sv,string);
2637 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2638 /* Unwrap this: */
2639 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr);
2640 */
2641
2642 char *pv;
2643 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
2644 if (flags & SV_CONST_RETURN) {
2645 pv = (char *) SvPVX_const(tmpstr);
2646 } else {
2647 pv = (flags & SV_MUTABLE_RETURN)
2648 ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
2649 }
2650 if (lp)
2651 *lp = SvCUR(tmpstr);
50adf7d2 2652 } else {
deb46114 2653 pv = sv_2pv_flags(tmpstr, lp, flags);
50adf7d2 2654 }
deb46114
NC
2655 if (SvUTF8(tmpstr))
2656 SvUTF8_on(sv);
2657 else
2658 SvUTF8_off(sv);
2659 return pv;
50adf7d2 2660 }
deb46114
NC
2661 }
2662 {
75dfc8ec 2663 SV *tsv;
f9277f47 2664 MAGIC *mg;
d8eae41e
NC
2665 const SV *const referent = (SV*)SvRV(sv);
2666
2667 if (!referent) {
396482e1 2668 tsv = sv_2mortal(newSVpvs("NULLREF"));
042dae7a
NC
2669 } else if (SvTYPE(referent) == SVt_PVMG
2670 && ((SvFLAGS(referent) &
2671 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
2672 == (SVs_OBJECT|SVs_SMG))
2673 && (mg = mg_find(referent, PERL_MAGIC_qr))) {
c445ea15 2674 return stringify_regexp(sv, mg, lp);
d8eae41e
NC
2675 } else {
2676 const char *const typestr = sv_reftype(referent, 0);
2677
2678 tsv = sv_newmortal();
2679 if (SvOBJECT(referent)) {
2680 const char *const name = HvNAME_get(SvSTASH(referent));
2681 Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
2682 name ? name : "__ANON__" , typestr,
2683 PTR2UV(referent));
2684 }
2685 else
2686 Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr,
2687 PTR2UV(referent));
c080367d 2688 }
042dae7a
NC
2689 if (lp)
2690 *lp = SvCUR(tsv);
2691 return SvPVX(tsv);
463ee0b2 2692 }
79072805 2693 }
0336b60e 2694 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2695 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2696 report_uninit(sv);
cdb061a3
NC
2697 if (lp)
2698 *lp = 0;
73d840c0 2699 return (char *)"";
79072805 2700 }
79072805 2701 }
28e5dec8
JH
2702 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
2703 /* I'm assuming that if both IV and NV are equally valid then
2704 converting the IV is going to be more efficient */
e1ec3a88
AL
2705 const U32 isIOK = SvIOK(sv);
2706 const U32 isUIOK = SvIsUV(sv);
28e5dec8
JH
2707 char buf[TYPE_CHARS(UV)];
2708 char *ebuf, *ptr;
2709
2710 if (SvTYPE(sv) < SVt_PVIV)
2711 sv_upgrade(sv, SVt_PVIV);
4ea1d550 2712 ptr = uiv_2buf(buf, SvIVX(sv), SvUVX(sv), isUIOK, &ebuf);
5902b6a9
NC
2713 /* inlined from sv_setpvn */
2714 SvGROW_mutable(sv, (STRLEN)(ebuf - ptr + 1));
4d84ee25 2715 Move(ptr,SvPVX_mutable(sv),ebuf - ptr,char);
28e5dec8
JH
2716 SvCUR_set(sv, ebuf - ptr);
2717 s = SvEND(sv);
2718 *s = '\0';
2719 if (isIOK)
2720 SvIOK_on(sv);
2721 else
2722 SvIOKp_on(sv);
2723 if (isUIOK)
2724 SvIsUV_on(sv);
2725 }
2726 else if (SvNOKp(sv)) {
c81271c3 2727 const int olderrno = errno;
79072805
LW
2728 if (SvTYPE(sv) < SVt_PVNV)
2729 sv_upgrade(sv, SVt_PVNV);
1c846c1f 2730 /* The +20 is pure guesswork. Configure test needed. --jhi */
5902b6a9 2731 s = SvGROW_mutable(sv, NV_DIG + 20);
c81271c3 2732 /* some Xenix systems wipe out errno here */
79072805 2733#ifdef apollo
463ee0b2 2734 if (SvNVX(sv) == 0.0)
79072805
LW
2735 (void)strcpy(s,"0");
2736 else
2737#endif /*apollo*/
bbce6d69 2738 {
2d4389e4 2739 Gconvert(SvNVX(sv), NV_DIG, 0, s);
bbce6d69 2740 }
79072805 2741 errno = olderrno;
a0d0e21e
LW
2742#ifdef FIXNEGATIVEZERO
2743 if (*s == '-' && s[1] == '0' && !s[2])
2744 strcpy(s,"0");
2745#endif
79072805
LW
2746 while (*s) s++;
2747#ifdef hcx
2748 if (s[-1] == '.')
46fc3d4c 2749 *--s = '\0';
79072805
LW
2750#endif
2751 }
79072805 2752 else {
041457d9 2753 if (!PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP) && ckWARN(WARN_UNINITIALIZED))
29489e7c 2754 report_uninit(sv);
cdb061a3 2755 if (lp)
00b6aa41 2756 *lp = 0;
25da4f38
IZ
2757 if (SvTYPE(sv) < SVt_PV)
2758 /* Typically the caller expects that sv_any is not NULL now. */
2759 sv_upgrade(sv, SVt_PV);
73d840c0 2760 return (char *)"";
79072805 2761 }
cdb061a3 2762 {
823a54a3 2763 const STRLEN len = s - SvPVX_const(sv);
cdb061a3
NC
2764 if (lp)
2765 *lp = len;
2766 SvCUR_set(sv, len);
2767 }
79072805 2768 SvPOK_on(sv);
1d7c1841 2769 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3f7c398e 2770 PTR2UV(sv),SvPVX_const(sv)));
4d84ee25
NC
2771 if (flags & SV_CONST_RETURN)
2772 return (char *)SvPVX_const(sv);
10516c54
NC
2773 if (flags & SV_MUTABLE_RETURN)
2774 return SvPVX_mutable(sv);
463ee0b2
LW
2775 return SvPVX(sv);
2776}
2777
645c22ef 2778/*
6050d10e
JP
2779=for apidoc sv_copypv
2780
2781Copies a stringified representation of the source SV into the
2782destination SV. Automatically performs any necessary mg_get and
54f0641b 2783coercion of numeric values into strings. Guaranteed to preserve
6050d10e 2784UTF-8 flag even from overloaded objects. Similar in nature to
54f0641b
NIS
2785sv_2pv[_flags] but operates directly on an SV instead of just the
2786string. Mostly uses sv_2pv_flags to do its work, except when that
6050d10e
JP
2787would lose the UTF-8'ness of the PV.
2788
2789=cut
2790*/
2791
2792void
2793Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
2794{
446eaa42 2795 STRLEN len;
53c1dcc0 2796 const char * const s = SvPV_const(ssv,len);
cb50f42d 2797 sv_setpvn(dsv,s,len);
446eaa42 2798 if (SvUTF8(ssv))
cb50f42d 2799 SvUTF8_on(dsv);
446eaa42 2800 else
cb50f42d 2801 SvUTF8_off(dsv);
6050d10e
JP
2802}
2803
2804/*
645c22ef
DM
2805=for apidoc sv_2pvbyte
2806
2807Return a pointer to the byte-encoded representation of the SV, and set *lp
1e54db1a 2808to its length. May cause the SV to be downgraded from UTF-8 as a
645c22ef
DM
2809side-effect.
2810
2811Usually accessed via the C<SvPVbyte> macro.
2812
2813=cut
2814*/
2815
7340a771
GS
2816char *
2817Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
2818{
0875d2fe 2819 sv_utf8_downgrade(sv,0);
97972285 2820 return lp ? SvPV(sv,*lp) : SvPV_nolen(sv);
7340a771
GS
2821}
2822
645c22ef 2823/*
035cbb0e
RGS
2824=for apidoc sv_2pvutf8
2825
2826Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
2827to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
2828
2829Usually accessed via the C<SvPVutf8> macro.
2830
2831=cut
2832*/
645c22ef 2833
7340a771
GS
2834char *
2835Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
2836{
035cbb0e
RGS
2837 sv_utf8_upgrade(sv);
2838 return lp ? SvPV(sv,*lp) : SvPV_nolen(sv);
7340a771 2839}
1c846c1f 2840
7ee2227d 2841
645c22ef
DM
2842/*
2843=for apidoc sv_2bool
2844
2845This function is only called on magical items, and is only used by
8cf8f3d1 2846sv_true() or its macro equivalent.
645c22ef
DM
2847
2848=cut
2849*/
2850
463ee0b2 2851bool
864dbfa3 2852Perl_sv_2bool(pTHX_ register SV *sv)
463ee0b2 2853{
97aff369 2854 dVAR;
5b295bef 2855 SvGETMAGIC(sv);
463ee0b2 2856
a0d0e21e
LW
2857 if (!SvOK(sv))
2858 return 0;
2859 if (SvROK(sv)) {
fabdb6c0
AL
2860 if (SvAMAGIC(sv)) {
2861 SV * const tmpsv = AMG_CALLun(sv,bool_);
2862 if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
2863 return (bool)SvTRUE(tmpsv);
2864 }
2865 return SvRV(sv) != 0;
a0d0e21e 2866 }
463ee0b2 2867 if (SvPOKp(sv)) {
53c1dcc0
AL
2868 register XPV* const Xpvtmp = (XPV*)SvANY(sv);
2869 if (Xpvtmp &&
339049b0 2870 (*sv->sv_u.svu_pv > '0' ||
11343788 2871 Xpvtmp->xpv_cur > 1 ||
339049b0 2872 (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0')))
463ee0b2
LW
2873 return 1;
2874 else
2875 return 0;
2876 }
2877 else {
2878 if (SvIOKp(sv))
2879 return SvIVX(sv) != 0;
2880 else {
2881 if (SvNOKp(sv))
2882 return SvNVX(sv) != 0.0;
2883 else
2884 return FALSE;
2885 }
2886 }
79072805
LW
2887}
2888
c461cf8f
JH
2889/*
2890=for apidoc sv_utf8_upgrade
2891
78ea37eb 2892Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 2893Forces the SV to string form if it is not already.
4411f3b6
NIS
2894Always sets the SvUTF8 flag to avoid future validity checks even
2895if all the bytes have hibit clear.
c461cf8f 2896
13a6c0e0
JH
2897This is not as a general purpose byte encoding to Unicode interface:
2898use the Encode extension for that.
2899
8d6d96c1
HS
2900=for apidoc sv_utf8_upgrade_flags
2901
78ea37eb 2902Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 2903Forces the SV to string form if it is not already.
8d6d96c1
HS
2904Always sets the SvUTF8 flag to avoid future validity checks even
2905if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
2906will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
2907C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
2908
13a6c0e0
JH
2909This is not as a general purpose byte encoding to Unicode interface:
2910use the Encode extension for that.
2911
8d6d96c1
HS
2912=cut
2913*/
2914
2915STRLEN
2916Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
2917{
97aff369 2918 dVAR;
808c356f
RGS
2919 if (sv == &PL_sv_undef)
2920 return 0;
e0e62c2a
NIS
2921 if (!SvPOK(sv)) {
2922 STRLEN len = 0;
d52b7888
NC
2923 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
2924 (void) sv_2pv_flags(sv,&len, flags);
2925 if (SvUTF8(sv))
2926 return len;
2927 } else {
2928 (void) SvPV_force(sv,len);
2929 }
e0e62c2a 2930 }
4411f3b6 2931
f5cee72b 2932 if (SvUTF8(sv)) {
5fec3b1d 2933 return SvCUR(sv);
f5cee72b 2934 }
5fec3b1d 2935
765f542d
NC
2936 if (SvIsCOW(sv)) {
2937 sv_force_normal_flags(sv, 0);
db42d148
NIS
2938 }
2939
88632417 2940 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
799ef3cb 2941 sv_recode_to_utf8(sv, PL_encoding);
9f4817db 2942 else { /* Assume Latin-1/EBCDIC */
c4e7c712
NC
2943 /* This function could be much more efficient if we
2944 * had a FLAG in SVs to signal if there are any hibit
2945 * chars in the PV. Given that there isn't such a flag
2946 * make the loop as fast as possible. */
00b6aa41 2947 const U8 * const s = (U8 *) SvPVX_const(sv);
c4420975 2948 const U8 * const e = (U8 *) SvEND(sv);
93524f2b 2949 const U8 *t = s;
c4e7c712
NC
2950
2951 while (t < e) {
53c1dcc0 2952 const U8 ch = *t++;
00b6aa41
AL
2953 /* Check for hi bit */
2954 if (!NATIVE_IS_INVARIANT(ch)) {
2955 STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */
2956 U8 * const recoded = bytes_to_utf8((U8*)s, &len);
2957
2958 SvPV_free(sv); /* No longer using what was there before. */
2959 SvPV_set(sv, (char*)recoded);
2960 SvCUR_set(sv, len - 1);
2961 SvLEN_set(sv, len); /* No longer know the real size. */
c4e7c712 2962 break;
00b6aa41 2963 }
c4e7c712
NC
2964 }
2965 /* Mark as UTF-8 even if no hibit - saves scanning loop */
2966 SvUTF8_on(sv);
560a288e 2967 }
4411f3b6 2968 return SvCUR(sv);
560a288e
GS
2969}
2970
c461cf8f
JH
2971/*
2972=for apidoc sv_utf8_downgrade
2973
78ea37eb
ST
2974Attempts to convert the PV of an SV from characters to bytes.
2975If the PV contains a character beyond byte, this conversion will fail;
2976in this case, either returns false or, if C<fail_ok> is not
c461cf8f
JH
2977true, croaks.
2978
13a6c0e0
JH
2979This is not as a general purpose Unicode to byte encoding interface:
2980use the Encode extension for that.
2981
c461cf8f
JH
2982=cut
2983*/
2984
560a288e
GS
2985bool
2986Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
2987{
97aff369 2988 dVAR;
78ea37eb 2989 if (SvPOKp(sv) && SvUTF8(sv)) {
fa301091 2990 if (SvCUR(sv)) {
03cfe0ae 2991 U8 *s;
652088fc 2992 STRLEN len;
fa301091 2993
765f542d
NC
2994 if (SvIsCOW(sv)) {
2995 sv_force_normal_flags(sv, 0);
2996 }
03cfe0ae
NIS
2997 s = (U8 *) SvPV(sv, len);
2998 if (!utf8_to_bytes(s, &len)) {
fa301091
JH
2999 if (fail_ok)
3000 return FALSE;
3001 else {
3002 if (PL_op)
3003 Perl_croak(aTHX_ "Wide character in %s",
53e06cf0 3004 OP_DESC(PL_op));
fa301091
JH
3005 else
3006 Perl_croak(aTHX_ "Wide character");
3007 }
4b3603a4 3008 }
b162af07 3009 SvCUR_set(sv, len);
67e989fb 3010 }
560a288e 3011 }
ffebcc3e 3012 SvUTF8_off(sv);
560a288e
GS
3013 return TRUE;
3014}
3015
c461cf8f
JH
3016/*
3017=for apidoc sv_utf8_encode
3018
78ea37eb
ST
3019Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3020flag off so that it looks like octets again.
c461cf8f
JH
3021
3022=cut
3023*/
3024
560a288e
GS
3025void
3026Perl_sv_utf8_encode(pTHX_ register SV *sv)
3027{
4411f3b6 3028 (void) sv_utf8_upgrade(sv);
4c94c214
NC
3029 if (SvIsCOW(sv)) {
3030 sv_force_normal_flags(sv, 0);
3031 }
3032 if (SvREADONLY(sv)) {
3033 Perl_croak(aTHX_ PL_no_modify);
3034 }
560a288e
GS
3035 SvUTF8_off(sv);
3036}
3037
4411f3b6
NIS
3038/*
3039=for apidoc sv_utf8_decode
3040
78ea37eb
ST
3041If the PV of the SV is an octet sequence in UTF-8
3042and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3043so that it looks like a character. If the PV contains only single-byte
3044characters, the C<SvUTF8> flag stays being off.
3045Scans PV for validity and returns false if the PV is invalid UTF-8.
4411f3b6
NIS
3046
3047=cut
3048*/
3049
560a288e
GS
3050bool
3051Perl_sv_utf8_decode(pTHX_ register SV *sv)
3052{
78ea37eb 3053 if (SvPOKp(sv)) {
93524f2b
NC
3054 const U8 *c;
3055 const U8 *e;
9cbac4c7 3056
645c22ef
DM
3057 /* The octets may have got themselves encoded - get them back as
3058 * bytes
3059 */
3060 if (!sv_utf8_downgrade(sv, TRUE))
560a288e
GS
3061 return FALSE;
3062
3063 /* it is actually just a matter of turning the utf8 flag on, but
3064 * we want to make sure everything inside is valid utf8 first.
3065 */
93524f2b 3066 c = (const U8 *) SvPVX_const(sv);
63cd0674 3067 if (!is_utf8_string(c, SvCUR(sv)+1))
67e989fb 3068 return FALSE;
93524f2b 3069 e = (const U8 *) SvEND(sv);
511c2ff0 3070 while (c < e) {
b64e5050 3071 const U8 ch = *c++;
c4d5f83a 3072 if (!UTF8_IS_INVARIANT(ch)) {
67e989fb
JH
3073 SvUTF8_on(sv);
3074 break;
3075 }
560a288e 3076 }
560a288e
GS
3077 }
3078 return TRUE;
3079}
3080
954c1994
GS
3081/*
3082=for apidoc sv_setsv
3083
645c22ef
DM
3084Copies the contents of the source SV C<ssv> into the destination SV
3085C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3086function if the source SV needs to be reused. Does not handle 'set' magic.
3087Loosely speaking, it performs a copy-by-value, obliterating any previous
3088content of the destination.
3089
3090You probably want to use one of the assortment of wrappers, such as
3091C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3092C<SvSetMagicSV_nosteal>.
3093
8d6d96c1
HS
3094=for apidoc sv_setsv_flags
3095
645c22ef
DM
3096Copies the contents of the source SV C<ssv> into the destination SV
3097C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3098function if the source SV needs to be reused. Does not handle 'set' magic.
3099Loosely speaking, it performs a copy-by-value, obliterating any previous
3100content of the destination.
3101If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
5fcdf167
NC
3102C<ssv> if appropriate, else not. If the C<flags> parameter has the
3103C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
3104and C<sv_setsv_nomg> are implemented in terms of this function.
645c22ef
DM
3105
3106You probably want to use one of the assortment of wrappers, such as
3107C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3108C<SvSetMagicSV_nosteal>.
3109
3110This is the primary function for copying scalars, and most other
3111copy-ish functions and macros use this underneath.
8d6d96c1
HS
3112
3113=cut
3114*/
3115
5d0301b7 3116static void
2eb42952 3117S_glob_assign_glob(pTHX_ SV *dstr, SV *sstr, const int dtype)
5d0301b7
NC
3118{
3119 if (dtype != SVt_PVGV) {
3120 const char * const name = GvNAME(sstr);
3121 const STRLEN len = GvNAMELEN(sstr);
3122 /* don't upgrade SVt_PVLV: it can hold a glob */
3123 if (dtype != SVt_PVLV)
3124 sv_upgrade(dstr, SVt_PVGV);
bd61b366 3125 sv_magic(dstr, dstr, PERL_MAGIC_glob, NULL, 0);
5d0301b7
NC
3126 GvSTASH(dstr) = GvSTASH(sstr);
3127 if (GvSTASH(dstr))
3128 Perl_sv_add_backref(aTHX_ (SV*)GvSTASH(dstr), dstr);
3129 GvNAME(dstr) = savepvn(name, len);
3130 GvNAMELEN(dstr) = len;
3131 SvFAKE_on(dstr); /* can coerce to non-glob */
3132 }
3133
3134#ifdef GV_UNIQUE_CHECK
3135 if (GvUNIQUE((GV*)dstr)) {
3136 Perl_croak(aTHX_ PL_no_modify);
3137 }
3138#endif
3139
3140 (void)SvOK_off(dstr);
3141 GvINTRO_off(dstr); /* one-shot flag */
3142 gp_free((GV*)dstr);
3143 GvGP(dstr) = gp_ref(GvGP(sstr));
3144 if (SvTAINTED(sstr))
3145 SvTAINT(dstr);
3146 if (GvIMPORTED(dstr) != GVf_IMPORTED
3147 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3148 {
3149 GvIMPORTED_on(dstr);
3150 }
3151 GvMULTI_on(dstr);
3152 return;
3153}
3154
b8473700 3155static void
2eb42952 3156S_glob_assign_ref(pTHX_ SV *dstr, SV *sstr) {
b8473700
NC
3157 SV * const sref = SvREFCNT_inc(SvRV(sstr));
3158 SV *dref = NULL;
3159 const int intro = GvINTRO(dstr);
2440974c 3160 SV **location;
3386d083 3161 U8 import_flag = 0;
27242d61
NC
3162 const U32 stype = SvTYPE(sref);
3163
b8473700
NC
3164
3165#ifdef GV_UNIQUE_CHECK
3166 if (GvUNIQUE((GV*)dstr)) {
3167 Perl_croak(aTHX_ PL_no_modify);
3168 }
3169#endif
3170
3171 if (intro) {
3172 GvINTRO_off(dstr); /* one-shot flag */
3173 GvLINE(dstr) = CopLINE(PL_curcop);
3174 GvEGV(dstr) = (GV*)dstr;
3175 }
3176 GvMULTI_on(dstr);
27242d61 3177 switch (stype) {
b8473700 3178 case SVt_PVCV:
27242d61
NC
3179 location = (SV **) &GvCV(dstr);
3180 import_flag = GVf_IMPORTED_CV;
3181 goto common;
3182 case SVt_PVHV:
3183 location = (SV **) &GvHV(dstr);
3184 import_flag = GVf_IMPORTED_HV;
3185 goto common;
3186 case SVt_PVAV:
3187 location = (SV **) &GvAV(dstr);
3188 import_flag = GVf_IMPORTED_AV;
3189 goto common;
3190 case SVt_PVIO:
3191 location = (SV **) &GvIOp(dstr);
3192 goto common;
3193 case SVt_PVFM:
3194 location = (SV **) &GvFORM(dstr);
3195 default:
3196 location = &GvSV(dstr);
3197 import_flag = GVf_IMPORTED_SV;
3198 common:
b8473700 3199 if (intro) {
27242d61
NC
3200 if (stype == SVt_PVCV) {
3201 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
3202 SvREFCNT_dec(GvCV(dstr));
3203 GvCV(dstr) = NULL;
3204 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3205 PL_sub_generation++;
3206 }
b8473700 3207 }
27242d61 3208 SAVEGENERICSV(*location);
b8473700
NC
3209 }
3210 else
27242d61
NC
3211 dref = *location;
3212 if (stype == SVt_PVCV && *location != sref) {
3213 CV* const cv = (CV*)*location;
b8473700
NC
3214 if (cv) {
3215 if (!GvCVGEN((GV*)dstr) &&
3216 (CvROOT(cv) || CvXSUB(cv)))
3217 {
3218 /* Redefining a sub - warning is mandatory if
3219 it was a const and its value changed. */
3220 if (CvCONST(cv) && CvCONST((CV*)sref)
3221 && cv_const_sv(cv) == cv_const_sv((CV*)sref)) {
bb263b4e 3222 /*EMPTY*/
b8473700
NC
3223 /* They are 2 constant subroutines generated from
3224 the same constant. This probably means that
3225 they are really the "same" proxy subroutine
3226 instantiated in 2 places. Most likely this is
3227 when a constant is exported twice. Don't warn.
3228 */
3229 }
3230 else if (ckWARN(WARN_REDEFINE)
3231 || (CvCONST(cv)
3232 && (!CvCONST((CV*)sref)
3233 || sv_cmp(cv_const_sv(cv),
3234 cv_const_sv((CV*)sref))))) {
3235 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
3236 CvCONST(cv)
3237 ? "Constant subroutine %s::%s redefined"
3238 : "Subroutine %s::%s redefined",
3239 HvNAME_get(GvSTASH((GV*)dstr)),
3240 GvENAME((GV*)dstr));
3241 }
3242 }
3243 if (!intro)
3244 cv_ckproto(cv, (GV*)dstr,
bd61b366 3245 SvPOK(sref) ? SvPVX_const(sref) : NULL);
b8473700 3246 }
b8473700
NC
3247 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3248 GvASSUMECV_on(dstr);
3249 PL_sub_generation++;
3250 }
2440974c 3251 *location = sref;
3386d083
NC
3252 if (import_flag && !(GvFLAGS(dstr) & import_flag)
3253 && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) {
3254 GvFLAGS(dstr) |= import_flag;
b8473700
NC
3255 }
3256 break;
3257 }
3258 if (dref)
3259 SvREFCNT_dec(dref);
3260 if (SvTAINTED(sstr))
3261 SvTAINT(dstr);
3262 return;
3263}
3264
8d6d96c1
HS
3265void
3266Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
3267{
97aff369 3268 dVAR;
8990e307
LW
3269 register U32 sflags;
3270 register int dtype;
3271 register int stype;
463ee0b2 3272
79072805
LW
3273 if (sstr == dstr)
3274 return;
765f542d 3275 SV_CHECK_THINKFIRST_COW_DROP(dstr);
79072805 3276 if (!sstr)
3280af22 3277 sstr = &PL_sv_undef;
8990e307
LW
3278 stype = SvTYPE(sstr);
3279 dtype = SvTYPE(dstr);
79072805 3280
a0d0e21e 3281 SvAMAGIC_off(dstr);
7a5fa8a2 3282 if ( SvVOK(dstr) )
ece467f9
JP
3283 {
3284 /* need to nuke the magic */
3285 mg_free(dstr);
3286 SvRMAGICAL_off(dstr);
3287 }
9e7bc3e8 3288
463ee0b2 3289 /* There's a lot of redundancy below but we're going for speed here */
79072805 3290
8990e307 3291 switch (stype) {
79072805 3292 case SVt_NULL:
aece5585 3293 undef_sstr:
20408e3c
GS
3294 if (dtype != SVt_PVGV) {
3295 (void)SvOK_off(dstr);
3296 return;
3297 }
3298 break;
463ee0b2 3299 case SVt_IV:
aece5585
GA
3300 if (SvIOK(sstr)) {
3301 switch (dtype) {
3302 case SVt_NULL:
8990e307 3303 sv_upgrade(dstr, SVt_IV);
aece5585
GA
3304 break;
3305 case SVt_NV:
8990e307 3306 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
3307 break;
3308 case SVt_RV:
3309 case SVt_PV:
a0d0e21e 3310 sv_upgrade(dstr, SVt_PVIV);
aece5585
GA
3311 break;
3312 }
3313 (void)SvIOK_only(dstr);
45977657 3314 SvIV_set(dstr, SvIVX(sstr));
25da4f38
IZ
3315 if (SvIsUV(sstr))
3316 SvIsUV_on(dstr);
37c25af0
NC
3317 /* SvTAINTED can only be true if the SV has taint magic, which in
3318 turn means that the SV type is PVMG (or greater). This is the
3319 case statement for SVt_IV, so this cannot be true (whatever gcov
3320 may say). */
3321 assert(!SvTAINTED(sstr));
aece5585 3322 return;
8990e307 3323 }
aece5585
GA
3324 goto undef_sstr;
3325
463ee0b2 3326 case SVt_NV:
aece5585
GA
3327 if (SvNOK(sstr)) {
3328 switch (dtype) {
3329 case SVt_NULL:
3330 case SVt_IV:
8990e307 3331 sv_upgrade(dstr, SVt_NV);
aece5585
GA
3332 break;
3333 case SVt_RV:
3334 case SVt_PV:
3335 case SVt_PVIV:
a0d0e21e 3336 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
3337 break;
3338 }
9d6ce603 3339 SvNV_set(dstr, SvNVX(sstr));
aece5585 3340 (void)SvNOK_only(dstr);
37c25af0
NC
3341 /* SvTAINTED can only be true if the SV has taint magic, which in
3342 turn means that the SV type is PVMG (or greater). This is the
3343 case statement for SVt_NV, so this cannot be true (whatever gcov
3344 may say). */
3345 assert(!SvTAINTED(sstr));
aece5585 3346 return;
8990e307 3347 }
aece5585
GA
3348 goto undef_sstr;
3349
ed6116ce 3350 case SVt_RV:
8990e307 3351 if (dtype < SVt_RV)
ed6116ce 3352 sv_upgrade(dstr, SVt_RV);
ed6116ce 3353 break;
fc36a67e 3354 case SVt_PVFM:
f8c7b90f 3355#ifdef PERL_OLD_COPY_ON_WRITE
d89fc664
NC
3356 if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) {
3357 if (dtype < SVt_PVIV)
3358 sv_upgrade(dstr, SVt_PVIV);
3359 break;
3360 }
3361 /* Fall through */
3362#endif
3363 case SVt_PV:
8990e307 3364 if (dtype < SVt_PV)
463ee0b2 3365 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
3366 break;
3367 case SVt_PVIV:
8990e307 3368 if (dtype < SVt_PVIV)
463ee0b2 3369 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
3370 break;
3371 case SVt_PVNV:
8990e307 3372 if (dtype < SVt_PVNV)
463ee0b2 3373 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 3374 break;
4633a7c4
LW
3375 case SVt_PVAV:
3376 case SVt_PVHV:
3377 case SVt_PVCV:
4633a7c4 3378 case SVt_PVIO:
a3b680e6
AL
3379 {
3380 const char * const type = sv_reftype(sstr,0);
533c011a 3381 if (PL_op)
a3b680e6 3382 Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_NAME(PL_op));
4633a7c4 3383 else
a3b680e6
AL
3384 Perl_croak(aTHX_ "Bizarre copy of %s", type);
3385 }
4633a7c4
LW
3386 break;
3387
79072805 3388 case SVt_PVGV:
8990e307 3389 if (dtype <= SVt_PVGV) {
2eb42952 3390 S_glob_assign_glob(aTHX_ dstr, sstr, dtype);
b8c701c1 3391 return;
79072805 3392 }
5f66b61c 3393 /*FALLTHROUGH*/
79072805
LW
3394
3395 default:
8d6d96c1 3396 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
973f89ab 3397 mg_get(sstr);
eb160463 3398 if ((int)SvTYPE(sstr) != stype) {
973f89ab 3399 stype = SvTYPE(sstr);
b8c701c1 3400 if (stype == SVt_PVGV && dtype <= SVt_PVGV) {
2eb42952 3401 S_glob_assign_glob(aTHX_ dstr, sstr, dtype);
b8c701c1
NC
3402 return;
3403 }
973f89ab
CS
3404 }
3405 }
ded42b9f 3406 if (stype == SVt_PVLV)
862a34c6 3407 SvUPGRADE(dstr, SVt_PVNV);
ded42b9f 3408 else
862a34c6 3409 SvUPGRADE(dstr, (U32)stype);
79072805
LW
3410 }
3411
8990e307
LW
3412 sflags = SvFLAGS(sstr);
3413
3414 if (sflags & SVf_ROK) {
acaa9288
NC
3415 if (dtype == SVt_PVGV &&
3416 SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) {
3417 sstr = SvRV(sstr);
3418 if (sstr == dstr) {
3419 if (GvIMPORTED(dstr) != GVf_IMPORTED
3420 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3421 {
3422 GvIMPORTED_on(dstr);
3423 }
3424 GvMULTI_on(dstr);
3425 return;
3426 }
2eb42952 3427 S_glob_assign_glob(aTHX_ dstr, sstr, dtype);
acaa9288
NC
3428 return;
3429 }
3430
8990e307 3431 if (dtype >= SVt_PV) {
b8c701c1 3432 if (dtype == SVt_PVGV) {
2eb42952 3433 S_glob_assign_ref(aTHX_ dstr, sstr);
b8c701c1
NC
3434 return;
3435 }
3f7c398e 3436 if (SvPVX_const(dstr)) {
8bd4d4c5 3437 SvPV_free(dstr);
b162af07
SP
3438 SvLEN_set(dstr, 0);
3439 SvCUR_set(dstr, 0);
a0d0e21e 3440 }
8990e307 3441 }
a0d0e21e 3442 (void)SvOK_off(dstr);
b162af07 3443 SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
dfd48732
NC
3444 SvFLAGS(dstr) |= sflags & (SVf_ROK|SVf_AMAGIC);
3445 assert(!(sflags & SVp_NOK));
3446 assert(!(sflags & SVp_IOK));
3447 assert(!(sflags & SVf_NOK));
3448 assert(!(sflags & SVf_IOK));
ed6116ce 3449 }
8990e307 3450 else if (sflags & SVp_POK) {
765f542d 3451 bool isSwipe = 0;
79072805
LW
3452
3453 /*
3454 * Check to see if we can just swipe the string. If so, it's a
3455 * possible small lose on short strings, but a big win on long ones.
3f7c398e
SP
3456 * It might even be a win on short strings if SvPVX_const(dstr)
3457 * has to be allocated and SvPVX_const(sstr) has to be freed.
79072805
LW
3458 */
3459
120fac95
NC
3460 /* Whichever path we take through the next code, we want this true,
3461 and doing it now facilitates the COW check. */
3462 (void)SvPOK_only(dstr);
3463
765f542d 3464 if (
b8f9541a
NC
3465 /* We're not already COW */
3466 ((sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY)
f8c7b90f 3467#ifndef PERL_OLD_COPY_ON_WRITE
b8f9541a
NC
3468 /* or we are, but dstr isn't a suitable target. */
3469 || (SvFLAGS(dstr) & CAN_COW_MASK) != CAN_COW_FLAGS
3470#endif
3471 )
765f542d 3472 &&
765f542d
NC
3473 !(isSwipe =
3474 (sflags & SVs_TEMP) && /* slated for free anyway? */
3475 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
5fcdf167
NC
3476 (!(flags & SV_NOSTEAL)) &&
3477 /* and we're allowed to steal temps */
765f542d
NC
3478 SvREFCNT(sstr) == 1 && /* and no other references to it? */
3479 SvLEN(sstr) && /* and really is a string */
645c22ef 3480 /* and won't be needed again, potentially */
765f542d 3481 !(PL_op && PL_op->op_type == OP_AASSIGN))
f8c7b90f 3482#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 3483 && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS
120fac95 3484 && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS
765f542d
NC
3485 && SvTYPE(sstr) >= SVt_PVIV)
3486#endif
3487 ) {
3488 /* Failed the swipe test, and it's not a shared hash key either.
3489 Have to copy the string. */
3490 STRLEN len = SvCUR(sstr);
3491 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
3f7c398e 3492 Move(SvPVX_const(sstr),SvPVX(dstr),len,char);
765f542d
NC
3493 SvCUR_set(dstr, len);
3494 *SvEND(dstr) = '\0';
765f542d 3495 } else {
f8c7b90f 3496 /* If PERL_OLD_COPY_ON_WRITE is not defined, then isSwipe will always
765f542d 3497 be true in here. */
765f542d
NC
3498 /* Either it's a shared hash key, or it's suitable for
3499 copy-on-write or we can swipe the string. */
46187eeb 3500 if (DEBUG_C_TEST) {
ed252734 3501 PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
e419cbc5
NC
3502 sv_dump(sstr);
3503 sv_dump(dstr);
46187eeb 3504 }
f8c7b90f 3505#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
3506 if (!isSwipe) {
3507 /* I believe I should acquire a global SV mutex if
3508 it's a COW sv (not a shared hash key) to stop
3509 it going un copy-on-write.
3510 If the source SV has gone un copy on write between up there
3511 and down here, then (assert() that) it is of the correct
3512 form to make it copy on write again */
3513 if ((sflags & (SVf_FAKE | SVf_READONLY))
3514 != (SVf_FAKE | SVf_READONLY)) {
3515 SvREADONLY_on(sstr);
3516 SvFAKE_on(sstr);
3517 /* Make the source SV into a loop of 1.
3518 (about to become 2) */
a29f6d03 3519 SV_COW_NEXT_SV_SET(sstr, sstr);
765f542d
NC
3520 }
3521 }
3522#endif
3523 /* Initial code is common. */
94010e71
NC
3524 if (SvPVX_const(dstr)) { /* we know that dtype >= SVt_PV */
3525 SvPV_free(dstr);
79072805 3526 }
765f542d 3527
765f542d
NC
3528 if (!isSwipe) {
3529 /* making another shared SV. */
3530 STRLEN cur = SvCUR(sstr);
3531 STRLEN len = SvLEN(sstr);
f8c7b90f 3532#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 3533 if (len) {
b8f9541a 3534 assert (SvTYPE(dstr) >= SVt_PVIV);
765f542d
NC
3535 /* SvIsCOW_normal */
3536 /* splice us in between source and next-after-source. */
a29f6d03
NC
3537 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
3538 SV_COW_NEXT_SV_SET(sstr, dstr);
940132f3 3539 SvPV_set(dstr, SvPVX_mutable(sstr));
a604c751
NC
3540 } else
3541#endif
3542 {
765f542d 3543 /* SvIsCOW_shared_hash */
46187eeb
NC
3544 DEBUG_C(PerlIO_printf(Perl_debug_log,
3545 "Copy on write: Sharing hash\n"));
b8f9541a 3546
bdd68bc3 3547 assert (SvTYPE(dstr) >= SVt_PV);
765f542d 3548 SvPV_set(dstr,
d1db91c6 3549 HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)))));
bdd68bc3 3550 }
87a1ef3d
SP
3551 SvLEN_set(dstr, len);
3552 SvCUR_set(dstr, cur);
765f542d
NC
3553 SvREADONLY_on(dstr);
3554 SvFAKE_on(dstr);
3555 /* Relesase a global SV mutex. */
3556 }
3557 else
765f542d 3558 { /* Passes the swipe test. */
78d1e721 3559 SvPV_set(dstr, SvPVX_mutable(sstr));
765f542d
NC
3560 SvLEN_set(dstr, SvLEN(sstr));
3561 SvCUR_set(dstr, SvCUR(sstr));
3562
3563 SvTEMP_off(dstr);
3564 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
6136c704 3565 SvPV_set(sstr, NULL);
765f542d
NC
3566 SvLEN_set(sstr, 0);
3567 SvCUR_set(sstr, 0);
3568 SvTEMP_off(sstr);
3569 }
3570 }
8990e307 3571 if (sflags & SVp_NOK) {
9d6ce603 3572 SvNV_set(dstr, SvNVX(sstr));
79072805 3573 }
8990e307 3574 if (sflags & SVp_IOK) {
23525414
NC
3575 SvRELEASE_IVX(dstr);
3576 SvIV_set(dstr, SvIVX(sstr));
3577 /* Must do this otherwise some other overloaded use of 0x80000000
3578 gets confused. I guess SVpbm_VALID */
2b1c7e3e 3579 if (sflags & SVf_IVisUV)
25da4f38 3580 SvIsUV_on(dstr);
79072805 3581 }
23525414 3582 SvFLAGS(dstr) |= sflags & (SVf_IOK|SVp_IOK|SVf_NOK|SVp_NOK|SVf_UTF8);
4f2da183
NC
3583 {
3584 const MAGIC * const smg = SvVOK(sstr);
3585 if (smg) {
3586 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
3587 smg->mg_ptr, smg->mg_len);
3588 SvRMAGICAL_on(dstr);
3589 }
7a5fa8a2 3590 }
79072805 3591 }
5d581361 3592 else if (sflags & (SVp_IOK|SVp_NOK)) {
c2468cc7 3593 (void)SvOK_off(dstr);
5d581361
NC
3594 SvFLAGS(dstr) |= sflags & (SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_NOK|SVp_NOK);
3595 if (sflags & SVp_IOK) {
3596 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
3597 SvIV_set(dstr, SvIVX(sstr));
3598 }
3332b3c1 3599 if (sflags & SVp_NOK) {
9d6ce603 3600 SvNV_set(dstr, SvNVX(sstr));
3332b3c1
JH
3601 }
3602 }
79072805 3603 else {
20408e3c 3604 if (dtype == SVt_PVGV) {
e476b1b5 3605 if (ckWARN(WARN_MISC))
9014280d 3606 Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
20408e3c
GS
3607 }
3608 else
3609 (void)SvOK_off(dstr);
a0d0e21e 3610 }
27c9684d
AP
3611 if (SvTAINTED(sstr))
3612 SvTAINT(dstr);
79072805
LW
3613}
3614
954c1994
GS
3615/*
3616=for apidoc sv_setsv_mg
3617
3618Like C<sv_setsv>, but also handles 'set' magic.
3619
3620=cut
3621*/
3622
79072805 3623void
864dbfa3 3624Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
ef50df4b
GS
3625{
3626 sv_setsv(dstr,sstr);
3627 SvSETMAGIC(dstr);
3628}
3629
f8c7b90f 3630#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
3631SV *
3632Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr)
3633{
3634 STRLEN cur = SvCUR(sstr);
3635 STRLEN len = SvLEN(sstr);
3636 register char *new_pv;
3637
3638 if (DEBUG_C_TEST) {
3639 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
3640 sstr, dstr);
3641 sv_dump(sstr);
3642 if (dstr)
3643 sv_dump(dstr);
3644 }
3645
3646 if (dstr) {
3647 if (SvTHINKFIRST(dstr))
3648 sv_force_normal_flags(dstr, SV_COW_DROP_PV);
3f7c398e
SP
3649 else if (SvPVX_const(dstr))
3650 Safefree(SvPVX_const(dstr));
ed252734
NC
3651 }
3652 else
3653 new_SV(dstr);
862a34c6 3654 SvUPGRADE(dstr, SVt_PVIV);
ed252734
NC
3655
3656 assert (SvPOK(sstr));
3657 assert (SvPOKp(sstr));
3658 assert (!SvIOK(sstr));
3659 assert (!SvIOKp(sstr));
3660 assert (!SvNOK(sstr));
3661 assert (!SvNOKp(sstr));
3662
3663 if (SvIsCOW(sstr)) {
3664
3665 if (SvLEN(sstr) == 0) {
3666 /* source is a COW shared hash key. */
ed252734
NC
3667 DEBUG_C(PerlIO_printf(Perl_debug_log,
3668 "Fast copy on write: Sharing hash\n"));
d1db91c6 3669 new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr))));
ed252734
NC
3670 goto common_exit;
3671 }
3672 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
3673 } else {
3674 assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS);
862a34c6 3675 SvUPGRADE(sstr, SVt_PVIV);
ed252734
NC
3676 SvREADONLY_on(sstr);
3677 SvFAKE_on(sstr);
3678 DEBUG_C(PerlIO_printf(Perl_debug_log,
3679 "Fast copy on write: Converting sstr to COW\n"));
3680 SV_COW_NEXT_SV_SET(dstr, sstr);
3681 }
3682 SV_COW_NEXT_SV_SET(sstr, dstr);
940132f3 3683 new_pv = SvPVX_mutable(sstr);
ed252734
NC
3684
3685 common_exit:
3686 SvPV_set(dstr, new_pv);
3687 SvFLAGS(dstr) = (SVt_PVIV|SVf_POK|SVp_POK|SVf_FAKE|SVf_READONLY);
3688 if (SvUTF8(sstr))
3689 SvUTF8_on(dstr);
87a1ef3d
SP
3690 SvLEN_set(dstr, len);
3691 SvCUR_set(dstr, cur);
ed252734
NC
3692 if (DEBUG_C_TEST) {
3693 sv_dump(dstr);
3694 }
3695 return dstr;
3696}
3697#endif
3698
954c1994
GS
3699/*
3700=for apidoc sv_setpvn
3701
3702Copies a string into an SV. The C<len> parameter indicates the number of
9e09f5f2
MHM
3703bytes to be copied. If the C<ptr> argument is NULL the SV will become
3704undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>.
954c1994
GS
3705
3706=cut
3707*/
3708
ef50df4b 3709void
864dbfa3 3710Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
79072805 3711{
97aff369 3712 dVAR;
c6f8c383 3713 register char *dptr;
22c522df 3714
765f542d 3715 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2 3716 if (!ptr) {
a0d0e21e 3717 (void)SvOK_off(sv);
463ee0b2
LW
3718 return;
3719 }
22c522df
JH
3720 else {
3721 /* len is STRLEN which is unsigned, need to copy to signed */
a3b680e6 3722 const IV iv = len;
9c5ffd7c
JH
3723 if (iv < 0)
3724 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
22c522df 3725 }
862a34c6 3726 SvUPGRADE(sv, SVt_PV);
c6f8c383 3727
5902b6a9 3728 dptr = SvGROW(sv, len + 1);
c6f8c383
GA
3729 Move(ptr,dptr,len,char);
3730 dptr[len] = '\0';
79072805 3731 SvCUR_set(sv, len);
1aa99e6b 3732 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 3733 SvTAINT(sv);
79072805
LW
3734}
3735
954c1994
GS
3736/*
3737=for apidoc sv_setpvn_mg
3738
3739Like C<sv_setpvn>, but also handles 'set' magic.
3740
3741=cut
3742*/
3743
79072805 3744void
864dbfa3 3745Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
3746{
3747 sv_setpvn(sv,ptr,len);
3748 SvSETMAGIC(sv);
3749}
3750
954c1994
GS
3751/*
3752=for apidoc sv_setpv
3753
3754Copies a string into an SV. The string must be null-terminated. Does not
3755handle 'set' magic. See C<sv_setpv_mg>.
3756
3757=cut
3758*/
3759
ef50df4b 3760void
864dbfa3 3761Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
79072805 3762{
97aff369 3763 dVAR;
79072805
LW
3764 register STRLEN len;
3765
765f542d 3766 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2 3767 if (!ptr) {
a0d0e21e 3768 (void)SvOK_off(sv);
463ee0b2
LW
3769 return;
3770 }
79072805 3771 len = strlen(ptr);