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