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