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