This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [perl #36048] Refactor S_more_*v into one function
[perl5.git] / sv.c
CommitLineData
a0d0e21e 1/* sv.c
79072805 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
241d1a3b 4 * 2000, 2001, 2002, 2003, 2004, 2005, 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
4977e971
NC
66Normally, this allocation is done using arenas, which by default are
67approximately 4K chunks of memory parcelled up into N heads or bodies. The
68first slot in each arena is reserved, and is used to hold a link to the next
69arena. In the case of heads, the unused first slot also contains some flags
70and a note of the number of slots. Snaked through each arena chain is a
5e045b90 71linked list of free items; when this becomes empty, an extra arena is
4977e971 72allocated and divided up into N items which are threaded into the free list.
645c22ef
DM
73
74The following global variables are associated with arenas:
75
76 PL_sv_arenaroot pointer to list of SV arenas
77 PL_sv_root pointer to list of free SV structures
78
79 PL_foo_arenaroot pointer to list of foo arenas,
80 PL_foo_root pointer to list of free foo bodies
81 ... for foo in xiv, xnv, xrv, xpv etc.
82
83Note that some of the larger and more rarely used body types (eg xpvio)
84are not allocated using arenas, but are instead just malloc()/free()ed as
85required. Also, if PURIFY is defined, arenas are abandoned altogether,
86with all items individually malloc()ed. In addition, a few SV heads are
87not allocated from an arena, but are instead directly created as static
4977e971
NC
88or auto variables, eg PL_sv_undef. The size of arenas can be changed from
89the default by setting PERL_ARENA_SIZE appropriately at compile time.
645c22ef
DM
90
91The SV arena serves the secondary purpose of allowing still-live SVs
92to be located and destroyed during final cleanup.
93
94At the lowest level, the macros new_SV() and del_SV() grab and free
95an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
96to return the SV to the free list with error checking.) new_SV() calls
97more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
98SVs in the free list have their SvTYPE field set to all ones.
99
100Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
101that allocate and return individual body types. Normally these are mapped
ff276b08
RG
102to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be
103instead mapped directly to malloc()/free() if PURIFY is defined. The
645c22ef
DM
104new/del functions remove from, or add to, the appropriate PL_foo_root
105list, and call more_xiv() etc to add a new arena if the list is empty.
106
ff276b08 107At the time of very final cleanup, sv_free_arenas() is called from
645c22ef
DM
108perl_destruct() to physically free all the arenas allocated since the
109start of the interpreter. Note that this also clears PL_he_arenaroot,
110which is otherwise dealt with in hv.c.
111
112Manipulation of any of the PL_*root pointers is protected by enclosing
113LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
114if threads are enabled.
115
116The function visit() scans the SV arenas list, and calls a specified
117function for each SV it finds which is still live - ie which has an SvTYPE
118other than all 1's, and a non-zero SvREFCNT. visit() is used by the
119following functions (specified as [function that calls visit()] / [function
120called by visit() for each SV]):
121
122 sv_report_used() / do_report_used()
123 dump all remaining SVs (debugging aid)
124
125 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
126 Attempt to free all objects pointed to by RVs,
127 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
128 try to do the same for all objects indirectly
129 referenced by typeglobs too. Called once from
130 perl_destruct(), prior to calling sv_clean_all()
131 below.
132
133 sv_clean_all() / do_clean_all()
134 SvREFCNT_dec(sv) each remaining SV, possibly
135 triggering an sv_free(). It also sets the
136 SVf_BREAK flag on the SV to indicate that the
137 refcnt has been artificially lowered, and thus
138 stopping sv_free() from giving spurious warnings
139 about SVs which unexpectedly have a refcnt
140 of zero. called repeatedly from perl_destruct()
141 until there are no SVs left.
142
143=head2 Summary
144
145Private API to rest of sv.c
146
147 new_SV(), del_SV(),
148
149 new_XIV(), del_XIV(),
150 new_XNV(), del_XNV(),
151 etc
152
153Public API:
154
8cf8f3d1 155 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
645c22ef
DM
156
157
158=cut
159
160============================================================================ */
161
162
51371543 163
4561caa4
CS
164/*
165 * "A time to plant, and a time to uproot what was planted..."
166 */
167
cac9b346 168
fd0854ff
DM
169#ifdef DEBUG_LEAKING_SCALARS
170# ifdef NETWARE
171# define FREE_SV_DEBUG_FILE(sv) PerlMemfree((sv)->sv_debug_file)
172# else
173# define FREE_SV_DEBUG_FILE(sv) PerlMemShared_free((sv)->sv_debug_file)
174# endif
175#else
176# define FREE_SV_DEBUG_FILE(sv)
177#endif
178
053fc874
GS
179#define plant_SV(p) \
180 STMT_START { \
fd0854ff 181 FREE_SV_DEBUG_FILE(p); \
053fc874
GS
182 SvANY(p) = (void *)PL_sv_root; \
183 SvFLAGS(p) = SVTYPEMASK; \
184 PL_sv_root = (p); \
185 --PL_sv_count; \
186 } STMT_END
a0d0e21e 187
fba3b22e 188/* sv_mutex must be held while calling uproot_SV() */
053fc874
GS
189#define uproot_SV(p) \
190 STMT_START { \
191 (p) = PL_sv_root; \
192 PL_sv_root = (SV*)SvANY(p); \
193 ++PL_sv_count; \
194 } STMT_END
195
645c22ef 196
cac9b346
NC
197/* make some more SVs by adding another arena */
198
199/* sv_mutex must be held while calling more_sv() */
200STATIC SV*
201S_more_sv(pTHX)
202{
203 SV* sv;
204
205 if (PL_nice_chunk) {
206 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
207 PL_nice_chunk = Nullch;
208 PL_nice_chunk_size = 0;
209 }
210 else {
211 char *chunk; /* must use New here to match call to */
2e7ed132
NC
212 New(704,chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
213 sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
cac9b346
NC
214 }
215 uproot_SV(sv);
216 return sv;
217}
218
645c22ef
DM
219/* new_SV(): return a new, empty SV head */
220
eba0f806
DM
221#ifdef DEBUG_LEAKING_SCALARS
222/* provide a real function for a debugger to play with */
223STATIC SV*
224S_new_SV(pTHX)
225{
226 SV* sv;
227
228 LOCK_SV_MUTEX;
229 if (PL_sv_root)
230 uproot_SV(sv);
231 else
cac9b346 232 sv = S_more_sv(aTHX);
eba0f806
DM
233 UNLOCK_SV_MUTEX;
234 SvANY(sv) = 0;
235 SvREFCNT(sv) = 1;
236 SvFLAGS(sv) = 0;
fd0854ff
DM
237 sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
238 sv->sv_debug_line = (U16) ((PL_copline == NOLINE) ?
239 (PL_curcop ? CopLINE(PL_curcop) : 0) : PL_copline);
240 sv->sv_debug_inpad = 0;
241 sv->sv_debug_cloned = 0;
242# ifdef NETWARE
243 sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL;
244# else
245 sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL;
246# endif
247
eba0f806
DM
248 return sv;
249}
250# define new_SV(p) (p)=S_new_SV(aTHX)
251
252#else
253# define new_SV(p) \
053fc874
GS
254 STMT_START { \
255 LOCK_SV_MUTEX; \
256 if (PL_sv_root) \
257 uproot_SV(p); \
258 else \
cac9b346 259 (p) = S_more_sv(aTHX); \
053fc874
GS
260 UNLOCK_SV_MUTEX; \
261 SvANY(p) = 0; \
262 SvREFCNT(p) = 1; \
263 SvFLAGS(p) = 0; \
264 } STMT_END
eba0f806 265#endif
463ee0b2 266
645c22ef
DM
267
268/* del_SV(): return an empty SV head to the free list */
269
a0d0e21e 270#ifdef DEBUGGING
4561caa4 271
053fc874
GS
272#define del_SV(p) \
273 STMT_START { \
274 LOCK_SV_MUTEX; \
aea4f609 275 if (DEBUG_D_TEST) \
053fc874
GS
276 del_sv(p); \
277 else \
278 plant_SV(p); \
279 UNLOCK_SV_MUTEX; \
280 } STMT_END
a0d0e21e 281
76e3520e 282STATIC void
cea2e8a9 283S_del_sv(pTHX_ SV *p)
463ee0b2 284{
aea4f609 285 if (DEBUG_D_TEST) {
4633a7c4 286 SV* sva;
a3b680e6 287 bool ok = 0;
3280af22 288 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
a3b680e6
AL
289 SV *sv = sva + 1;
290 SV *svend = &sva[SvREFCNT(sva)];
c0ff570e 291 if (p >= sv && p < svend) {
a0d0e21e 292 ok = 1;
c0ff570e
NC
293 break;
294 }
a0d0e21e
LW
295 }
296 if (!ok) {
0453d815 297 if (ckWARN_d(WARN_INTERNAL))
9014280d 298 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
472d47bc
SB
299 "Attempt to free non-arena SV: 0x%"UVxf
300 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
a0d0e21e
LW
301 return;
302 }
303 }
4561caa4 304 plant_SV(p);
463ee0b2 305}
a0d0e21e 306
4561caa4
CS
307#else /* ! DEBUGGING */
308
309#define del_SV(p) plant_SV(p)
310
311#endif /* DEBUGGING */
463ee0b2 312
645c22ef
DM
313
314/*
ccfc67b7
JH
315=head1 SV Manipulation Functions
316
645c22ef
DM
317=for apidoc sv_add_arena
318
319Given a chunk of memory, link it to the head of the list of arenas,
320and split it into a list of free SVs.
321
322=cut
323*/
324
4633a7c4 325void
864dbfa3 326Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
463ee0b2 327{
4633a7c4 328 SV* sva = (SV*)ptr;
463ee0b2
LW
329 register SV* sv;
330 register SV* svend;
4633a7c4
LW
331
332 /* The first SV in an arena isn't an SV. */
3280af22 333 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
4633a7c4
LW
334 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
335 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
336
3280af22
NIS
337 PL_sv_arenaroot = sva;
338 PL_sv_root = sva + 1;
4633a7c4
LW
339
340 svend = &sva[SvREFCNT(sva) - 1];
341 sv = sva + 1;
463ee0b2 342 while (sv < svend) {
a0d0e21e 343 SvANY(sv) = (void *)(SV*)(sv + 1);
03e36789 344#ifdef DEBUGGING
978b032e 345 SvREFCNT(sv) = 0;
03e36789
NC
346#endif
347 /* Must always set typemask because it's awlays checked in on cleanup
348 when the arenas are walked looking for objects. */
8990e307 349 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
350 sv++;
351 }
352 SvANY(sv) = 0;
03e36789
NC
353#ifdef DEBUGGING
354 SvREFCNT(sv) = 0;
355#endif
4633a7c4
LW
356 SvFLAGS(sv) = SVTYPEMASK;
357}
358
055972dc
DM
359/* visit(): call the named function for each non-free SV in the arenas
360 * whose flags field matches the flags/mask args. */
645c22ef 361
5226ed68 362STATIC I32
055972dc 363S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
8990e307 364{
4633a7c4 365 SV* sva;
5226ed68 366 I32 visited = 0;
8990e307 367
3280af22 368 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
a3b680e6
AL
369 register SV * const svend = &sva[SvREFCNT(sva)];
370 register SV* sv;
4561caa4 371 for (sv = sva + 1; sv < svend; ++sv) {
055972dc
DM
372 if (SvTYPE(sv) != SVTYPEMASK
373 && (sv->sv_flags & mask) == flags
374 && SvREFCNT(sv))
375 {
acfe0abc 376 (FCALL)(aTHX_ sv);
5226ed68
JH
377 ++visited;
378 }
8990e307
LW
379 }
380 }
5226ed68 381 return visited;
8990e307
LW
382}
383
758a08c3
JH
384#ifdef DEBUGGING
385
645c22ef
DM
386/* called by sv_report_used() for each live SV */
387
388static void
acfe0abc 389do_report_used(pTHX_ SV *sv)
645c22ef
DM
390{
391 if (SvTYPE(sv) != SVTYPEMASK) {
392 PerlIO_printf(Perl_debug_log, "****\n");
393 sv_dump(sv);
394 }
395}
758a08c3 396#endif
645c22ef
DM
397
398/*
399=for apidoc sv_report_used
400
401Dump the contents of all SVs not yet freed. (Debugging aid).
402
403=cut
404*/
405
8990e307 406void
864dbfa3 407Perl_sv_report_used(pTHX)
4561caa4 408{
ff270d3a 409#ifdef DEBUGGING
055972dc 410 visit(do_report_used, 0, 0);
ff270d3a 411#endif
4561caa4
CS
412}
413
645c22ef
DM
414/* called by sv_clean_objs() for each live SV */
415
416static void
acfe0abc 417do_clean_objs(pTHX_ SV *sv)
645c22ef
DM
418{
419 SV* rv;
420
421 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
422 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv)));
423 if (SvWEAKREF(sv)) {
424 sv_del_backref(sv);
425 SvWEAKREF_off(sv);
b162af07 426 SvRV_set(sv, NULL);
645c22ef
DM
427 } else {
428 SvROK_off(sv);
b162af07 429 SvRV_set(sv, NULL);
645c22ef
DM
430 SvREFCNT_dec(rv);
431 }
432 }
433
434 /* XXX Might want to check arrays, etc. */
435}
436
437/* called by sv_clean_objs() for each live SV */
438
439#ifndef DISABLE_DESTRUCTOR_KLUDGE
440static void
acfe0abc 441do_clean_named_objs(pTHX_ SV *sv)
645c22ef
DM
442{
443 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
444 if ( SvOBJECT(GvSV(sv)) ||
445 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
446 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
447 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
448 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
449 {
450 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
ec5f3c78 451 SvFLAGS(sv) |= SVf_BREAK;
645c22ef
DM
452 SvREFCNT_dec(sv);
453 }
454 }
455}
456#endif
457
458/*
459=for apidoc sv_clean_objs
460
461Attempt to destroy all objects not yet freed
462
463=cut
464*/
465
4561caa4 466void
864dbfa3 467Perl_sv_clean_objs(pTHX)
4561caa4 468{
3280af22 469 PL_in_clean_objs = TRUE;
055972dc 470 visit(do_clean_objs, SVf_ROK, SVf_ROK);
4561caa4 471#ifndef DISABLE_DESTRUCTOR_KLUDGE
2d0f3c12 472 /* some barnacles may yet remain, clinging to typeglobs */
055972dc 473 visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
4561caa4 474#endif
3280af22 475 PL_in_clean_objs = FALSE;
4561caa4
CS
476}
477
645c22ef
DM
478/* called by sv_clean_all() for each live SV */
479
480static void
acfe0abc 481do_clean_all(pTHX_ SV *sv)
645c22ef
DM
482{
483 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
484 SvFLAGS(sv) |= SVf_BREAK;
0e705b3b
DM
485 if (PL_comppad == (AV*)sv) {
486 PL_comppad = Nullav;
487 PL_curpad = Null(SV**);
488 }
645c22ef
DM
489 SvREFCNT_dec(sv);
490}
491
492/*
493=for apidoc sv_clean_all
494
495Decrement the refcnt of each remaining SV, possibly triggering a
496cleanup. This function may have to be called multiple times to free
ff276b08 497SVs which are in complex self-referential hierarchies.
645c22ef
DM
498
499=cut
500*/
501
5226ed68 502I32
864dbfa3 503Perl_sv_clean_all(pTHX)
8990e307 504{
5226ed68 505 I32 cleaned;
3280af22 506 PL_in_clean_all = TRUE;
055972dc 507 cleaned = visit(do_clean_all, 0,0);
3280af22 508 PL_in_clean_all = FALSE;
5226ed68 509 return cleaned;
8990e307 510}
463ee0b2 511
645c22ef
DM
512/*
513=for apidoc sv_free_arenas
514
515Deallocate the memory used by all arenas. Note that all the individual SV
516heads and bodies within the arenas must already have been freed.
517
518=cut
519*/
520
4633a7c4 521void
864dbfa3 522Perl_sv_free_arenas(pTHX)
4633a7c4
LW
523{
524 SV* sva;
525 SV* svanext;
7b2c381c 526 void *arena, *arenanext;
4633a7c4
LW
527
528 /* Free arenas here, but be careful about fake ones. (We assume
529 contiguity of the fake ones with the corresponding real ones.) */
530
3280af22 531 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
4633a7c4
LW
532 svanext = (SV*) SvANY(sva);
533 while (svanext && SvFAKE(svanext))
534 svanext = (SV*) SvANY(svanext);
535
536 if (!SvFAKE(sva))
1df70142 537 Safefree(sva);
4633a7c4 538 }
5f05dabc 539
612f20c3 540 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
7b2c381c 541 arenanext = *(void **)arena;
612f20c3
GS
542 Safefree(arena);
543 }
544 PL_xnv_arenaroot = 0;
bf9cdc68 545 PL_xnv_root = 0;
612f20c3 546
612f20c3 547 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
7b2c381c 548 arenanext = *(void **)arena;
612f20c3
GS
549 Safefree(arena);
550 }
551 PL_xpv_arenaroot = 0;
bf9cdc68 552 PL_xpv_root = 0;
612f20c3 553
7b2c381c
NC
554 for (arena = PL_xpviv_arenaroot; arena; arena = arenanext) {
555 arenanext = *(void **)arena;
612f20c3
GS
556 Safefree(arena);
557 }
558 PL_xpviv_arenaroot = 0;
bf9cdc68 559 PL_xpviv_root = 0;
612f20c3 560
7b2c381c
NC
561 for (arena = PL_xpvnv_arenaroot; arena; arena = arenanext) {
562 arenanext = *(void **)arena;
612f20c3
GS
563 Safefree(arena);
564 }
565 PL_xpvnv_arenaroot = 0;
bf9cdc68 566 PL_xpvnv_root = 0;
612f20c3 567
7b2c381c
NC
568 for (arena = PL_xpvcv_arenaroot; arena; arena = arenanext) {
569 arenanext = *(void **)arena;
612f20c3
GS
570 Safefree(arena);
571 }
572 PL_xpvcv_arenaroot = 0;
bf9cdc68 573 PL_xpvcv_root = 0;
612f20c3 574
7b2c381c
NC
575 for (arena = PL_xpvav_arenaroot; arena; arena = arenanext) {
576 arenanext = *(void **)arena;
612f20c3
GS
577 Safefree(arena);
578 }
579 PL_xpvav_arenaroot = 0;
bf9cdc68 580 PL_xpvav_root = 0;
612f20c3 581
7b2c381c
NC
582 for (arena = PL_xpvhv_arenaroot; arena; arena = arenanext) {
583 arenanext = *(void **)arena;
612f20c3
GS
584 Safefree(arena);
585 }
586 PL_xpvhv_arenaroot = 0;
bf9cdc68 587 PL_xpvhv_root = 0;
612f20c3 588
7b2c381c
NC
589 for (arena = PL_xpvmg_arenaroot; arena; arena = arenanext) {
590 arenanext = *(void **)arena;
612f20c3
GS
591 Safefree(arena);
592 }
593 PL_xpvmg_arenaroot = 0;
bf9cdc68 594 PL_xpvmg_root = 0;
612f20c3 595
7b2c381c
NC
596 for (arena = PL_xpvgv_arenaroot; arena; arena = arenanext) {
597 arenanext = *(void **)arena;
727879eb
NC
598 Safefree(arena);
599 }
600 PL_xpvgv_arenaroot = 0;
601 PL_xpvgv_root = 0;
602
7b2c381c
NC
603 for (arena = PL_xpvlv_arenaroot; arena; arena = arenanext) {
604 arenanext = *(void **)arena;
612f20c3
GS
605 Safefree(arena);
606 }
607 PL_xpvlv_arenaroot = 0;
bf9cdc68 608 PL_xpvlv_root = 0;
612f20c3 609
7b2c381c
NC
610 for (arena = PL_xpvbm_arenaroot; arena; arena = arenanext) {
611 arenanext = *(void **)arena;
612f20c3
GS
612 Safefree(arena);
613 }
614 PL_xpvbm_arenaroot = 0;
bf9cdc68 615 PL_xpvbm_root = 0;
612f20c3 616
b1135e3d
NC
617 {
618 HE *he;
619 HE *he_next;
620 for (he = PL_he_arenaroot; he; he = he_next) {
621 he_next = HeNEXT(he);
622 Safefree(he);
623 }
612f20c3
GS
624 }
625 PL_he_arenaroot = 0;
bf9cdc68 626 PL_he_root = 0;
612f20c3 627
892b45be 628#if defined(USE_ITHREADS)
b1135e3d
NC
629 {
630 struct ptr_tbl_ent *pte;
631 struct ptr_tbl_ent *pte_next;
632 for (pte = PL_pte_arenaroot; pte; pte = pte_next) {
633 pte_next = pte->next;
634 Safefree(pte);
635 }
32e691d0
NC
636 }
637 PL_pte_arenaroot = 0;
638 PL_pte_root = 0;
892b45be 639#endif
32e691d0 640
3280af22
NIS
641 if (PL_nice_chunk)
642 Safefree(PL_nice_chunk);
643 PL_nice_chunk = Nullch;
644 PL_nice_chunk_size = 0;
645 PL_sv_arenaroot = 0;
646 PL_sv_root = 0;
4633a7c4
LW
647}
648
29489e7c
DM
649/* ---------------------------------------------------------------------
650 *
651 * support functions for report_uninit()
652 */
653
654/* the maxiumum size of array or hash where we will scan looking
655 * for the undefined element that triggered the warning */
656
657#define FUV_MAX_SEARCH_SIZE 1000
658
659/* Look for an entry in the hash whose value has the same SV as val;
660 * If so, return a mortal copy of the key. */
661
662STATIC SV*
663S_find_hash_subscript(pTHX_ HV *hv, SV* val)
664{
27da23d5 665 dVAR;
29489e7c 666 register HE **array;
29489e7c
DM
667 I32 i;
668
669 if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
670 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
671 return Nullsv;
672
673 array = HvARRAY(hv);
674
675 for (i=HvMAX(hv); i>0; i--) {
f54cb97a 676 register HE *entry;
29489e7c
DM
677 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
678 if (HeVAL(entry) != val)
679 continue;
680 if ( HeVAL(entry) == &PL_sv_undef ||
681 HeVAL(entry) == &PL_sv_placeholder)
682 continue;
683 if (!HeKEY(entry))
684 return Nullsv;
685 if (HeKLEN(entry) == HEf_SVKEY)
686 return sv_mortalcopy(HeKEY_sv(entry));
687 return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry)));
688 }
689 }
690 return Nullsv;
691}
692
693/* Look for an entry in the array whose value has the same SV as val;
694 * If so, return the index, otherwise return -1. */
695
696STATIC I32
697S_find_array_subscript(pTHX_ AV *av, SV* val)
698{
699 SV** svp;
700 I32 i;
701 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
702 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
703 return -1;
704
705 svp = AvARRAY(av);
706 for (i=AvFILLp(av); i>=0; i--) {
707 if (svp[i] == val && svp[i] != &PL_sv_undef)
708 return i;
709 }
710 return -1;
711}
712
713/* S_varname(): return the name of a variable, optionally with a subscript.
714 * If gv is non-zero, use the name of that global, along with gvtype (one
715 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
716 * targ. Depending on the value of the subscript_type flag, return:
717 */
718
719#define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
720#define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
721#define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
722#define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
723
724STATIC SV*
bfed75c6 725S_varname(pTHX_ GV *gv, const char *gvtype, PADOFFSET targ,
29489e7c
DM
726 SV* keyname, I32 aindex, int subscript_type)
727{
728 AV *av;
a3b680e6 729 SV *sv;
29489e7c 730
a3b680e6 731 SV * const name = sv_newmortal();
29489e7c
DM
732 if (gv) {
733
734 /* simulate gv_fullname4(), but add literal '^' for $^FOO names
735 * XXX get rid of all this if gv_fullnameX() ever supports this
736 * directly */
737
bfed75c6 738 const char *p;
29489e7c
DM
739 HV *hv = GvSTASH(gv);
740 sv_setpv(name, gvtype);
741 if (!hv)
742 p = "???";
bfcb3514 743 else if (!(p=HvNAME_get(hv)))
29489e7c 744 p = "__ANON__";
29489e7c
DM
745 if (strNE(p, "main")) {
746 sv_catpv(name,p);
747 sv_catpvn(name,"::", 2);
748 }
749 if (GvNAMELEN(gv)>= 1 &&
750 ((unsigned int)*GvNAME(gv)) <= 26)
751 { /* handle $^FOO */
752 Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1);
753 sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1);
754 }
755 else
756 sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv));
757 }
758 else {
759 U32 u;
760 CV *cv = find_runcv(&u);
761 if (!cv || !CvPADLIST(cv))
762 return Nullsv;;
763 av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE));
764 sv = *av_fetch(av, targ, FALSE);
765 /* SvLEN in a pad name is not to be trusted */
f9926b10 766 sv_setpv(name, SvPV_nolen_const(sv));
29489e7c
DM
767 }
768
769 if (subscript_type == FUV_SUBSCRIPT_HASH) {
770 *SvPVX(name) = '$';
771 sv = NEWSV(0,0);
772 Perl_sv_catpvf(aTHX_ name, "{%s}",
3f7c398e 773 pv_display(sv,SvPVX_const(keyname), SvCUR(keyname), 0, 32));
29489e7c
DM
774 SvREFCNT_dec(sv);
775 }
776 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
777 *SvPVX(name) = '$';
265a12b8 778 Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
29489e7c
DM
779 }
780 else if (subscript_type == FUV_SUBSCRIPT_WITHIN)
781 sv_insert(name, 0, 0, "within ", 7);
782
783 return name;
784}
785
786
787/*
788=for apidoc find_uninit_var
789
790Find the name of the undefined variable (if any) that caused the operator o
791to issue a "Use of uninitialized value" warning.
792If match is true, only return a name if it's value matches uninit_sv.
793So roughly speaking, if a unary operator (such as OP_COS) generates a
794warning, then following the direct child of the op may yield an
795OP_PADSV or OP_GV that gives the name of the undefined variable. On the
796other hand, with OP_ADD there are two branches to follow, so we only print
797the variable name if we get an exact match.
798
799The name is returned as a mortal SV.
800
801Assumes that PL_op is the op that originally triggered the error, and that
802PL_comppad/PL_curpad points to the currently executing pad.
803
804=cut
805*/
806
807STATIC SV *
808S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match)
809{
27da23d5 810 dVAR;
29489e7c
DM
811 SV *sv;
812 AV *av;
813 SV **svp;
814 GV *gv;
815 OP *o, *o2, *kid;
816
817 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
818 uninit_sv == &PL_sv_placeholder)))
819 return Nullsv;
820
821 switch (obase->op_type) {
822
823 case OP_RV2AV:
824 case OP_RV2HV:
825 case OP_PADAV:
826 case OP_PADHV:
827 {
f54cb97a
AL
828 const bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
829 const bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV);
112dcc46
RGS
830 I32 index = 0;
831 SV *keysv = Nullsv;
29489e7c
DM
832 int subscript_type = FUV_SUBSCRIPT_WITHIN;
833
834 if (pad) { /* @lex, %lex */
835 sv = PAD_SVl(obase->op_targ);
836 gv = Nullgv;
837 }
838 else {
839 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
840 /* @global, %global */
841 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
842 if (!gv)
843 break;
844 sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv);
845 }
846 else /* @{expr}, %{expr} */
847 return find_uninit_var(cUNOPx(obase)->op_first,
848 uninit_sv, match);
849 }
850
851 /* attempt to find a match within the aggregate */
852 if (hash) {
853 keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
854 if (keysv)
855 subscript_type = FUV_SUBSCRIPT_HASH;
856 }
857 else {
858 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
859 if (index >= 0)
860 subscript_type = FUV_SUBSCRIPT_ARRAY;
861 }
862
863 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
864 break;
865
866 return S_varname(aTHX_ gv, hash ? "%" : "@", obase->op_targ,
867 keysv, index, subscript_type);
868 }
869
870 case OP_PADSV:
871 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
872 break;
873 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
874 Nullsv, 0, FUV_SUBSCRIPT_NONE);
875
876 case OP_GVSV:
877 gv = cGVOPx_gv(obase);
878 if (!gv || (match && GvSV(gv) != uninit_sv))
879 break;
880 return S_varname(aTHX_ gv, "$", 0, Nullsv, 0, FUV_SUBSCRIPT_NONE);
881
882 case OP_AELEMFAST:
883 if (obase->op_flags & OPf_SPECIAL) { /* lexical array */
884 if (match) {
885 av = (AV*)PAD_SV(obase->op_targ);
886 if (!av || SvRMAGICAL(av))
887 break;
888 svp = av_fetch(av, (I32)obase->op_private, FALSE);
889 if (!svp || *svp != uninit_sv)
890 break;
891 }
892 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
893 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
894 }
895 else {
896 gv = cGVOPx_gv(obase);
897 if (!gv)
898 break;
899 if (match) {
900 av = GvAV(gv);
901 if (!av || SvRMAGICAL(av))
902 break;
903 svp = av_fetch(av, (I32)obase->op_private, FALSE);
904 if (!svp || *svp != uninit_sv)
905 break;
906 }
907 return S_varname(aTHX_ gv, "$", 0,
908 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
909 }
910 break;
911
912 case OP_EXISTS:
913 o = cUNOPx(obase)->op_first;
914 if (!o || o->op_type != OP_NULL ||
915 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
916 break;
917 return find_uninit_var(cBINOPo->op_last, uninit_sv, match);
918
919 case OP_AELEM:
920 case OP_HELEM:
921 if (PL_op == obase)
922 /* $a[uninit_expr] or $h{uninit_expr} */
923 return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match);
924
925 gv = Nullgv;
926 o = cBINOPx(obase)->op_first;
927 kid = cBINOPx(obase)->op_last;
928
929 /* get the av or hv, and optionally the gv */
930 sv = Nullsv;
931 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
932 sv = PAD_SV(o->op_targ);
933 }
934 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
935 && cUNOPo->op_first->op_type == OP_GV)
936 {
937 gv = cGVOPx_gv(cUNOPo->op_first);
938 if (!gv)
939 break;
940 sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv);
941 }
942 if (!sv)
943 break;
944
945 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
946 /* index is constant */
947 if (match) {
948 if (SvMAGICAL(sv))
949 break;
950 if (obase->op_type == OP_HELEM) {
951 HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0);
952 if (!he || HeVAL(he) != uninit_sv)
953 break;
954 }
955 else {
956 svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE);
957 if (!svp || *svp != uninit_sv)
958 break;
959 }
960 }
961 if (obase->op_type == OP_HELEM)
962 return S_varname(aTHX_ gv, "%", o->op_targ,
963 cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH);
964 else
965 return S_varname(aTHX_ gv, "@", o->op_targ, Nullsv,
966 SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY);
967 ;
968 }
969 else {
970 /* index is an expression;
971 * attempt to find a match within the aggregate */
972 if (obase->op_type == OP_HELEM) {
973 SV *keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
974 if (keysv)
975 return S_varname(aTHX_ gv, "%", o->op_targ,
976 keysv, 0, FUV_SUBSCRIPT_HASH);
977 }
978 else {
f54cb97a 979 const I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
29489e7c 980 if (index >= 0)
f54cb97a 981 return S_varname(aTHX_ gv, "@", o->op_targ,
29489e7c
DM
982 Nullsv, index, FUV_SUBSCRIPT_ARRAY);
983 }
984 if (match)
985 break;
986 return S_varname(aTHX_ gv,
987 (o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
988 ? "@" : "%",
989 o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN);
990 }
991
992 break;
993
994 case OP_AASSIGN:
995 /* only examine RHS */
996 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match);
997
998 case OP_OPEN:
999 o = cUNOPx(obase)->op_first;
1000 if (o->op_type == OP_PUSHMARK)
1001 o = o->op_sibling;
1002
1003 if (!o->op_sibling) {
1004 /* one-arg version of open is highly magical */
1005
1006 if (o->op_type == OP_GV) { /* open FOO; */
1007 gv = cGVOPx_gv(o);
1008 if (match && GvSV(gv) != uninit_sv)
1009 break;
7a5fa8a2 1010 return S_varname(aTHX_ gv, "$", 0,
29489e7c
DM
1011 Nullsv, 0, FUV_SUBSCRIPT_NONE);
1012 }
1013 /* other possibilities not handled are:
1014 * open $x; or open my $x; should return '${*$x}'
1015 * open expr; should return '$'.expr ideally
1016 */
1017 break;
1018 }
1019 goto do_op;
1020
1021 /* ops where $_ may be an implicit arg */
1022 case OP_TRANS:
1023 case OP_SUBST:
1024 case OP_MATCH:
1025 if ( !(obase->op_flags & OPf_STACKED)) {
1026 if (uninit_sv == ((obase->op_private & OPpTARGET_MY)
1027 ? PAD_SVl(obase->op_targ)
1028 : DEFSV))
1029 {
1030 sv = sv_newmortal();
616d8c9c 1031 sv_setpvn(sv, "$_", 2);
29489e7c
DM
1032 return sv;
1033 }
1034 }
1035 goto do_op;
1036
1037 case OP_PRTF:
1038 case OP_PRINT:
1039 /* skip filehandle as it can't produce 'undef' warning */
1040 o = cUNOPx(obase)->op_first;
1041 if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK)
1042 o = o->op_sibling->op_sibling;
1043 goto do_op2;
1044
1045
e21bd382 1046 case OP_RV2SV:
29489e7c
DM
1047 case OP_CUSTOM:
1048 case OP_ENTERSUB:
1049 match = 1; /* XS or custom code could trigger random warnings */
1050 goto do_op;
1051
1052 case OP_SCHOMP:
1053 case OP_CHOMP:
1054 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
1055 return sv_2mortal(newSVpv("${$/}", 0));
1056 /* FALL THROUGH */
1057
1058 default:
1059 do_op:
1060 if (!(obase->op_flags & OPf_KIDS))
1061 break;
1062 o = cUNOPx(obase)->op_first;
1063
1064 do_op2:
1065 if (!o)
1066 break;
1067
1068 /* if all except one arg are constant, or have no side-effects,
1069 * or are optimized away, then it's unambiguous */
1070 o2 = Nullop;
1071 for (kid=o; kid; kid = kid->op_sibling) {
1072 if (kid &&
1073 ( (kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid)))
1074 || (kid->op_type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
1075 || (kid->op_type == OP_PUSHMARK)
1076 )
1077 )
1078 continue;
1079 if (o2) { /* more than one found */
1080 o2 = Nullop;
1081 break;
1082 }
1083 o2 = kid;
1084 }
1085 if (o2)
1086 return find_uninit_var(o2, uninit_sv, match);
1087
1088 /* scan all args */
1089 while (o) {
1090 sv = find_uninit_var(o, uninit_sv, 1);
1091 if (sv)
1092 return sv;
1093 o = o->op_sibling;
1094 }
1095 break;
1096 }
1097 return Nullsv;
1098}
1099
1100
645c22ef
DM
1101/*
1102=for apidoc report_uninit
1103
1104Print appropriate "Use of uninitialized variable" warning
1105
1106=cut
1107*/
1108
1d7c1841 1109void
29489e7c
DM
1110Perl_report_uninit(pTHX_ SV* uninit_sv)
1111{
1112 if (PL_op) {
112dcc46 1113 SV* varname = Nullsv;
29489e7c
DM
1114 if (uninit_sv) {
1115 varname = find_uninit_var(PL_op, uninit_sv,0);
1116 if (varname)
1117 sv_insert(varname, 0, 0, " ", 1);
1118 }
9014280d 1119 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
93524f2b 1120 varname ? SvPV_nolen_const(varname) : "",
29489e7c
DM
1121 " in ", OP_DESC(PL_op));
1122 }
1d7c1841 1123 else
29489e7c
DM
1124 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1125 "", "", "");
1d7c1841
GS
1126}
1127
950dc694
JC
1128#define USE_S_MORE_THINGY
1129
1130#ifdef USE_S_MORE_THINGY
1131
1132#define S_more_thingy(TYPE,lctype) \
1133STATIC void \
1134S_more_## lctype (pTHX) \
1135{ \
1136 TYPE* lctype; \
1137 TYPE* lctype ## end; \
1138 void *ptr; \
1139 New(711, ptr, PERL_ARENA_SIZE/sizeof(TYPE), TYPE); \
1140 *((void **) ptr) = (void *)PL_## lctype ## _arenaroot; \
1141 PL_## lctype ## _arenaroot = ptr; \
1142 \
1143 lctype = (TYPE*) ptr; \
1144 lctype ## end = &lctype[PERL_ARENA_SIZE / sizeof(TYPE) - 1]; \
1145 \
1146 /* fudge by sizeof XPVIV */ \
1147 lctype += (sizeof(XPVIV) - 1) / sizeof(TYPE) + 1; \
1148 \
1149 PL_ ## lctype ## _root = lctype; \
1150 while ( lctype < lctype ## end) { \
1151 *(TYPE**) lctype = (TYPE*)(lctype + 1); \
1152 lctype++; \
1153 } \
1154 *(TYPE**) lctype = 0; \
1155}
1156
1157#define S_more_thingy_allocated(lctype) \
1158STATIC void \
1159S_more_## lctype (pTHX) \
1160{ \
1161 lctype ## _allocated * lctype ; \
1162 lctype ## _allocated * lctype ## end; \
1163 void *ptr; \
1164 New(711, ptr, PERL_ARENA_SIZE/sizeof(lctype ## _allocated ), lctype ## _allocated ); \
1165 *((void **) ptr) = (void *)PL_ ## lctype ## _arenaroot; \
1166 PL_## lctype ## _arenaroot = ptr; \
1167 \
1168 lctype = (lctype ## _allocated *) ptr; \
1169 lctype ## end = &lctype[PERL_ARENA_SIZE / sizeof(lctype ## _allocated ) - 1]; \
1170 \
1171 /* fudge by sizeof XPVIV */ \
1172 lctype += (sizeof(XPVIV) - 1) / sizeof(lctype ## _allocated ) + 1; \
1173 \
1174 PL_ ## lctype ## _root = lctype; \
1175 while ( lctype < lctype ## end) { \
1176 *(lctype ## _allocated **) lctype = (lctype ## _allocated *)(lctype + 1); \
1177 lctype++; \
1178 } \
1179 *(lctype ## _allocated **) lctype = 0; \
1180}
1181
1182S_more_thingy(NV, xnv)
1183
1184S_more_thingy_allocated(xpv)
1185
1186S_more_thingy_allocated(xpviv)
1187
1188S_more_thingy(XPVNV, xpvnv)
1189
1190S_more_thingy(XPVCV, xpvcv)
1191
1192S_more_thingy_allocated(xpvav)
1193
1194S_more_thingy_allocated(xpvhv)
1195
1196S_more_thingy(XPVGV, xpvgv)
1197
1198S_more_thingy(XPVMG, xpvmg)
1199
1200S_more_thingy(XPVBM, xpvbm)
1201
1202S_more_thingy(XPVLV, xpvlv)
1203
1204
1205#else
1206
1207
645c22ef
DM
1208/* allocate another arena's worth of NV bodies */
1209
cbe51380 1210STATIC void
cea2e8a9 1211S_more_xnv(pTHX)
463ee0b2 1212{
cac9b346
NC
1213 NV* xnv;
1214 NV* xnvend;
7b2c381c
NC
1215 void *ptr;
1216 New(711, ptr, PERL_ARENA_SIZE/sizeof(NV), NV);
1217 *((void **) ptr) = (void *)PL_xnv_arenaroot;
612f20c3
GS
1218 PL_xnv_arenaroot = ptr;
1219
1220 xnv = (NV*) ptr;
9c17f24a 1221 xnvend = &xnv[PERL_ARENA_SIZE / sizeof(NV) - 1];
65202027 1222 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
3280af22 1223 PL_xnv_root = xnv;
463ee0b2 1224 while (xnv < xnvend) {
65202027 1225 *(NV**)xnv = (NV*)(xnv + 1);
463ee0b2
LW
1226 xnv++;
1227 }
cac9b346
NC
1228 *(NV**)xnv = 0;
1229}
1230
1231/* allocate another arena's worth of struct xpv */
1232
1233STATIC void
1234S_more_xpv(pTHX)
1235{
59813432
NC
1236 xpv_allocated* xpv;
1237 xpv_allocated* xpvend;
1238 New(713, xpv, PERL_ARENA_SIZE/sizeof(xpv_allocated), xpv_allocated);
1239 *((xpv_allocated**)xpv) = PL_xpv_arenaroot;
cac9b346
NC
1240 PL_xpv_arenaroot = xpv;
1241
59813432 1242 xpvend = &xpv[PERL_ARENA_SIZE / sizeof(xpv_allocated) - 1];
cac9b346
NC
1243 PL_xpv_root = ++xpv;
1244 while (xpv < xpvend) {
59813432 1245 *((xpv_allocated**)xpv) = xpv + 1;
cac9b346
NC
1246 xpv++;
1247 }
59813432 1248 *((xpv_allocated**)xpv) = 0;
cac9b346
NC
1249}
1250
1251/* allocate another arena's worth of struct xpviv */
1252
1253STATIC void
1254S_more_xpviv(pTHX)
1255{
311a25d9
NC
1256 xpviv_allocated* xpviv;
1257 xpviv_allocated* xpvivend;
1258 New(713, xpviv, PERL_ARENA_SIZE/sizeof(xpviv_allocated), xpviv_allocated);
1259 *((xpviv_allocated**)xpviv) = PL_xpviv_arenaroot;
cac9b346
NC
1260 PL_xpviv_arenaroot = xpviv;
1261
311a25d9 1262 xpvivend = &xpviv[PERL_ARENA_SIZE / sizeof(xpviv_allocated) - 1];
cac9b346
NC
1263 PL_xpviv_root = ++xpviv;
1264 while (xpviv < xpvivend) {
311a25d9 1265 *((xpviv_allocated**)xpviv) = xpviv + 1;
cac9b346
NC
1266 xpviv++;
1267 }
311a25d9 1268 *((xpviv_allocated**)xpviv) = 0;
cac9b346
NC
1269}
1270
1271/* allocate another arena's worth of struct xpvnv */
1272
1273STATIC void
1274S_more_xpvnv(pTHX)
1275{
1276 XPVNV* xpvnv;
1277 XPVNV* xpvnvend;
1278 New(715, xpvnv, PERL_ARENA_SIZE/sizeof(XPVNV), XPVNV);
7b2c381c 1279 *((XPVNV**)xpvnv) = PL_xpvnv_arenaroot;
cac9b346
NC
1280 PL_xpvnv_arenaroot = xpvnv;
1281
1282 xpvnvend = &xpvnv[PERL_ARENA_SIZE / sizeof(XPVNV) - 1];
1283 PL_xpvnv_root = ++xpvnv;
1284 while (xpvnv < xpvnvend) {
7b2c381c 1285 *((XPVNV**)xpvnv) = xpvnv + 1;
cac9b346
NC
1286 xpvnv++;
1287 }
7b2c381c 1288 *((XPVNV**)xpvnv) = 0;
cac9b346
NC
1289}
1290
1291/* allocate another arena's worth of struct xpvcv */
1292
1293STATIC void
1294S_more_xpvcv(pTHX)
1295{
1296 XPVCV* xpvcv;
1297 XPVCV* xpvcvend;
1298 New(716, xpvcv, PERL_ARENA_SIZE/sizeof(XPVCV), XPVCV);
7b2c381c 1299 *((XPVCV**)xpvcv) = PL_xpvcv_arenaroot;
cac9b346
NC
1300 PL_xpvcv_arenaroot = xpvcv;
1301
1302 xpvcvend = &xpvcv[PERL_ARENA_SIZE / sizeof(XPVCV) - 1];
1303 PL_xpvcv_root = ++xpvcv;
1304 while (xpvcv < xpvcvend) {
7b2c381c 1305 *((XPVCV**)xpvcv) = xpvcv + 1;
cac9b346
NC
1306 xpvcv++;
1307 }
7b2c381c 1308 *((XPVCV**)xpvcv) = 0;
cac9b346
NC
1309}
1310
1311/* allocate another arena's worth of struct xpvav */
1312
1313STATIC void
1314S_more_xpvav(pTHX)
1315{
59813432
NC
1316 xpvav_allocated* xpvav;
1317 xpvav_allocated* xpvavend;
1318 New(717, xpvav, PERL_ARENA_SIZE/sizeof(xpvav_allocated),
1319 xpvav_allocated);
1320 *((xpvav_allocated**)xpvav) = PL_xpvav_arenaroot;
cac9b346
NC
1321 PL_xpvav_arenaroot = xpvav;
1322
59813432 1323 xpvavend = &xpvav[PERL_ARENA_SIZE / sizeof(xpvav_allocated) - 1];
cac9b346
NC
1324 PL_xpvav_root = ++xpvav;
1325 while (xpvav < xpvavend) {
59813432 1326 *((xpvav_allocated**)xpvav) = xpvav + 1;
cac9b346
NC
1327 xpvav++;
1328 }
59813432 1329 *((xpvav_allocated**)xpvav) = 0;
cac9b346
NC
1330}
1331
1332/* allocate another arena's worth of struct xpvhv */
1333
1334STATIC void
1335S_more_xpvhv(pTHX)
1336{
59813432
NC
1337 xpvhv_allocated* xpvhv;
1338 xpvhv_allocated* xpvhvend;
1339 New(718, xpvhv, PERL_ARENA_SIZE/sizeof(xpvhv_allocated),
1340 xpvhv_allocated);
1341 *((xpvhv_allocated**)xpvhv) = PL_xpvhv_arenaroot;
cac9b346
NC
1342 PL_xpvhv_arenaroot = xpvhv;
1343
59813432 1344 xpvhvend = &xpvhv[PERL_ARENA_SIZE / sizeof(xpvhv_allocated) - 1];
cac9b346
NC
1345 PL_xpvhv_root = ++xpvhv;
1346 while (xpvhv < xpvhvend) {
59813432 1347 *((xpvhv_allocated**)xpvhv) = xpvhv + 1;
cac9b346
NC
1348 xpvhv++;
1349 }
59813432 1350 *((xpvhv_allocated**)xpvhv) = 0;
cac9b346
NC
1351}
1352
1353/* allocate another arena's worth of struct xpvmg */
1354
1355STATIC void
1356S_more_xpvmg(pTHX)
1357{
1358 XPVMG* xpvmg;
1359 XPVMG* xpvmgend;
1360 New(719, xpvmg, PERL_ARENA_SIZE/sizeof(XPVMG), XPVMG);
7b2c381c 1361 *((XPVMG**)xpvmg) = PL_xpvmg_arenaroot;
cac9b346
NC
1362 PL_xpvmg_arenaroot = xpvmg;
1363
1364 xpvmgend = &xpvmg[PERL_ARENA_SIZE / sizeof(XPVMG) - 1];
1365 PL_xpvmg_root = ++xpvmg;
1366 while (xpvmg < xpvmgend) {
7b2c381c 1367 *((XPVMG**)xpvmg) = xpvmg + 1;
cac9b346
NC
1368 xpvmg++;
1369 }
7b2c381c 1370 *((XPVMG**)xpvmg) = 0;
cac9b346
NC
1371}
1372
1373/* allocate another arena's worth of struct xpvgv */
1374
1375STATIC void
1376S_more_xpvgv(pTHX)
1377{
1378 XPVGV* xpvgv;
1379 XPVGV* xpvgvend;
1380 New(720, xpvgv, PERL_ARENA_SIZE/sizeof(XPVGV), XPVGV);
7b2c381c 1381 *((XPVGV**)xpvgv) = PL_xpvgv_arenaroot;
cac9b346
NC
1382 PL_xpvgv_arenaroot = xpvgv;
1383
1384 xpvgvend = &xpvgv[PERL_ARENA_SIZE / sizeof(XPVGV) - 1];
1385 PL_xpvgv_root = ++xpvgv;
1386 while (xpvgv < xpvgvend) {
7b2c381c 1387 *((XPVGV**)xpvgv) = xpvgv + 1;
cac9b346
NC
1388 xpvgv++;
1389 }
7b2c381c 1390 *((XPVGV**)xpvgv) = 0;
cac9b346
NC
1391}
1392
1393/* allocate another arena's worth of struct xpvlv */
1394
1395STATIC void
1396S_more_xpvlv(pTHX)
1397{
1398 XPVLV* xpvlv;
1399 XPVLV* xpvlvend;
1400 New(720, xpvlv, PERL_ARENA_SIZE/sizeof(XPVLV), XPVLV);
7b2c381c 1401 *((XPVLV**)xpvlv) = PL_xpvlv_arenaroot;
cac9b346
NC
1402 PL_xpvlv_arenaroot = xpvlv;
1403
1404 xpvlvend = &xpvlv[PERL_ARENA_SIZE / sizeof(XPVLV) - 1];
1405 PL_xpvlv_root = ++xpvlv;
1406 while (xpvlv < xpvlvend) {
7b2c381c 1407 *((XPVLV**)xpvlv) = xpvlv + 1;
cac9b346
NC
1408 xpvlv++;
1409 }
7b2c381c 1410 *((XPVLV**)xpvlv) = 0;
cac9b346
NC
1411}
1412
1413/* allocate another arena's worth of struct xpvbm */
1414
1415STATIC void
1416S_more_xpvbm(pTHX)
1417{
1418 XPVBM* xpvbm;
1419 XPVBM* xpvbmend;
1420 New(721, xpvbm, PERL_ARENA_SIZE/sizeof(XPVBM), XPVBM);
7b2c381c 1421 *((XPVBM**)xpvbm) = PL_xpvbm_arenaroot;
cac9b346
NC
1422 PL_xpvbm_arenaroot = xpvbm;
1423
1424 xpvbmend = &xpvbm[PERL_ARENA_SIZE / sizeof(XPVBM) - 1];
1425 PL_xpvbm_root = ++xpvbm;
1426 while (xpvbm < xpvbmend) {
7b2c381c 1427 *((XPVBM**)xpvbm) = xpvbm + 1;
cac9b346
NC
1428 xpvbm++;
1429 }
7b2c381c 1430 *((XPVBM**)xpvbm) = 0;
cac9b346 1431}
612f20c3 1432
950dc694
JC
1433#endif
1434
cac9b346
NC
1435/* grab a new NV body from the free list, allocating more if necessary */
1436
1437STATIC XPVNV*
1438S_new_xnv(pTHX)
1439{
1440 NV* xnv;
1441 LOCK_SV_MUTEX;
1442 if (!PL_xnv_root)
1443 S_more_xnv(aTHX);
1444 xnv = PL_xnv_root;
1445 PL_xnv_root = *(NV**)xnv;
1446 UNLOCK_SV_MUTEX;
1447 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
1448}
1449
1450/* return an NV body to the free list */
1451
1452STATIC void
1453S_del_xnv(pTHX_ XPVNV *p)
1454{
1455 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
1456 LOCK_SV_MUTEX;
1457 *(NV**)xnv = PL_xnv_root;
1458 PL_xnv_root = xnv;
1459 UNLOCK_SV_MUTEX;
ed6116ce
LW
1460}
1461
645c22ef
DM
1462/* grab a new struct xpv from the free list, allocating more if necessary */
1463
76e3520e 1464STATIC XPV*
cea2e8a9 1465S_new_xpv(pTHX)
463ee0b2 1466{
59813432 1467 xpv_allocated* xpv;
cbe51380
GS
1468 LOCK_SV_MUTEX;
1469 if (!PL_xpv_root)
cac9b346 1470 S_more_xpv(aTHX);
cbe51380 1471 xpv = PL_xpv_root;
59813432 1472 PL_xpv_root = *(xpv_allocated**)xpv;
cbe51380 1473 UNLOCK_SV_MUTEX;
59813432
NC
1474 /* If xpv_allocated is the same structure as XPV then the two OFFSETs
1475 sum to zero, and the pointer is unchanged. If the allocated structure
1476 is smaller (no initial IV actually allocated) then the net effect is
1477 to subtract the size of the IV from the pointer, to return a new pointer
1478 as if an initial IV were actually allocated. */
1479 return (XPV*)((char*)xpv - STRUCT_OFFSET(XPV, xpv_cur)
1480 + STRUCT_OFFSET(xpv_allocated, xpv_cur));
463ee0b2
LW
1481}
1482
645c22ef
DM
1483/* return a struct xpv to the free list */
1484
76e3520e 1485STATIC void
cea2e8a9 1486S_del_xpv(pTHX_ XPV *p)
463ee0b2 1487{
59813432
NC
1488 xpv_allocated* xpv
1489 = (xpv_allocated*)((char*)(p) + STRUCT_OFFSET(XPV, xpv_cur)
1490 - STRUCT_OFFSET(xpv_allocated, xpv_cur));
cbe51380 1491 LOCK_SV_MUTEX;
59813432
NC
1492 *(xpv_allocated**)xpv = PL_xpv_root;
1493 PL_xpv_root = xpv;
cbe51380 1494 UNLOCK_SV_MUTEX;
463ee0b2
LW
1495}
1496
645c22ef
DM
1497/* grab a new struct xpviv from the free list, allocating more if necessary */
1498
932e9ff9
VB
1499STATIC XPVIV*
1500S_new_xpviv(pTHX)
1501{
311a25d9 1502 xpviv_allocated* xpviv;
932e9ff9
VB
1503 LOCK_SV_MUTEX;
1504 if (!PL_xpviv_root)
cac9b346 1505 S_more_xpviv(aTHX);
932e9ff9 1506 xpviv = PL_xpviv_root;
311a25d9 1507 PL_xpviv_root = *(xpviv_allocated**)xpviv;
932e9ff9 1508 UNLOCK_SV_MUTEX;
311a25d9
NC
1509 /* If xpviv_allocated is the same structure as XPVIV then the two OFFSETs
1510 sum to zero, and the pointer is unchanged. If the allocated structure
1511 is smaller (no initial IV actually allocated) then the net effect is
1512 to subtract the size of the IV from the pointer, to return a new pointer
1513 as if an initial IV were actually allocated. */
1514 return (XPVIV*)((char*)xpviv - STRUCT_OFFSET(XPVIV, xpv_cur)
1515 + STRUCT_OFFSET(xpviv_allocated, xpv_cur));
932e9ff9
VB
1516}
1517
645c22ef
DM
1518/* return a struct xpviv to the free list */
1519
932e9ff9
VB
1520STATIC void
1521S_del_xpviv(pTHX_ XPVIV *p)
1522{
311a25d9
NC
1523 xpviv_allocated* xpviv
1524 = (xpviv_allocated*)((char*)(p) + STRUCT_OFFSET(XPVIV, xpv_cur)
1525 - STRUCT_OFFSET(xpviv_allocated, xpv_cur));
932e9ff9 1526 LOCK_SV_MUTEX;
311a25d9
NC
1527 *(xpviv_allocated**)xpviv = PL_xpviv_root;
1528 PL_xpviv_root = xpviv;
932e9ff9
VB
1529 UNLOCK_SV_MUTEX;
1530}
1531
645c22ef
DM
1532/* grab a new struct xpvnv from the free list, allocating more if necessary */
1533
932e9ff9
VB
1534STATIC XPVNV*
1535S_new_xpvnv(pTHX)
1536{
1537 XPVNV* xpvnv;
1538 LOCK_SV_MUTEX;
1539 if (!PL_xpvnv_root)
cac9b346 1540 S_more_xpvnv(aTHX);
932e9ff9 1541 xpvnv = PL_xpvnv_root;
7b2c381c 1542 PL_xpvnv_root = *(XPVNV**)xpvnv;
932e9ff9
VB
1543 UNLOCK_SV_MUTEX;
1544 return xpvnv;
1545}
1546
645c22ef
DM
1547/* return a struct xpvnv to the free list */
1548
932e9ff9
VB
1549STATIC void
1550S_del_xpvnv(pTHX_ XPVNV *p)
1551{
1552 LOCK_SV_MUTEX;
7b2c381c 1553 *(XPVNV**)p = PL_xpvnv_root;
932e9ff9
VB
1554 PL_xpvnv_root = p;
1555 UNLOCK_SV_MUTEX;
1556}
1557
645c22ef
DM
1558/* grab a new struct xpvcv from the free list, allocating more if necessary */
1559
932e9ff9
VB
1560STATIC XPVCV*
1561S_new_xpvcv(pTHX)
1562{
1563 XPVCV* xpvcv;
1564 LOCK_SV_MUTEX;
1565 if (!PL_xpvcv_root)
cac9b346 1566 S_more_xpvcv(aTHX);
932e9ff9 1567 xpvcv = PL_xpvcv_root;
7b2c381c 1568 PL_xpvcv_root = *(XPVCV**)xpvcv;
932e9ff9
VB
1569 UNLOCK_SV_MUTEX;
1570 return xpvcv;
1571}
1572
645c22ef
DM
1573/* return a struct xpvcv to the free list */
1574
932e9ff9
VB
1575STATIC void
1576S_del_xpvcv(pTHX_ XPVCV *p)
1577{
1578 LOCK_SV_MUTEX;
7b2c381c 1579 *(XPVCV**)p = PL_xpvcv_root;
932e9ff9
VB
1580 PL_xpvcv_root = p;
1581 UNLOCK_SV_MUTEX;
1582}
1583
645c22ef
DM
1584/* grab a new struct xpvav from the free list, allocating more if necessary */
1585
932e9ff9
VB
1586STATIC XPVAV*
1587S_new_xpvav(pTHX)
1588{
59813432 1589 xpvav_allocated* xpvav;
932e9ff9
VB
1590 LOCK_SV_MUTEX;
1591 if (!PL_xpvav_root)
cac9b346 1592 S_more_xpvav(aTHX);
932e9ff9 1593 xpvav = PL_xpvav_root;
59813432 1594 PL_xpvav_root = *(xpvav_allocated**)xpvav;
932e9ff9 1595 UNLOCK_SV_MUTEX;
59813432
NC
1596 return (XPVAV*)((char*)xpvav - STRUCT_OFFSET(XPVAV, xav_fill)
1597 + STRUCT_OFFSET(xpvav_allocated, xav_fill));
932e9ff9
VB
1598}
1599
645c22ef
DM
1600/* return a struct xpvav to the free list */
1601
932e9ff9
VB
1602STATIC void
1603S_del_xpvav(pTHX_ XPVAV *p)
1604{
59813432
NC
1605 xpvav_allocated* xpvav
1606 = (xpvav_allocated*)((char*)(p) + STRUCT_OFFSET(XPVAV, xav_fill)
1607 - STRUCT_OFFSET(xpvav_allocated, xav_fill));
932e9ff9 1608 LOCK_SV_MUTEX;
59813432
NC
1609 *(xpvav_allocated**)xpvav = PL_xpvav_root;
1610 PL_xpvav_root = xpvav;
932e9ff9
VB
1611 UNLOCK_SV_MUTEX;
1612}
1613
645c22ef
DM
1614/* grab a new struct xpvhv from the free list, allocating more if necessary */
1615
932e9ff9
VB
1616STATIC XPVHV*
1617S_new_xpvhv(pTHX)
1618{
59813432 1619 xpvhv_allocated* xpvhv;
932e9ff9
VB
1620 LOCK_SV_MUTEX;
1621 if (!PL_xpvhv_root)
cac9b346 1622 S_more_xpvhv(aTHX);
932e9ff9 1623 xpvhv = PL_xpvhv_root;
59813432 1624 PL_xpvhv_root = *(xpvhv_allocated**)xpvhv;
932e9ff9 1625 UNLOCK_SV_MUTEX;
59813432
NC
1626 return (XPVHV*)((char*)xpvhv - STRUCT_OFFSET(XPVHV, xhv_fill)
1627 + STRUCT_OFFSET(xpvhv_allocated, xhv_fill));
932e9ff9
VB
1628}
1629
645c22ef
DM
1630/* return a struct xpvhv to the free list */
1631
932e9ff9
VB
1632STATIC void
1633S_del_xpvhv(pTHX_ XPVHV *p)
1634{
59813432
NC
1635 xpvhv_allocated* xpvhv
1636 = (xpvhv_allocated*)((char*)(p) + STRUCT_OFFSET(XPVHV, xhv_fill)
1637 - STRUCT_OFFSET(xpvhv_allocated, xhv_fill));
932e9ff9 1638 LOCK_SV_MUTEX;
59813432
NC
1639 *(xpvhv_allocated**)xpvhv = PL_xpvhv_root;
1640 PL_xpvhv_root = xpvhv;
932e9ff9
VB
1641 UNLOCK_SV_MUTEX;
1642}
1643
645c22ef
DM
1644/* grab a new struct xpvmg from the free list, allocating more if necessary */
1645
932e9ff9
VB
1646STATIC XPVMG*
1647S_new_xpvmg(pTHX)
1648{
1649 XPVMG* xpvmg;
1650 LOCK_SV_MUTEX;
1651 if (!PL_xpvmg_root)
cac9b346 1652 S_more_xpvmg(aTHX);
932e9ff9 1653 xpvmg = PL_xpvmg_root;
7b2c381c 1654 PL_xpvmg_root = *(XPVMG**)xpvmg;
932e9ff9
VB
1655 UNLOCK_SV_MUTEX;
1656 return xpvmg;
1657}
1658
645c22ef
DM
1659/* return a struct xpvmg to the free list */
1660
932e9ff9
VB
1661STATIC void
1662S_del_xpvmg(pTHX_ XPVMG *p)
1663{
1664 LOCK_SV_MUTEX;
7b2c381c 1665 *(XPVMG**)p = PL_xpvmg_root;
932e9ff9
VB
1666 PL_xpvmg_root = p;
1667 UNLOCK_SV_MUTEX;
1668}
1669
727879eb
NC
1670/* grab a new struct xpvgv from the free list, allocating more if necessary */
1671
1672STATIC XPVGV*
1673S_new_xpvgv(pTHX)
1674{
1675 XPVGV* xpvgv;
1676 LOCK_SV_MUTEX;
1677 if (!PL_xpvgv_root)
cac9b346 1678 S_more_xpvgv(aTHX);
727879eb 1679 xpvgv = PL_xpvgv_root;
7b2c381c 1680 PL_xpvgv_root = *(XPVGV**)xpvgv;
727879eb
NC
1681 UNLOCK_SV_MUTEX;
1682 return xpvgv;
1683}
1684
1685/* return a struct xpvgv to the free list */
1686
1687STATIC void
1688S_del_xpvgv(pTHX_ XPVGV *p)
1689{
1690 LOCK_SV_MUTEX;
7b2c381c 1691 *(XPVGV**)p = PL_xpvgv_root;
727879eb
NC
1692 PL_xpvgv_root = p;
1693 UNLOCK_SV_MUTEX;
1694}
1695
645c22ef
DM
1696/* grab a new struct xpvlv from the free list, allocating more if necessary */
1697
932e9ff9
VB
1698STATIC XPVLV*
1699S_new_xpvlv(pTHX)
1700{
1701 XPVLV* xpvlv;
1702 LOCK_SV_MUTEX;
1703 if (!PL_xpvlv_root)
cac9b346 1704 S_more_xpvlv(aTHX);
932e9ff9 1705 xpvlv = PL_xpvlv_root;
7b2c381c 1706 PL_xpvlv_root = *(XPVLV**)xpvlv;
932e9ff9
VB
1707 UNLOCK_SV_MUTEX;
1708 return xpvlv;
1709}
1710
645c22ef
DM
1711/* return a struct xpvlv to the free list */
1712
932e9ff9
VB
1713STATIC void
1714S_del_xpvlv(pTHX_ XPVLV *p)
1715{
1716 LOCK_SV_MUTEX;
7b2c381c 1717 *(XPVLV**)p = PL_xpvlv_root;
932e9ff9
VB
1718 PL_xpvlv_root = p;
1719 UNLOCK_SV_MUTEX;
1720}
1721
645c22ef
DM
1722/* grab a new struct xpvbm from the free list, allocating more if necessary */
1723
932e9ff9
VB
1724STATIC XPVBM*
1725S_new_xpvbm(pTHX)
1726{
1727 XPVBM* xpvbm;
1728 LOCK_SV_MUTEX;
1729 if (!PL_xpvbm_root)
cac9b346 1730 S_more_xpvbm(aTHX);
932e9ff9 1731 xpvbm = PL_xpvbm_root;
7b2c381c 1732 PL_xpvbm_root = *(XPVBM**)xpvbm;
932e9ff9
VB
1733 UNLOCK_SV_MUTEX;
1734 return xpvbm;
1735}
1736
645c22ef
DM
1737/* return a struct xpvbm to the free list */
1738
932e9ff9
VB
1739STATIC void
1740S_del_xpvbm(pTHX_ XPVBM *p)
1741{
1742 LOCK_SV_MUTEX;
7b2c381c 1743 *(XPVBM**)p = PL_xpvbm_root;
932e9ff9
VB
1744 PL_xpvbm_root = p;
1745 UNLOCK_SV_MUTEX;
1746}
1747
7bab3ede
MB
1748#define my_safemalloc(s) (void*)safemalloc(s)
1749#define my_safefree(p) safefree((char*)p)
463ee0b2 1750
d33b2eba 1751#ifdef PURIFY
463ee0b2 1752
d33b2eba
GS
1753#define new_XNV() my_safemalloc(sizeof(XPVNV))
1754#define del_XNV(p) my_safefree(p)
463ee0b2 1755
d33b2eba
GS
1756#define new_XPV() my_safemalloc(sizeof(XPV))
1757#define del_XPV(p) my_safefree(p)
9b94d1dd 1758
d33b2eba
GS
1759#define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1760#define del_XPVIV(p) my_safefree(p)
932e9ff9 1761
d33b2eba
GS
1762#define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1763#define del_XPVNV(p) my_safefree(p)
932e9ff9 1764
d33b2eba
GS
1765#define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1766#define del_XPVCV(p) my_safefree(p)
932e9ff9 1767
d33b2eba
GS
1768#define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1769#define del_XPVAV(p) my_safefree(p)
1770
1771#define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1772#define del_XPVHV(p) my_safefree(p)
1c846c1f 1773
d33b2eba
GS
1774#define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1775#define del_XPVMG(p) my_safefree(p)
1776
727879eb
NC
1777#define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1778#define del_XPVGV(p) my_safefree(p)
1779
d33b2eba
GS
1780#define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1781#define del_XPVLV(p) my_safefree(p)
1782
1783#define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1784#define del_XPVBM(p) my_safefree(p)
1785
1786#else /* !PURIFY */
1787
d33b2eba
GS
1788#define new_XNV() (void*)new_xnv()
1789#define del_XNV(p) del_xnv((XPVNV*) p)
9b94d1dd 1790
d33b2eba
GS
1791#define new_XPV() (void*)new_xpv()
1792#define del_XPV(p) del_xpv((XPV *)p)
1793
1794#define new_XPVIV() (void*)new_xpviv()
1795#define del_XPVIV(p) del_xpviv((XPVIV *)p)
1796
1797#define new_XPVNV() (void*)new_xpvnv()
1798#define del_XPVNV(p) del_xpvnv((XPVNV *)p)
1799
1800#define new_XPVCV() (void*)new_xpvcv()
1801#define del_XPVCV(p) del_xpvcv((XPVCV *)p)
1802
1803#define new_XPVAV() (void*)new_xpvav()
1804#define del_XPVAV(p) del_xpvav((XPVAV *)p)
1805
1806#define new_XPVHV() (void*)new_xpvhv()
1807#define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1c846c1f 1808
d33b2eba
GS
1809#define new_XPVMG() (void*)new_xpvmg()
1810#define del_XPVMG(p) del_xpvmg((XPVMG *)p)
1811
727879eb
NC
1812#define new_XPVGV() (void*)new_xpvgv()
1813#define del_XPVGV(p) del_xpvgv((XPVGV *)p)
1814
d33b2eba
GS
1815#define new_XPVLV() (void*)new_xpvlv()
1816#define del_XPVLV(p) del_xpvlv((XPVLV *)p)
1817
1818#define new_XPVBM() (void*)new_xpvbm()
1819#define del_XPVBM(p) del_xpvbm((XPVBM *)p)
1820
1821#endif /* PURIFY */
9b94d1dd 1822
d33b2eba
GS
1823#define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1824#define del_XPVFM(p) my_safefree(p)
1c846c1f 1825
d33b2eba
GS
1826#define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1827#define del_XPVIO(p) my_safefree(p)
8990e307 1828
954c1994
GS
1829/*
1830=for apidoc sv_upgrade
1831
ff276b08 1832Upgrade an SV to a more complex form. Generally adds a new body type to the
645c22ef 1833SV, then copies across as much information as possible from the old body.
ff276b08 1834You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
954c1994
GS
1835
1836=cut
1837*/
1838
63f97190 1839void
864dbfa3 1840Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
79072805 1841{
e763e3dc 1842
d2e56290
NC
1843 char* pv;
1844 U32 cur;
1845 U32 len;
1846 IV iv;
1847 NV nv;
1848 MAGIC* magic;
1849 HV* stash;
79072805 1850
765f542d
NC
1851 if (mt != SVt_PV && SvIsCOW(sv)) {
1852 sv_force_normal_flags(sv, 0);
f130fd45
NIS
1853 }
1854
79072805 1855 if (SvTYPE(sv) == mt)
63f97190 1856 return;
79072805 1857
d2e56290
NC
1858 pv = NULL;
1859 cur = 0;
1860 len = 0;
1861 iv = 0;
1862 nv = 0.0;
1863 magic = NULL;
1864 stash = Nullhv;
1865
79072805
LW
1866 switch (SvTYPE(sv)) {
1867 case SVt_NULL:
79072805 1868 break;
79072805 1869 case SVt_IV:
463ee0b2 1870 iv = SvIVX(sv);
ed6116ce 1871 if (mt == SVt_NV)
463ee0b2 1872 mt = SVt_PVNV;
ed6116ce
LW
1873 else if (mt < SVt_PVIV)
1874 mt = SVt_PVIV;
79072805
LW
1875 break;
1876 case SVt_NV:
463ee0b2 1877 nv = SvNVX(sv);
79072805 1878 del_XNV(SvANY(sv));
ed6116ce 1879 if (mt < SVt_PVNV)
79072805
LW
1880 mt = SVt_PVNV;
1881 break;
ed6116ce
LW
1882 case SVt_RV:
1883 pv = (char*)SvRV(sv);
ed6116ce 1884 break;
79072805 1885 case SVt_PV:
4d84ee25 1886 pv = SvPVX_mutable(sv);
79072805
LW
1887 cur = SvCUR(sv);
1888 len = SvLEN(sv);
79072805 1889 del_XPV(SvANY(sv));
748a9306
LW
1890 if (mt <= SVt_IV)
1891 mt = SVt_PVIV;
1892 else if (mt == SVt_NV)
1893 mt = SVt_PVNV;
79072805
LW
1894 break;
1895 case SVt_PVIV:
4d84ee25 1896 pv = SvPVX_mutable(sv);
79072805
LW
1897 cur = SvCUR(sv);
1898 len = SvLEN(sv);
463ee0b2 1899 iv = SvIVX(sv);
79072805
LW
1900 del_XPVIV(SvANY(sv));
1901 break;
1902 case SVt_PVNV:
4d84ee25 1903 pv = SvPVX_mutable(sv);
79072805
LW
1904 cur = SvCUR(sv);
1905 len = SvLEN(sv);
463ee0b2
LW
1906 iv = SvIVX(sv);
1907 nv = SvNVX(sv);
79072805
LW
1908 del_XPVNV(SvANY(sv));
1909 break;
1910 case SVt_PVMG:
0ec50a73
NC
1911 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1912 there's no way that it can be safely upgraded, because perl.c
1913 expects to Safefree(SvANY(PL_mess_sv)) */
1914 assert(sv != PL_mess_sv);
bce8f412
NC
1915 /* This flag bit is used to mean other things in other scalar types.
1916 Given that it only has meaning inside the pad, it shouldn't be set
1917 on anything that can get upgraded. */
1918 assert((SvFLAGS(sv) & SVpad_TYPED) == 0);
4d84ee25 1919 pv = SvPVX_mutable(sv);
79072805
LW
1920 cur = SvCUR(sv);
1921 len = SvLEN(sv);
463ee0b2
LW
1922 iv = SvIVX(sv);
1923 nv = SvNVX(sv);
79072805
LW
1924 magic = SvMAGIC(sv);
1925 stash = SvSTASH(sv);
1926 del_XPVMG(SvANY(sv));
1927 break;
1928 default:
cea2e8a9 1929 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
79072805
LW
1930 }
1931
ffb05e06
NC
1932 SvFLAGS(sv) &= ~SVTYPEMASK;
1933 SvFLAGS(sv) |= mt;
1934
79072805
LW
1935 switch (mt) {
1936 case SVt_NULL:
cea2e8a9 1937 Perl_croak(aTHX_ "Can't upgrade to undef");
79072805 1938 case SVt_IV:
339049b0 1939 SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
45977657 1940 SvIV_set(sv, iv);
79072805
LW
1941 break;
1942 case SVt_NV:
1943 SvANY(sv) = new_XNV();
9d6ce603 1944 SvNV_set(sv, nv);
79072805 1945 break;
ed6116ce 1946 case SVt_RV:
339049b0 1947 SvANY(sv) = &sv->sv_u.svu_rv;
b162af07 1948 SvRV_set(sv, (SV*)pv);
ed6116ce 1949 break;
79072805
LW
1950 case SVt_PVHV:
1951 SvANY(sv) = new_XPVHV();
463ee0b2
LW
1952 HvFILL(sv) = 0;
1953 HvMAX(sv) = 0;
8aacddc1 1954 HvTOTALKEYS(sv) = 0;
bd4b1eb5
NC
1955
1956 /* Fall through... */
1957 if (0) {
1958 case SVt_PVAV:
1959 SvANY(sv) = new_XPVAV();
1960 AvMAX(sv) = -1;
1961 AvFILLp(sv) = -1;
1962 AvALLOC(sv) = 0;
11ca45c0 1963 AvREAL_only(sv);
bd4b1eb5
NC
1964 }
1965 /* to here. */
c2bfdfaf
NC
1966 /* XXX? Only SVt_NULL is ever upgraded to AV or HV? */
1967 assert(!pv);
8bd4d4c5
NC
1968 /* FIXME. Should be able to remove all this if()... if the above
1969 assertion is genuinely always true. */
1970 if(SvOOK(sv)) {
1971 pv -= iv;
1972 SvFLAGS(sv) &= ~SVf_OOK;
1973 }
1974 Safefree(pv);
bd4b1eb5 1975 SvPV_set(sv, (char*)0);
b162af07
SP
1976 SvMAGIC_set(sv, magic);
1977 SvSTASH_set(sv, stash);
79072805 1978 break;
bd4b1eb5
NC
1979
1980 case SVt_PVIO:
1981 SvANY(sv) = new_XPVIO();
1982 Zero(SvANY(sv), 1, XPVIO);
1983 IoPAGE_LEN(sv) = 60;
1984 goto set_magic_common;
1985 case SVt_PVFM:
1986 SvANY(sv) = new_XPVFM();
1987 Zero(SvANY(sv), 1, XPVFM);
1988 goto set_magic_common;
1989 case SVt_PVBM:
1990 SvANY(sv) = new_XPVBM();
1991 BmRARE(sv) = 0;
1992 BmUSEFUL(sv) = 0;
1993 BmPREVIOUS(sv) = 0;
1994 goto set_magic_common;
1995 case SVt_PVGV:
1996 SvANY(sv) = new_XPVGV();
1997 GvGP(sv) = 0;
1998 GvNAME(sv) = 0;
1999 GvNAMELEN(sv) = 0;
2000 GvSTASH(sv) = 0;
2001 GvFLAGS(sv) = 0;
2002 goto set_magic_common;
79072805
LW
2003 case SVt_PVCV:
2004 SvANY(sv) = new_XPVCV();
748a9306 2005 Zero(SvANY(sv), 1, XPVCV);
bd4b1eb5
NC
2006 goto set_magic_common;
2007 case SVt_PVLV:
2008 SvANY(sv) = new_XPVLV();
2009 LvTARGOFF(sv) = 0;
2010 LvTARGLEN(sv) = 0;
2011 LvTARG(sv) = 0;
2012 LvTYPE(sv) = 0;
93a17b20 2013 GvGP(sv) = 0;
79072805
LW
2014 GvNAME(sv) = 0;
2015 GvNAMELEN(sv) = 0;
2016 GvSTASH(sv) = 0;
a5f75d66 2017 GvFLAGS(sv) = 0;
bd4b1eb5
NC
2018 /* Fall through. */
2019 if (0) {
2020 case SVt_PVMG:
2021 SvANY(sv) = new_XPVMG();
2022 }
2023 set_magic_common:
b162af07
SP
2024 SvMAGIC_set(sv, magic);
2025 SvSTASH_set(sv, stash);
bd4b1eb5
NC
2026 /* Fall through. */
2027 if (0) {
2028 case SVt_PVNV:
2029 SvANY(sv) = new_XPVNV();
2030 }
9d6ce603 2031 SvNV_set(sv, nv);
bd4b1eb5
NC
2032 /* Fall through. */
2033 if (0) {
2034 case SVt_PVIV:
2035 SvANY(sv) = new_XPVIV();
2036 if (SvNIOK(sv))
2037 (void)SvIOK_on(sv);
2038 SvNOK_off(sv);
2039 }
2040 SvIV_set(sv, iv);
2041 /* Fall through. */
2042 if (0) {
2043 case SVt_PV:
2044 SvANY(sv) = new_XPV();
2045 }
f880fe2f 2046 SvPV_set(sv, pv);
b162af07
SP
2047 SvCUR_set(sv, cur);
2048 SvLEN_set(sv, len);
8990e307
LW
2049 break;
2050 }
79072805
LW
2051}
2052
645c22ef
DM
2053/*
2054=for apidoc sv_backoff
2055
2056Remove any string offset. You should normally use the C<SvOOK_off> macro
2057wrapper instead.
2058
2059=cut
2060*/
2061
79072805 2062int
864dbfa3 2063Perl_sv_backoff(pTHX_ register SV *sv)
79072805
LW
2064{
2065 assert(SvOOK(sv));
b79f7545
NC
2066 assert(SvTYPE(sv) != SVt_PVHV);
2067 assert(SvTYPE(sv) != SVt_PVAV);
463ee0b2 2068 if (SvIVX(sv)) {
3f7c398e 2069 const char *s = SvPVX_const(sv);
b162af07 2070 SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
f880fe2f 2071 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
79072805 2072 SvIV_set(sv, 0);
463ee0b2 2073 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
2074 }
2075 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 2076 return 0;
79072805
LW
2077}
2078
954c1994
GS
2079/*
2080=for apidoc sv_grow
2081
645c22ef
DM
2082Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
2083upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
2084Use the C<SvGROW> wrapper instead.
954c1994
GS
2085
2086=cut
2087*/
2088
79072805 2089char *
864dbfa3 2090Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
79072805
LW
2091{
2092 register char *s;
2093
55497cff 2094#ifdef HAS_64K_LIMIT
79072805 2095 if (newlen >= 0x10000) {
1d7c1841
GS
2096 PerlIO_printf(Perl_debug_log,
2097 "Allocation too large: %"UVxf"\n", (UV)newlen);
79072805
LW
2098 my_exit(1);
2099 }
55497cff 2100#endif /* HAS_64K_LIMIT */
a0d0e21e
LW
2101 if (SvROK(sv))
2102 sv_unref(sv);
79072805
LW
2103 if (SvTYPE(sv) < SVt_PV) {
2104 sv_upgrade(sv, SVt_PV);
93524f2b 2105 s = SvPVX_mutable(sv);
79072805
LW
2106 }
2107 else if (SvOOK(sv)) { /* pv is offset? */
2108 sv_backoff(sv);
93524f2b 2109 s = SvPVX_mutable(sv);
79072805
LW
2110 if (newlen > SvLEN(sv))
2111 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
c6f8c383
GA
2112#ifdef HAS_64K_LIMIT
2113 if (newlen >= 0x10000)
2114 newlen = 0xFFFF;
2115#endif
79072805 2116 }
bc44a8a2 2117 else
4d84ee25 2118 s = SvPVX_mutable(sv);
54f0641b 2119
79072805 2120 if (newlen > SvLEN(sv)) { /* need more room? */
7a9b70e9 2121 newlen = PERL_STRLEN_ROUNDUP(newlen);
8d6dde3e 2122 if (SvLEN(sv) && s) {
7bab3ede 2123#ifdef MYMALLOC
93524f2b 2124 const STRLEN l = malloced_size((void*)SvPVX_const(sv));
8d6dde3e
IZ
2125 if (newlen <= l) {
2126 SvLEN_set(sv, l);
2127 return s;
2128 } else
c70c8a0a 2129#endif
1936d2a7 2130 s = saferealloc(s, newlen);
8d6dde3e 2131 }
bfed75c6 2132 else {
1936d2a7 2133 s = safemalloc(newlen);
3f7c398e
SP
2134 if (SvPVX_const(sv) && SvCUR(sv)) {
2135 Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
40565179 2136 }
4e83176d 2137 }
79072805 2138 SvPV_set(sv, s);
e1ec3a88 2139 SvLEN_set(sv, newlen);
79072805
LW
2140 }
2141 return s;
2142}
2143
954c1994
GS
2144/*
2145=for apidoc sv_setiv
2146
645c22ef
DM
2147Copies an integer into the given SV, upgrading first if necessary.
2148Does not handle 'set' magic. See also C<sv_setiv_mg>.
954c1994
GS
2149
2150=cut
2151*/
2152
79072805 2153void
864dbfa3 2154Perl_sv_setiv(pTHX_ register SV *sv, IV i)
79072805 2155{
765f542d 2156 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2
LW
2157 switch (SvTYPE(sv)) {
2158 case SVt_NULL:
79072805 2159 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
2160 break;
2161 case SVt_NV:
2162 sv_upgrade(sv, SVt_PVNV);
2163 break;
ed6116ce 2164 case SVt_RV:
463ee0b2 2165 case SVt_PV:
79072805 2166 sv_upgrade(sv, SVt_PVIV);
463ee0b2 2167 break;
a0d0e21e
LW
2168
2169 case SVt_PVGV:
a0d0e21e
LW
2170 case SVt_PVAV:
2171 case SVt_PVHV:
2172 case SVt_PVCV:
2173 case SVt_PVFM:
2174 case SVt_PVIO:
411caa50 2175 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
53e06cf0 2176 OP_DESC(PL_op));
463ee0b2 2177 }
a0d0e21e 2178 (void)SvIOK_only(sv); /* validate number */
45977657 2179 SvIV_set(sv, i);
463ee0b2 2180 SvTAINT(sv);
79072805
LW
2181}
2182
954c1994
GS
2183/*
2184=for apidoc sv_setiv_mg
2185
2186Like C<sv_setiv>, but also handles 'set' magic.
2187
2188=cut
2189*/
2190
79072805 2191void
864dbfa3 2192Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
ef50df4b
GS
2193{
2194 sv_setiv(sv,i);
2195 SvSETMAGIC(sv);
2196}
2197
954c1994
GS
2198/*
2199=for apidoc sv_setuv
2200
645c22ef
DM
2201Copies an unsigned integer into the given SV, upgrading first if necessary.
2202Does not handle 'set' magic. See also C<sv_setuv_mg>.
954c1994
GS
2203
2204=cut
2205*/
2206
ef50df4b 2207void
864dbfa3 2208Perl_sv_setuv(pTHX_ register SV *sv, UV u)
55497cff 2209{
55ada374
NC
2210 /* With these two if statements:
2211 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 2212
55ada374
NC
2213 without
2214 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 2215
55ada374
NC
2216 If you wish to remove them, please benchmark to see what the effect is
2217 */
28e5dec8
JH
2218 if (u <= (UV)IV_MAX) {
2219 sv_setiv(sv, (IV)u);
2220 return;
2221 }
25da4f38
IZ
2222 sv_setiv(sv, 0);
2223 SvIsUV_on(sv);
607fa7f2 2224 SvUV_set(sv, u);
55497cff 2225}
2226
954c1994
GS
2227/*
2228=for apidoc sv_setuv_mg
2229
2230Like C<sv_setuv>, but also handles 'set' magic.
2231
2232=cut
2233*/
2234
55497cff 2235void
864dbfa3 2236Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
ef50df4b 2237{
55ada374
NC
2238 /* With these two if statements:
2239 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 2240
55ada374
NC
2241 without
2242 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 2243
55ada374
NC
2244 If you wish to remove them, please benchmark to see what the effect is
2245 */
28e5dec8
JH
2246 if (u <= (UV)IV_MAX) {
2247 sv_setiv(sv, (IV)u);
2248 } else {
2249 sv_setiv(sv, 0);
2250 SvIsUV_on(sv);
2251 sv_setuv(sv,u);
2252 }
ef50df4b
GS
2253 SvSETMAGIC(sv);
2254}
2255
954c1994
GS
2256/*
2257=for apidoc sv_setnv
2258
645c22ef
DM
2259Copies a double into the given SV, upgrading first if necessary.
2260Does not handle 'set' magic. See also C<sv_setnv_mg>.
954c1994
GS
2261
2262=cut
2263*/
2264
ef50df4b 2265void
65202027 2266Perl_sv_setnv(pTHX_ register SV *sv, NV num)
79072805 2267{
765f542d 2268 SV_CHECK_THINKFIRST_COW_DROP(sv);
a0d0e21e
LW
2269 switch (SvTYPE(sv)) {
2270 case SVt_NULL:
2271 case SVt_IV:
79072805 2272 sv_upgrade(sv, SVt_NV);
a0d0e21e 2273 break;
a0d0e21e
LW
2274 case SVt_RV:
2275 case SVt_PV:
2276 case SVt_PVIV:
79072805 2277 sv_upgrade(sv, SVt_PVNV);
a0d0e21e 2278 break;
827b7e14 2279
a0d0e21e 2280 case SVt_PVGV:
a0d0e21e
LW
2281 case SVt_PVAV:
2282 case SVt_PVHV:
2283 case SVt_PVCV:
2284 case SVt_PVFM:
2285 case SVt_PVIO:
411caa50 2286 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
53e06cf0 2287 OP_NAME(PL_op));
79072805 2288 }
9d6ce603 2289 SvNV_set(sv, num);
a0d0e21e 2290 (void)SvNOK_only(sv); /* validate number */
463ee0b2 2291 SvTAINT(sv);
79072805
LW
2292}
2293
954c1994
GS
2294/*
2295=for apidoc sv_setnv_mg
2296
2297Like C<sv_setnv>, but also handles 'set' magic.
2298
2299=cut
2300*/
2301
ef50df4b 2302void
65202027 2303Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
ef50df4b
GS
2304{
2305 sv_setnv(sv,num);
2306 SvSETMAGIC(sv);
2307}
2308
645c22ef
DM
2309/* Print an "isn't numeric" warning, using a cleaned-up,
2310 * printable version of the offending string
2311 */
2312
76e3520e 2313STATIC void
cea2e8a9 2314S_not_a_number(pTHX_ SV *sv)
a0d0e21e 2315{
94463019
JH
2316 SV *dsv;
2317 char tmpbuf[64];
2318 char *pv;
2319
2320 if (DO_UTF8(sv)) {
2321 dsv = sv_2mortal(newSVpv("", 0));
2322 pv = sv_uni_display(dsv, sv, 10, 0);
2323 } else {
2324 char *d = tmpbuf;
2325 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
2326 /* each *s can expand to 4 chars + "...\0",
2327 i.e. need room for 8 chars */
ecdeb87c 2328
e62f0680
NC
2329 const char *s, *end;
2330 for (s = SvPVX_const(sv), end = s + SvCUR(sv); s < end && d < limit;
2331 s++) {
94463019
JH
2332 int ch = *s & 0xFF;
2333 if (ch & 128 && !isPRINT_LC(ch)) {
2334 *d++ = 'M';
2335 *d++ = '-';
2336 ch &= 127;
2337 }
2338 if (ch == '\n') {
2339 *d++ = '\\';
2340 *d++ = 'n';
2341 }
2342 else if (ch == '\r') {
2343 *d++ = '\\';
2344 *d++ = 'r';
2345 }
2346 else if (ch == '\f') {
2347 *d++ = '\\';
2348 *d++ = 'f';
2349 }
2350 else if (ch == '\\') {
2351 *d++ = '\\';
2352 *d++ = '\\';
2353 }
2354 else if (ch == '\0') {
2355 *d++ = '\\';
2356 *d++ = '0';
2357 }
2358 else if (isPRINT_LC(ch))
2359 *d++ = ch;
2360 else {
2361 *d++ = '^';
2362 *d++ = toCTRL(ch);
2363 }
2364 }
2365 if (s < end) {
2366 *d++ = '.';
2367 *d++ = '.';
2368 *d++ = '.';
2369 }
2370 *d = '\0';
2371 pv = tmpbuf;
a0d0e21e 2372 }
a0d0e21e 2373
533c011a 2374 if (PL_op)
9014280d 2375 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019
JH
2376 "Argument \"%s\" isn't numeric in %s", pv,
2377 OP_DESC(PL_op));
a0d0e21e 2378 else
9014280d 2379 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019 2380 "Argument \"%s\" isn't numeric", pv);
a0d0e21e
LW
2381}
2382
c2988b20
NC
2383/*
2384=for apidoc looks_like_number
2385
645c22ef
DM
2386Test if the content of an SV looks like a number (or is a number).
2387C<Inf> and C<Infinity> are treated as numbers (so will not issue a
2388non-numeric warning), even if your atof() doesn't grok them.
c2988b20
NC
2389
2390=cut
2391*/
2392
2393I32
2394Perl_looks_like_number(pTHX_ SV *sv)
2395{
a3b680e6 2396 register const char *sbegin;
c2988b20
NC
2397 STRLEN len;
2398
2399 if (SvPOK(sv)) {
3f7c398e 2400 sbegin = SvPVX_const(sv);
c2988b20
NC
2401 len = SvCUR(sv);
2402 }
2403 else if (SvPOKp(sv))
83003860 2404 sbegin = SvPV_const(sv, len);
c2988b20 2405 else
e0ab1c0e 2406 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
c2988b20
NC
2407 return grok_number(sbegin, len, NULL);
2408}
25da4f38
IZ
2409
2410/* Actually, ISO C leaves conversion of UV to IV undefined, but
2411 until proven guilty, assume that things are not that bad... */
2412
645c22ef
DM
2413/*
2414 NV_PRESERVES_UV:
2415
2416 As 64 bit platforms often have an NV that doesn't preserve all bits of
28e5dec8
JH
2417 an IV (an assumption perl has been based on to date) it becomes necessary
2418 to remove the assumption that the NV always carries enough precision to
2419 recreate the IV whenever needed, and that the NV is the canonical form.
2420 Instead, IV/UV and NV need to be given equal rights. So as to not lose
645c22ef 2421 precision as a side effect of conversion (which would lead to insanity
28e5dec8
JH
2422 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
2423 1) to distinguish between IV/UV/NV slots that have cached a valid
2424 conversion where precision was lost and IV/UV/NV slots that have a
2425 valid conversion which has lost no precision
645c22ef 2426 2) to ensure that if a numeric conversion to one form is requested that
28e5dec8
JH
2427 would lose precision, the precise conversion (or differently
2428 imprecise conversion) is also performed and cached, to prevent
2429 requests for different numeric formats on the same SV causing
2430 lossy conversion chains. (lossless conversion chains are perfectly
2431 acceptable (still))
2432
2433
2434 flags are used:
2435 SvIOKp is true if the IV slot contains a valid value
2436 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
2437 SvNOKp is true if the NV slot contains a valid value
2438 SvNOK is true only if the NV value is accurate
2439
2440 so
645c22ef 2441 while converting from PV to NV, check to see if converting that NV to an
28e5dec8
JH
2442 IV(or UV) would lose accuracy over a direct conversion from PV to
2443 IV(or UV). If it would, cache both conversions, return NV, but mark
2444 SV as IOK NOKp (ie not NOK).
2445
645c22ef 2446 While converting from PV to IV, check to see if converting that IV to an
28e5dec8
JH
2447 NV would lose accuracy over a direct conversion from PV to NV. If it
2448 would, cache both conversions, flag similarly.
2449
2450 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
2451 correctly because if IV & NV were set NV *always* overruled.
645c22ef
DM
2452 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
2453 changes - now IV and NV together means that the two are interchangeable:
28e5dec8 2454 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
d460ef45 2455
645c22ef
DM
2456 The benefit of this is that operations such as pp_add know that if
2457 SvIOK is true for both left and right operands, then integer addition
2458 can be used instead of floating point (for cases where the result won't
2459 overflow). Before, floating point was always used, which could lead to
28e5dec8
JH
2460 loss of precision compared with integer addition.
2461
2462 * making IV and NV equal status should make maths accurate on 64 bit
2463 platforms
2464 * may speed up maths somewhat if pp_add and friends start to use
645c22ef 2465 integers when possible instead of fp. (Hopefully the overhead in
28e5dec8
JH
2466 looking for SvIOK and checking for overflow will not outweigh the
2467 fp to integer speedup)
2468 * will slow down integer operations (callers of SvIV) on "inaccurate"
2469 values, as the change from SvIOK to SvIOKp will cause a call into
2470 sv_2iv each time rather than a macro access direct to the IV slot
2471 * should speed up number->string conversion on integers as IV is
645c22ef 2472 favoured when IV and NV are equally accurate
28e5dec8
JH
2473
2474 ####################################################################
645c22ef
DM
2475 You had better be using SvIOK_notUV if you want an IV for arithmetic:
2476 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
2477 On the other hand, SvUOK is true iff UV.
28e5dec8
JH
2478 ####################################################################
2479
645c22ef 2480 Your mileage will vary depending your CPU's relative fp to integer
28e5dec8
JH
2481 performance ratio.
2482*/
2483
2484#ifndef NV_PRESERVES_UV
645c22ef
DM
2485# define IS_NUMBER_UNDERFLOW_IV 1
2486# define IS_NUMBER_UNDERFLOW_UV 2
2487# define IS_NUMBER_IV_AND_UV 2
2488# define IS_NUMBER_OVERFLOW_IV 4
2489# define IS_NUMBER_OVERFLOW_UV 5
2490
2491/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
28e5dec8
JH
2492
2493/* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2494STATIC int
645c22ef 2495S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
28e5dec8 2496{
3f7c398e 2497 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
2498 if (SvNVX(sv) < (NV)IV_MIN) {
2499 (void)SvIOKp_on(sv);
2500 (void)SvNOK_on(sv);
45977657 2501 SvIV_set(sv, IV_MIN);
28e5dec8
JH
2502 return IS_NUMBER_UNDERFLOW_IV;
2503 }
2504 if (SvNVX(sv) > (NV)UV_MAX) {
2505 (void)SvIOKp_on(sv);
2506 (void)SvNOK_on(sv);
2507 SvIsUV_on(sv);
607fa7f2 2508 SvUV_set(sv, UV_MAX);
28e5dec8
JH
2509 return IS_NUMBER_OVERFLOW_UV;
2510 }
c2988b20
NC
2511 (void)SvIOKp_on(sv);
2512 (void)SvNOK_on(sv);
2513 /* Can't use strtol etc to convert this string. (See truth table in
2514 sv_2iv */
2515 if (SvNVX(sv) <= (UV)IV_MAX) {
45977657 2516 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2517 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2518 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2519 } else {
2520 /* Integer is imprecise. NOK, IOKp */
2521 }
2522 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2523 }
2524 SvIsUV_on(sv);
607fa7f2 2525 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2526 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2527 if (SvUVX(sv) == UV_MAX) {
2528 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2529 possibly be preserved by NV. Hence, it must be overflow.
2530 NOK, IOKp */
2531 return IS_NUMBER_OVERFLOW_UV;
2532 }
2533 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2534 } else {
2535 /* Integer is imprecise. NOK, IOKp */
28e5dec8 2536 }
c2988b20 2537 return IS_NUMBER_OVERFLOW_IV;
28e5dec8 2538}
645c22ef
DM
2539#endif /* !NV_PRESERVES_UV*/
2540
891f9566
YST
2541/* sv_2iv() is now a macro using Perl_sv_2iv_flags();
2542 * this function provided for binary compatibility only
2543 */
2544
2545IV
2546Perl_sv_2iv(pTHX_ register SV *sv)
2547{
2548 return sv_2iv_flags(sv, SV_GMAGIC);
2549}
2550
645c22ef 2551/*
891f9566 2552=for apidoc sv_2iv_flags
645c22ef 2553
891f9566
YST
2554Return the integer value of an SV, doing any necessary string
2555conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2556Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
645c22ef
DM
2557
2558=cut
2559*/
28e5dec8 2560
a0d0e21e 2561IV
891f9566 2562Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags)
79072805
LW
2563{
2564 if (!sv)
2565 return 0;
8990e307 2566 if (SvGMAGICAL(sv)) {
891f9566
YST
2567 if (flags & SV_GMAGIC)
2568 mg_get(sv);
463ee0b2
LW
2569 if (SvIOKp(sv))
2570 return SvIVX(sv);
748a9306 2571 if (SvNOKp(sv)) {
25da4f38 2572 return I_V(SvNVX(sv));
748a9306 2573 }
36477c24 2574 if (SvPOKp(sv) && SvLEN(sv))
2575 return asIV(sv);
3fe9a6f1 2576 if (!SvROK(sv)) {
d008e5eb 2577 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2578 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2579 report_uninit(sv);
c6ee37c5 2580 }
36477c24 2581 return 0;
3fe9a6f1 2582 }
463ee0b2 2583 }
ed6116ce 2584 if (SvTHINKFIRST(sv)) {
a0d0e21e 2585 if (SvROK(sv)) {
a0d0e21e 2586 SV* tmpstr;
1554e226 2587 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2588 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2589 return SvIV(tmpstr);
56431972 2590 return PTR2IV(SvRV(sv));
a0d0e21e 2591 }
765f542d
NC
2592 if (SvIsCOW(sv)) {
2593 sv_force_normal_flags(sv, 0);
47deb5e7 2594 }
0336b60e 2595 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2596 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2597 report_uninit(sv);
ed6116ce
LW
2598 return 0;
2599 }
79072805 2600 }
25da4f38
IZ
2601 if (SvIOKp(sv)) {
2602 if (SvIsUV(sv)) {
2603 return (IV)(SvUVX(sv));
2604 }
2605 else {
2606 return SvIVX(sv);
2607 }
463ee0b2 2608 }
748a9306 2609 if (SvNOKp(sv)) {
28e5dec8
JH
2610 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2611 * without also getting a cached IV/UV from it at the same time
2612 * (ie PV->NV conversion should detect loss of accuracy and cache
2613 * IV or UV at same time to avoid this. NWC */
25da4f38
IZ
2614
2615 if (SvTYPE(sv) == SVt_NV)
2616 sv_upgrade(sv, SVt_PVNV);
2617
28e5dec8
JH
2618 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2619 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2620 certainly cast into the IV range at IV_MAX, whereas the correct
2621 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2622 cases go to UV */
2623 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2624 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2625 if (SvNVX(sv) == (NV) SvIVX(sv)
2626#ifndef NV_PRESERVES_UV
2627 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2628 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2629 /* Don't flag it as "accurately an integer" if the number
2630 came from a (by definition imprecise) NV operation, and
2631 we're outside the range of NV integer precision */
2632#endif
2633 ) {
2634 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2635 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2636 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2637 PTR2UV(sv),
2638 SvNVX(sv),
2639 SvIVX(sv)));
2640
2641 } else {
2642 /* IV not precise. No need to convert from PV, as NV
2643 conversion would already have cached IV if it detected
2644 that PV->IV would be better than PV->NV->IV
2645 flags already correct - don't set public IOK. */
2646 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2647 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2648 PTR2UV(sv),
2649 SvNVX(sv),
2650 SvIVX(sv)));
2651 }
2652 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2653 but the cast (NV)IV_MIN rounds to a the value less (more
2654 negative) than IV_MIN which happens to be equal to SvNVX ??
2655 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2656 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2657 (NV)UVX == NVX are both true, but the values differ. :-(
2658 Hopefully for 2s complement IV_MIN is something like
2659 0x8000000000000000 which will be exact. NWC */
d460ef45 2660 }
25da4f38 2661 else {
607fa7f2 2662 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2663 if (
2664 (SvNVX(sv) == (NV) SvUVX(sv))
2665#ifndef NV_PRESERVES_UV
2666 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2667 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2668 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2669 /* Don't flag it as "accurately an integer" if the number
2670 came from a (by definition imprecise) NV operation, and
2671 we're outside the range of NV integer precision */
2672#endif
2673 )
2674 SvIOK_on(sv);
25da4f38
IZ
2675 SvIsUV_on(sv);
2676 ret_iv_max:
1c846c1f 2677 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 2678 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 2679 PTR2UV(sv),
57def98f
JH
2680 SvUVX(sv),
2681 SvUVX(sv)));
25da4f38
IZ
2682 return (IV)SvUVX(sv);
2683 }
748a9306
LW
2684 }
2685 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2686 UV value;
504618e9 2687 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
25da4f38
IZ
2688 /* We want to avoid a possible problem when we cache an IV which
2689 may be later translated to an NV, and the resulting NV is not
c2988b20
NC
2690 the same as the direct translation of the initial string
2691 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2692 be careful to ensure that the value with the .456 is around if the
2693 NV value is requested in the future).
1c846c1f 2694
25da4f38
IZ
2695 This means that if we cache such an IV, we need to cache the
2696 NV as well. Moreover, we trade speed for space, and do not
28e5dec8 2697 cache the NV if we are sure it's not needed.
25da4f38 2698 */
16b7a9a4 2699
c2988b20
NC
2700 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2701 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2702 == IS_NUMBER_IN_UV) {
5e045b90 2703 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8
JH
2704 if (SvTYPE(sv) < SVt_PVIV)
2705 sv_upgrade(sv, SVt_PVIV);
f7bbb42a 2706 (void)SvIOK_on(sv);
c2988b20
NC
2707 } else if (SvTYPE(sv) < SVt_PVNV)
2708 sv_upgrade(sv, SVt_PVNV);
28e5dec8 2709
c2988b20
NC
2710 /* If NV preserves UV then we only use the UV value if we know that
2711 we aren't going to call atof() below. If NVs don't preserve UVs
2712 then the value returned may have more precision than atof() will
2713 return, even though value isn't perfectly accurate. */
2714 if ((numtype & (IS_NUMBER_IN_UV
2715#ifdef NV_PRESERVES_UV
2716 | IS_NUMBER_NOT_INT
2717#endif
2718 )) == IS_NUMBER_IN_UV) {
2719 /* This won't turn off the public IOK flag if it was set above */
2720 (void)SvIOKp_on(sv);
2721
2722 if (!(numtype & IS_NUMBER_NEG)) {
2723 /* positive */;
2724 if (value <= (UV)IV_MAX) {
45977657 2725 SvIV_set(sv, (IV)value);
c2988b20 2726 } else {
607fa7f2 2727 SvUV_set(sv, value);
c2988b20
NC
2728 SvIsUV_on(sv);
2729 }
2730 } else {
2731 /* 2s complement assumption */
2732 if (value <= (UV)IV_MIN) {
45977657 2733 SvIV_set(sv, -(IV)value);
c2988b20
NC
2734 } else {
2735 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2736 I'm assuming it will be rare. */
c2988b20
NC
2737 if (SvTYPE(sv) < SVt_PVNV)
2738 sv_upgrade(sv, SVt_PVNV);
2739 SvNOK_on(sv);
2740 SvIOK_off(sv);
2741 SvIOKp_on(sv);
9d6ce603 2742 SvNV_set(sv, -(NV)value);
45977657 2743 SvIV_set(sv, IV_MIN);
c2988b20
NC
2744 }
2745 }
2746 }
2747 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2748 will be in the previous block to set the IV slot, and the next
2749 block to set the NV slot. So no else here. */
2750
2751 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2752 != IS_NUMBER_IN_UV) {
2753 /* It wasn't an (integer that doesn't overflow the UV). */
3f7c398e 2754 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8 2755
c2988b20
NC
2756 if (! numtype && ckWARN(WARN_NUMERIC))
2757 not_a_number(sv);
28e5dec8 2758
65202027 2759#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2760 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2761 PTR2UV(sv), SvNVX(sv)));
65202027 2762#else
1779d84d 2763 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
c2988b20 2764 PTR2UV(sv), SvNVX(sv)));
65202027 2765#endif
28e5dec8
JH
2766
2767
2768#ifdef NV_PRESERVES_UV
c2988b20
NC
2769 (void)SvIOKp_on(sv);
2770 (void)SvNOK_on(sv);
2771 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2772 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2773 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2774 SvIOK_on(sv);
28e5dec8 2775 } else {
c2988b20
NC
2776 /* Integer is imprecise. NOK, IOKp */
2777 }
2778 /* UV will not work better than IV */
2779 } else {
2780 if (SvNVX(sv) > (NV)UV_MAX) {
2781 SvIsUV_on(sv);
2782 /* Integer is inaccurate. NOK, IOKp, is UV */
607fa7f2 2783 SvUV_set(sv, UV_MAX);
c2988b20
NC
2784 SvIsUV_on(sv);
2785 } else {
607fa7f2 2786 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2787 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2788 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2789 SvIOK_on(sv);
28e5dec8
JH
2790 SvIsUV_on(sv);
2791 } else {
c2988b20
NC
2792 /* Integer is imprecise. NOK, IOKp, is UV */
2793 SvIsUV_on(sv);
28e5dec8 2794 }
28e5dec8 2795 }
c2988b20
NC
2796 goto ret_iv_max;
2797 }
28e5dec8 2798#else /* NV_PRESERVES_UV */
c2988b20
NC
2799 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2800 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2801 /* The IV slot will have been set from value returned by
2802 grok_number above. The NV slot has just been set using
2803 Atof. */
560b0c46 2804 SvNOK_on(sv);
c2988b20
NC
2805 assert (SvIOKp(sv));
2806 } else {
2807 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2808 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2809 /* Small enough to preserve all bits. */
2810 (void)SvIOKp_on(sv);
2811 SvNOK_on(sv);
45977657 2812 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2813 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2814 SvIOK_on(sv);
2815 /* Assumption: first non-preserved integer is < IV_MAX,
2816 this NV is in the preserved range, therefore: */
2817 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2818 < (UV)IV_MAX)) {
32fdb065 2819 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
2820 }
2821 } else {
2822 /* IN_UV NOT_INT
2823 0 0 already failed to read UV.
2824 0 1 already failed to read UV.
2825 1 0 you won't get here in this case. IV/UV
2826 slot set, public IOK, Atof() unneeded.
2827 1 1 already read UV.
2828 so there's no point in sv_2iuv_non_preserve() attempting
2829 to use atol, strtol, strtoul etc. */
2830 if (sv_2iuv_non_preserve (sv, numtype)
2831 >= IS_NUMBER_OVERFLOW_IV)
2832 goto ret_iv_max;
2833 }
2834 }
28e5dec8 2835#endif /* NV_PRESERVES_UV */
25da4f38 2836 }
28e5dec8 2837 } else {
599cee73 2838 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 2839 report_uninit(sv);
25da4f38
IZ
2840 if (SvTYPE(sv) < SVt_IV)
2841 /* Typically the caller expects that sv_any is not NULL now. */
2842 sv_upgrade(sv, SVt_IV);
a0d0e21e 2843 return 0;
79072805 2844 }
1d7c1841
GS
2845 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2846 PTR2UV(sv),SvIVX(sv)));
25da4f38 2847 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
2848}
2849
891f9566
YST
2850/* sv_2uv() is now a macro using Perl_sv_2uv_flags();
2851 * this function provided for binary compatibility only
2852 */
2853
2854UV
2855Perl_sv_2uv(pTHX_ register SV *sv)
2856{
2857 return sv_2uv_flags(sv, SV_GMAGIC);
2858}
2859
645c22ef 2860/*
891f9566 2861=for apidoc sv_2uv_flags
645c22ef
DM
2862
2863Return the unsigned integer value of an SV, doing any necessary string
891f9566
YST
2864conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2865Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
645c22ef
DM
2866
2867=cut
2868*/
2869
ff68c719 2870UV
891f9566 2871Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags)
ff68c719 2872{
2873 if (!sv)
2874 return 0;
2875 if (SvGMAGICAL(sv)) {
891f9566
YST
2876 if (flags & SV_GMAGIC)
2877 mg_get(sv);
ff68c719 2878 if (SvIOKp(sv))
2879 return SvUVX(sv);
2880 if (SvNOKp(sv))
2881 return U_V(SvNVX(sv));
36477c24 2882 if (SvPOKp(sv) && SvLEN(sv))
2883 return asUV(sv);
3fe9a6f1 2884 if (!SvROK(sv)) {
d008e5eb 2885 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2886 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2887 report_uninit(sv);
c6ee37c5 2888 }
36477c24 2889 return 0;
3fe9a6f1 2890 }
ff68c719 2891 }
2892 if (SvTHINKFIRST(sv)) {
2893 if (SvROK(sv)) {
ff68c719 2894 SV* tmpstr;
1554e226 2895 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2896 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2897 return SvUV(tmpstr);
56431972 2898 return PTR2UV(SvRV(sv));
ff68c719 2899 }
765f542d
NC
2900 if (SvIsCOW(sv)) {
2901 sv_force_normal_flags(sv, 0);
8a818333 2902 }
0336b60e 2903 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2904 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2905 report_uninit(sv);
ff68c719 2906 return 0;
2907 }
2908 }
25da4f38
IZ
2909 if (SvIOKp(sv)) {
2910 if (SvIsUV(sv)) {
2911 return SvUVX(sv);
2912 }
2913 else {
2914 return (UV)SvIVX(sv);
2915 }
ff68c719 2916 }
2917 if (SvNOKp(sv)) {
28e5dec8
JH
2918 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2919 * without also getting a cached IV/UV from it at the same time
2920 * (ie PV->NV conversion should detect loss of accuracy and cache
2921 * IV or UV at same time to avoid this. */
2922 /* IV-over-UV optimisation - choose to cache IV if possible */
2923
25da4f38
IZ
2924 if (SvTYPE(sv) == SVt_NV)
2925 sv_upgrade(sv, SVt_PVNV);
28e5dec8
JH
2926
2927 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2928 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2929 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2930 if (SvNVX(sv) == (NV) SvIVX(sv)
2931#ifndef NV_PRESERVES_UV
2932 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2933 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2934 /* Don't flag it as "accurately an integer" if the number
2935 came from a (by definition imprecise) NV operation, and
2936 we're outside the range of NV integer precision */
2937#endif
2938 ) {
2939 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2940 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2941 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2942 PTR2UV(sv),
2943 SvNVX(sv),
2944 SvIVX(sv)));
2945
2946 } else {
2947 /* IV not precise. No need to convert from PV, as NV
2948 conversion would already have cached IV if it detected
2949 that PV->IV would be better than PV->NV->IV
2950 flags already correct - don't set public IOK. */
2951 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2952 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2953 PTR2UV(sv),
2954 SvNVX(sv),
2955 SvIVX(sv)));
2956 }
2957 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2958 but the cast (NV)IV_MIN rounds to a the value less (more
2959 negative) than IV_MIN which happens to be equal to SvNVX ??
2960 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2961 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2962 (NV)UVX == NVX are both true, but the values differ. :-(
2963 Hopefully for 2s complement IV_MIN is something like
2964 0x8000000000000000 which will be exact. NWC */
d460ef45 2965 }
28e5dec8 2966 else {
607fa7f2 2967 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2968 if (
2969 (SvNVX(sv) == (NV) SvUVX(sv))
2970#ifndef NV_PRESERVES_UV
2971 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2972 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2973 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2974 /* Don't flag it as "accurately an integer" if the number
2975 came from a (by definition imprecise) NV operation, and
2976 we're outside the range of NV integer precision */
2977#endif
2978 )
2979 SvIOK_on(sv);
2980 SvIsUV_on(sv);
1c846c1f 2981 DEBUG_c(PerlIO_printf(Perl_debug_log,
28e5dec8 2982 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
57def98f 2983 PTR2UV(sv),
28e5dec8
JH
2984 SvUVX(sv),
2985 SvUVX(sv)));
25da4f38 2986 }
ff68c719 2987 }
2988 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2989 UV value;
504618e9 2990 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
25da4f38
IZ
2991
2992 /* We want to avoid a possible problem when we cache a UV which
2993 may be later translated to an NV, and the resulting NV is not
2994 the translation of the initial data.
1c846c1f 2995
25da4f38
IZ
2996 This means that if we cache such a UV, we need to cache the
2997 NV as well. Moreover, we trade speed for space, and do not
2998 cache the NV if not needed.
2999 */
16b7a9a4 3000
c2988b20
NC
3001 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
3002 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3003 == IS_NUMBER_IN_UV) {
5e045b90 3004 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8 3005 if (SvTYPE(sv) < SVt_PVIV)
f7bbb42a
JH
3006 sv_upgrade(sv, SVt_PVIV);
3007 (void)SvIOK_on(sv);
c2988b20
NC
3008 } else if (SvTYPE(sv) < SVt_PVNV)
3009 sv_upgrade(sv, SVt_PVNV);
d460ef45 3010
c2988b20
NC
3011 /* If NV preserves UV then we only use the UV value if we know that
3012 we aren't going to call atof() below. If NVs don't preserve UVs
3013 then the value returned may have more precision than atof() will
3014 return, even though it isn't accurate. */
3015 if ((numtype & (IS_NUMBER_IN_UV
3016#ifdef NV_PRESERVES_UV
3017 | IS_NUMBER_NOT_INT
3018#endif
3019 )) == IS_NUMBER_IN_UV) {
3020 /* This won't turn off the public IOK flag if it was set above */
3021 (void)SvIOKp_on(sv);
3022
3023 if (!(numtype & IS_NUMBER_NEG)) {
3024 /* positive */;
3025 if (value <= (UV)IV_MAX) {
45977657 3026 SvIV_set(sv, (IV)value);
28e5dec8
JH
3027 } else {
3028 /* it didn't overflow, and it was positive. */
607fa7f2 3029 SvUV_set(sv, value);
28e5dec8
JH
3030 SvIsUV_on(sv);
3031 }
c2988b20
NC
3032 } else {
3033 /* 2s complement assumption */
3034 if (value <= (UV)IV_MIN) {
45977657 3035 SvIV_set(sv, -(IV)value);
c2988b20
NC
3036 } else {
3037 /* Too negative for an IV. This is a double upgrade, but
d1be9408 3038 I'm assuming it will be rare. */
c2988b20
NC
3039 if (SvTYPE(sv) < SVt_PVNV)
3040 sv_upgrade(sv, SVt_PVNV);
3041 SvNOK_on(sv);
3042 SvIOK_off(sv);
3043 SvIOKp_on(sv);
9d6ce603 3044 SvNV_set(sv, -(NV)value);
45977657 3045 SvIV_set(sv, IV_MIN);
c2988b20
NC
3046 }
3047 }
3048 }
3049
3050 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3051 != IS_NUMBER_IN_UV) {
3052 /* It wasn't an integer, or it overflowed the UV. */
3f7c398e 3053 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8 3054
c2988b20 3055 if (! numtype && ckWARN(WARN_NUMERIC))
28e5dec8
JH
3056 not_a_number(sv);
3057
3058#if defined(USE_LONG_DOUBLE)
c2988b20
NC
3059 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
3060 PTR2UV(sv), SvNVX(sv)));
28e5dec8 3061#else
1779d84d 3062 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
c2988b20 3063 PTR2UV(sv), SvNVX(sv)));
28e5dec8
JH
3064#endif
3065
3066#ifdef NV_PRESERVES_UV
c2988b20
NC
3067 (void)SvIOKp_on(sv);
3068 (void)SvNOK_on(sv);
3069 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 3070 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
3071 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
3072 SvIOK_on(sv);
3073 } else {
3074 /* Integer is imprecise. NOK, IOKp */
3075 }
3076 /* UV will not work better than IV */
3077 } else {
3078 if (SvNVX(sv) > (NV)UV_MAX) {
3079 SvIsUV_on(sv);
3080 /* Integer is inaccurate. NOK, IOKp, is UV */
607fa7f2 3081 SvUV_set(sv, UV_MAX);
c2988b20
NC
3082 SvIsUV_on(sv);
3083 } else {
607fa7f2 3084 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
3085 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
3086 NV preservse UV so can do correct comparison. */
3087 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
3088 SvIOK_on(sv);
3089 SvIsUV_on(sv);
3090 } else {
3091 /* Integer is imprecise. NOK, IOKp, is UV */
3092 SvIsUV_on(sv);
3093 }
3094 }
3095 }
28e5dec8 3096#else /* NV_PRESERVES_UV */
c2988b20
NC
3097 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3098 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
3099 /* The UV slot will have been set from value returned by
3100 grok_number above. The NV slot has just been set using
3101 Atof. */
560b0c46 3102 SvNOK_on(sv);
c2988b20
NC
3103 assert (SvIOKp(sv));
3104 } else {
3105 if (((UV)1 << NV_PRESERVES_UV_BITS) >
3106 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
3107 /* Small enough to preserve all bits. */
3108 (void)SvIOKp_on(sv);
3109 SvNOK_on(sv);
45977657 3110 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
3111 if ((NV)(SvIVX(sv)) == SvNVX(sv))
3112 SvIOK_on(sv);
3113 /* Assumption: first non-preserved integer is < IV_MAX,
3114 this NV is in the preserved range, therefore: */
3115 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
3116 < (UV)IV_MAX)) {
32fdb065 3117 Perl_croak(aTHX_ "sv_2uv 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
3118 }
3119 } else
3120 sv_2iuv_non_preserve (sv, numtype);
3121 }
28e5dec8 3122#endif /* NV_PRESERVES_UV */
f7bbb42a 3123 }
ff68c719 3124 }
3125 else {
d008e5eb 3126 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 3127 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 3128 report_uninit(sv);
c6ee37c5 3129 }
25da4f38
IZ
3130 if (SvTYPE(sv) < SVt_IV)
3131 /* Typically the caller expects that sv_any is not NULL now. */
3132 sv_upgrade(sv, SVt_IV);
ff68c719 3133 return 0;
3134 }
25da4f38 3135
1d7c1841
GS
3136 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
3137 PTR2UV(sv),SvUVX(sv)));
25da4f38 3138 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719 3139}
3140
645c22ef
DM
3141/*
3142=for apidoc sv_2nv
3143
3144Return the num value of an SV, doing any necessary string or integer
3145conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
3146macros.
3147
3148=cut
3149*/
3150
65202027 3151NV
864dbfa3 3152Perl_sv_2nv(pTHX_ register SV *sv)
79072805
LW
3153{
3154 if (!sv)
3155 return 0.0;
8990e307 3156 if (SvGMAGICAL(sv)) {
463ee0b2
LW
3157 mg_get(sv);
3158 if (SvNOKp(sv))
3159 return SvNVX(sv);
a0d0e21e 3160 if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 3161 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
504618e9 3162 !grok_number(SvPVX_const(sv), SvCUR(sv), NULL))
a0d0e21e 3163 not_a_number(sv);
3f7c398e 3164 return Atof(SvPVX_const(sv));
a0d0e21e 3165 }
25da4f38 3166 if (SvIOKp(sv)) {
1c846c1f 3167 if (SvIsUV(sv))
65202027 3168 return (NV)SvUVX(sv);
25da4f38 3169 else
65202027 3170 return (NV)SvIVX(sv);
25da4f38 3171 }
16d20bd9 3172 if (!SvROK(sv)) {
d008e5eb 3173 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 3174 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 3175 report_uninit(sv);
c6ee37c5 3176 }
66a1b24b 3177 return (NV)0;
16d20bd9 3178 }
463ee0b2 3179 }
ed6116ce 3180 if (SvTHINKFIRST(sv)) {
a0d0e21e 3181 if (SvROK(sv)) {
a0d0e21e 3182 SV* tmpstr;
1554e226 3183 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 3184 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 3185 return SvNV(tmpstr);
56431972 3186 return PTR2NV(SvRV(sv));
a0d0e21e 3187 }
765f542d
NC
3188 if (SvIsCOW(sv)) {
3189 sv_force_normal_flags(sv, 0);
8a818333 3190 }
0336b60e 3191 if (SvREADONLY(sv) && !SvOK(sv)) {
599cee73 3192 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 3193 report_uninit(sv);
ed6116ce
LW
3194 return 0.0;
3195 }
79072805
LW
3196 }
3197 if (SvTYPE(sv) < SVt_NV) {
463ee0b2
LW
3198 if (SvTYPE(sv) == SVt_IV)
3199 sv_upgrade(sv, SVt_PVNV);
3200 else
3201 sv_upgrade(sv, SVt_NV);
906f284f 3202#ifdef USE_LONG_DOUBLE
097ee67d 3203 DEBUG_c({
f93f4e46 3204 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
3205 PerlIO_printf(Perl_debug_log,
3206 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
3207 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
3208 RESTORE_NUMERIC_LOCAL();
3209 });
65202027 3210#else
572bbb43 3211 DEBUG_c({
f93f4e46 3212 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 3213 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
1d7c1841 3214 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
3215 RESTORE_NUMERIC_LOCAL();
3216 });
572bbb43 3217#endif
79072805
LW
3218 }
3219 else if (SvTYPE(sv) < SVt_PVNV)
3220 sv_upgrade(sv, SVt_PVNV);
59d8ce62
NC
3221 if (SvNOKp(sv)) {
3222 return SvNVX(sv);
61604483 3223 }
59d8ce62 3224 if (SvIOKp(sv)) {
9d6ce603 3225 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
28e5dec8
JH
3226#ifdef NV_PRESERVES_UV
3227 SvNOK_on(sv);
3228#else
3229 /* Only set the public NV OK flag if this NV preserves the IV */
3230 /* Check it's not 0xFFFFFFFFFFFFFFFF */
3231 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
3232 : (SvIVX(sv) == I_V(SvNVX(sv))))
3233 SvNOK_on(sv);
3234 else
3235 SvNOKp_on(sv);
3236#endif
93a17b20 3237 }
748a9306 3238 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 3239 UV value;
3f7c398e 3240 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
c2988b20 3241 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
a0d0e21e 3242 not_a_number(sv);
28e5dec8 3243#ifdef NV_PRESERVES_UV
c2988b20
NC
3244 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3245 == IS_NUMBER_IN_UV) {
5e045b90 3246 /* It's definitely an integer */
9d6ce603 3247 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
c2988b20 3248 } else
3f7c398e 3249 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
3250 SvNOK_on(sv);
3251#else
3f7c398e 3252 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
3253 /* Only set the public NV OK flag if this NV preserves the value in
3254 the PV at least as well as an IV/UV would.
3255 Not sure how to do this 100% reliably. */
3256 /* if that shift count is out of range then Configure's test is
3257 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
3258 UV_BITS */
3259 if (((UV)1 << NV_PRESERVES_UV_BITS) >
c2988b20 3260 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
28e5dec8 3261 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
c2988b20
NC
3262 } else if (!(numtype & IS_NUMBER_IN_UV)) {
3263 /* Can't use strtol etc to convert this string, so don't try.
3264 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
3265 SvNOK_on(sv);
3266 } else {
3267 /* value has been set. It may not be precise. */
3268 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
3269 /* 2s complement assumption for (UV)IV_MIN */
3270 SvNOK_on(sv); /* Integer is too negative. */
3271 } else {
3272 SvNOKp_on(sv);
3273 SvIOKp_on(sv);
6fa402ec 3274
c2988b20 3275 if (numtype & IS_NUMBER_NEG) {
45977657 3276 SvIV_set(sv, -(IV)value);
c2988b20 3277 } else if (value <= (UV)IV_MAX) {
45977657 3278 SvIV_set(sv, (IV)value);
c2988b20 3279 } else {
607fa7f2 3280 SvUV_set(sv, value);
c2988b20
NC
3281 SvIsUV_on(sv);
3282 }
3283
3284 if (numtype & IS_NUMBER_NOT_INT) {
3285 /* I believe that even if the original PV had decimals,
3286 they are lost beyond the limit of the FP precision.
3287 However, neither is canonical, so both only get p
3288 flags. NWC, 2000/11/25 */
3289 /* Both already have p flags, so do nothing */
3290 } else {
66a1b24b 3291 const NV nv = SvNVX(sv);
c2988b20
NC
3292 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
3293 if (SvIVX(sv) == I_V(nv)) {
3294 SvNOK_on(sv);
3295 SvIOK_on(sv);
3296 } else {
3297 SvIOK_on(sv);
3298 /* It had no "." so it must be integer. */
3299 }
3300 } else {
3301 /* between IV_MAX and NV(UV_MAX).
3302 Could be slightly > UV_MAX */
6fa402ec 3303
c2988b20
NC
3304 if (numtype & IS_NUMBER_NOT_INT) {
3305 /* UV and NV both imprecise. */
3306 } else {
66a1b24b 3307 const UV nv_as_uv = U_V(nv);
c2988b20
NC
3308
3309 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
3310 SvNOK_on(sv);
3311 SvIOK_on(sv);
3312 } else {
3313 SvIOK_on(sv);
3314 }
3315 }
3316 }
3317 }
3318 }
3319 }
28e5dec8 3320#endif /* NV_PRESERVES_UV */
93a17b20 3321 }
79072805 3322 else {
599cee73 3323 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 3324 report_uninit(sv);
25da4f38
IZ
3325 if (SvTYPE(sv) < SVt_NV)
3326 /* Typically the caller expects that sv_any is not NULL now. */
28e5dec8
JH
3327 /* XXX Ilya implies that this is a bug in callers that assume this
3328 and ideally should be fixed. */
25da4f38 3329 sv_upgrade(sv, SVt_NV);
a0d0e21e 3330 return 0.0;
79072805 3331 }
572bbb43 3332#if defined(USE_LONG_DOUBLE)
097ee67d 3333 DEBUG_c({
f93f4e46 3334 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
3335 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
3336 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
3337 RESTORE_NUMERIC_LOCAL();
3338 });
65202027 3339#else
572bbb43 3340 DEBUG_c({
f93f4e46 3341 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 3342 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
1d7c1841 3343 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
3344 RESTORE_NUMERIC_LOCAL();
3345 });
572bbb43 3346#endif
463ee0b2 3347 return SvNVX(sv);
79072805
LW
3348}
3349
645c22ef
DM
3350/* asIV(): extract an integer from the string value of an SV.
3351 * Caller must validate PVX */
3352
76e3520e 3353STATIC IV
cea2e8a9 3354S_asIV(pTHX_ SV *sv)
36477c24 3355{
c2988b20 3356 UV value;
66a1b24b 3357 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
c2988b20
NC
3358
3359 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3360 == IS_NUMBER_IN_UV) {
645c22ef 3361 /* It's definitely an integer */
c2988b20
NC
3362 if (numtype & IS_NUMBER_NEG) {
3363 if (value < (UV)IV_MIN)
3364 return -(IV)value;
3365 } else {
3366 if (value < (UV)IV_MAX)
3367 return (IV)value;
3368 }
3369 }
d008e5eb 3370 if (!numtype) {
d008e5eb
GS
3371 if (ckWARN(WARN_NUMERIC))
3372 not_a_number(sv);
3373 }
3f7c398e 3374 return I_V(Atof(SvPVX_const(sv)));
36477c24 3375}
3376
645c22ef
DM
3377/* asUV(): extract an unsigned integer from the string value of an SV
3378 * Caller must validate PVX */
3379
76e3520e 3380STATIC UV
cea2e8a9 3381S_asUV(pTHX_ SV *sv)
36477c24 3382{
c2988b20 3383 UV value;
504618e9 3384 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
36477c24 3385
c2988b20
NC
3386 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3387 == IS_NUMBER_IN_UV) {
645c22ef 3388 /* It's definitely an integer */
6fa402ec 3389 if (!(numtype & IS_NUMBER_NEG))
c2988b20
NC
3390 return value;
3391 }
d008e5eb 3392 if (!numtype) {
d008e5eb
GS
3393 if (ckWARN(WARN_NUMERIC))
3394 not_a_number(sv);
3395 }
3f7c398e 3396 return U_V(Atof(SvPVX_const(sv)));
36477c24 3397}
3398
645c22ef
DM
3399/*
3400=for apidoc sv_2pv_nolen
3401
3402Like C<sv_2pv()>, but doesn't return the length too. You should usually
3403use the macro wrapper C<SvPV_nolen(sv)> instead.
3404=cut
3405*/
3406
79072805 3407char *
864dbfa3 3408Perl_sv_2pv_nolen(pTHX_ register SV *sv)
1fa8b10d 3409{
dafda6d1 3410 return sv_2pv(sv, 0);
1fa8b10d
JD
3411}
3412
645c22ef
DM
3413/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
3414 * UV as a string towards the end of buf, and return pointers to start and
3415 * end of it.
3416 *
3417 * We assume that buf is at least TYPE_CHARS(UV) long.
3418 */
3419
864dbfa3 3420static char *
25da4f38
IZ
3421uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
3422{
25da4f38
IZ
3423 char *ptr = buf + TYPE_CHARS(UV);
3424 char *ebuf = ptr;
3425 int sign;
25da4f38
IZ
3426
3427 if (is_uv)
3428 sign = 0;
3429 else if (iv >= 0) {
3430 uv = iv;
3431 sign = 0;
3432 } else {
3433 uv = -iv;
3434 sign = 1;
3435 }
3436 do {
eb160463 3437 *--ptr = '0' + (char)(uv % 10);
25da4f38
IZ
3438 } while (uv /= 10);
3439 if (sign)
3440 *--ptr = '-';
3441 *peob = ebuf;
3442 return ptr;
3443}
3444
09540bc3
JH
3445/* sv_2pv() is now a macro using Perl_sv_2pv_flags();
3446 * this function provided for binary compatibility only
3447 */
3448
3449char *
3450Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
3451{
3452 return sv_2pv_flags(sv, lp, SV_GMAGIC);
3453}
3454
645c22ef
DM
3455/*
3456=for apidoc sv_2pv_flags
3457
ff276b08 3458Returns a pointer to the string value of an SV, and sets *lp to its length.
645c22ef
DM
3459If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
3460if necessary.
3461Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
3462usually end up here too.
3463
3464=cut
3465*/
3466
8d6d96c1
HS
3467char *
3468Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
3469{
79072805
LW
3470 register char *s;
3471 int olderrno;
cb50f42d 3472 SV *tsv, *origsv;
25da4f38
IZ
3473 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
3474 char *tmpbuf = tbuf;
79072805 3475
463ee0b2 3476 if (!sv) {
cdb061a3
NC
3477 if (lp)
3478 *lp = 0;
73d840c0 3479 return (char *)"";
463ee0b2 3480 }
8990e307 3481 if (SvGMAGICAL(sv)) {
8d6d96c1
HS
3482 if (flags & SV_GMAGIC)
3483 mg_get(sv);
463ee0b2 3484 if (SvPOKp(sv)) {
cdb061a3
NC
3485 if (lp)
3486 *lp = SvCUR(sv);
10516c54
NC
3487 if (flags & SV_MUTABLE_RETURN)
3488 return SvPVX_mutable(sv);
4d84ee25
NC
3489 if (flags & SV_CONST_RETURN)
3490 return (char *)SvPVX_const(sv);
463ee0b2
LW
3491 return SvPVX(sv);
3492 }
cf2093f6 3493 if (SvIOKp(sv)) {
1c846c1f 3494 if (SvIsUV(sv))
57def98f 3495 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
cf2093f6 3496 else
57def98f 3497 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
46fc3d4c 3498 tsv = Nullsv;
a0d0e21e 3499 goto tokensave;
463ee0b2
LW
3500 }
3501 if (SvNOKp(sv)) {
2d4389e4 3502 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
46fc3d4c 3503 tsv = Nullsv;
a0d0e21e 3504 goto tokensave;
463ee0b2 3505 }
16d20bd9 3506 if (!SvROK(sv)) {
d008e5eb 3507 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 3508 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 3509 report_uninit(sv);
c6ee37c5 3510 }
cdb061a3
NC
3511 if (lp)
3512 *lp = 0;
73d840c0 3513 return (char *)"";
16d20bd9 3514 }
463ee0b2 3515 }
ed6116ce
LW
3516 if (SvTHINKFIRST(sv)) {
3517 if (SvROK(sv)) {
a0d0e21e 3518 SV* tmpstr;
e1ec3a88 3519 register const char *typestr;
1554e226 3520 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
b4b9a328 3521 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
50adf7d2
NC
3522 /* Unwrap this: */
3523 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr); */
3524
3525 char *pv;
3526 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
3527 if (flags & SV_CONST_RETURN) {
3528 pv = (char *) SvPVX_const(tmpstr);
3529 } else {
3530 pv = (flags & SV_MUTABLE_RETURN)
3531 ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
3532 }
3533 if (lp)
3534 *lp = SvCUR(tmpstr);
3535 } else {
3536 pv = sv_2pv_flags(tmpstr, lp, flags);
3537 }
446eaa42
YST
3538 if (SvUTF8(tmpstr))
3539 SvUTF8_on(sv);
3540 else
3541 SvUTF8_off(sv);
3542 return pv;
3543 }
cb50f42d 3544 origsv = sv;
ed6116ce
LW
3545 sv = (SV*)SvRV(sv);
3546 if (!sv)
e1ec3a88 3547 typestr = "NULLREF";
ed6116ce 3548 else {
f9277f47
IZ
3549 MAGIC *mg;
3550
ed6116ce 3551 switch (SvTYPE(sv)) {
f9277f47
IZ
3552 case SVt_PVMG:
3553 if ( ((SvFLAGS(sv) &
1c846c1f 3554 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
faf82a0b 3555 == (SVs_OBJECT|SVs_SMG))
14befaf4 3556 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
e1ec3a88 3557 const regexp *re = (regexp *)mg->mg_obj;
1bd3ad17 3558
2cd61cdb 3559 if (!mg->mg_ptr) {
e1ec3a88 3560 const char *fptr = "msix";
8782bef2
GB
3561 char reflags[6];
3562 char ch;
3563 int left = 0;
3564 int right = 4;
ff385a1b 3565 char need_newline = 0;
eb160463 3566 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
8782bef2 3567
155aba94 3568 while((ch = *fptr++)) {
8782bef2
GB
3569 if(reganch & 1) {
3570 reflags[left++] = ch;
3571 }
3572 else {
3573 reflags[right--] = ch;
3574 }
3575 reganch >>= 1;
3576 }
3577 if(left != 4) {
3578 reflags[left] = '-';
3579 left = 5;
3580 }
3581
3582 mg->mg_len = re->prelen + 4 + left;
ff385a1b
JF
3583 /*
3584 * If /x was used, we have to worry about a regex
3585 * ending with a comment later being embedded
3586 * within another regex. If so, we don't want this
3587 * regex's "commentization" to leak out to the
3588 * right part of the enclosing regex, we must cap
3589 * it with a newline.
3590 *
3591 * So, if /x was used, we scan backwards from the
3592 * end of the regex. If we find a '#' before we
3593 * find a newline, we need to add a newline
3594 * ourself. If we find a '\n' first (or if we
3595 * don't find '#' or '\n'), we don't need to add
3596 * anything. -jfriedl
3597 */
3598 if (PMf_EXTENDED & re->reganch)
3599 {
e1ec3a88 3600 const char *endptr = re->precomp + re->prelen;
ff385a1b
JF
3601 while (endptr >= re->precomp)
3602 {
e1ec3a88 3603 const char c = *(endptr--);
ff385a1b
JF
3604 if (c == '\n')
3605 break; /* don't need another */
3606 if (c == '#') {
3607 /* we end while in a comment, so we
3608 need a newline */
3609 mg->mg_len++; /* save space for it */
3610 need_newline = 1; /* note to add it */
ab01544f 3611 break;
ff385a1b
JF
3612 }
3613 }
3614 }
3615
8782bef2
GB
3616 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3617 Copy("(?", mg->mg_ptr, 2, char);
3618 Copy(reflags, mg->mg_ptr+2, left, char);
3619 Copy(":", mg->mg_ptr+left+2, 1, char);
3620 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
ff385a1b
JF
3621 if (need_newline)
3622 mg->mg_ptr[mg->mg_len - 2] = '\n';
1bd3ad17
IZ
3623 mg->mg_ptr[mg->mg_len - 1] = ')';
3624 mg->mg_ptr[mg->mg_len] = 0;
3625 }
3280af22 3626 PL_reginterp_cnt += re->program[0].next_off;
cb50f42d
YST
3627
3628 if (re->reganch & ROPT_UTF8)
3629 SvUTF8_on(origsv);
3630 else
3631 SvUTF8_off(origsv);
cdb061a3
NC
3632 if (lp)
3633 *lp = mg->mg_len;
1bd3ad17 3634 return mg->mg_ptr;
f9277f47
IZ
3635 }
3636 /* Fall through */
ed6116ce
LW
3637 case SVt_NULL:
3638 case SVt_IV:
3639 case SVt_NV:
3640 case SVt_RV:
3641 case SVt_PV:
3642 case SVt_PVIV:
3643 case SVt_PVNV:
e1ec3a88
AL
3644 case SVt_PVBM: typestr = SvROK(sv) ? "REF" : "SCALAR"; break;
3645 case SVt_PVLV: typestr = SvROK(sv) ? "REF"
be65207d
DM
3646 /* tied lvalues should appear to be
3647 * scalars for backwards compatitbility */
3648 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
3649 ? "SCALAR" : "LVALUE"; break;
e1ec3a88
AL
3650 case SVt_PVAV: typestr = "ARRAY"; break;
3651 case SVt_PVHV: typestr = "HASH"; break;
3652 case SVt_PVCV: typestr = "CODE"; break;
3653 case SVt_PVGV: typestr = "GLOB"; break;
3654 case SVt_PVFM: typestr = "FORMAT"; break;
3655 case SVt_PVIO: typestr = "IO"; break;
3656 default: typestr = "UNKNOWN"; break;
ed6116ce 3657 }
46fc3d4c 3658 tsv = NEWSV(0,0);
a5cb6b62 3659 if (SvOBJECT(sv)) {
bfcb3514 3660 const char *name = HvNAME_get(SvSTASH(sv));
a5cb6b62 3661 Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
e1ec3a88 3662 name ? name : "__ANON__" , typestr, PTR2UV(sv));
a5cb6b62 3663 }
ed6116ce 3664 else
e1ec3a88 3665 Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv));
a0d0e21e 3666 goto tokensaveref;
463ee0b2 3667 }
cdb061a3
NC
3668 if (lp)
3669 *lp = strlen(typestr);
73d840c0 3670 return (char *)typestr;
79072805 3671 }
0336b60e 3672 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 3673 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 3674 report_uninit(sv);
cdb061a3
NC
3675 if (lp)
3676 *lp = 0;
73d840c0 3677 return (char *)"";
79072805 3678 }
79072805 3679 }
28e5dec8
JH
3680 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3681 /* I'm assuming that if both IV and NV are equally valid then
3682 converting the IV is going to be more efficient */
e1ec3a88
AL
3683 const U32 isIOK = SvIOK(sv);
3684 const U32 isUIOK = SvIsUV(sv);
28e5dec8
JH
3685 char buf[TYPE_CHARS(UV)];
3686 char *ebuf, *ptr;
3687
3688 if (SvTYPE(sv) < SVt_PVIV)
3689 sv_upgrade(sv, SVt_PVIV);
3690 if (isUIOK)
3691 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3692 else
3693 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
5902b6a9
NC
3694 /* inlined from sv_setpvn */
3695 SvGROW_mutable(sv, (STRLEN)(ebuf - ptr + 1));
4d84ee25 3696 Move(ptr,SvPVX_mutable(sv),ebuf - ptr,char);
28e5dec8
JH
3697 SvCUR_set(sv, ebuf - ptr);
3698 s = SvEND(sv);
3699 *s = '\0';
3700 if (isIOK)
3701 SvIOK_on(sv);
3702 else
3703 SvIOKp_on(sv);
3704 if (isUIOK)
3705 SvIsUV_on(sv);
3706 }
3707 else if (SvNOKp(sv)) {
79072805
LW
3708 if (SvTYPE(sv) < SVt_PVNV)
3709 sv_upgrade(sv, SVt_PVNV);
1c846c1f 3710 /* The +20 is pure guesswork. Configure test needed. --jhi */
5902b6a9 3711 s = SvGROW_mutable(sv, NV_DIG + 20);
79072805 3712 olderrno = errno; /* some Xenix systems wipe out errno here */
79072805 3713#ifdef apollo
463ee0b2 3714 if (SvNVX(sv) == 0.0)
79072805
LW
3715 (void)strcpy(s,"0");
3716 else
3717#endif /*apollo*/
bbce6d69 3718 {
2d4389e4 3719 Gconvert(SvNVX(sv), NV_DIG, 0, s);
bbce6d69 3720 }
79072805 3721 errno = olderrno;
a0d0e21e
LW
3722#ifdef FIXNEGATIVEZERO
3723 if (*s == '-' && s[1] == '0' && !s[2])
3724 strcpy(s,"0");
3725#endif
79072805
LW
3726 while (*s) s++;
3727#ifdef hcx
3728 if (s[-1] == '.')
46fc3d4c 3729 *--s = '\0';
79072805
LW
3730#endif
3731 }
79072805 3732 else {
0336b60e
IZ
3733 if (ckWARN(WARN_UNINITIALIZED)
3734 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 3735 report_uninit(sv);
cdb061a3 3736 if (lp)
a0d0e21e 3737 *lp = 0;
25da4f38
IZ
3738 if (SvTYPE(sv) < SVt_PV)
3739 /* Typically the caller expects that sv_any is not NULL now. */
3740 sv_upgrade(sv, SVt_PV);
73d840c0 3741 return (char *)"";
79072805 3742 }
cdb061a3
NC
3743 {
3744 STRLEN len = s - SvPVX_const(sv);
3745 if (lp)
3746 *lp = len;
3747 SvCUR_set(sv, len);
3748 }
79072805 3749 SvPOK_on(sv);
1d7c1841 3750 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3f7c398e 3751 PTR2UV(sv),SvPVX_const(sv)));
4d84ee25
NC
3752 if (flags & SV_CONST_RETURN)
3753 return (char *)SvPVX_const(sv);
10516c54
NC
3754 if (flags & SV_MUTABLE_RETURN)
3755 return SvPVX_mutable(sv);
463ee0b2 3756 return SvPVX(sv);
a0d0e21e
LW
3757
3758 tokensave:
3759 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3760 /* Sneaky stuff here */
3761
3762 tokensaveref:
46fc3d4c 3763 if (!tsv)
96827780 3764 tsv = newSVpv(tmpbuf, 0);
46fc3d4c 3765 sv_2mortal(tsv);
cdb061a3
NC
3766 if (lp)
3767 *lp = SvCUR(tsv);
46fc3d4c 3768 return SvPVX(tsv);
a0d0e21e
LW
3769 }
3770 else {
27da23d5 3771 dVAR;
a0d0e21e 3772 STRLEN len;
73d840c0 3773 const char *t;
46fc3d4c 3774
3775 if (tsv) {
3776 sv_2mortal(tsv);
3f7c398e 3777 t = SvPVX_const(tsv);
46fc3d4c 3778 len = SvCUR(tsv);
3779 }
3780 else {
96827780
MB
3781 t = tmpbuf;
3782 len = strlen(tmpbuf);
46fc3d4c 3783 }
a0d0e21e 3784#ifdef FIXNEGATIVEZERO
46fc3d4c 3785 if (len == 2 && t[0] == '-' && t[1] == '0') {
3786 t = "0";
3787 len = 1;
3788 }
a0d0e21e 3789#endif
862a34c6 3790 SvUPGRADE(sv, SVt_PV);
cdb061a3
NC
3791 if (lp)
3792 *lp = len;
5902b6a9 3793 s = SvGROW_mutable(sv, len + 1);
a0d0e21e 3794 SvCUR_set(sv, len);
6bf554b4 3795 SvPOKp_on(sv);
e90e2364 3796 return strcpy(s, t);
a0d0e21e 3797 }
463ee0b2
LW
3798}
3799
645c22ef 3800/*
6050d10e
JP
3801=for apidoc sv_copypv
3802
3803Copies a stringified representation of the source SV into the
3804destination SV. Automatically performs any necessary mg_get and
54f0641b 3805coercion of numeric values into strings. Guaranteed to preserve
6050d10e 3806UTF-8 flag even from overloaded objects. Similar in nature to
54f0641b
NIS
3807sv_2pv[_flags] but operates directly on an SV instead of just the
3808string. Mostly uses sv_2pv_flags to do its work, except when that
6050d10e
JP
3809would lose the UTF-8'ness of the PV.
3810
3811=cut
3812*/
3813
3814void
3815Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3816{
446eaa42 3817 STRLEN len;
4d84ee25
NC
3818 const char *s;
3819 s = SvPV_const(ssv,len);
cb50f42d 3820 sv_setpvn(dsv,s,len);
446eaa42 3821 if (SvUTF8(ssv))
cb50f42d 3822 SvUTF8_on(dsv);
446eaa42 3823 else
cb50f42d 3824 SvUTF8_off(dsv);
6050d10e
JP
3825}
3826
3827/*
645c22ef
DM
3828=for apidoc sv_2pvbyte_nolen
3829
3830Return a pointer to the byte-encoded representation of the SV.
1e54db1a 3831May cause the SV to be downgraded from UTF-8 as a side-effect.
645c22ef
DM
3832
3833Usually accessed via the C<SvPVbyte_nolen> macro.
3834
3835=cut
3836*/
3837
7340a771
GS
3838char *
3839Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3840{
dafda6d1 3841 return sv_2pvbyte(sv, 0);
7340a771
GS
3842}
3843
645c22ef
DM
3844/*
3845=for apidoc sv_2pvbyte
3846
3847Return a pointer to the byte-encoded representation of the SV, and set *lp
1e54db1a 3848to its length. May cause the SV to be downgraded from UTF-8 as a
645c22ef
DM
3849side-effect.
3850
3851Usually accessed via the C<SvPVbyte> macro.
3852
3853=cut
3854*/
3855
7340a771
GS
3856char *
3857Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3858{
0875d2fe 3859 sv_utf8_downgrade(sv,0);
97972285 3860 return lp ? SvPV(sv,*lp) : SvPV_nolen(sv);
7340a771
GS
3861}
3862
645c22ef
DM
3863/*
3864=for apidoc sv_2pvutf8_nolen
3865
1e54db1a
JH
3866Return a pointer to the UTF-8-encoded representation of the SV.
3867May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3868
3869Usually accessed via the C<SvPVutf8_nolen> macro.
3870
3871=cut
3872*/
3873
7340a771
GS
3874char *
3875Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3876{
dafda6d1 3877 return sv_2pvutf8(sv, 0);
7340a771
GS
3878}
3879
645c22ef
DM
3880/*
3881=for apidoc sv_2pvutf8
3882
1e54db1a
JH
3883Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3884to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3885
3886Usually accessed via the C<SvPVutf8> macro.
3887
3888=cut
3889*/
3890
7340a771
GS
3891char *
3892Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3893{
560a288e 3894 sv_utf8_upgrade(sv);
7d59b7e4 3895 return SvPV(sv,*lp);
7340a771 3896}
1c846c1f 3897
645c22ef
DM
3898/*
3899=for apidoc sv_2bool
3900
3901This function is only called on magical items, and is only used by
8cf8f3d1 3902sv_true() or its macro equivalent.
645c22ef
DM
3903
3904=cut
3905*/
3906
463ee0b2 3907bool
864dbfa3 3908Perl_sv_2bool(pTHX_ register SV *sv)
463ee0b2 3909{
8990e307 3910 if (SvGMAGICAL(sv))
463ee0b2
LW
3911 mg_get(sv);
3912
a0d0e21e
LW
3913 if (!SvOK(sv))
3914 return 0;
3915 if (SvROK(sv)) {
a0d0e21e 3916 SV* tmpsv;
1554e226 3917 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
9e3013b1 3918 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
8a31060d 3919 return (bool)SvTRUE(tmpsv);
a0d0e21e
LW
3920 return SvRV(sv) != 0;
3921 }
463ee0b2 3922 if (SvPOKp(sv)) {
11343788
MB
3923 register XPV* Xpvtmp;
3924 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
339049b0 3925 (*sv->sv_u.svu_pv > '0' ||
11343788 3926 Xpvtmp->xpv_cur > 1 ||
339049b0 3927 (Xpvtmp->xpv_cur && *sv->sv_u.svu_pv != '0')))
463ee0b2
LW
3928 return 1;
3929 else
3930 return 0;
3931 }
3932 else {
3933 if (SvIOKp(sv))
3934 return SvIVX(sv) != 0;
3935 else {
3936 if (SvNOKp(sv))
3937 return SvNVX(sv) != 0.0;
3938 else
3939 return FALSE;
3940 }
3941 }
79072805
LW
3942}
3943
09540bc3
JH
3944/* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3945 * this function provided for binary compatibility only
3946 */
3947
3948
3949STRLEN
3950Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3951{
3952 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3953}
3954
c461cf8f
JH
3955/*
3956=for apidoc sv_utf8_upgrade
3957
78ea37eb 3958Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3959Forces the SV to string form if it is not already.
4411f3b6
NIS
3960Always sets the SvUTF8 flag to avoid future validity checks even
3961if all the bytes have hibit clear.
c461cf8f 3962
13a6c0e0
JH
3963This is not as a general purpose byte encoding to Unicode interface:
3964use the Encode extension for that.
3965
8d6d96c1
HS
3966=for apidoc sv_utf8_upgrade_flags
3967
78ea37eb 3968Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3969Forces the SV to string form if it is not already.
8d6d96c1
HS
3970Always sets the SvUTF8 flag to avoid future validity checks even
3971if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3972will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3973C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3974
13a6c0e0
JH
3975This is not as a general purpose byte encoding to Unicode interface:
3976use the Encode extension for that.
3977
8d6d96c1
HS
3978=cut
3979*/
3980
3981STRLEN
3982Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3983{
808c356f
RGS
3984 if (sv == &PL_sv_undef)
3985 return 0;
e0e62c2a
NIS
3986 if (!SvPOK(sv)) {
3987 STRLEN len = 0;
d52b7888
NC
3988 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3989 (void) sv_2pv_flags(sv,&len, flags);
3990 if (SvUTF8(sv))
3991 return len;
3992 } else {
3993 (void) SvPV_force(sv,len);
3994 }
e0e62c2a 3995 }
4411f3b6 3996
f5cee72b 3997 if (SvUTF8(sv)) {
5fec3b1d 3998 return SvCUR(sv);
f5cee72b 3999 }
5fec3b1d 4000
765f542d
NC
4001 if (SvIsCOW(sv)) {
4002 sv_force_normal_flags(sv, 0);
db42d148
NIS
4003 }
4004
88632417 4005 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
799ef3cb 4006 sv_recode_to_utf8(sv, PL_encoding);
9f4817db 4007 else { /* Assume Latin-1/EBCDIC */
c4e7c712
NC
4008 /* This function could be much more efficient if we
4009 * had a FLAG in SVs to signal if there are any hibit
4010 * chars in the PV. Given that there isn't such a flag
4011 * make the loop as fast as possible. */
93524f2b
NC
4012 const U8 *s = (U8 *) SvPVX_const(sv);
4013 const U8 *e = (U8 *) SvEND(sv);
4014 const U8 *t = s;
c4e7c712
NC
4015 int hibit = 0;
4016
4017 while (t < e) {
4018 U8 ch = *t++;
4019 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
4020 break;
4021 }
4022 if (hibit) {
4023 STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */
1e2ebb21 4024 U8 *recoded = bytes_to_utf8((U8*)s, &len);
c4e7c712
NC
4025
4026 SvPV_free(sv); /* No longer using what was there before. */
4027
1e2ebb21 4028 SvPV_set(sv, (char*)recoded);
c4e7c712
NC
4029 SvCUR_set(sv, len - 1);
4030 SvLEN_set(sv, len); /* No longer know the real size. */
4031 }
4032 /* Mark as UTF-8 even if no hibit - saves scanning loop */
4033 SvUTF8_on(sv);
560a288e 4034 }
4411f3b6 4035 return SvCUR(sv);
560a288e
GS
4036}
4037
c461cf8f
JH
4038/*
4039=for apidoc sv_utf8_downgrade
4040
78ea37eb
TS
4041Attempts to convert the PV of an SV from characters to bytes.
4042If the PV contains a character beyond byte, this conversion will fail;
4043in this case, either returns false or, if C<fail_ok> is not
c461cf8f
JH
4044true, croaks.
4045
13a6c0e0
JH
4046This is not as a general purpose Unicode to byte encoding interface:
4047use the Encode extension for that.
4048
c461cf8f
JH
4049=cut
4050*/
4051
560a288e
GS
4052bool
4053Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
4054{
78ea37eb 4055 if (SvPOKp(sv) && SvUTF8(sv)) {
fa301091 4056 if (SvCUR(sv)) {
03cfe0ae 4057 U8 *s;
652088fc 4058 STRLEN len;
fa301091 4059
765f542d
NC
4060 if (SvIsCOW(sv)) {
4061 sv_force_normal_flags(sv, 0);
4062 }
03cfe0ae
NIS
4063 s = (U8 *) SvPV(sv, len);
4064 if (!utf8_to_bytes(s, &len)) {
fa301091
JH
4065 if (fail_ok)
4066 return FALSE;
4067 else {
4068 if (PL_op)
4069 Perl_croak(aTHX_ "Wide character in %s",
53e06cf0 4070 OP_DESC(PL_op));
fa301091
JH
4071 else
4072 Perl_croak(aTHX_ "Wide character");
4073 }
4b3603a4 4074 }
b162af07 4075 SvCUR_set(sv, len);
67e989fb 4076 }
560a288e 4077 }
ffebcc3e 4078 SvUTF8_off(sv);
560a288e
GS
4079 return TRUE;
4080}
4081
c461cf8f
JH
4082/*
4083=for apidoc sv_utf8_encode
4084
78ea37eb
TS
4085Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
4086flag off so that it looks like octets again.
c461cf8f
JH
4087
4088=cut
4089*/
4090
560a288e
GS
4091void
4092Perl_sv_utf8_encode(pTHX_ register SV *sv)
4093{
4411f3b6 4094 (void) sv_utf8_upgrade(sv);
4c94c214
NC
4095 if (SvIsCOW(sv)) {
4096 sv_force_normal_flags(sv, 0);
4097 }
4098 if (SvREADONLY(sv)) {
4099 Perl_croak(aTHX_ PL_no_modify);
4100 }
560a288e
GS
4101 SvUTF8_off(sv);
4102}
4103
4411f3b6
NIS
4104/*
4105=for apidoc sv_utf8_decode
4106
78ea37eb
TS
4107If the PV of the SV is an octet sequence in UTF-8
4108and contains a multiple-byte character, the C<SvUTF8> flag is turned on
4109so that it looks like a character. If the PV contains only single-byte
4110characters, the C<SvUTF8> flag stays being off.
4111Scans PV for validity and returns false if the PV is invalid UTF-8.
4411f3b6
NIS
4112
4113=cut
4114*/
4115
560a288e
GS
4116bool
4117Perl_sv_utf8_decode(pTHX_ register SV *sv)
4118{
78ea37eb 4119 if (SvPOKp(sv)) {
93524f2b
NC
4120 const U8 *c;
4121 const U8 *e;
9cbac4c7 4122
645c22ef
DM
4123 /* The octets may have got themselves encoded - get them back as
4124 * bytes
4125 */
4126 if (!sv_utf8_downgrade(sv, TRUE))
560a288e
GS
4127 return FALSE;
4128
4129 /* it is actually just a matter of turning the utf8 flag on, but
4130 * we want to make sure everything inside is valid utf8 first.
4131 */
93524f2b 4132 c = (const U8 *) SvPVX_const(sv);
63cd0674 4133 if (!is_utf8_string(c, SvCUR(sv)+1))
67e989fb 4134 return FALSE;
93524f2b 4135 e = (const U8 *) SvEND(sv);
511c2ff0 4136 while (c < e) {
c4d5f83a
NIS
4137 U8 ch = *c++;
4138 if (!UTF8_IS_INVARIANT(ch)) {
67e989fb
JH
4139 SvUTF8_on(sv);
4140 break;
4141 }
560a288e 4142 }
560a288e
GS
4143 }
4144 return TRUE;
4145}
4146
09540bc3
JH
4147/* sv_setsv() is now a macro using Perl_sv_setsv_flags();
4148 * this function provided for binary compatibility only
4149 */
4150
4151void
4152Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
4153{
4154 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
4155}
4156
954c1994
GS
4157/*
4158=for apidoc sv_setsv
4159
645c22ef
DM
4160Copies the contents of the source SV C<ssv> into the destination SV
4161C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
4162function if the source SV needs to be reused. Does not handle 'set' magic.
4163Loosely speaking, it performs a copy-by-value, obliterating any previous
4164content of the destination.
4165
4166You probably want to use one of the assortment of wrappers, such as
4167C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
4168C<SvSetMagicSV_nosteal>.
4169
8d6d96c1
HS
4170=for apidoc sv_setsv_flags
4171
645c22ef
DM
4172Copies the contents of the source SV C<ssv> into the destination SV
4173C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
4174function if the source SV needs to be reused. Does not handle 'set' magic.
4175Loosely speaking, it performs a copy-by-value, obliterating any previous
4176content of the destination.
4177If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
5fcdf167
NC
4178C<ssv> if appropriate, else not. If the C<flags> parameter has the
4179C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
4180and C<sv_setsv_nomg> are implemented in terms of this function.
645c22ef
DM
4181
4182You probably want to use one of the assortment of wrappers, such as
4183C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
4184C<SvSetMagicSV_nosteal>.
4185
4186This is the primary function for copying scalars, and most other
4187copy-ish functions and macros use this underneath.
8d6d96c1
HS
4188
4189=cut
4190*/
4191
4192void
4193Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
4194{
8990e307
LW
4195 register U32 sflags;
4196 register int dtype;
4197 register int stype;
463ee0b2 4198
79072805
LW
4199 if (sstr == dstr)
4200 return;
765f542d 4201 SV_CHECK_THINKFIRST_COW_DROP(dstr);
79072805 4202 if (!sstr)
3280af22 4203 sstr = &PL_sv_undef;
8990e307
LW
4204 stype = SvTYPE(sstr);
4205 dtype = SvTYPE(dstr);
79072805 4206
a0d0e21e 4207 SvAMAGIC_off(dstr);
7a5fa8a2 4208 if ( SvVOK(dstr) )
ece467f9
JP
4209 {
4210 /* need to nuke the magic */
4211 mg_free(dstr);
4212 SvRMAGICAL_off(dstr);
4213 }
9e7bc3e8 4214
463ee0b2 4215 /* There's a lot of redundancy below but we're going for speed here */
79072805 4216
8990e307 4217 switch (stype) {
79072805 4218 case SVt_NULL:
aece5585 4219 undef_sstr:
20408e3c
GS
4220 if (dtype != SVt_PVGV) {
4221 (void)SvOK_off(dstr);
4222 return;
4223 }
4224 break;
463ee0b2 4225 case SVt_IV:
aece5585
GA
4226 if (SvIOK(sstr)) {
4227 switch (dtype) {
4228 case SVt_NULL:
8990e307 4229 sv_upgrade(dstr, SVt_IV);
aece5585
GA
4230 break;
4231 case SVt_NV:
8990e307 4232 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
4233 break;
4234 case SVt_RV:
4235 case SVt_PV:
a0d0e21e 4236 sv_upgrade(dstr, SVt_PVIV);
aece5585
GA
4237 break;
4238 }
4239 (void)SvIOK_only(dstr);
45977657 4240 SvIV_set(dstr, SvIVX(sstr));
25da4f38
IZ
4241 if (SvIsUV(sstr))
4242 SvIsUV_on(dstr);
27c9684d
AP
4243 if (SvTAINTED(sstr))
4244 SvTAINT(dstr);
aece5585 4245 return;
8990e307 4246 }
aece5585
GA
4247 goto undef_sstr;
4248
463ee0b2 4249 case SVt_NV:
aece5585
GA
4250 if (SvNOK(sstr)) {
4251 switch (dtype) {
4252 case SVt_NULL:
4253 case SVt_IV:
8990e307 4254 sv_upgrade(dstr, SVt_NV);
aece5585
GA
4255 break;
4256 case SVt_RV:
4257 case SVt_PV:
4258 case SVt_PVIV:
a0d0e21e 4259 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
4260 break;
4261 }
9d6ce603 4262 SvNV_set(dstr, SvNVX(sstr));
aece5585 4263 (void)SvNOK_only(dstr);
27c9684d
AP
4264 if (SvTAINTED(sstr))
4265 SvTAINT(dstr);
aece5585 4266 return;
8990e307 4267 }
aece5585
GA
4268 goto undef_sstr;
4269
ed6116ce 4270 case SVt_RV:
8990e307 4271 if (dtype < SVt_RV)
ed6116ce 4272 sv_upgrade(dstr, SVt_RV);
c07a80fd 4273 else if (dtype == SVt_PVGV &&
23bb1b96 4274 SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) {
c07a80fd 4275 sstr = SvRV(sstr);
a5f75d66 4276 if (sstr == dstr) {
1d7c1841
GS
4277 if (GvIMPORTED(dstr) != GVf_IMPORTED
4278 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4279 {
a5f75d66 4280 GvIMPORTED_on(dstr);
1d7c1841 4281 }
a5f75d66
AD
4282 GvMULTI_on(dstr);
4283 return;
4284 }
c07a80fd 4285 goto glob_assign;
4286 }
ed6116ce 4287 break;
fc36a67e 4288 case SVt_PVFM:
f8c7b90f 4289#ifdef PERL_OLD_COPY_ON_WRITE
d89fc664
NC
4290 if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) {
4291 if (dtype < SVt_PVIV)
4292 sv_upgrade(dstr, SVt_PVIV);
4293 break;
4294 }
4295 /* Fall through */
4296#endif
4297 case SVt_PV:
8990e307 4298 if (dtype < SVt_PV)
463ee0b2 4299 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
4300 break;
4301 case SVt_PVIV:
8990e307 4302 if (dtype < SVt_PVIV)
463ee0b2 4303 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
4304 break;
4305 case SVt_PVNV:
8990e307 4306 if (dtype < SVt_PVNV)
463ee0b2 4307 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 4308 break;
4633a7c4
LW
4309 case SVt_PVAV:
4310 case SVt_PVHV:
4311 case SVt_PVCV:
4633a7c4 4312 case SVt_PVIO:
a3b680e6
AL
4313 {
4314 const char * const type = sv_reftype(sstr,0);
533c011a 4315 if (PL_op)
a3b680e6 4316 Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_NAME(PL_op));
4633a7c4 4317 else
a3b680e6
AL
4318 Perl_croak(aTHX_ "Bizarre copy of %s", type);
4319 }
4633a7c4
LW
4320 break;
4321
79072805 4322 case SVt_PVGV:
8990e307 4323 if (dtype <= SVt_PVGV) {
c07a80fd 4324 glob_assign:
a5f75d66 4325 if (dtype != SVt_PVGV) {
a3b680e6
AL
4326 const char * const name = GvNAME(sstr);
4327 const STRLEN len = GvNAMELEN(sstr);
b76195c2
DM
4328 /* don't upgrade SVt_PVLV: it can hold a glob */
4329 if (dtype != SVt_PVLV)
4330 sv_upgrade(dstr, SVt_PVGV);
14befaf4 4331 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
85aff577 4332 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
a0d0e21e
LW
4333 GvNAME(dstr) = savepvn(name, len);
4334 GvNAMELEN(dstr) = len;
4335 SvFAKE_on(dstr); /* can coerce to non-glob */
4336 }
7bac28a0 4337 /* ahem, death to those who redefine active sort subs */
3280af22
NIS
4338 else if (PL_curstackinfo->si_type == PERLSI_SORT
4339 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
cea2e8a9 4340 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
7bac28a0 4341 GvNAME(dstr));
5bd07a3d 4342
7fb37951
AMS
4343#ifdef GV_UNIQUE_CHECK
4344 if (GvUNIQUE((GV*)dstr)) {
5bd07a3d
DM
4345 Perl_croak(aTHX_ PL_no_modify);
4346 }
4347#endif
4348
a0d0e21e 4349 (void)SvOK_off(dstr);
a5f75d66 4350 GvINTRO_off(dstr); /* one-shot flag */
1edc1566 4351 gp_free((GV*)dstr);
79072805 4352 GvGP(dstr) = gp_ref(GvGP(sstr));
27c9684d
AP
4353 if (SvTAINTED(sstr))
4354 SvTAINT(dstr);
1d7c1841
GS
4355 if (GvIMPORTED(dstr) != GVf_IMPORTED
4356 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4357 {
a5f75d66 4358 GvIMPORTED_on(dstr);
1d7c1841 4359 }
a5f75d66 4360 GvMULTI_on(dstr);
79072805
LW
4361 return;
4362 }
4363 /* FALL THROUGH */
4364
4365 default:
8d6d96c1 4366 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
973f89ab 4367 mg_get(sstr);
eb160463 4368 if ((int)SvTYPE(sstr) != stype) {
973f89ab
CS
4369 stype = SvTYPE(sstr);
4370 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
4371 goto glob_assign;
4372 }
4373 }
ded42b9f 4374 if (stype == SVt_PVLV)
862a34c6 4375 SvUPGRADE(dstr, SVt_PVNV);
ded42b9f 4376 else
862a34c6 4377 SvUPGRADE(dstr, (U32)stype);
79072805
LW
4378 }
4379
8990e307
LW
4380 sflags = SvFLAGS(sstr);
4381
4382 if (sflags & SVf_ROK) {
4383 if (dtype >= SVt_PV) {
4384 if (dtype == SVt_PVGV) {
4385 SV *sref = SvREFCNT_inc(SvRV(sstr));
4386 SV *dref = 0;
a3b680e6 4387 const int intro = GvINTRO(dstr);
a0d0e21e 4388
7fb37951
AMS
4389#ifdef GV_UNIQUE_CHECK
4390 if (GvUNIQUE((GV*)dstr)) {
5bd07a3d
DM
4391 Perl_croak(aTHX_ PL_no_modify);
4392 }
4393#endif
4394
a0d0e21e 4395 if (intro) {
a5f75d66 4396 GvINTRO_off(dstr); /* one-shot flag */
1d7c1841 4397 GvLINE(dstr) = CopLINE(PL_curcop);
1edc1566 4398 GvEGV(dstr) = (GV*)dstr;
a0d0e21e 4399 }
a5f75d66 4400 GvMULTI_on(dstr);
8990e307
LW
4401 switch (SvTYPE(sref)) {
4402 case SVt_PVAV:
a0d0e21e 4403 if (intro)
890ed176 4404 SAVEGENERICSV(GvAV(dstr));
a0d0e21e
LW
4405 else
4406 dref = (SV*)GvAV(dstr);
8990e307 4407 GvAV(dstr) = (AV*)sref;
39bac7f7 4408 if (!GvIMPORTED_AV(dstr)
1d7c1841
GS
4409 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4410 {
a5f75d66 4411 GvIMPORTED_AV_on(dstr);
1d7c1841 4412 }
8990e307
LW
4413 break;
4414 case SVt_PVHV:
a0d0e21e 4415 if (intro)
890ed176 4416 SAVEGENERICSV(GvHV(dstr));
a0d0e21e
LW
4417 else
4418 dref = (SV*)GvHV(dstr);
8990e307 4419 GvHV(dstr) = (HV*)sref;
39bac7f7 4420 if (!GvIMPORTED_HV(dstr)
1d7c1841
GS
4421 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4422 {
a5f75d66 4423 GvIMPORTED_HV_on(dstr);
1d7c1841 4424 }
8990e307
LW
4425 break;
4426 case SVt_PVCV:
8ebc5c01 4427 if (intro) {
4428 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
4429 SvREFCNT_dec(GvCV(dstr));
4430 GvCV(dstr) = Nullcv;
68dc0745 4431 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3280af22 4432 PL_sub_generation++;
8ebc5c01 4433 }
890ed176 4434 SAVEGENERICSV(GvCV(dstr));
8ebc5c01 4435 }
68dc0745 4436 else
4437 dref = (SV*)GvCV(dstr);
4438 if (GvCV(dstr) != (CV*)sref) {
748a9306 4439 CV* cv = GvCV(dstr);
4633a7c4 4440 if (cv) {
68dc0745 4441 if (!GvCVGEN((GV*)dstr) &&
4442 (CvROOT(cv) || CvXSUB(cv)))
4443 {
7bac28a0 4444 /* ahem, death to those who redefine
4445 * active sort subs */
3280af22
NIS
4446 if (PL_curstackinfo->si_type == PERLSI_SORT &&
4447 PL_sortcop == CvSTART(cv))
1c846c1f 4448 Perl_croak(aTHX_
7bac28a0 4449 "Can't redefine active sort subroutine %s",
4450 GvENAME((GV*)dstr));
beab0874
JT
4451 /* Redefining a sub - warning is mandatory if
4452 it was a const and its value changed. */
4453 if (ckWARN(WARN_REDEFINE)
4454 || (CvCONST(cv)
4455 && (!CvCONST((CV*)sref)
4456 || sv_cmp(cv_const_sv(cv),
4457 cv_const_sv((CV*)sref)))))
4458 {
9014280d 4459 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
beab0874 4460 CvCONST(cv)
910764e6
RGS
4461 ? "Constant subroutine %s::%s redefined"
4462 : "Subroutine %s::%s redefined",
bfcb3514 4463 HvNAME_get(GvSTASH((GV*)dstr)),
beab0874
JT
4464 GvENAME((GV*)dstr));
4465 }
9607fc9c 4466 }
fb24441d
RGS
4467 if (!intro)
4468 cv_ckproto(cv, (GV*)dstr,
93524f2b
NC
4469 SvPOK(sref)
4470 ? SvPVX_const(sref) : Nullch);
4633a7c4 4471 }
a5f75d66 4472 GvCV(dstr) = (CV*)sref;
7a4c00b4 4473 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
a5f75d66 4474 GvASSUMECV_on(dstr);
3280af22 4475 PL_sub_generation++;
a5f75d66 4476 }
39bac7f7 4477 if (!GvIMPORTED_CV(dstr)
1d7c1841
GS
4478 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4479 {
a5f75d66 4480 GvIMPORTED_CV_on(dstr);
1d7c1841 4481 }
8990e307 4482 break;
91bba347
LW
4483 case SVt_PVIO:
4484 if (intro)
890ed176 4485 SAVEGENERICSV(GvIOp(dstr));
91bba347
LW
4486 else
4487 dref = (SV*)GvIOp(dstr);
4488 GvIOp(dstr) = (IO*)sref;
4489 break;
f4d13ee9
JH
4490 case SVt_PVFM:
4491 if (intro)
890ed176 4492 SAVEGENERICSV(GvFORM(dstr));
f4d13ee9
JH
4493 else
4494 dref = (SV*)GvFORM(dstr);
4495 GvFORM(dstr) = (CV*)sref;
4496 break;
8990e307 4497 default:
a0d0e21e 4498 if (intro)
890ed176 4499 SAVEGENERICSV(GvSV(dstr));
a0d0e21e
LW
4500 else
4501 dref = (SV*)GvSV(dstr);
8990e307 4502 GvSV(dstr) = sref;
39bac7f7 4503 if (!GvIMPORTED_SV(dstr)
1d7c1841
GS
4504 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4505 {
a5f75d66 4506 GvIMPORTED_SV_on(dstr);
1d7c1841 4507 }
8990e307
LW
4508 break;
4509 }
4510 if (dref)
4511 SvREFCNT_dec(dref);
27c9684d
AP
4512 if (SvTAINTED(sstr))
4513 SvTAINT(dstr);
8990e307
LW
4514 return;
4515 }
3f7c398e 4516 if (SvPVX_const(dstr)) {
8bd4d4c5 4517 SvPV_free(dstr);
b162af07
SP
4518 SvLEN_set(dstr, 0);
4519 SvCUR_set(dstr, 0);
a0d0e21e 4520 }
8990e307 4521 }
a0d0e21e 4522 (void)SvOK_off(dstr);
b162af07 4523 SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
ed6116ce 4524 SvROK_on(dstr);
8990e307 4525 if (sflags & SVp_NOK) {
3332b3c1
JH
4526 SvNOKp_on(dstr);
4527 /* Only set the public OK flag if the source has public OK. */
4528 if (sflags & SVf_NOK)
4529 SvFLAGS(dstr) |= SVf_NOK;
9d6ce603 4530 SvNV_set(dstr, SvNVX(sstr));
ed6116ce 4531 }
8990e307 4532 if (sflags & SVp_IOK) {
3332b3c1
JH
4533 (void)SvIOKp_on(dstr);
4534 if (sflags & SVf_IOK)
4535 SvFLAGS(dstr) |= SVf_IOK;
2b1c7e3e 4536 if (sflags & SVf_IVisUV)
25da4f38 4537 SvIsUV_on(dstr);
45977657 4538 SvIV_set(dstr, SvIVX(sstr));
ed6116ce 4539 }
a0d0e21e
LW
4540 if (SvAMAGIC(sstr)) {
4541 SvAMAGIC_on(dstr);
4542 }
ed6116ce 4543 }
8990e307 4544 else if (sflags & SVp_POK) {
765f542d 4545 bool isSwipe = 0;
79072805
LW
4546
4547 /*
4548 * Check to see if we can just swipe the string. If so, it's a
4549 * possible small lose on short strings, but a big win on long ones.
3f7c398e
SP
4550 * It might even be a win on short strings if SvPVX_const(dstr)
4551 * has to be allocated and SvPVX_const(sstr) has to be freed.
79072805
LW
4552 */
4553
120fac95
NC
4554 /* Whichever path we take through the next code, we want this true,
4555 and doing it now facilitates the COW check. */
4556 (void)SvPOK_only(dstr);
4557
765f542d 4558 if (
b8f9541a
NC
4559 /* We're not already COW */
4560 ((sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY)
f8c7b90f 4561#ifndef PERL_OLD_COPY_ON_WRITE
b8f9541a
NC
4562 /* or we are, but dstr isn't a suitable target. */
4563 || (SvFLAGS(dstr) & CAN_COW_MASK) != CAN_COW_FLAGS
4564#endif
4565 )
765f542d 4566 &&
765f542d
NC
4567 !(isSwipe =
4568 (sflags & SVs_TEMP) && /* slated for free anyway? */
4569 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
5fcdf167
NC
4570 (!(flags & SV_NOSTEAL)) &&
4571 /* and we're allowed to steal temps */
765f542d
NC
4572 SvREFCNT(sstr) == 1 && /* and no other references to it? */
4573 SvLEN(sstr) && /* and really is a string */
645c22ef 4574 /* and won't be needed again, potentially */
765f542d 4575 !(PL_op && PL_op->op_type == OP_AASSIGN))
f8c7b90f 4576#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 4577 && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS
120fac95 4578 && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS
765f542d
NC
4579 && SvTYPE(sstr) >= SVt_PVIV)
4580#endif
4581 ) {
4582 /* Failed the swipe test, and it's not a shared hash key either.
4583 Have to copy the string. */
4584 STRLEN len = SvCUR(sstr);
4585 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
3f7c398e 4586 Move(SvPVX_const(sstr),SvPVX(dstr),len,char);
765f542d
NC
4587 SvCUR_set(dstr, len);
4588 *SvEND(dstr) = '\0';
765f542d 4589 } else {
f8c7b90f 4590 /* If PERL_OLD_COPY_ON_WRITE is not defined, then isSwipe will always
765f542d 4591 be true in here. */
765f542d
NC
4592 /* Either it's a shared hash key, or it's suitable for
4593 copy-on-write or we can swipe the string. */
46187eeb 4594 if (DEBUG_C_TEST) {
ed252734 4595 PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
e419cbc5
NC
4596 sv_dump(sstr);
4597 sv_dump(dstr);
46187eeb 4598 }
f8c7b90f 4599#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
4600 if (!isSwipe) {
4601 /* I believe I should acquire a global SV mutex if
4602 it's a COW sv (not a shared hash key) to stop
4603 it going un copy-on-write.
4604 If the source SV has gone un copy on write between up there
4605 and down here, then (assert() that) it is of the correct
4606 form to make it copy on write again */
4607 if ((sflags & (SVf_FAKE | SVf_READONLY))
4608 != (SVf_FAKE | SVf_READONLY)) {
4609 SvREADONLY_on(sstr);
4610 SvFAKE_on(sstr);
4611 /* Make the source SV into a loop of 1.
4612 (about to become 2) */
a29f6d03 4613 SV_COW_NEXT_SV_SET(sstr, sstr);
765f542d
NC
4614 }
4615 }
4616#endif
4617 /* Initial code is common. */
3f7c398e 4618 if (SvPVX_const(dstr)) { /* we know that dtype >= SVt_PV */
a5f75d66
AD
4619 if (SvOOK(dstr)) {
4620 SvFLAGS(dstr) &= ~SVf_OOK;
3f7c398e 4621 Safefree(SvPVX_const(dstr) - SvIVX(dstr));
a5f75d66 4622 }
50483b2c 4623 else if (SvLEN(dstr))
3f7c398e 4624 Safefree(SvPVX_const(dstr));
79072805 4625 }
765f542d 4626
765f542d
NC
4627 if (!isSwipe) {
4628 /* making another shared SV. */
4629 STRLEN cur = SvCUR(sstr);
4630 STRLEN len = SvLEN(sstr);
f8c7b90f 4631#ifdef PERL_OLD_COPY_ON_WRITE
765f542d 4632 if (len) {
b8f9541a 4633 assert (SvTYPE(dstr) >= SVt_PVIV);
765f542d
NC
4634 /* SvIsCOW_normal */
4635 /* splice us in between source and next-after-source. */
a29f6d03
NC
4636 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4637 SV_COW_NEXT_SV_SET(sstr, dstr);
940132f3 4638 SvPV_set(dstr, SvPVX_mutable(sstr));
a604c751
NC
4639 } else
4640#endif
4641 {
765f542d 4642 /* SvIsCOW_shared_hash */
46187eeb
NC
4643 DEBUG_C(PerlIO_printf(Perl_debug_log,
4644 "Copy on write: Sharing hash\n"));
b8f9541a 4645
bdd68bc3 4646 assert (SvTYPE(dstr) >= SVt_PV);
765f542d 4647 SvPV_set(dstr,
d1db91c6 4648 HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)))));
bdd68bc3 4649 }
87a1ef3d
SP
4650 SvLEN_set(dstr, len);
4651 SvCUR_set(dstr, cur);
765f542d
NC
4652 SvREADONLY_on(dstr);
4653 SvFAKE_on(dstr);
4654 /* Relesase a global SV mutex. */
4655 }
4656 else
765f542d 4657 { /* Passes the swipe test. */
78d1e721 4658 SvPV_set(dstr, SvPVX_mutable(sstr));
765f542d
NC
4659 SvLEN_set(dstr, SvLEN(sstr));
4660 SvCUR_set(dstr, SvCUR(sstr));
4661
4662 SvTEMP_off(dstr);
4663 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
4664 SvPV_set(sstr, Nullch);
4665 SvLEN_set(sstr, 0);
4666 SvCUR_set(sstr, 0);
4667 SvTEMP_off(sstr);
4668 }
4669 }
9aa983d2 4670 if (sflags & SVf_UTF8)
a7cb1f99 4671 SvUTF8_on(dstr);
8990e307 4672 if (sflags & SVp_NOK) {
3332b3c1
JH
4673 SvNOKp_on(dstr);
4674 if (sflags & SVf_NOK)
4675 SvFLAGS(dstr) |= SVf_NOK;
9d6ce603 4676 SvNV_set(dstr, SvNVX(sstr));
79072805 4677 }
8990e307 4678 if (sflags & SVp_IOK) {
3332b3c1
JH
4679 (void)SvIOKp_on(dstr);
4680 if (sflags & SVf_IOK)
4681 SvFLAGS(dstr) |= SVf_IOK;
2b1c7e3e 4682 if (sflags & SVf_IVisUV)
25da4f38 4683 SvIsUV_on(dstr);
45977657 4684 SvIV_set(dstr, SvIVX(sstr));
79072805 4685 }
92f0c265 4686 if (SvVOK(sstr)) {
7a5fa8a2 4687 MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring);
ece467f9
JP
4688 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4689 smg->mg_ptr, smg->mg_len);
439cb1c4 4690 SvRMAGICAL_on(dstr);
7a5fa8a2 4691 }
79072805 4692 }
8990e307 4693 else if (sflags & SVp_IOK) {
3332b3c1
JH
4694 if (sflags & SVf_IOK)
4695 (void)SvIOK_only(dstr);
4696 else {
9cbac4c7
DM
4697 (void)SvOK_off(dstr);
4698 (void)SvIOKp_on(dstr);
3332b3c1
JH
4699 }
4700 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
2b1c7e3e 4701 if (sflags & SVf_IVisUV)
25da4f38 4702 SvIsUV_on(dstr);
45977657 4703 SvIV_set(dstr, SvIVX(sstr));
3332b3c1
JH
4704 if (sflags & SVp_NOK) {
4705 if (sflags & SVf_NOK)
4706 (void)SvNOK_on(dstr);
4707 else
4708 (void)SvNOKp_on(dstr);
9d6ce603 4709 SvNV_set(dstr, SvNVX(sstr));
3332b3c1
JH
4710 }
4711 }
4712 else if (sflags & SVp_NOK) {
4713 if (sflags & SVf_NOK)
4714 (void)SvNOK_only(dstr);
4715 else {
9cbac4c7 4716 (void)SvOK_off(dstr);
3332b3c1
JH
4717 SvNOKp_on(dstr);
4718 }
9d6ce603 4719 SvNV_set(dstr, SvNVX(sstr));
79072805
LW
4720 }
4721 else {
20408e3c 4722 if (dtype == SVt_PVGV) {
e476b1b5 4723 if (ckWARN(WARN_MISC))
9014280d 4724 Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
20408e3c
GS
4725 }
4726 else
4727 (void)SvOK_off(dstr);
a0d0e21e 4728 }
27c9684d
AP
4729 if (SvTAINTED(sstr))
4730 SvTAINT(dstr);
79072805
LW
4731}
4732
954c1994
GS
4733/*
4734=for apidoc sv_setsv_mg
4735
4736Like C<sv_setsv>, but also handles 'set' magic.
4737
4738=cut
4739*/
4740
79072805 4741void
864dbfa3 4742Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
ef50df4b
GS
4743{
4744 sv_setsv(dstr,sstr);
4745 SvSETMAGIC(dstr);
4746}
4747
f8c7b90f 4748#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
4749SV *
4750Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr)
4751{
4752 STRLEN cur = SvCUR(sstr);
4753 STRLEN len = SvLEN(sstr);
4754 register char *new_pv;
4755
4756 if (DEBUG_C_TEST) {
4757 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4758 sstr, dstr);
4759 sv_dump(sstr);
4760 if (dstr)
4761 sv_dump(dstr);
4762 }
4763
4764 if (dstr) {
4765 if (SvTHINKFIRST(dstr))
4766 sv_force_normal_flags(dstr, SV_COW_DROP_PV);
3f7c398e
SP
4767 else if (SvPVX_const(dstr))
4768 Safefree(SvPVX_const(dstr));
ed252734
NC
4769 }
4770 else
4771 new_SV(dstr);
862a34c6 4772 SvUPGRADE(dstr, SVt_PVIV);
ed252734
NC
4773
4774 assert (SvPOK(sstr));
4775 assert (SvPOKp(sstr));
4776 assert (!SvIOK(sstr));
4777 assert (!SvIOKp(sstr));
4778 assert (!SvNOK(sstr));
4779 assert (!SvNOKp(sstr));
4780
4781 if (SvIsCOW(sstr)) {
4782
4783 if (SvLEN(sstr) == 0) {
4784 /* source is a COW shared hash key. */
ed252734
NC
4785 DEBUG_C(PerlIO_printf(Perl_debug_log,
4786 "Fast copy on write: Sharing hash\n"));
d1db91c6 4787 new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr))));
ed252734
NC
4788 goto common_exit;
4789 }
4790 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4791 } else {
4792 assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS);
862a34c6 4793 SvUPGRADE(sstr, SVt_PVIV);
ed252734
NC
4794 SvREADONLY_on(sstr);
4795 SvFAKE_on(sstr);
4796 DEBUG_C(PerlIO_printf(Perl_debug_log,
4797 "Fast copy on write: Converting sstr to COW\n"));
4798 SV_COW_NEXT_SV_SET(dstr, sstr);
4799 }
4800 SV_COW_NEXT_SV_SET(sstr, dstr);
940132f3 4801 new_pv = SvPVX_mutable(sstr);
ed252734
NC
4802
4803 common_exit:
4804 SvPV_set(dstr, new_pv);
4805 SvFLAGS(dstr) = (SVt_PVIV|SVf_POK|SVp_POK|SVf_FAKE|SVf_READONLY);
4806 if (SvUTF8(sstr))
4807 SvUTF8_on(dstr);
87a1ef3d
SP
4808 SvLEN_set(dstr, len);
4809 SvCUR_set(dstr, cur);
ed252734
NC
4810 if (DEBUG_C_TEST) {
4811 sv_dump(dstr);
4812 }
4813 return dstr;
4814}
4815#endif
4816
954c1994
GS
4817/*
4818=for apidoc sv_setpvn
4819
4820Copies a string into an SV. The C<len> parameter indicates the number of
9e09f5f2
MHM
4821bytes to be copied. If the C<ptr> argument is NULL the SV will become
4822undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>.
954c1994
GS
4823
4824=cut
4825*/
4826
ef50df4b 4827void
864dbfa3 4828Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
79072805 4829{
c6f8c383 4830 register char *dptr;
22c522df 4831
765f542d 4832 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2 4833 if (!ptr) {
a0d0e21e 4834 (void)SvOK_off(sv);
463ee0b2
LW
4835 return;
4836 }
22c522df
JH
4837 else {
4838 /* len is STRLEN which is unsigned, need to copy to signed */
a3b680e6 4839 const IV iv = len;
9c5ffd7c
JH
4840 if (iv < 0)
4841 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
22c522df 4842 }
862a34c6 4843 SvUPGRADE(sv, SVt_PV);
c6f8c383 4844
5902b6a9 4845 dptr = SvGROW(sv, len + 1);
c6f8c383
GA
4846 Move(ptr,dptr,len,char);
4847 dptr[len] = '\0';
79072805 4848 SvCUR_set(sv, len);
1aa99e6b 4849 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4850 SvTAINT(sv);
79072805
LW
4851}
4852
954c1994
GS
4853/*
4854=for apidoc sv_setpvn_mg
4855
4856Like C<sv_setpvn>, but also handles 'set' magic.
4857
4858=cut
4859*/
4860
79072805 4861void
864dbfa3 4862Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
4863{
4864 sv_setpvn(sv,ptr,len);
4865 SvSETMAGIC(sv);
4866}
4867
954c1994
GS
4868/*
4869=for apidoc sv_setpv
4870
4871Copies a string into an SV. The string must be null-terminated. Does not
4872handle 'set' magic. See C<sv_setpv_mg>.
4873
4874=cut
4875*/
4876
ef50df4b 4877void
864dbfa3 4878Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
4879{
4880 register STRLEN len;
4881
765f542d 4882 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2 4883 if (!ptr) {
a0d0e21e 4884 (void)SvOK_off(sv);
463ee0b2
LW
4885 return;
4886 }
79072805 4887 len = strlen(ptr);
862a34c6 4888 SvUPGRADE(sv, SVt_PV);
c6f8c383 4889
79072805 4890 SvGROW(sv, len + 1);
463ee0b2 4891 Move(ptr,SvPVX(sv),len+1,char);
79072805 4892 SvCUR_set(sv, len);
1aa99e6b 4893 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2
LW
4894 SvTAINT(sv);
4895}
4896
954c1994
GS
4897/*
4898=for apidoc sv_setpv_mg
4899
4900Like C<sv_setpv>, but also handles 'set' magic.
4901
4902=cut
4903*/
4904
463ee0b2 4905void
864dbfa3 4906Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b
GS
4907{
4908 sv_setpv(sv,ptr);
4909 SvSETMAGIC(sv);
4910}
4911
954c1994
GS
4912/*
4913=for apidoc sv_usepvn
4914
4915Tells an SV to use C<ptr> to find its string value. Normally the string is
1c846c1f 4916stored inside the SV but sv_usepvn allows the SV to use an outside string.
954c1994
GS
4917The C<ptr> should point to memory that was allocated by C<malloc>. The
4918string length, C<len>, must be supplied. This function will realloc the
4919memory pointed to by C<ptr>, so that pointer should not be freed or used by
4920the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
4921See C<sv_usepvn_mg>.
4922
4923=cut
4924*/
4925
ef50df4b 4926void
864dbfa3 4927Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
463ee0b2 4928{
1936d2a7 4929 STRLEN allocate;
765f542d 4930 SV_CHECK_THINKFIRST_COW_DROP(sv);
862a34c6 4931 SvUPGRADE(sv, SVt_PV);
463ee0b2 4932 if (!ptr) {
a0d0e21e 4933 (void)SvOK_off(sv);
463ee0b2
LW
4934 return;
4935 }
3f7c398e 4936 if (SvPVX_const(sv))
8bd4d4c5 4937 SvPV_free(sv);
1936d2a7
NC
4938
4939 allocate = PERL_STRLEN_ROUNDUP(len + 1);
7a9b70e9 4940 ptr = saferealloc (ptr, allocate);
f880fe2f 4941 SvPV_set(sv, ptr);
463ee0b2 4942 SvCUR_set(sv, len);
1936d2a7 4943 SvLEN_set(sv, allocate);
463ee0b2 4944 *SvEND(sv) = '\0';
1aa99e6b 4945 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4946 SvTAINT(sv);
79072805
LW
4947}
4948
954c1994
GS
4949/*
4950=for apidoc sv_usepvn_mg
4951
4952Like C<sv_usepvn>, but also handles 'set' magic.
4953
4954=cut
4955*/
4956
ef50df4b 4957void
864dbfa3 4958Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
ef50df4b 4959{
51c1089b 4960 sv_usepvn(sv,ptr,len);
ef50df4b
GS
4961 SvSETMAGIC(sv);
4962}
4963
f8c7b90f 4964#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
4965/* Need to do this *after* making the SV normal, as we need the buffer
4966 pointer to remain valid until after we've copied it. If we let go too early,
4967 another thread could invalidate it by unsharing last of the same hash key
4968 (which it can do by means other than releasing copy-on-write Svs)
4969 or by changing the other copy-on-write SVs in the loop. */
4970STATIC void
bdd68bc3 4971S_sv_release_COW(pTHX_ register SV *sv, const char *pvx, STRLEN len, SV *after)
765f542d
NC
4972{
4973 if (len) { /* this SV was SvIsCOW_normal(sv) */
4974 /* we need to find the SV pointing to us. */
4975 SV *current = SV_COW_NEXT_SV(after);
7a5fa8a2 4976
765f542d
NC
4977 if (current == sv) {
4978 /* The SV we point to points back to us (there were only two of us
4979 in the loop.)
4980 Hence other SV is no longer copy on write either. */
4981 SvFAKE_off(after);
4982 SvREADONLY_off(after);
4983 } else {
4984 /* We need to follow the pointers around the loop. */
4985 SV *next;
4986 while ((next = SV_COW_NEXT_SV(current)) != sv) {
4987 assert (next);
4988 current = next;
4989 /* don't loop forever if the structure is bust, and we have
4990 a pointer into a closed loop. */
4991 assert (current != after);
3f7c398e 4992 assert (SvPVX_const(current) == pvx);
765f542d
NC
4993 }
4994 /* Make the SV before us point to the SV after us. */
a29f6d03 4995 SV_COW_NEXT_SV_SET(current, after);
765f542d
NC
4996 }
4997 } else {
bdd68bc3 4998 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
765f542d
NC
4999 }
5000}
5001
5002int
5003Perl_sv_release_IVX(pTHX_ register SV *sv)
5004{
5005 if (SvIsCOW(sv))
5006 sv_force_normal_flags(sv, 0);
0c34ef67
MHM
5007 SvOOK_off(sv);
5008 return 0;
765f542d
NC
5009}
5010#endif
645c22ef
DM
5011/*
5012=for apidoc sv_force_normal_flags
5013
5014Undo various types of fakery on an SV: if the PV is a shared string, make
5015a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
765f542d
NC
5016an xpvmg; if we're a copy-on-write scalar, this is the on-write time when
5017we do the copy, and is also used locally. If C<SV_COW_DROP_PV> is set
5018then a copy-on-write scalar drops its PV buffer (if any) and becomes
5019SvPOK_off rather than making a copy. (Used where this scalar is about to be
d3050d9d 5020set to some other value.) In addition, the C<flags> parameter gets passed to
765f542d
NC
5021C<sv_unref_flags()> when unrefing. C<sv_force_normal> calls this function
5022with flags set to 0.
645c22ef
DM
5023
5024=cut
5025*/
5026
6fc92669 5027void
840a7b70 5028Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
0f15f207 5029{
f8c7b90f 5030#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
5031 if (SvREADONLY(sv)) {
5032 /* At this point I believe I should acquire a global SV mutex. */
5033 if (SvFAKE(sv)) {
a28509cc
AL
5034 const char *pvx = SvPVX_const(sv);
5035 const STRLEN len = SvLEN(sv);
5036 const STRLEN cur = SvCUR(sv);
a28509cc 5037 SV * const next = SV_COW_NEXT_SV(sv); /* next COW sv in the loop. */
46187eeb
NC
5038 if (DEBUG_C_TEST) {
5039 PerlIO_printf(Perl_debug_log,
5040 "Copy on write: Force normal %ld\n",
5041 (long) flags);
e419cbc5 5042 sv_dump(sv);
46187eeb 5043 }
765f542d
NC
5044 SvFAKE_off(sv);
5045 SvREADONLY_off(sv);
5046 /* This SV doesn't own the buffer, so need to New() a new one: */
f880fe2f 5047 SvPV_set(sv, (char*)0);
87a1ef3d 5048 SvLEN_set(sv, 0);
765f542d
NC
5049 if (flags & SV_COW_DROP_PV) {
5050 /* OK, so we don't need to copy our buffer. */
5051 SvPOK_off(sv);
5052 } else {
5053 SvGROW(sv, cur + 1);
5054 Move(pvx,SvPVX(sv),cur,char);
87a1ef3d 5055 SvCUR_set(sv, cur);
765f542d
NC
5056 *SvEND(sv) = '\0';
5057 }
bdd68bc3 5058 sv_release_COW(sv, pvx, len, next);
46187eeb 5059 if (DEBUG_C_TEST) {
e419cbc5 5060 sv_dump(sv);
46187eeb 5061 }
765f542d 5062 }
923e4eb5 5063 else if (IN_PERL_RUNTIME)
765f542d
NC
5064 Perl_croak(aTHX_ PL_no_modify);
5065 /* At this point I believe that I can drop the global SV mutex. */
5066 }
5067#else
2213622d 5068 if (SvREADONLY(sv)) {
1c846c1f 5069 if (SvFAKE(sv)) {
a433f3d2 5070 const char *pvx = SvPVX_const(sv);
66a1b24b 5071 const STRLEN len = SvCUR(sv);
10bcdfd6
NC
5072 SvFAKE_off(sv);
5073 SvREADONLY_off(sv);
66a1b24b
AL
5074 SvPV_set(sv, Nullch);
5075 SvLEN_set(sv, 0);
1c846c1f 5076 SvGROW(sv, len + 1);
3f7c398e 5077 Move(pvx,SvPVX_const(sv),len,char);
1c846c1f 5078 *SvEND(sv) = '\0';
bdd68bc3 5079 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
1c846c1f 5080 }
923e4eb5 5081 else if (IN_PERL_RUNTIME)
cea2e8a9 5082 Perl_croak(aTHX_ PL_no_modify);
0f15f207 5083 }
765f542d 5084#endif
2213622d 5085 if (SvROK(sv))
840a7b70 5086 sv_unref_flags(sv, flags);
6fc92669
GS
5087 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
5088 sv_unglob(sv);
0f15f207 5089}
1c846c1f 5090
645c22ef
DM
5091/*
5092=for apidoc sv_force_normal
5093
5094Undo various types of fakery on an SV: if the PV is a shared string, make
5095a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
5096an xpvmg. See also C<sv_force_normal_flags>.
5097
5098=cut
5099*/
5100
840a7b70
IZ
5101void
5102Perl_sv_force_normal(pTHX_ register SV *sv)
5103{
5104 sv_force_normal_flags(sv, 0);
5105}
5106
954c1994
GS
5107/*
5108=for apidoc sv_chop
5109
1c846c1f 5110Efficient removal of characters from the beginning of the string buffer.
954c1994
GS
5111SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
5112the string buffer. The C<ptr> becomes the first character of the adjusted
645c22ef 5113string. Uses the "OOK hack".
3f7c398e 5114Beware: after this function returns, C<ptr> and SvPVX_const(sv) may no longer
31869a79 5115refer to the same chunk of data.
954c1994
GS
5116
5117=cut
5118*/
5119
79072805 5120void
f54cb97a 5121Perl_sv_chop(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
5122{
5123 register STRLEN delta;
a0d0e21e 5124 if (!ptr || !SvPOKp(sv))
79072805 5125 return;
3f7c398e 5126 delta = ptr - SvPVX_const(sv);
2213622d 5127 SV_CHECK_THINKFIRST(sv);
79072805
LW
5128 if (SvTYPE(sv) < SVt_PVIV)
5129 sv_upgrade(sv,SVt_PVIV);
5130
5131 if (!SvOOK(sv)) {
50483b2c 5132 if (!SvLEN(sv)) { /* make copy of shared string */
3f7c398e 5133 const char *pvx = SvPVX_const(sv);
a28509cc 5134 const STRLEN len = SvCUR(sv);
50483b2c 5135 SvGROW(sv, len + 1);
3f7c398e 5136 Move(pvx,SvPVX_const(sv),len,char);
50483b2c
JD
5137 *SvEND(sv) = '\0';
5138 }
45977657 5139 SvIV_set(sv, 0);
a4bfb290
AB
5140 /* Same SvOOK_on but SvOOK_on does a SvIOK_off
5141 and we do that anyway inside the SvNIOK_off
5142 */
7a5fa8a2 5143 SvFLAGS(sv) |= SVf_OOK;
79072805 5144 }
a4bfb290 5145 SvNIOK_off(sv);
b162af07
SP
5146 SvLEN_set(sv, SvLEN(sv) - delta);
5147 SvCUR_set(sv, SvCUR(sv) - delta);
f880fe2f 5148 SvPV_set(sv, SvPVX(sv) + delta);
45977657 5149 SvIV_set(sv, SvIVX(sv) + delta);
79072805
LW
5150}
5151
09540bc3
JH
5152/* sv_catpvn() is now a macro using Perl_sv_catpvn_flags();
5153 * this function provided for binary compatibility only
5154 */
5155
5156void
5157Perl_sv_catpvn(pTHX_ SV *dsv, const char* sstr, STRLEN slen)
5158{
5159 sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC);
5160}
5161
954c1994
GS
5162/*
5163=for apidoc sv_catpvn
5164
5165Concatenates the string onto the end of the string which is in the SV. The
1e54db1a
JH
5166C<len> indicates number of bytes to copy. If the SV has the UTF-8
5167status set, then the bytes appended should be valid UTF-8.
d5ce4a7c 5168Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>.
954c1994 5169
8d6d96c1
HS
5170=for apidoc sv_catpvn_flags
5171
5172Concatenates the string onto the end of the string which is in the SV. The
1e54db1a
JH
5173C<len> indicates number of bytes to copy. If the SV has the UTF-8
5174status set, then the bytes appended should be valid UTF-8.
8d6d96c1
HS
5175If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if
5176appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented
5177in terms of this function.
5178
5179=cut
5180*/
5181
5182void
5183Perl_sv_catpvn_flags(pTHX_ register SV *dsv, register const char *sstr, register STRLEN slen, I32 flags)
5184{
5185 STRLEN dlen;
f54cb97a 5186 const char *dstr = SvPV_force_flags(dsv, dlen, flags);
8d6d96c1 5187
8d6d96c1
HS
5188 SvGROW(dsv, dlen + slen + 1);
5189 if (sstr == dstr)
3f7c398e 5190 sstr = SvPVX_const(dsv);
8d6d96c1 5191 Move(sstr, SvPVX(dsv) + dlen, slen, char);
b162af07 5192 SvCUR_set(dsv, SvCUR(dsv) + slen);
8d6d96c1
HS
5193 *SvEND(dsv) = '\0';
5194 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
5195 SvTAINT(dsv);
79072805
LW
5196}
5197
954c1994
GS
5198/*
5199=for apidoc sv_catpvn_mg
5200
5201Like C<sv_catpvn>, but also handles 'set' magic.
5202
5203=cut
5204*/
5205
79072805 5206void
864dbfa3 5207Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
5208{
5209 sv_catpvn(sv,ptr,len);
5210 SvSETMAGIC(sv);
5211}
5212
09540bc3
JH
5213/* sv_catsv() is now a macro using Perl_sv_catsv_flags();
5214 * this function provided for binary compatibility only
5215 */
5216
5217void
5218Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
5219{
5220 sv_catsv_flags(dstr, sstr, SV_GMAGIC);
5221}
5222
954c1994
GS
5223/*
5224=for apidoc sv_catsv
5225
13e8c8e3
JH
5226Concatenates the string from SV C<ssv> onto the end of the string in
5227SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
5228not 'set' magic. See C<sv_catsv_mg>.
954c1994 5229
8d6d96c1
HS
5230=for apidoc sv_catsv_flags
5231
5232Concatenates the string from SV C<ssv> onto the end of the string in
5233SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC>
5234bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv>
5235and C<sv_catsv_nomg> are implemented in terms of this function.
5236
5237=cut */
5238
ef50df4b 5239void
8d6d96c1 5240Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags)
79072805 5241{
4d84ee25 5242 const char *spv;
13e8c8e3 5243 STRLEN slen;
46199a12 5244 if (!ssv)
79072805 5245 return;
4d84ee25 5246 if ((spv = SvPV_const(ssv, slen))) {
4fd84b44
AD
5247 /* sutf8 and dutf8 were type bool, but under USE_ITHREADS,
5248 gcc version 2.95.2 20000220 (Debian GNU/Linux) for
8cf8f3d1
NIS
5249 Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously
5250 get dutf8 = 0x20000000, (i.e. SVf_UTF8) even though
4fd84b44
AD
5251 dsv->sv_flags doesn't have that bit set.
5252 Andy Dougherty 12 Oct 2001
5253 */
b464bac0 5254 const I32 sutf8 = DO_UTF8(ssv);
4fd84b44 5255 I32 dutf8;
13e8c8e3 5256
8d6d96c1
HS
5257 if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC))
5258 mg_get(dsv);
5259 dutf8 = DO_UTF8(dsv);
5260
5261 if (dutf8 != sutf8) {
13e8c8e3 5262 if (dutf8) {
46199a12 5263 /* Not modifying source SV, so taking a temporary copy. */
8d6d96c1 5264 SV* csv = sv_2mortal(newSVpvn(spv, slen));
13e8c8e3 5265
46199a12 5266 sv_utf8_upgrade(csv);
93524f2b 5267 spv = SvPV_const(csv, slen);
13e8c8e3 5268 }
8d6d96c1
HS
5269 else
5270 sv_utf8_upgrade_nomg(dsv);
e84ff256 5271 }
8d6d96c1 5272 sv_catpvn_nomg(dsv, spv, slen);
560a288e 5273 }
79072805
LW
5274}
5275
954c1994
GS
5276/*
5277=for apidoc sv_catsv_mg
5278
5279Like C<sv_catsv>, but also handles 'set' magic.
5280
5281=cut
5282*/
5283
79072805 5284void
46199a12 5285Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv)
ef50df4b 5286{
46199a12
JH
5287 sv_catsv(dsv,ssv);
5288 SvSETMAGIC(dsv);
ef50df4b
GS
5289}
5290
954c1994
GS
5291/*
5292=for apidoc sv_catpv
5293
5294Concatenates the string onto the end of the string which is in the SV.
1e54db1a
JH
5295If the SV has the UTF-8 status set, then the bytes appended should be
5296valid UTF-8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
954c1994 5297
d5ce4a7c 5298=cut */
954c1994 5299
ef50df4b 5300void
0c981600 5301Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
5302{
5303 register STRLEN len;
463ee0b2 5304 STRLEN tlen;
748a9306 5305 char *junk;
79072805 5306
0c981600 5307 if (!ptr)
79072805 5308 return;
748a9306 5309 junk = SvPV_force(sv, tlen);
0c981600 5310 len = strlen(ptr);
463ee0b2 5311 SvGROW(sv, tlen + len + 1);
0c981600 5312 if (ptr == junk)
3f7c398e 5313 ptr = SvPVX_const(sv);
0c981600 5314 Move(ptr,SvPVX(sv)+tlen,len+1,char);
b162af07 5315 SvCUR_set(sv, SvCUR(sv) + len);
d41ff1b8 5316 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 5317 SvTAINT(sv);
79072805
LW
5318}
5319
954c1994
GS
5320/*
5321=for apidoc sv_catpv_mg
5322
5323Like C<sv_catpv>, but also handles 'set' magic.
5324
5325=cut
5326*/
5327
ef50df4b 5328void
0c981600 5329Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b 5330{
0c981600 5331 sv_catpv(sv,ptr);
ef50df4b
GS
5332 SvSETMAGIC(sv);
5333}
5334
645c22ef
DM
5335/*
5336=for apidoc newSV
5337
5338Create a new null SV, or if len > 0, create a new empty SVt_PV type SV
5339with an initial PV allocation of len+1. Normally accessed via the C<NEWSV>
5340macro.
5341
5342=cut
5343*/
5344
79072805 5345SV *
864dbfa3 5346Perl_newSV(pTHX_ STRLEN len)
79072805
LW
5347{
5348 register SV *sv;
1c846c1f 5349
4561caa4 5350 new_SV(sv);
79072805
LW
5351 if (len) {
5352 sv_upgrade(sv, SVt_PV);
5353 SvGROW(sv, len + 1);
5354 }
5355 return sv;
5356}
954c1994 5357/*
92110913 5358=for apidoc sv_magicext
954c1994 5359
68795e93 5360Adds magic to an SV, upgrading it if necessary. Applies the
2d8d5d5a 5361supplied vtable and returns a pointer to the magic added.
92110913 5362
2d8d5d5a
SH
5363Note that C<sv_magicext> will allow things that C<sv_magic> will not.
5364In particular, you can add magic to SvREADONLY SVs, and add more than
5365one instance of the same 'how'.
645c22ef 5366
2d8d5d5a
SH
5367If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
5368stored, if C<namlen> is zero then C<name> is stored as-is and - as another
5369special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
5370to contain an C<SV*> and is stored as-is with its REFCNT incremented.
92110913 5371
2d8d5d5a 5372(This is now used as a subroutine by C<sv_magic>.)
954c1994
GS
5373
5374=cut
5375*/
92110913 5376MAGIC *
e1ec3a88 5377Perl_sv_magicext(pTHX_ SV* sv, SV* obj, int how, const MGVTBL *vtable,
92110913 5378 const char* name, I32 namlen)
79072805
LW
5379{
5380 MAGIC* mg;
68795e93 5381
92110913 5382 if (SvTYPE(sv) < SVt_PVMG) {
862a34c6 5383 SvUPGRADE(sv, SVt_PVMG);
463ee0b2 5384 }
79072805
LW
5385 Newz(702,mg, 1, MAGIC);
5386 mg->mg_moremagic = SvMAGIC(sv);
b162af07 5387 SvMAGIC_set(sv, mg);
75f9d97a 5388
05f95b08
SB
5389 /* Sometimes a magic contains a reference loop, where the sv and
5390 object refer to each other. To prevent a reference loop that
5391 would prevent such objects being freed, we look for such loops
5392 and if we find one we avoid incrementing the object refcount.
87f0b213
JH
5393
5394 Note we cannot do this to avoid self-tie loops as intervening RV must
b5ccf5f2 5395 have its REFCNT incremented to keep it in existence.
87f0b213
JH
5396
5397 */
14befaf4
DM
5398 if (!obj || obj == sv ||
5399 how == PERL_MAGIC_arylen ||
5400 how == PERL_MAGIC_qr ||
8d2f4536 5401 how == PERL_MAGIC_symtab ||
75f9d97a
JH
5402 (SvTYPE(obj) == SVt_PVGV &&
5403 (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv ||
5404 GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv ||
2628be26 5405 GvFORM(obj) == (CV*)sv)))
75f9d97a 5406 {
8990e307 5407 mg->mg_obj = obj;
75f9d97a 5408 }
85e6fe83 5409 else {
8990e307 5410 mg->mg_obj = SvREFCNT_inc(obj);
85e6fe83
LW
5411 mg->mg_flags |= MGf_REFCOUNTED;
5412 }
b5ccf5f2
YST
5413
5414 /* Normal self-ties simply pass a null object, and instead of
5415 using mg_obj directly, use the SvTIED_obj macro to produce a
5416 new RV as needed. For glob "self-ties", we are tieing the PVIO
5417 with an RV obj pointing to the glob containing the PVIO. In
5418 this case, to avoid a reference loop, we need to weaken the
5419 reference.
5420 */
5421
5422 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
5423 obj && SvROK(obj) && GvIO(SvRV(obj)) == (IO*)sv)
5424 {
5425 sv_rvweaken(obj);
5426 }
5427
79072805 5428 mg->mg_type = how;
565764a8 5429 mg->mg_len = namlen;
9cbac4c7 5430 if (name) {
92110913 5431 if (namlen > 0)
1edc1566 5432 mg->mg_ptr = savepvn(name, namlen);
c6ee37c5 5433 else if (namlen == HEf_SVKEY)
1edc1566 5434 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
68795e93 5435 else
92110913 5436 mg->mg_ptr = (char *) name;
9cbac4c7 5437 }
92110913 5438 mg->mg_virtual = vtable;
68795e93 5439
92110913
NIS
5440 mg_magical(sv);
5441 if (SvGMAGICAL(sv))
5442 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
5443 return mg;
5444}
5445
5446/*
5447=for apidoc sv_magic
1c846c1f 5448
92110913
NIS
5449Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary,
5450then adds a new magic item of type C<how> to the head of the magic list.
5451
2d8d5d5a
SH
5452See C<sv_magicext> (which C<sv_magic> now calls) for a description of the
5453handling of the C<name> and C<namlen> arguments.
5454
4509d3fb
SB
5455You need to use C<sv_magicext> to add magic to SvREADONLY SVs and also
5456to add more than one instance of the same 'how'.
5457
92110913
NIS
5458=cut
5459*/
5460
5461void
5462Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
68795e93 5463{
e1ec3a88 5464 const MGVTBL *vtable = 0;
92110913 5465 MAGIC* mg;
92110913 5466
f8c7b90f 5467#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
5468 if (SvIsCOW(sv))
5469 sv_force_normal_flags(sv, 0);
5470#endif
92110913 5471 if (SvREADONLY(sv)) {
923e4eb5 5472 if (IN_PERL_RUNTIME
92110913
NIS
5473 && how != PERL_MAGIC_regex_global
5474 && how != PERL_MAGIC_bm
5475 && how != PERL_MAGIC_fm
5476 && how != PERL_MAGIC_sv
e6469971 5477 && how != PERL_MAGIC_backref
92110913
NIS
5478 )
5479 {
5480 Perl_croak(aTHX_ PL_no_modify);
5481 }
5482 }
5483 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
5484 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
68795e93
NIS
5485 /* sv_magic() refuses to add a magic of the same 'how' as an
5486 existing one
92110913
NIS
5487 */
5488 if (how == PERL_MAGIC_taint)
5489 mg->mg_len |= 1;
5490 return;
5491 }
5492 }
68795e93 5493
79072805 5494 switch (how) {
14befaf4 5495 case PERL_MAGIC_sv:
92110913 5496 vtable = &PL_vtbl_sv;
79072805 5497 break;
14befaf4 5498 case PERL_MAGIC_overload:
92110913 5499 vtable = &PL_vtbl_amagic;
a0d0e21e 5500 break;
14befaf4 5501 case PERL_MAGIC_overload_elem:
92110913 5502 vtable = &PL_vtbl_amagicelem;
a0d0e21e 5503 break;
14befaf4 5504 case PERL_MAGIC_overload_table:
92110913 5505 vtable = &PL_vtbl_ovrld;
a0d0e21e 5506 break;
14befaf4 5507 case PERL_MAGIC_bm:
92110913 5508 vtable = &PL_vtbl_bm;
79072805 5509 break;
14befaf4 5510 case PERL_MAGIC_regdata:
92110913 5511 vtable = &PL_vtbl_regdata;
6cef1e77 5512 break;
14befaf4 5513 case PERL_MAGIC_regdatum:
92110913 5514 vtable = &PL_vtbl_regdatum;
6cef1e77 5515 break;
14befaf4 5516 case PERL_MAGIC_env:
92110913 5517 vtable = &PL_vtbl_env;
79072805 5518 break;
14befaf4 5519 case PERL_MAGIC_fm:
92110913 5520 vtable = &PL_vtbl_fm;
55497cff 5521 break;
14befaf4 5522 case PERL_MAGIC_envelem:
92110913 5523 vtable = &PL_vtbl_envelem;
79072805 5524 break;
14befaf4 5525 case PERL_MAGIC_regex_global:
92110913 5526 vtable = &PL_vtbl_mglob;
93a17b20 5527 break;
14befaf4 5528 case PERL_MAGIC_isa:
92110913 5529 vtable = &PL_vtbl_isa;
463ee0b2 5530 break;
14befaf4 5531 case PERL_MAGIC_isaelem:
92110913 5532 vtable = &PL_vtbl_isaelem;
463ee0b2 5533 break;
14befaf4 5534 case PERL_MAGIC_nkeys:
92110913 5535 vtable = &PL_vtbl_nkeys;
16660edb 5536 break;
14befaf4 5537 case PERL_MAGIC_dbfile:
92110913 5538 vtable = 0;
93a17b20 5539 break;
14befaf4 5540 case PERL_MAGIC_dbline:
92110913 5541 vtable = &PL_vtbl_dbline;
79072805 5542 break;
36477c24 5543#ifdef USE_LOCALE_COLLATE
14befaf4 5544 case PERL_MAGIC_collxfrm:
92110913 5545 vtable = &PL_vtbl_collxfrm;
bbce6d69 5546 break;
36477c24 5547#endif /* USE_LOCALE_COLLATE */
14befaf4 5548 case PERL_MAGIC_tied:
92110913 5549 vtable = &PL_vtbl_pack;
463ee0b2 5550 break;
14befaf4
DM
5551 case PERL_MAGIC_tiedelem:
5552 case PERL_MAGIC_tiedscalar:
92110913 5553 vtable = &PL_vtbl_packelem;
463ee0b2 5554 break;
14befaf4 5555 case PERL_MAGIC_qr:
92110913 5556 vtable = &PL_vtbl_regexp;
c277df42 5557 break;
14befaf4 5558 case PERL_MAGIC_sig:
92110913 5559 vtable = &PL_vtbl_sig;
79072805 5560 break;
14befaf4 5561 case PERL_MAGIC_sigelem:
92110913 5562 vtable = &PL_vtbl_sigelem;
79072805 5563 break;
14befaf4 5564 case PERL_MAGIC_taint:
92110913 5565 vtable = &PL_vtbl_taint;
463ee0b2 5566 break;
14befaf4 5567 case PERL_MAGIC_uvar:
92110913 5568 vtable = &PL_vtbl_uvar;
79072805 5569 break;
14befaf4 5570 case PERL_MAGIC_vec:
92110913 5571 vtable = &PL_vtbl_vec;
79072805 5572 break;
a3874608 5573 case PERL_MAGIC_arylen_p:
bfcb3514 5574 case PERL_MAGIC_rhash:
8d2f4536 5575 case PERL_MAGIC_symtab:
ece467f9
JP
5576 case PERL_MAGIC_vstring:
5577 vtable = 0;
5578 break;
7e8c5dac
HS
5579 case PERL_MAGIC_utf8:
5580 vtable = &PL_vtbl_utf8;
5581 break;
14befaf4 5582 case PERL_MAGIC_substr:
92110913 5583 vtable = &PL_vtbl_substr;
79072805 5584 break;
14befaf4 5585 case PERL_MAGIC_defelem:
92110913 5586 vtable = &PL_vtbl_defelem;
5f05dabc 5587 break;
14befaf4 5588 case PERL_MAGIC_glob:
92110913 5589 vtable = &PL_vtbl_glob;
79072805 5590 break;
14befaf4 5591 case PERL_MAGIC_arylen:
92110913 5592 vtable = &PL_vtbl_arylen;
79072805 5593 break;
14befaf4 5594 case PERL_MAGIC_pos:
92110913 5595 vtable = &PL_vtbl_pos;
a0d0e21e 5596 break;
14befaf4 5597 case PERL_MAGIC_backref:
92110913 5598 vtable = &PL_vtbl_backref;
810b8aa5 5599 break;
14befaf4
DM
5600 case PERL_MAGIC_ext:
5601 /* Reserved for use by extensions not perl internals. */
4633a7c4
LW
5602 /* Useful for attaching extension internal data to perl vars. */
5603 /* Note that multiple extensions may clash if magical scalars */
5604 /* etc holding private data from one are passed to another. */
a0d0e21e 5605 break;
79072805 5606 default:
14befaf4 5607 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
463ee0b2 5608 }
68795e93 5609
92110913 5610 /* Rest of work is done else where */
27da23d5 5611 mg = sv_magicext(sv,obj,how,(MGVTBL*)vtable,name,namlen);
68795e93 5612
92110913
NIS
5613 switch (how) {
5614 case PERL_MAGIC_taint:
5615 mg->mg_len = 1;
5616 break;
5617 case PERL_MAGIC_ext:
5618 case PERL_MAGIC_dbfile:
5619 SvRMAGICAL_on(sv);
5620 break;
5621 }
463ee0b2
LW
5622}
5623
c461cf8f
JH
5624/*
5625=for apidoc sv_unmagic
5626
645c22ef 5627Removes all magic of type C<type> from an SV.
c461cf8f
JH
5628
5629=cut
5630*/
5631
463ee0b2 5632int
864dbfa3 5633Perl_sv_unmagic(pTHX_ SV *sv, int type)
463ee0b2
LW
5634{
5635 MAGIC* mg;
5636 MAGIC** mgp;
91bba347 5637 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
463ee0b2
LW
5638 return 0;
5639 mgp = &SvMAGIC(sv);
5640 for (mg = *mgp; mg; mg = *mgp) {
5641 if (mg->mg_type == type) {
e1ec3a88 5642 const MGVTBL* const vtbl = mg->mg_virtual;
463ee0b2 5643 *mgp = mg->mg_moremagic;
1d7c1841 5644 if (vtbl && vtbl->svt_free)
fc0dc3b3 5645 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
14befaf4 5646 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
92110913 5647 if (mg->mg_len > 0)
1edc1566 5648 Safefree(mg->mg_ptr);
565764a8 5649 else if (mg->mg_len == HEf_SVKEY)
1edc1566 5650 SvREFCNT_dec((SV*)mg->mg_ptr);
7e8c5dac
HS
5651 else if (mg->mg_type == PERL_MAGIC_utf8 && mg->mg_ptr)
5652 Safefree(mg->mg_ptr);
9cbac4c7 5653 }
a0d0e21e
LW
5654 if (mg->mg_flags & MGf_REFCOUNTED)
5655 SvREFCNT_dec(mg->mg_obj);
463ee0b2
LW
5656 Safefree(mg);
5657 }
5658 else
5659 mgp = &mg->mg_moremagic;
79072805 5660 }
91bba347 5661 if (!SvMAGIC(sv)) {
463ee0b2 5662 SvMAGICAL_off(sv);
06759ea0 5663 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
463ee0b2
LW
5664 }
5665
5666 return 0;
79072805
LW
5667}
5668
c461cf8f
JH
5669/*
5670=for apidoc sv_rvweaken
5671
645c22ef
DM
5672Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
5673referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
5674push a back-reference to this RV onto the array of backreferences
5675associated with that magic.
c461cf8f
JH
5676
5677=cut
5678*/
5679
810b8aa5 5680SV *
864dbfa3 5681Perl_sv_rvweaken(pTHX_ SV *sv)
810b8aa5
GS
5682{
5683 SV *tsv;
5684 if (!SvOK(sv)) /* let undefs pass */
5685 return sv;
5686 if (!SvROK(sv))
cea2e8a9 5687 Perl_croak(aTHX_ "Can't weaken a nonreference");
810b8aa5 5688 else if (SvWEAKREF(sv)) {
810b8aa5 5689 if (ckWARN(WARN_MISC))
9014280d 5690 Perl_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
810b8aa5
GS
5691 return sv;
5692 }
5693 tsv = SvRV(sv);
5694 sv_add_backref(tsv, sv);
5695 SvWEAKREF_on(sv);
1c846c1f 5696 SvREFCNT_dec(tsv);
810b8aa5
GS
5697 return sv;
5698}
5699
645c22ef
DM
5700/* Give tsv backref magic if it hasn't already got it, then push a
5701 * back-reference to sv onto the array associated with the backref magic.
5702 */
5703
810b8aa5 5704STATIC void
cea2e8a9 5705S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
810b8aa5
GS
5706{
5707 AV *av;
5708 MAGIC *mg;
14befaf4 5709 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, PERL_MAGIC_backref)))
810b8aa5
GS
5710 av = (AV*)mg->mg_obj;
5711 else {
5712 av = newAV();
14befaf4 5713 sv_magic(tsv, (SV*)av, PERL_MAGIC_backref, NULL, 0);
d99b02a1
DM
5714 /* av now has a refcnt of 2, which avoids it getting freed
5715 * before us during global cleanup. The extra ref is removed
5716 * by magic_killbackrefs() when tsv is being freed */
810b8aa5 5717 }
d91d49e8 5718 if (AvFILLp(av) >= AvMAX(av)) {
fdc9a813 5719 I32 i;
d91d49e8 5720 SV **svp = AvARRAY(av);
fdc9a813
AE
5721 for (i = AvFILLp(av); i >= 0; i--)
5722 if (!svp[i]) {
d91d49e8
MM
5723 svp[i] = sv; /* reuse the slot */
5724 return;
5725 }
d91d49e8
MM
5726 av_extend(av, AvFILLp(av)+1);
5727 }
5728 AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
810b8aa5
GS
5729}
5730
645c22ef
DM
5731/* delete a back-reference to ourselves from the backref magic associated
5732 * with the SV we point to.
5733 */
5734
1c846c1f 5735STATIC void
cea2e8a9 5736S_sv_del_backref(pTHX_ SV *sv)
810b8aa5
GS
5737{
5738 AV *av;
5739 SV **svp;
5740 I32 i;
5741 SV *tsv = SvRV(sv);
c04a4dfe 5742 MAGIC *mg = NULL;
14befaf4 5743 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref)))
cea2e8a9 5744 Perl_croak(aTHX_ "panic: del_backref");
810b8aa5
GS
5745 av = (AV *)mg->mg_obj;
5746 svp = AvARRAY(av);
fdc9a813
AE
5747 for (i = AvFILLp(av); i >= 0; i--)
5748 if (svp[i] == sv) svp[i] = Nullsv;
810b8aa5
GS
5749}
5750
954c1994
GS
5751/*
5752=for apidoc sv_insert
5753
5754Inserts a string at the specified offset/length within the SV. Similar to
5755the Perl substr() function.
5756
5757=cut
5758*/
5759
79072805 5760void
e1ec3a88 5761Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, const char *little, STRLEN littlelen)
79072805
LW
5762{
5763 register char *big;
5764 register char *mid;
5765 register char *midend;
5766 register char *bigend;
5767 register I32 i;
6ff81951 5768 STRLEN curlen;
1c846c1f 5769
79072805 5770
8990e307 5771 if (!bigstr)
cea2e8a9 5772 Perl_croak(aTHX_ "Can't modify non-existent substring");
6ff81951 5773 SvPV_force(bigstr, curlen);
60fa28ff 5774 (void)SvPOK_only_UTF8(bigstr);
6ff81951
GS
5775 if (offset + len > curlen) {
5776 SvGROW(bigstr, offset+len+1);
93524f2b 5777 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
6ff81951
GS
5778 SvCUR_set(bigstr, offset+len);
5779 }
79072805 5780
69b47968 5781 SvTAINT(bigstr);
79072805
LW
5782 i = littlelen - len;
5783 if (i > 0) { /* string might grow */
a0d0e21e 5784 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
79072805
LW
5785 mid = big + offset + len;
5786 midend = bigend = big + SvCUR(bigstr);
5787 bigend += i;
5788 *bigend = '\0';
5789 while (midend > mid) /* shove everything down */
5790 *--bigend = *--midend;
5791 Move(little,big+offset,littlelen,char);
b162af07 5792 SvCUR_set(bigstr, SvCUR(bigstr) + i);
79072805
LW
5793 SvSETMAGIC(bigstr);
5794 return;
5795 }
5796 else if (i == 0) {
463ee0b2 5797 Move(little,SvPVX(bigstr)+offset,len,char);
79072805
LW
5798 SvSETMAGIC(bigstr);
5799 return;
5800 }
5801
463ee0b2 5802 big = SvPVX(bigstr);
79072805
LW
5803 mid = big + offset;
5804 midend = mid + len;
5805 bigend = big + SvCUR(bigstr);
5806
5807 if (midend > bigend)
cea2e8a9 5808 Perl_croak(aTHX_ "panic: sv_insert");
79072805
LW
5809
5810 if (mid - big > bigend - midend) { /* faster to shorten from end */
5811 if (littlelen) {
5812 Move(little, mid, littlelen,char);
5813 mid += littlelen;
5814 }
5815 i = bigend - midend;
5816 if (i > 0) {
5817 Move(midend, mid, i,char);
5818 mid += i;
5819 }
5820 *mid = '\0';
5821 SvCUR_set(bigstr, mid - big);
5822 }
155aba94 5823 else if ((i = mid - big)) { /* faster from front */
79072805
LW
5824 midend -= littlelen;
5825 mid = midend;
5826 sv_chop(bigstr,midend-i);
5827 big += i;
5828 while (i--)
5829 *--midend = *--big;
5830 if (littlelen)
5831 Move(little, mid, littlelen,char);
5832 }
5833 else if (littlelen) {
5834 midend -= littlelen;
5835 sv_chop(bigstr,midend);
5836 Move(little,midend,littlelen,char);
5837 }
5838 else {
5839 sv_chop(bigstr,midend);
5840 }
5841 SvSETMAGIC(bigstr);
5842}
5843
c461cf8f
JH
5844/*
5845=for apidoc sv_replace
5846
5847Make the first argument a copy of the second, then delete the original.
645c22ef
DM
5848The target SV physically takes over ownership of the body of the source SV
5849and inherits its flags; however, the target keeps any magic it owns,
5850and any magic in the source is discarded.
ff276b08 5851Note that this is a rather specialist SV copying operation; most of the
645c22ef 5852time you'll want to use C<sv_setsv> or one of its many macro front-ends.
c461cf8f
JH
5853
5854=cut
5855*/
79072805
LW
5856
5857void
864dbfa3 5858Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
79072805 5859{
a3b680e6 5860 const U32 refcnt = SvREFCNT(sv);
765f542d 5861 SV_CHECK_THINKFIRST_COW_DROP(sv);
0453d815 5862 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
9014280d 5863 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Reference miscount in sv_replace()");
93a17b20 5864 if (SvMAGICAL(sv)) {
a0d0e21e
LW
5865 if (SvMAGICAL(nsv))
5866 mg_free(nsv);
5867 else
5868 sv_upgrade(nsv, SVt_PVMG);
b162af07 5869 SvMAGIC_set(nsv, SvMAGIC(sv));
a0d0e21e 5870 SvFLAGS(nsv) |= SvMAGICAL(sv);
93a17b20 5871 SvMAGICAL_off(sv);
b162af07 5872 SvMAGIC_set(sv, NULL);
93a17b20 5873 }
79072805
LW
5874 SvREFCNT(sv) = 0;
5875 sv_clear(sv);
477f5d66 5876 assert(!SvREFCNT(sv));
fd0854ff
DM
5877#ifdef DEBUG_LEAKING_SCALARS
5878 sv->sv_flags = nsv->sv_flags;
5879 sv->sv_any = nsv->sv_any;
5880 sv->sv_refcnt = nsv->sv_refcnt;
f34d0642 5881 sv->sv_u = nsv->sv_u;
fd0854ff 5882#else
79072805 5883 StructCopy(nsv,sv,SV);
fd0854ff 5884#endif
7b2c381c
NC
5885 /* Currently could join these into one piece of pointer arithmetic, but
5886 it would be unclear. */
5887 if(SvTYPE(sv) == SVt_IV)
5888 SvANY(sv)
339049b0 5889 = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
7b2c381c 5890 else if (SvTYPE(sv) == SVt_RV) {
339049b0 5891 SvANY(sv) = &sv->sv_u.svu_rv;
7b2c381c
NC
5892 }
5893
fd0854ff 5894
f8c7b90f 5895#ifdef PERL_OLD_COPY_ON_WRITE
d3d0e6f1
NC
5896 if (SvIsCOW_normal(nsv)) {
5897 /* We need to follow the pointers around the loop to make the
5898 previous SV point to sv, rather than nsv. */
5899 SV *next;
5900 SV *current = nsv;
5901 while ((next = SV_COW_NEXT_SV(current)) != nsv) {
5902 assert(next);
5903 current = next;
3f7c398e 5904 assert(SvPVX_const(current) == SvPVX_const(nsv));
d3d0e6f1
NC
5905 }
5906 /* Make the SV before us point to the SV after us. */
5907 if (DEBUG_C_TEST) {
5908 PerlIO_printf(Perl_debug_log, "previous is\n");
5909 sv_dump(current);
a29f6d03
NC
5910 PerlIO_printf(Perl_debug_log,
5911 "move it from 0x%"UVxf" to 0x%"UVxf"\n",
d3d0e6f1
NC
5912 (UV) SV_COW_NEXT_SV(current), (UV) sv);
5913 }
a29f6d03 5914 SV_COW_NEXT_SV_SET(current, sv);
d3d0e6f1
NC
5915 }
5916#endif
79072805 5917 SvREFCNT(sv) = refcnt;
1edc1566 5918 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
39cf41c2 5919 SvREFCNT(nsv) = 0;
463ee0b2 5920 del_SV(nsv);
79072805
LW
5921}
5922
c461cf8f
JH
5923/*
5924=for apidoc sv_clear
5925
645c22ef
DM
5926Clear an SV: call any destructors, free up any memory used by the body,
5927and free the body itself. The SV's head is I<not> freed, although
5928its type is set to all 1's so that it won't inadvertently be assumed
5929to be live during global destruction etc.
5930This function should only be called when REFCNT is zero. Most of the time
5931you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
5932instead.
c461cf8f
JH
5933
5934=cut
5935*/
5936
79072805 5937void
864dbfa3 5938Perl_sv_clear(pTHX_ register SV *sv)
79072805 5939{
27da23d5 5940 dVAR;
ec12f114 5941 HV* stash;
79072805
LW
5942 assert(sv);
5943 assert(SvREFCNT(sv) == 0);
5944
ed6116ce 5945 if (SvOBJECT(sv)) {
3280af22 5946 if (PL_defstash) { /* Still have a symbol table? */
39644a26 5947 dSP;
d460ef45 5948 do {
b464bac0 5949 CV* destructor;
4e8e7886 5950 stash = SvSTASH(sv);
32251b26 5951 destructor = StashHANDLER(stash,DESTROY);
4e8e7886 5952 if (destructor) {
5cc433a6
AB
5953 SV* tmpref = newRV(sv);
5954 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
4e8e7886 5955 ENTER;
e788e7d3 5956 PUSHSTACKi(PERLSI_DESTROY);
4e8e7886
GS
5957 EXTEND(SP, 2);
5958 PUSHMARK(SP);
5cc433a6 5959 PUSHs(tmpref);
4e8e7886 5960 PUTBACK;
44389ee9 5961 call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
7a5fa8a2
NIS
5962
5963
d3acc0f7 5964 POPSTACK;
3095d977 5965 SPAGAIN;
4e8e7886 5966 LEAVE;
5cc433a6
AB
5967 if(SvREFCNT(tmpref) < 2) {
5968 /* tmpref is not kept alive! */
5969 SvREFCNT(sv)--;
b162af07 5970 SvRV_set(tmpref, NULL);
5cc433a6
AB
5971 SvROK_off(tmpref);
5972 }
5973 SvREFCNT_dec(tmpref);
4e8e7886
GS
5974 }
5975 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
8ebc5c01 5976
6f44e0a4
JP
5977
5978 if (SvREFCNT(sv)) {
5979 if (PL_in_clean_objs)
cea2e8a9 5980 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
bfcb3514 5981 HvNAME_get(stash));
6f44e0a4
JP
5982 /* DESTROY gave object new lease on life */
5983 return;
5984 }
a0d0e21e 5985 }
4e8e7886 5986
a0d0e21e 5987 if (SvOBJECT(sv)) {
4e8e7886 5988 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
a0d0e21e
LW
5989 SvOBJECT_off(sv); /* Curse the object. */
5990 if (SvTYPE(sv) != SVt_PVIO)
3280af22 5991 --PL_sv_objcount; /* XXX Might want something more general */
a0d0e21e 5992 }
463ee0b2 5993 }
524189f1
JH
5994 if (SvTYPE(sv) >= SVt_PVMG) {
5995 if (SvMAGIC(sv))
5996 mg_free(sv);
bce8f412 5997 if (SvTYPE(sv) == SVt_PVMG && SvFLAGS(sv) & SVpad_TYPED)
524189f1
JH
5998 SvREFCNT_dec(SvSTASH(sv));
5999 }
ec12f114 6000 stash = NULL;
79072805 6001 switch (SvTYPE(sv)) {
8990e307 6002 case SVt_PVIO:
df0bd2f4
GS
6003 if (IoIFP(sv) &&
6004 IoIFP(sv) != PerlIO_stdin() &&
5f05dabc 6005 IoIFP(sv) != PerlIO_stdout() &&
6006 IoIFP(sv) != PerlIO_stderr())
93578b34 6007 {
f2b5be74 6008 io_close((IO*)sv, FALSE);
93578b34 6009 }
1d7c1841 6010 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
1236053a 6011 PerlDir_close(IoDIRP(sv));
1d7c1841 6012 IoDIRP(sv) = (DIR*)NULL;
8990e307
LW
6013 Safefree(IoTOP_NAME(sv));
6014 Safefree(IoFMT_NAME(sv));
6015 Safefree(IoBOTTOM_NAME(sv));
a0d0e21e 6016 /* FALL THROUGH */
79072805 6017 case SVt_PVBM:
a0d0e21e 6018 goto freescalar;
79072805 6019 case SVt_PVCV:
748a9306 6020 case SVt_PVFM:
85e6fe83 6021 cv_undef((CV*)sv);
a0d0e21e 6022 goto freescalar;
79072805 6023 case SVt_PVHV:
85e6fe83 6024 hv_undef((HV*)sv);
a0d0e21e 6025 break;
79072805 6026 case SVt_PVAV:
85e6fe83 6027 av_undef((AV*)sv);
a0d0e21e 6028 break;
02270b4e 6029 case SVt_PVLV:
dd28f7bb
DM
6030 if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
6031 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
6032 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
6033 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
6034 }
6035 else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */
6036 SvREFCNT_dec(LvTARG(sv));
02270b4e 6037 goto freescalar;
a0d0e21e 6038 case SVt_PVGV:
1edc1566 6039 gp_free((GV*)sv);
a0d0e21e 6040 Safefree(GvNAME(sv));
ec12f114
JPC
6041 /* cannot decrease stash refcount yet, as we might recursively delete
6042 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
6043 of stash until current sv is completely gone.
6044 -- JohnPC, 27 Mar 1998 */
6045 stash = GvSTASH(sv);
a0d0e21e 6046 /* FALL THROUGH */
79072805 6047 case SVt_PVMG:
79072805
LW
6048 case SVt_PVNV:
6049 case SVt_PVIV:
a0d0e21e 6050 freescalar:
5228ca4e
NC
6051 /* Don't bother with SvOOK_off(sv); as we're only going to free it. */
6052 if (SvOOK(sv)) {
93524f2b 6053 SvPV_set(sv, SvPVX_mutable(sv) - SvIVX(sv));
5228ca4e
NC
6054 /* Don't even bother with turning off the OOK flag. */
6055 }
79072805
LW
6056 /* FALL THROUGH */
6057 case SVt_PV:
a0d0e21e 6058 case SVt_RV:
810b8aa5
GS
6059 if (SvROK(sv)) {
6060 if (SvWEAKREF(sv))
6061 sv_del_backref(sv);
6062 else
6063 SvREFCNT_dec(SvRV(sv));
6064 }
f8c7b90f 6065#ifdef PERL_OLD_COPY_ON_WRITE
3f7c398e 6066 else if (SvPVX_const(sv)) {
765f542d
NC
6067 if (SvIsCOW(sv)) {
6068 /* I believe I need to grab the global SV mutex here and
6069 then recheck the COW status. */
46187eeb
NC
6070 if (DEBUG_C_TEST) {
6071 PerlIO_printf(Perl_debug_log, "Copy on write: clear\n");
e419cbc5 6072 sv_dump(sv);
46187eeb 6073 }
bdd68bc3
NC
6074 sv_release_COW(sv, SvPVX_const(sv), SvLEN(sv),
6075 SV_COW_NEXT_SV(sv));
765f542d
NC
6076 /* And drop it here. */
6077 SvFAKE_off(sv);
6078 } else if (SvLEN(sv)) {
3f7c398e 6079 Safefree(SvPVX_const(sv));
765f542d
NC
6080 }
6081 }
6082#else
3f7c398e
SP
6083 else if (SvPVX_const(sv) && SvLEN(sv))
6084 Safefree(SvPVX_const(sv));
6085 else if (SvPVX_const(sv) && SvREADONLY(sv) && SvFAKE(sv)) {
bdd68bc3 6086 unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
1c846c1f
NIS
6087 SvFAKE_off(sv);
6088 }
765f542d 6089#endif
79072805 6090 break;
a0d0e21e 6091/*
79072805 6092 case SVt_NV:
79072805 6093 case SVt_IV:
79072805
LW
6094 case SVt_NULL:
6095 break;
a0d0e21e 6096*/
79072805
LW
6097 }
6098
6099 switch (SvTYPE(sv)) {
6100 case SVt_NULL:
6101 break;
79072805 6102 case SVt_IV:
79072805
LW
6103 break;
6104 case SVt_NV:
6105 del_XNV(SvANY(sv));
6106 break;
ed6116ce 6107 case SVt_RV:
ed6116ce 6108 break;
79072805
LW
6109 case SVt_PV:
6110 del_XPV(SvANY(sv));
6111 break;
6112 case SVt_PVIV:
6113 del_XPVIV(SvANY(sv));
6114 break;
6115 case SVt_PVNV:
6116 del_XPVNV(SvANY(sv));
6117 break;
6118 case SVt_PVMG:
6119 del_XPVMG(SvANY(sv));
6120 break;
6121 case SVt_PVLV:
6122 del_XPVLV(SvANY(sv));
6123 break;
6124 case SVt_PVAV:
6125 del_XPVAV(SvANY(sv));
6126 break;
6127 case SVt_PVHV:
6128 del_XPVHV(SvANY(sv));
6129 break;
6130 case SVt_PVCV:
6131 del_XPVCV(SvANY(sv));
6132 break;
6133 case SVt_PVGV:
6134 del_XPVGV(SvANY(sv));
ec12f114
JPC
6135 /* code duplication for increased performance. */
6136 SvFLAGS(sv) &= SVf_BREAK;
6137 SvFLAGS(sv) |= SVTYPEMASK;
6138 /* decrease refcount of the stash that owns this GV, if any */
6139 if (stash)
6140 SvREFCNT_dec(stash);
6141 return; /* not break, SvFLAGS reset already happened */
79072805
LW
6142 case SVt_PVBM:
6143 del_XPVBM(SvANY(sv));
6144 break;
6145 case SVt_PVFM:
6146 del_XPVFM(SvANY(sv));
6147 break;
8990e307
LW
6148 case SVt_PVIO:
6149 del_XPVIO(SvANY(sv));
6150 break;
79072805 6151 }
a0d0e21e 6152 SvFLAGS(sv) &= SVf_BREAK;
8990e307 6153 SvFLAGS(sv) |= SVTYPEMASK;
79072805
LW
6154}
6155
645c22ef
DM
6156/*
6157=for apidoc sv_newref
6158
6159Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
6160instead.
6161
6162=cut
6163*/
6164
79072805 6165SV *
864dbfa3 6166Perl_sv_newref(pTHX_ SV *sv)
79072805 6167{
463ee0b2 6168 if (sv)
4db098f4 6169 (SvREFCNT(sv))++;
79072805
LW
6170 return sv;
6171}
6172
c461cf8f
JH
6173/*
6174=for apidoc sv_free
6175
645c22ef
DM
6176Decrement an SV's reference count, and if it drops to zero, call
6177C<sv_clear> to invoke destructors and free up any memory used by
6178the body; finally, deallocate the SV's head itself.
6179Normally called via a wrapper macro C<SvREFCNT_dec>.
c461cf8f
JH
6180
6181=cut
6182*/
6183
79072805 6184void
864dbfa3 6185Perl_sv_free(pTHX_ SV *sv)
79072805 6186{
27da23d5 6187 dVAR;
79072805
LW
6188 if (!sv)
6189 return;
a0d0e21e
LW
6190 if (SvREFCNT(sv) == 0) {
6191 if (SvFLAGS(sv) & SVf_BREAK)
645c22ef
DM
6192 /* this SV's refcnt has been artificially decremented to
6193 * trigger cleanup */
a0d0e21e 6194 return;
3280af22 6195 if (PL_in_clean_all) /* All is fair */
1edc1566 6196 return;
d689ffdd
JP
6197 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
6198 /* make sure SvREFCNT(sv)==0 happens very seldom */
6199 SvREFCNT(sv) = (~(U32)0)/2;
6200 return;
6201 }
0453d815 6202 if (ckWARN_d(WARN_INTERNAL))
d5dede04 6203 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
472d47bc
SB
6204 "Attempt to free unreferenced scalar: SV 0x%"UVxf
6205 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
79072805
LW
6206 return;
6207 }
4db098f4 6208 if (--(SvREFCNT(sv)) > 0)
8990e307 6209 return;
8c4d3c90
NC
6210 Perl_sv_free2(aTHX_ sv);
6211}
6212
6213void
6214Perl_sv_free2(pTHX_ SV *sv)
6215{
27da23d5 6216 dVAR;
463ee0b2
LW
6217#ifdef DEBUGGING
6218 if (SvTEMP(sv)) {
0453d815 6219 if (ckWARN_d(WARN_DEBUGGING))
9014280d 6220 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
472d47bc
SB
6221 "Attempt to free temp prematurely: SV 0x%"UVxf
6222 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
79072805 6223 return;
79072805 6224 }
463ee0b2 6225#endif
d689ffdd
JP
6226 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
6227 /* make sure SvREFCNT(sv)==0 happens very seldom */
6228 SvREFCNT(sv) = (~(U32)0)/2;
6229 return;
6230 }
79072805 6231 sv_clear(sv);
477f5d66
CS
6232 if (! SvREFCNT(sv))
6233 del_SV(sv);
79072805
LW
6234}
6235
954c1994
GS
6236/*
6237=for apidoc sv_len
6238
645c22ef
DM
6239Returns the length of the string in the SV. Handles magic and type
6240coercion. See also C<SvCUR>, which gives raw access to the xpv_cur slot.
954c1994
GS
6241
6242=cut
6243*/
6244
79072805 6245STRLEN
864dbfa3 6246Perl_sv_len(pTHX_ register SV *sv)
79072805 6247{
463ee0b2 6248 STRLEN len;
79072805
LW
6249
6250 if (!sv)
6251 return 0;
6252
8990e307 6253 if (SvGMAGICAL(sv))
565764a8 6254 len = mg_length(sv);
8990e307 6255 else
4d84ee25 6256 (void)SvPV_const(sv, len);
463ee0b2 6257 return len;
79072805
LW
6258}
6259
c461cf8f
JH
6260/*
6261=for apidoc sv_len_utf8
6262
6263Returns the number of characters in the string in an SV, counting wide
1e54db1a 6264UTF-8 bytes as a single character. Handles magic and type coercion.
c461cf8f
JH
6265
6266=cut
6267*/
6268
7e8c5dac
HS
6269/*
6270 * The length is cached in PERL_UTF8_magic, in the mg_len field. Also the
6271 * mg_ptr is used, by sv_pos_u2b(), see the comments of S_utf8_mg_pos_init().
6272 * (Note that the mg_len is not the length of the mg_ptr field.)
7a5fa8a2 6273 *
7e8c5dac
HS
6274 */
6275
a0ed51b3 6276STRLEN
864dbfa3 6277Perl_sv_len_utf8(pTHX_ register SV *sv)
a0ed51b3 6278{
a0ed51b3
LW
6279 if (!sv)
6280 return 0;
6281
a0ed51b3 6282 if (SvGMAGICAL(sv))
b76347f2 6283 return mg_length(sv);
a0ed51b3 6284 else
b76347f2 6285 {
7e8c5dac 6286 STRLEN len, ulen;
e62f0680 6287 const U8 *s = (U8*)SvPV_const(sv, len);
7e8c5dac
HS
6288 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : 0;
6289
e23c8137 6290 if (mg && mg->mg_len != -1 && (mg->mg_len > 0 || len == 0)) {
7e8c5dac 6291 ulen = mg->mg_len;
e23c8137
JH
6292#ifdef PERL_UTF8_CACHE_ASSERT
6293 assert(ulen == Perl_utf8_length(aTHX_ s, s + len));
6294#endif
6295 }
7e8c5dac
HS
6296 else {
6297 ulen = Perl_utf8_length(aTHX_ s, s + len);
6298 if (!mg && !SvREADONLY(sv)) {
6299 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
6300 mg = mg_find(sv, PERL_MAGIC_utf8);
6301 assert(mg);
6302 }
6303 if (mg)
6304 mg->mg_len = ulen;
6305 }
6306 return ulen;
6307 }
6308}
6309
6310/* S_utf8_mg_pos_init() is used to initialize the mg_ptr field of
6311 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
6312 * between UTF-8 and byte offsets. There are two (substr offset and substr
6313 * length, the i offset, PERL_MAGIC_UTF8_CACHESIZE) times two (UTF-8 offset
6314 * and byte offset) cache positions.
6315 *
6316 * The mg_len field is used by sv_len_utf8(), see its comments.
6317 * Note that the mg_len is not the length of the mg_ptr field.
6318 *
6319 */
6320STATIC bool
245d4a47
NC
6321S_utf8_mg_pos_init(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i,
6322 I32 offsetp, const U8 *s, const U8 *start)
7e8c5dac 6323{
7a5fa8a2 6324 bool found = FALSE;
7e8c5dac
HS
6325
6326 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
8f78557a 6327 if (!*mgp)
27da23d5 6328 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0, 0);
7e8c5dac 6329 assert(*mgp);
b76347f2 6330
7e8c5dac
HS
6331 if ((*mgp)->mg_ptr)
6332 *cachep = (STRLEN *) (*mgp)->mg_ptr;
6333 else {
6334 Newz(0, *cachep, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
6335 (*mgp)->mg_ptr = (char *) *cachep;
6336 }
6337 assert(*cachep);
6338
a3b680e6 6339 (*cachep)[i] = offsetp;
7e8c5dac
HS
6340 (*cachep)[i+1] = s - start;
6341 found = TRUE;
a0ed51b3 6342 }
7e8c5dac
HS
6343
6344 return found;
a0ed51b3
LW
6345}
6346
645c22ef 6347/*
7e8c5dac
HS
6348 * S_utf8_mg_pos() is used to query and update mg_ptr field of
6349 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
6350 * between UTF-8 and byte offsets. See also the comments of
6351 * S_utf8_mg_pos_init().
6352 *
6353 */
6354STATIC bool
245d4a47 6355S_utf8_mg_pos(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, I32 uoff, const U8 **sp, const U8 *start, const U8 *send)
7e8c5dac
HS
6356{
6357 bool found = FALSE;
6358
6359 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
6360 if (!*mgp)
6361 *mgp = mg_find(sv, PERL_MAGIC_utf8);
6362 if (*mgp && (*mgp)->mg_ptr) {
6363 *cachep = (STRLEN *) (*mgp)->mg_ptr;
e23c8137 6364 ASSERT_UTF8_CACHE(*cachep);
667208dd 6365 if ((*cachep)[i] == (STRLEN)uoff) /* An exact match. */
7a5fa8a2 6366 found = TRUE;
7e8c5dac
HS
6367 else { /* We will skip to the right spot. */
6368 STRLEN forw = 0;
6369 STRLEN backw = 0;
a3b680e6 6370 const U8* p = NULL;
7e8c5dac
HS
6371
6372 /* The assumption is that going backward is half
6373 * the speed of going forward (that's where the
6374 * 2 * backw in the below comes from). (The real
6375 * figure of course depends on the UTF-8 data.) */
6376
667208dd 6377 if ((*cachep)[i] > (STRLEN)uoff) {
7e8c5dac 6378 forw = uoff;
667208dd 6379 backw = (*cachep)[i] - (STRLEN)uoff;
7e8c5dac
HS
6380
6381 if (forw < 2 * backw)
6382 p = start;
6383 else
6384 p = start + (*cachep)[i+1];
6385 }
6386 /* Try this only for the substr offset (i == 0),
6387 * not for the substr length (i == 2). */
6388 else if (i == 0) { /* (*cachep)[i] < uoff */
a3b680e6 6389 const STRLEN ulen = sv_len_utf8(sv);
7e8c5dac 6390
667208dd
JH
6391 if ((STRLEN)uoff < ulen) {
6392 forw = (STRLEN)uoff - (*cachep)[i];
6393 backw = ulen - (STRLEN)uoff;
7e8c5dac
HS
6394
6395 if (forw < 2 * backw)
6396 p = start + (*cachep)[i+1];
6397 else
6398 p = send;
6399 }
6400
6401 /* If the string is not long enough for uoff,
6402 * we could extend it, but not at this low a level. */
6403 }
6404
6405 if (p) {
6406 if (forw < 2 * backw) {
6407 while (forw--)
6408 p += UTF8SKIP(p);
6409 }
6410 else {
6411 while (backw--) {
6412 p--;
6413 while (UTF8_IS_CONTINUATION(*p))
6414 p--;
6415 }
6416 }
6417
6418 /* Update the cache. */
667208dd 6419 (*cachep)[i] = (STRLEN)uoff;
7e8c5dac 6420 (*cachep)[i+1] = p - start;
8f78557a
AE
6421
6422 /* Drop the stale "length" cache */
6423 if (i == 0) {
6424 (*cachep)[2] = 0;
6425 (*cachep)[3] = 0;
6426 }
7a5fa8a2 6427
7e8c5dac
HS
6428 found = TRUE;
6429 }
6430 }
6431 if (found) { /* Setup the return values. */
6432 *offsetp = (*cachep)[i+1];
6433 *sp = start + *offsetp;
6434 if (*sp >= send) {
6435 *sp = send;
6436 *offsetp = send - start;
6437 }
6438 else if (*sp < start) {
6439 *sp = start;
6440 *offsetp = 0;
6441 }
6442 }
6443 }
e23c8137
JH
6444#ifdef PERL_UTF8_CACHE_ASSERT
6445 if (found) {
6446 U8 *s = start;
6447 I32 n = uoff;
6448
6449 while (n-- && s < send)
6450 s += UTF8SKIP(s);
6451
6452 if (i == 0) {
6453 assert(*offsetp == s - start);
6454 assert((*cachep)[0] == (STRLEN)uoff);
6455 assert((*cachep)[1] == *offsetp);
6456 }
6457 ASSERT_UTF8_CACHE(*cachep);
6458 }
6459#endif
7e8c5dac 6460 }
e23c8137 6461
7e8c5dac
HS
6462 return found;
6463}
7a5fa8a2 6464
7e8c5dac 6465/*
645c22ef
DM
6466=for apidoc sv_pos_u2b
6467
1e54db1a 6468Converts the value pointed to by offsetp from a count of UTF-8 chars from
645c22ef
DM
6469the start of the string, to a count of the equivalent number of bytes; if
6470lenp is non-zero, it does the same to lenp, but this time starting from
6471the offset, rather than from the start of the string. Handles magic and
6472type coercion.
6473
6474=cut
6475*/
6476
7e8c5dac
HS
6477/*
6478 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
6479 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
6480 * byte offsets. See also the comments of S_utf8_mg_pos().
6481 *
6482 */
6483
a0ed51b3 6484void
864dbfa3 6485Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
a0ed51b3 6486{
245d4a47 6487 const U8 *start;
a0ed51b3
LW
6488 STRLEN len;
6489
6490 if (!sv)
6491 return;
6492
245d4a47 6493 start = (U8*)SvPV_const(sv, len);
7e8c5dac 6494 if (len) {
b464bac0
AL
6495 STRLEN boffset = 0;
6496 STRLEN *cache = 0;
245d4a47
NC
6497 const U8 *s = start;
6498 I32 uoffset = *offsetp;
6499 const U8 *send = s + len;
6500 MAGIC *mg = 0;
6501 bool found = FALSE;
7e8c5dac 6502
bdf77a2a 6503 if (utf8_mg_pos(sv, &mg, &cache, 0, offsetp, *offsetp, &s, start, send))
7e8c5dac
HS
6504 found = TRUE;
6505 if (!found && uoffset > 0) {
6506 while (s < send && uoffset--)
6507 s += UTF8SKIP(s);
6508 if (s >= send)
6509 s = send;
a3b680e6 6510 if (utf8_mg_pos_init(sv, &mg, &cache, 0, *offsetp, s, start))
7e8c5dac
HS
6511 boffset = cache[1];
6512 *offsetp = s - start;
6513 }
6514 if (lenp) {
6515 found = FALSE;
6516 start = s;
ec062429 6517 if (utf8_mg_pos(sv, &mg, &cache, 2, lenp, *lenp, &s, start, send)) {
7e8c5dac
HS
6518 *lenp -= boffset;
6519 found = TRUE;
6520 }
6521 if (!found && *lenp > 0) {
6522 I32 ulen = *lenp;
6523 if (ulen > 0)
6524 while (s < send && ulen--)
6525 s += UTF8SKIP(s);
6526 if (s >= send)
6527 s = send;
a3b680e6 6528 utf8_mg_pos_init(sv, &mg, &cache, 2, *lenp, s, start);
7e8c5dac
HS
6529 }
6530 *lenp = s - start;
6531 }
e23c8137 6532 ASSERT_UTF8_CACHE(cache);
7e8c5dac
HS
6533 }
6534 else {
6535 *offsetp = 0;
6536 if (lenp)
6537 *lenp = 0;
a0ed51b3 6538 }
e23c8137 6539
a0ed51b3
LW
6540 return;
6541}
6542
645c22ef
DM
6543/*
6544=for apidoc sv_pos_b2u
6545
6546Converts the value pointed to by offsetp from a count of bytes from the
1e54db1a 6547start of the string, to a count of the equivalent number of UTF-8 chars.
645c22ef
DM
6548Handles magic and type coercion.
6549
6550=cut
6551*/
6552
7e8c5dac
HS
6553/*
6554 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
6555 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
6556 * byte offsets. See also the comments of S_utf8_mg_pos().
6557 *
6558 */
6559
a0ed51b3 6560void
7e8c5dac 6561Perl_sv_pos_b2u(pTHX_ register SV* sv, I32* offsetp)
a0ed51b3 6562{
83003860 6563 const U8* s;
a0ed51b3
LW
6564 STRLEN len;
6565
6566 if (!sv)
6567 return;
6568
83003860 6569 s = (const U8*)SvPV_const(sv, len);
eb160463 6570 if ((I32)len < *offsetp)
a0dbb045 6571 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset");
7e8c5dac 6572 else {
83003860 6573 const U8* send = s + *offsetp;
7e8c5dac
HS
6574 MAGIC* mg = NULL;
6575 STRLEN *cache = NULL;
6576
6577 len = 0;
6578
6579 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
6580 mg = mg_find(sv, PERL_MAGIC_utf8);
6581 if (mg && mg->mg_ptr) {
6582 cache = (STRLEN *) mg->mg_ptr;
c5661c80 6583 if (cache[1] == (STRLEN)*offsetp) {
7e8c5dac
HS
6584 /* An exact match. */
6585 *offsetp = cache[0];
6586
6587 return;
6588 }
c5661c80 6589 else if (cache[1] < (STRLEN)*offsetp) {
7e8c5dac
HS
6590 /* We already know part of the way. */
6591 len = cache[0];
6592 s += cache[1];
7a5fa8a2 6593 /* Let the below loop do the rest. */
7e8c5dac
HS
6594 }
6595 else { /* cache[1] > *offsetp */
6596 /* We already know all of the way, now we may
6597 * be able to walk back. The same assumption
6598 * is made as in S_utf8_mg_pos(), namely that
6599 * walking backward is twice slower than
6600 * walking forward. */
6601 STRLEN forw = *offsetp;
6602 STRLEN backw = cache[1] - *offsetp;
6603
6604 if (!(forw < 2 * backw)) {
83003860 6605 const U8 *p = s + cache[1];
7e8c5dac 6606 STRLEN ubackw = 0;
7a5fa8a2 6607
a5b510f2
AE
6608 cache[1] -= backw;
6609
7e8c5dac
HS
6610 while (backw--) {
6611 p--;
0aeb64d0 6612 while (UTF8_IS_CONTINUATION(*p)) {
7e8c5dac 6613 p--;
0aeb64d0
JH
6614 backw--;
6615 }
7e8c5dac
HS
6616 ubackw++;
6617 }
6618
6619 cache[0] -= ubackw;
0aeb64d0 6620 *offsetp = cache[0];
a67d7df9
TS
6621
6622 /* Drop the stale "length" cache */
6623 cache[2] = 0;
6624 cache[3] = 0;
6625
0aeb64d0 6626 return;
7e8c5dac
HS
6627 }
6628 }
6629 }
e23c8137 6630 ASSERT_UTF8_CACHE(cache);
a0dbb045 6631 }
7e8c5dac
HS
6632
6633 while (s < send) {
6634 STRLEN n = 1;
6635
6636 /* Call utf8n_to_uvchr() to validate the sequence
6637 * (unless a simple non-UTF character) */
6638 if (!UTF8_IS_INVARIANT(*s))
6639 utf8n_to_uvchr(s, UTF8SKIP(s), &n, 0);
6640 if (n > 0) {
6641 s += n;
6642 len++;
6643 }
6644 else
6645 break;
6646 }
6647
6648 if (!SvREADONLY(sv)) {
6649 if (!mg) {
6650 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
6651 mg = mg_find(sv, PERL_MAGIC_utf8);
6652 }
6653 assert(mg);
6654
6655 if (!mg->mg_ptr) {
979acdb5 6656 Newz(0, cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
7e8c5dac
HS
6657 mg->mg_ptr = (char *) cache;
6658 }
6659 assert(cache);
6660
6661 cache[0] = len;
6662 cache[1] = *offsetp;
a67d7df9
TS
6663 /* Drop the stale "length" cache */
6664 cache[2] = 0;
6665 cache[3] = 0;
7e8c5dac
HS
6666 }
6667
6668 *offsetp = len;
a0ed51b3 6669 }
a0ed51b3
LW
6670 return;
6671}
6672
954c1994
GS
6673/*
6674=for apidoc sv_eq
6675
6676Returns a boolean indicating whether the strings in the two SVs are
645c22ef
DM
6677identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will
6678coerce its args to strings if necessary.
954c1994
GS
6679
6680=cut
6681*/
6682
79072805 6683I32
e01b9e88 6684Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2)
79072805 6685{
e1ec3a88 6686 const char *pv1;
463ee0b2 6687 STRLEN cur1;
e1ec3a88 6688 const char *pv2;
463ee0b2 6689 STRLEN cur2;
e01b9e88 6690 I32 eq = 0;
553e1bcc
AT
6691 char *tpv = Nullch;
6692 SV* svrecode = Nullsv;
79072805 6693
e01b9e88 6694 if (!sv1) {
79072805
LW
6695 pv1 = "";
6696 cur1 = 0;
6697 }
463ee0b2 6698 else
4d84ee25 6699 pv1 = SvPV_const(sv1, cur1);
79072805 6700
e01b9e88
SC
6701 if (!sv2){
6702 pv2 = "";
6703 cur2 = 0;
92d29cee 6704 }
e01b9e88 6705 else
4d84ee25 6706 pv2 = SvPV_const(sv2, cur2);
79072805 6707
cf48d248 6708 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
799ef3cb
JH
6709 /* Differing utf8ness.
6710 * Do not UTF8size the comparands as a side-effect. */
6711 if (PL_encoding) {
6712 if (SvUTF8(sv1)) {
553e1bcc
AT
6713 svrecode = newSVpvn(pv2, cur2);
6714 sv_recode_to_utf8(svrecode, PL_encoding);
93524f2b 6715 pv2 = SvPV_const(svrecode, cur2);
799ef3cb
JH
6716 }
6717 else {
553e1bcc
AT
6718 svrecode = newSVpvn(pv1, cur1);
6719 sv_recode_to_utf8(svrecode, PL_encoding);
93524f2b 6720 pv1 = SvPV_const(svrecode, cur1);
799ef3cb
JH
6721 }
6722 /* Now both are in UTF-8. */
0a1bd7ac
DM
6723 if (cur1 != cur2) {
6724 SvREFCNT_dec(svrecode);
799ef3cb 6725 return FALSE;
0a1bd7ac 6726 }
799ef3cb
JH
6727 }
6728 else {
6729 bool is_utf8 = TRUE;
6730
6731 if (SvUTF8(sv1)) {
6732 /* sv1 is the UTF-8 one,
6733 * if is equal it must be downgrade-able */
e1ec3a88 6734 char *pv = (char*)bytes_from_utf8((const U8*)pv1,
799ef3cb
JH
6735 &cur1, &is_utf8);
6736 if (pv != pv1)
553e1bcc 6737 pv1 = tpv = pv;
799ef3cb
JH
6738 }
6739 else {
6740 /* sv2 is the UTF-8 one,
6741 * if is equal it must be downgrade-able */
e1ec3a88 6742 char *pv = (char *)bytes_from_utf8((const U8*)pv2,
799ef3cb
JH
6743 &cur2, &is_utf8);
6744 if (pv != pv2)
553e1bcc 6745 pv2 = tpv = pv;
799ef3cb
JH
6746 }
6747 if (is_utf8) {
6748 /* Downgrade not possible - cannot be eq */
bf694877 6749 assert (tpv == 0);
799ef3cb
JH
6750 return FALSE;
6751 }
6752 }
cf48d248
JH
6753 }
6754
6755 if (cur1 == cur2)
765f542d 6756 eq = (pv1 == pv2) || memEQ(pv1, pv2, cur1);
e01b9e88 6757
553e1bcc
AT
6758 if (svrecode)
6759 SvREFCNT_dec(svrecode);
799ef3cb 6760
553e1bcc
AT
6761 if (tpv)
6762 Safefree(tpv);
cf48d248 6763
e01b9e88 6764 return eq;
79072805
LW
6765}
6766
954c1994
GS
6767/*
6768=for apidoc sv_cmp
6769
6770Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
6771string in C<sv1> is less than, equal to, or greater than the string in
645c22ef
DM
6772C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will
6773coerce its args to strings if necessary. See also C<sv_cmp_locale>.
954c1994
GS
6774
6775=cut
6776*/
6777
79072805 6778I32
e01b9e88 6779Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2)
79072805 6780{
560a288e 6781 STRLEN cur1, cur2;
e1ec3a88
AL
6782 const char *pv1, *pv2;
6783 char *tpv = Nullch;
cf48d248 6784 I32 cmp;
553e1bcc 6785 SV *svrecode = Nullsv;
560a288e 6786
e01b9e88
SC
6787 if (!sv1) {
6788 pv1 = "";
560a288e
GS
6789 cur1 = 0;
6790 }
e01b9e88 6791 else
4d84ee25 6792 pv1 = SvPV_const(sv1, cur1);
560a288e 6793
553e1bcc 6794 if (!sv2) {
e01b9e88 6795 pv2 = "";
560a288e
GS
6796 cur2 = 0;
6797 }
e01b9e88 6798 else
4d84ee25 6799 pv2 = SvPV_const(sv2, cur2);
79072805 6800
cf48d248 6801 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
799ef3cb
JH
6802 /* Differing utf8ness.
6803 * Do not UTF8size the comparands as a side-effect. */
cf48d248 6804 if (SvUTF8(sv1)) {
799ef3cb 6805 if (PL_encoding) {
553e1bcc
AT
6806 svrecode = newSVpvn(pv2, cur2);
6807 sv_recode_to_utf8(svrecode, PL_encoding);
93524f2b 6808 pv2 = SvPV_const(svrecode, cur2);
799ef3cb
JH
6809 }
6810 else {
e1ec3a88 6811 pv2 = tpv = (char*)bytes_to_utf8((const U8*)pv2, &cur2);
799ef3cb 6812 }
cf48d248
JH
6813 }
6814 else {
799ef3cb 6815 if (PL_encoding) {
553e1bcc
AT
6816 svrecode = newSVpvn(pv1, cur1);
6817 sv_recode_to_utf8(svrecode, PL_encoding);
93524f2b 6818 pv1 = SvPV_const(svrecode, cur1);
799ef3cb
JH
6819 }
6820 else {
e1ec3a88 6821 pv1 = tpv = (char*)bytes_to_utf8((const U8*)pv1, &cur1);
799ef3cb 6822 }
cf48d248
JH
6823 }
6824 }
6825
e01b9e88 6826 if (!cur1) {
cf48d248 6827 cmp = cur2 ? -1 : 0;
e01b9e88 6828 } else if (!cur2) {
cf48d248
JH
6829 cmp = 1;
6830 } else {
e1ec3a88 6831 const I32 retval = memcmp((const void*)pv1, (const void*)pv2, cur1 < cur2 ? cur1 : cur2);
e01b9e88
SC
6832
6833 if (retval) {
cf48d248 6834 cmp = retval < 0 ? -1 : 1;
e01b9e88 6835 } else if (cur1 == cur2) {
cf48d248
JH
6836 cmp = 0;
6837 } else {
6838 cmp = cur1 < cur2 ? -1 : 1;
e01b9e88 6839 }
cf48d248 6840 }
16660edb 6841
553e1bcc
AT
6842 if (svrecode)
6843 SvREFCNT_dec(svrecode);
799ef3cb 6844
553e1bcc
AT
6845 if (tpv)
6846 Safefree(tpv);
cf48d248
JH
6847
6848 return cmp;
bbce6d69 6849}
16660edb 6850
c461cf8f
JH
6851/*
6852=for apidoc sv_cmp_locale
6853
645c22ef
DM
6854Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
6855'use bytes' aware, handles get magic, and will coerce its args to strings
6856if necessary. See also C<sv_cmp_locale>. See also C<sv_cmp>.
c461cf8f
JH
6857
6858=cut
6859*/
6860
bbce6d69 6861I32
864dbfa3 6862Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
bbce6d69 6863{
36477c24 6864#ifdef USE_LOCALE_COLLATE
16660edb 6865
bbce6d69 6866 char *pv1, *pv2;
6867 STRLEN len1, len2;
6868 I32 retval;
16660edb 6869
3280af22 6870 if (PL_collation_standard)
bbce6d69 6871 goto raw_compare;
16660edb 6872
bbce6d69 6873 len1 = 0;
8ac85365 6874 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
bbce6d69 6875 len2 = 0;
8ac85365 6876 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
16660edb 6877
bbce6d69 6878 if (!pv1 || !len1) {
6879 if (pv2 && len2)
6880 return -1;
6881 else
6882 goto raw_compare;
6883 }
6884 else {
6885 if (!pv2 || !len2)
6886 return 1;
6887 }
16660edb 6888
bbce6d69 6889 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
16660edb 6890
bbce6d69 6891 if (retval)
16660edb 6892 return retval < 0 ? -1 : 1;
6893
bbce6d69 6894 /*
6895 * When the result of collation is equality, that doesn't mean
6896 * that there are no differences -- some locales exclude some
6897 * characters from consideration. So to avoid false equalities,
6898 * we use the raw string as a tiebreaker.
6899 */
16660edb 6900
bbce6d69 6901 raw_compare:
6902 /* FALL THROUGH */
16660edb 6903
36477c24 6904#endif /* USE_LOCALE_COLLATE */
16660edb 6905
bbce6d69 6906 return sv_cmp(sv1, sv2);
6907}
79072805 6908
645c22ef 6909
36477c24 6910#ifdef USE_LOCALE_COLLATE
645c22ef 6911
7a4c00b4 6912/*
645c22ef
DM
6913=for apidoc sv_collxfrm
6914
6915Add Collate Transform magic to an SV if it doesn't already have it.
6916
6917Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the
6918scalar data of the variable, but transformed to such a format that a normal
6919memory comparison can be used to compare the data according to the locale
6920settings.
6921
6922=cut
6923*/
6924
bbce6d69 6925char *
864dbfa3 6926Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp)
bbce6d69 6927{
7a4c00b4 6928 MAGIC *mg;
16660edb 6929
14befaf4 6930 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
3280af22 6931 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
93524f2b
NC
6932 const char *s;
6933 char *xf;
bbce6d69 6934 STRLEN len, xlen;
6935
7a4c00b4 6936 if (mg)
6937 Safefree(mg->mg_ptr);
93524f2b 6938 s = SvPV_const(sv, len);
bbce6d69 6939 if ((xf = mem_collxfrm(s, len, &xlen))) {
ff0cee69 6940 if (SvREADONLY(sv)) {
6941 SAVEFREEPV(xf);
6942 *nxp = xlen;
3280af22 6943 return xf + sizeof(PL_collation_ix);
ff0cee69 6944 }
7a4c00b4 6945 if (! mg) {
14befaf4
DM
6946 sv_magic(sv, 0, PERL_MAGIC_collxfrm, 0, 0);
6947 mg = mg_find(sv, PERL_MAGIC_collxfrm);
7a4c00b4 6948 assert(mg);
bbce6d69 6949 }
7a4c00b4 6950 mg->mg_ptr = xf;
565764a8 6951 mg->mg_len = xlen;
7a4c00b4 6952 }
6953 else {
ff0cee69 6954 if (mg) {
6955 mg->mg_ptr = NULL;
565764a8 6956 mg->mg_len = -1;
ff0cee69 6957 }
bbce6d69 6958 }
6959 }
7a4c00b4 6960 if (mg && mg->mg_ptr) {
565764a8 6961 *nxp = mg->mg_len;
3280af22 6962 return mg->mg_ptr + sizeof(PL_collation_ix);
bbce6d69 6963 }
6964 else {
6965 *nxp = 0;
6966 return NULL;
16660edb 6967 }
79072805
LW
6968}
6969
36477c24 6970#endif /* USE_LOCALE_COLLATE */
bbce6d69 6971
c461cf8f
JH
6972/*
6973=for apidoc sv_gets
6974
6975Get a line from the filehandle and store it into the SV, optionally
6976appending to the currently-stored string.
6977
6978=cut
6979*/
6980
79072805 6981char *
864dbfa3 6982Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append)
79072805 6983{
e1ec3a88 6984 const char *rsptr;
c07a80fd 6985 STRLEN rslen;
6986 register STDCHAR rslast;
6987 register STDCHAR *bp;
6988 register I32 cnt;
9c5ffd7c 6989 I32 i = 0;
8bfdd7d9 6990 I32 rspara = 0;
e311fd51 6991 I32 recsize;
c07a80fd 6992
bc44a8a2
NC
6993 if (SvTHINKFIRST(sv))
6994 sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
765f542d
NC
6995 /* XXX. If you make this PVIV, then copy on write can copy scalars read
6996 from <>.
6997 However, perlbench says it's slower, because the existing swipe code
6998 is faster than copy on write.
6999 Swings and roundabouts. */
862a34c6 7000 SvUPGRADE(sv, SVt_PV);
99491443 7001
ff68c719 7002 SvSCREAM_off(sv);
efd8b2ba
AE
7003
7004 if (append) {
7005 if (PerlIO_isutf8(fp)) {
7006 if (!SvUTF8(sv)) {
7007 sv_utf8_upgrade_nomg(sv);
7008 sv_pos_u2b(sv,&append,0);
7009 }
7010 } else if (SvUTF8(sv)) {
7011 SV *tsv = NEWSV(0,0);
7012 sv_gets(tsv, fp, 0);
7013 sv_utf8_upgrade_nomg(tsv);
7014 SvCUR_set(sv,append);
7015 sv_catsv(sv,tsv);
7016 sv_free(tsv);
7017 goto return_string_or_null;
7018 }
7019 }
7020
7021 SvPOK_only(sv);
7022 if (PerlIO_isutf8(fp))
7023 SvUTF8_on(sv);
c07a80fd 7024
923e4eb5 7025 if (IN_PERL_COMPILETIME) {
8bfdd7d9
HS
7026 /* we always read code in line mode */
7027 rsptr = "\n";
7028 rslen = 1;
7029 }
7030 else if (RsSNARF(PL_rs)) {
7a5fa8a2
NIS
7031 /* If it is a regular disk file use size from stat() as estimate
7032 of amount we are going to read - may result in malloc-ing
7033 more memory than we realy need if layers bellow reduce
e468d35b
NIS
7034 size we read (e.g. CRLF or a gzip layer)
7035 */
e311fd51 7036 Stat_t st;
e468d35b 7037 if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode)) {
f54cb97a 7038 const Off_t offset = PerlIO_tell(fp);
58f1856e 7039 if (offset != (Off_t) -1 && st.st_size + append > offset) {
e468d35b
NIS
7040 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
7041 }
7042 }
c07a80fd 7043 rsptr = NULL;
7044 rslen = 0;
7045 }
3280af22 7046 else if (RsRECORD(PL_rs)) {
e311fd51 7047 I32 bytesread;
5b2b9c68
HM
7048 char *buffer;
7049
7050 /* Grab the size of the record we're getting */
3280af22 7051 recsize = SvIV(SvRV(PL_rs));
e311fd51 7052 buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
5b2b9c68
HM
7053 /* Go yank in */
7054#ifdef VMS
7055 /* VMS wants read instead of fread, because fread doesn't respect */
7056 /* RMS record boundaries. This is not necessarily a good thing to be */
e468d35b
NIS
7057 /* doing, but we've got no other real choice - except avoid stdio
7058 as implementation - perhaps write a :vms layer ?
7059 */
5b2b9c68
HM
7060 bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
7061#else
7062 bytesread = PerlIO_read(fp, buffer, recsize);
7063#endif
27e6ca2d
AE
7064 if (bytesread < 0)
7065 bytesread = 0;
e311fd51 7066 SvCUR_set(sv, bytesread += append);
e670df4e 7067 buffer[bytesread] = '\0';
efd8b2ba 7068 goto return_string_or_null;
5b2b9c68 7069 }
3280af22 7070 else if (RsPARA(PL_rs)) {
c07a80fd 7071 rsptr = "\n\n";
7072 rslen = 2;
8bfdd7d9 7073 rspara = 1;
c07a80fd 7074 }
7d59b7e4
NIS
7075 else {
7076 /* Get $/ i.e. PL_rs into same encoding as stream wants */
7077 if (PerlIO_isutf8(fp)) {
7078 rsptr = SvPVutf8(PL_rs, rslen);
7079 }
7080 else {
7081 if (SvUTF8(PL_rs)) {
7082 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
7083 Perl_croak(aTHX_ "Wide character in $/");
7084 }
7085 }
93524f2b 7086 rsptr = SvPV_const(PL_rs, rslen);
7d59b7e4
NIS
7087 }
7088 }
7089
c07a80fd 7090 rslast = rslen ? rsptr[rslen - 1] : '\0';
7091
8bfdd7d9 7092 if (rspara) { /* have to do this both before and after */
79072805 7093 do { /* to make sure file boundaries work right */
760ac839 7094 if (PerlIO_eof(fp))
a0d0e21e 7095 return 0;
760ac839 7096 i = PerlIO_getc(fp);
79072805 7097 if (i != '\n') {
a0d0e21e
LW
7098 if (i == -1)
7099 return 0;
760ac839 7100 PerlIO_ungetc(fp,i);
79072805
LW
7101 break;
7102 }
7103 } while (i != EOF);
7104 }
c07a80fd 7105
760ac839
LW
7106 /* See if we know enough about I/O mechanism to cheat it ! */
7107
7108 /* This used to be #ifdef test - it is made run-time test for ease
1c846c1f 7109 of abstracting out stdio interface. One call should be cheap
760ac839
LW
7110 enough here - and may even be a macro allowing compile
7111 time optimization.
7112 */
7113
7114 if (PerlIO_fast_gets(fp)) {
7115
7116 /*
7117 * We're going to steal some values from the stdio struct
7118 * and put EVERYTHING in the innermost loop into registers.
7119 */
7120 register STDCHAR *ptr;
7121 STRLEN bpx;
7122 I32 shortbuffered;
7123
16660edb 7124#if defined(VMS) && defined(PERLIO_IS_STDIO)
7125 /* An ungetc()d char is handled separately from the regular
7126 * buffer, so we getc() it back out and stuff it in the buffer.
7127 */
7128 i = PerlIO_getc(fp);
7129 if (i == EOF) return 0;
7130 *(--((*fp)->_ptr)) = (unsigned char) i;
7131 (*fp)->_cnt++;
7132#endif
c07a80fd 7133
c2960299 7134 /* Here is some breathtakingly efficient cheating */
c07a80fd 7135
a20bf0c3 7136 cnt = PerlIO_get_cnt(fp); /* get count into register */
e468d35b 7137 /* make sure we have the room */
7a5fa8a2 7138 if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
e468d35b 7139 /* Not room for all of it
7a5fa8a2 7140 if we are looking for a separator and room for some
e468d35b
NIS
7141 */
7142 if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
7a5fa8a2 7143 /* just process what we have room for */
79072805
LW
7144 shortbuffered = cnt - SvLEN(sv) + append + 1;
7145 cnt -= shortbuffered;
7146 }
7147 else {
7148 shortbuffered = 0;
bbce6d69 7149 /* remember that cnt can be negative */
eb160463 7150 SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
79072805
LW
7151 }
7152 }
7a5fa8a2 7153 else
79072805 7154 shortbuffered = 0;
3f7c398e 7155 bp = (STDCHAR*)SvPVX_const(sv) + append; /* move these two too to registers */
a20bf0c3 7156 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
16660edb 7157 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7158 "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
16660edb 7159 DEBUG_P(PerlIO_printf(Perl_debug_log,
ba7abf9d 7160 "Screamer: entering: PerlIO * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 7161 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 7162 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
79072805
LW
7163 for (;;) {
7164 screamer:
93a17b20 7165 if (cnt > 0) {
c07a80fd 7166 if (rslen) {
760ac839
LW
7167 while (cnt > 0) { /* this | eat */
7168 cnt--;
c07a80fd 7169 if ((*bp++ = *ptr++) == rslast) /* really | dust */
7170 goto thats_all_folks; /* screams | sed :-) */
7171 }
7172 }
7173 else {
1c846c1f
NIS
7174 Copy(ptr, bp, cnt, char); /* this | eat */
7175 bp += cnt; /* screams | dust */
c07a80fd 7176 ptr += cnt; /* louder | sed :-) */
a5f75d66 7177 cnt = 0;
93a17b20 7178 }
79072805
LW
7179 }
7180
748a9306 7181 if (shortbuffered) { /* oh well, must extend */
79072805
LW
7182 cnt = shortbuffered;
7183 shortbuffered = 0;
3f7c398e 7184 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
79072805
LW
7185 SvCUR_set(sv, bpx);
7186 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
3f7c398e 7187 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
79072805
LW
7188 continue;
7189 }
7190
16660edb 7191 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841
GS
7192 "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n",
7193 PTR2UV(ptr),(long)cnt));
cc00df79 7194 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
ba7abf9d 7195#if 0
16660edb 7196 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7197 "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 7198 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 7199 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
ba7abf9d 7200#endif
1c846c1f 7201 /* This used to call 'filbuf' in stdio form, but as that behaves like
774d564b 7202 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
7203 another abstraction. */
760ac839 7204 i = PerlIO_getc(fp); /* get more characters */
ba7abf9d 7205#if 0
16660edb 7206 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7207 "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 7208 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 7209 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
ba7abf9d 7210#endif
a20bf0c3
JH
7211 cnt = PerlIO_get_cnt(fp);
7212 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
16660edb 7213 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7214 "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
79072805 7215
748a9306
LW
7216 if (i == EOF) /* all done for ever? */
7217 goto thats_really_all_folks;
7218
3f7c398e 7219 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
79072805
LW
7220 SvCUR_set(sv, bpx);
7221 SvGROW(sv, bpx + cnt + 2);
3f7c398e 7222 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
c07a80fd 7223
eb160463 7224 *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */
79072805 7225
c07a80fd 7226 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
79072805 7227 goto thats_all_folks;
79072805
LW
7228 }
7229
7230thats_all_folks:
3f7c398e 7231 if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX_const(sv)) < rslen) ||
36477c24 7232 memNE((char*)bp - rslen, rsptr, rslen))
760ac839 7233 goto screamer; /* go back to the fray */
79072805
LW
7234thats_really_all_folks:
7235 if (shortbuffered)
7236 cnt += shortbuffered;
16660edb 7237 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7238 "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
cc00df79 7239 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */
16660edb 7240 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7241 "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 7242 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 7243 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
79072805 7244 *bp = '\0';
3f7c398e 7245 SvCUR_set(sv, bp - (STDCHAR*)SvPVX_const(sv)); /* set length */
16660edb 7246 DEBUG_P(PerlIO_printf(Perl_debug_log,
fb73857a 7247 "Screamer: done, len=%ld, string=|%.*s|\n",
3f7c398e 7248 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX_const(sv)));
760ac839
LW
7249 }
7250 else
79072805 7251 {
6edd2cd5 7252 /*The big, slow, and stupid way. */
27da23d5 7253#ifdef USE_HEAP_INSTEAD_OF_STACK /* Even slower way. */
6edd2cd5
JH
7254 STDCHAR *buf = 0;
7255 New(0, buf, 8192, STDCHAR);
7256 assert(buf);
4d2c4e07 7257#else
6edd2cd5 7258 STDCHAR buf[8192];
4d2c4e07 7259#endif
79072805 7260
760ac839 7261screamer2:
c07a80fd 7262 if (rslen) {
6867be6d 7263 const register STDCHAR *bpe = buf + sizeof(buf);
760ac839 7264 bp = buf;
eb160463 7265 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
760ac839
LW
7266 ; /* keep reading */
7267 cnt = bp - buf;
c07a80fd 7268 }
7269 else {
760ac839 7270 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
16660edb 7271 /* Accomodate broken VAXC compiler, which applies U8 cast to
7272 * both args of ?: operator, causing EOF to change into 255
7273 */
37be0adf 7274 if (cnt > 0)
cbe9e203
JH
7275 i = (U8)buf[cnt - 1];
7276 else
37be0adf 7277 i = EOF;
c07a80fd 7278 }
79072805 7279
cbe9e203
JH
7280 if (cnt < 0)
7281 cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
7282 if (append)
7283 sv_catpvn(sv, (char *) buf, cnt);
7284 else
7285 sv_setpvn(sv, (char *) buf, cnt);
c07a80fd 7286
7287 if (i != EOF && /* joy */
7288 (!rslen ||
7289 SvCUR(sv) < rslen ||
3f7c398e 7290 memNE(SvPVX_const(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
79072805
LW
7291 {
7292 append = -1;
63e4d877
CS
7293 /*
7294 * If we're reading from a TTY and we get a short read,
7295 * indicating that the user hit his EOF character, we need
7296 * to notice it now, because if we try to read from the TTY
7297 * again, the EOF condition will disappear.
7298 *
7299 * The comparison of cnt to sizeof(buf) is an optimization
7300 * that prevents unnecessary calls to feof().
7301 *
7302 * - jik 9/25/96
7303 */
7304 if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
7305 goto screamer2;
79072805 7306 }
6edd2cd5 7307
27da23d5 7308#ifdef USE_HEAP_INSTEAD_OF_STACK
6edd2cd5
JH
7309 Safefree(buf);
7310#endif
79072805
LW
7311 }
7312
8bfdd7d9 7313 if (rspara) { /* have to do this both before and after */
c07a80fd 7314 while (i != EOF) { /* to make sure file boundaries work right */
760ac839 7315 i = PerlIO_getc(fp);
79072805 7316 if (i != '\n') {
760ac839 7317 PerlIO_ungetc(fp,i);
79072805
LW
7318 break;
7319 }
7320 }
7321 }
c07a80fd 7322
efd8b2ba 7323return_string_or_null:
c07a80fd 7324 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
79072805
LW
7325}
7326
954c1994
GS
7327/*
7328=for apidoc sv_inc
7329
645c22ef
DM
7330Auto-increment of the value in the SV, doing string to numeric conversion
7331if necessary. Handles 'get' magic.
954c1994
GS
7332
7333=cut
7334*/
7335
79072805 7336void
864dbfa3 7337Perl_sv_inc(pTHX_ register SV *sv)
79072805
LW
7338{
7339 register char *d;
463ee0b2 7340 int flags;
79072805
LW
7341
7342 if (!sv)
7343 return;
b23a5f78
GB
7344 if (SvGMAGICAL(sv))
7345 mg_get(sv);
ed6116ce 7346 if (SvTHINKFIRST(sv)) {
765f542d
NC
7347 if (SvIsCOW(sv))
7348 sv_force_normal_flags(sv, 0);
0f15f207 7349 if (SvREADONLY(sv)) {
923e4eb5 7350 if (IN_PERL_RUNTIME)
cea2e8a9 7351 Perl_croak(aTHX_ PL_no_modify);
0f15f207 7352 }
a0d0e21e 7353 if (SvROK(sv)) {
b5be31e9 7354 IV i;
9e7bc3e8
JD
7355 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
7356 return;
56431972 7357 i = PTR2IV(SvRV(sv));
b5be31e9
SM
7358 sv_unref(sv);
7359 sv_setiv(sv, i);
a0d0e21e 7360 }
ed6116ce 7361 }
8990e307 7362 flags = SvFLAGS(sv);
28e5dec8
JH
7363 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
7364 /* It's (privately or publicly) a float, but not tested as an
7365 integer, so test it to see. */
d460ef45 7366 (void) SvIV(sv);
28e5dec8
JH
7367 flags = SvFLAGS(sv);
7368 }
7369 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
7370 /* It's publicly an integer, or privately an integer-not-float */
59d8ce62 7371#ifdef PERL_PRESERVE_IVUV
28e5dec8 7372 oops_its_int:
59d8ce62 7373#endif
25da4f38
IZ
7374 if (SvIsUV(sv)) {
7375 if (SvUVX(sv) == UV_MAX)
a1e868e7 7376 sv_setnv(sv, UV_MAX_P1);
25da4f38
IZ
7377 else
7378 (void)SvIOK_only_UV(sv);
607fa7f2 7379 SvUV_set(sv, SvUVX(sv) + 1);
25da4f38
IZ
7380 } else {
7381 if (SvIVX(sv) == IV_MAX)
28e5dec8 7382 sv_setuv(sv, (UV)IV_MAX + 1);
25da4f38
IZ
7383 else {
7384 (void)SvIOK_only(sv);
45977657 7385 SvIV_set(sv, SvIVX(sv) + 1);
1c846c1f 7386 }
55497cff 7387 }
79072805
LW
7388 return;
7389 }
28e5dec8
JH
7390 if (flags & SVp_NOK) {
7391 (void)SvNOK_only(sv);
9d6ce603 7392 SvNV_set(sv, SvNVX(sv) + 1.0);
28e5dec8
JH
7393 return;
7394 }
7395
3f7c398e 7396 if (!(flags & SVp_POK) || !*SvPVX_const(sv)) {
28e5dec8
JH
7397 if ((flags & SVTYPEMASK) < SVt_PVIV)
7398 sv_upgrade(sv, SVt_IV);
7399 (void)SvIOK_only(sv);
45977657 7400 SvIV_set(sv, 1);
79072805
LW
7401 return;
7402 }
463ee0b2 7403 d = SvPVX(sv);
79072805
LW
7404 while (isALPHA(*d)) d++;
7405 while (isDIGIT(*d)) d++;
7406 if (*d) {
28e5dec8 7407#ifdef PERL_PRESERVE_IVUV
d1be9408 7408 /* Got to punt this as an integer if needs be, but we don't issue
28e5dec8
JH
7409 warnings. Probably ought to make the sv_iv_please() that does
7410 the conversion if possible, and silently. */
504618e9 7411 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
28e5dec8
JH
7412 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
7413 /* Need to try really hard to see if it's an integer.
7414 9.22337203685478e+18 is an integer.
7415 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
7416 so $a="9.22337203685478e+18"; $a+0; $a++
7417 needs to be the same as $a="9.22337203685478e+18"; $a++
7418 or we go insane. */
d460ef45 7419
28e5dec8
JH
7420 (void) sv_2iv(sv);
7421 if (SvIOK(sv))
7422 goto oops_its_int;
7423
7424 /* sv_2iv *should* have made this an NV */
7425 if (flags & SVp_NOK) {
7426 (void)SvNOK_only(sv);
9d6ce603 7427 SvNV_set(sv, SvNVX(sv) + 1.0);
28e5dec8
JH
7428 return;
7429 }
7430 /* I don't think we can get here. Maybe I should assert this
7431 And if we do get here I suspect that sv_setnv will croak. NWC
7432 Fall through. */
7433#if defined(USE_LONG_DOUBLE)
7434 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
3f7c398e 7435 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8 7436#else
1779d84d 7437 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
3f7c398e 7438 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8
JH
7439#endif
7440 }
7441#endif /* PERL_PRESERVE_IVUV */
3f7c398e 7442 sv_setnv(sv,Atof(SvPVX_const(sv)) + 1.0);
79072805
LW
7443 return;
7444 }
7445 d--;
3f7c398e 7446 while (d >= SvPVX_const(sv)) {
79072805
LW
7447 if (isDIGIT(*d)) {
7448 if (++*d <= '9')
7449 return;
7450 *(d--) = '0';
7451 }
7452 else {
9d116dd7
JH
7453#ifdef EBCDIC
7454 /* MKS: The original code here died if letters weren't consecutive.
7455 * at least it didn't have to worry about non-C locales. The
7456 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
1c846c1f 7457 * arranged in order (although not consecutively) and that only
9d116dd7
JH
7458 * [A-Za-z] are accepted by isALPHA in the C locale.
7459 */
7460 if (*d != 'z' && *d != 'Z') {
7461 do { ++*d; } while (!isALPHA(*d));
7462 return;
7463 }
7464 *(d--) -= 'z' - 'a';
7465#else
79072805
LW
7466 ++*d;
7467 if (isALPHA(*d))
7468 return;
7469 *(d--) -= 'z' - 'a' + 1;
9d116dd7 7470#endif
79072805
LW
7471 }
7472 }
7473 /* oh,oh, the number grew */
7474 SvGROW(sv, SvCUR(sv) + 2);
b162af07 7475 SvCUR_set(sv, SvCUR(sv) + 1);
3f7c398e 7476 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX_const(sv); d--)
79072805
LW
7477 *d = d[-1];
7478 if (isDIGIT(d[1]))
7479 *d = '1';
7480 else
7481 *d = d[1];
7482}
7483
954c1994
GS
7484/*
7485=for apidoc sv_dec
7486
645c22ef
DM
7487Auto-decrement of the value in the SV, doing string to numeric conversion
7488if necessary. Handles 'get' magic.
954c1994
GS
7489
7490=cut
7491*/
7492
79072805 7493void
864dbfa3 7494Perl_sv_dec(pTHX_ register SV *sv)
79072805 7495{
463ee0b2
LW
7496 int flags;
7497
79072805
LW
7498 if (!sv)
7499 return;
b23a5f78
GB
7500 if (SvGMAGICAL(sv))
7501 mg_get(sv);
ed6116ce 7502 if (SvTHINKFIRST(sv)) {
765f542d
NC
7503 if (SvIsCOW(sv))
7504 sv_force_normal_flags(sv, 0);
0f15f207 7505 if (SvREADONLY(sv)) {
923e4eb5 7506 if (IN_PERL_RUNTIME)
cea2e8a9 7507 Perl_croak(aTHX_ PL_no_modify);
0f15f207 7508 }
a0d0e21e 7509 if (SvROK(sv)) {
b5be31e9 7510 IV i;
9e7bc3e8
JD
7511 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
7512 return;
56431972 7513 i = PTR2IV(SvRV(sv));
b5be31e9
SM
7514 sv_unref(sv);
7515 sv_setiv(sv, i);
a0d0e21e 7516 }
ed6116ce 7517 }
28e5dec8
JH
7518 /* Unlike sv_inc we don't have to worry about string-never-numbers
7519 and keeping them magic. But we mustn't warn on punting */
8990e307 7520 flags = SvFLAGS(sv);
28e5dec8
JH
7521 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
7522 /* It's publicly an integer, or privately an integer-not-float */
59d8ce62 7523#ifdef PERL_PRESERVE_IVUV
28e5dec8 7524 oops_its_int:
59d8ce62 7525#endif
25da4f38
IZ
7526 if (SvIsUV(sv)) {
7527 if (SvUVX(sv) == 0) {
7528 (void)SvIOK_only(sv);
45977657 7529 SvIV_set(sv, -1);
25da4f38
IZ
7530 }
7531 else {
7532 (void)SvIOK_only_UV(sv);
607fa7f2 7533 SvUV_set(sv, SvUVX(sv) + 1);
1c846c1f 7534 }
25da4f38
IZ
7535 } else {
7536 if (SvIVX(sv) == IV_MIN)
65202027 7537 sv_setnv(sv, (NV)IV_MIN - 1.0);
25da4f38
IZ
7538 else {
7539 (void)SvIOK_only(sv);
45977657 7540 SvIV_set(sv, SvIVX(sv) - 1);
1c846c1f 7541 }
55497cff 7542 }
7543 return;
7544 }
28e5dec8 7545 if (flags & SVp_NOK) {
9d6ce603 7546 SvNV_set(sv, SvNVX(sv) - 1.0);
28e5dec8
JH
7547 (void)SvNOK_only(sv);
7548 return;
7549 }
8990e307 7550 if (!(flags & SVp_POK)) {
4633a7c4
LW
7551 if ((flags & SVTYPEMASK) < SVt_PVNV)
7552 sv_upgrade(sv, SVt_NV);
f599b64b 7553 SvNV_set(sv, 1.0);
a0d0e21e 7554 (void)SvNOK_only(sv);
79072805
LW
7555 return;
7556 }
28e5dec8
JH
7557#ifdef PERL_PRESERVE_IVUV
7558 {
504618e9 7559 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
28e5dec8
JH
7560 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
7561 /* Need to try really hard to see if it's an integer.
7562 9.22337203685478e+18 is an integer.
7563 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
7564 so $a="9.22337203685478e+18"; $a+0; $a--
7565 needs to be the same as $a="9.22337203685478e+18"; $a--
7566 or we go insane. */
d460ef45 7567
28e5dec8
JH
7568 (void) sv_2iv(sv);
7569 if (SvIOK(sv))
7570 goto oops_its_int;
7571
7572 /* sv_2iv *should* have made this an NV */
7573 if (flags & SVp_NOK) {
7574 (void)SvNOK_only(sv);
9d6ce603 7575 SvNV_set(sv, SvNVX(sv) - 1.0);
28e5dec8
JH
7576 return;
7577 }
7578 /* I don't think we can get here. Maybe I should assert this
7579 And if we do get here I suspect that sv_setnv will croak. NWC
7580 Fall through. */
7581#if defined(USE_LONG_DOUBLE)
7582 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
3f7c398e 7583 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8 7584#else
1779d84d 7585 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
3f7c398e 7586 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8
JH
7587#endif
7588 }
7589 }
7590#endif /* PERL_PRESERVE_IVUV */
3f7c398e 7591 sv_setnv(sv,Atof(SvPVX_const(sv)) - 1.0); /* punt */
79072805
LW
7592}
7593
954c1994
GS
7594/*
7595=for apidoc sv_mortalcopy
7596
645c22ef 7597Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
d4236ebc
DM
7598The new SV is marked as mortal. It will be destroyed "soon", either by an
7599explicit call to FREETMPS, or by an implicit call at places such as
7600statement boundaries. See also C<sv_newmortal> and C<sv_2mortal>.
954c1994
GS
7601
7602=cut
7603*/
7604
79072805
LW
7605/* Make a string that will exist for the duration of the expression
7606 * evaluation. Actually, it may have to last longer than that, but
7607 * hopefully we won't free it until it has been assigned to a
7608 * permanent location. */
7609
7610SV *
864dbfa3 7611Perl_sv_mortalcopy(pTHX_ SV *oldstr)
79072805 7612{
463ee0b2 7613 register SV *sv;
b881518d 7614
4561caa4 7615 new_SV(sv);
79072805 7616 sv_setsv(sv,oldstr);
677b06e3
GS
7617 EXTEND_MORTAL(1);
7618 PL_tmps_stack[++PL_tmps_ix] = sv;
8990e307
LW
7619 SvTEMP_on(sv);
7620 return sv;
7621}
7622
954c1994
GS
7623/*
7624=for apidoc sv_newmortal
7625
645c22ef 7626Creates a new null SV which is mortal. The reference count of the SV is
d4236ebc
DM
7627set to 1. It will be destroyed "soon", either by an explicit call to
7628FREETMPS, or by an implicit call at places such as statement boundaries.
7629See also C<sv_mortalcopy> and C<sv_2mortal>.
954c1994
GS
7630
7631=cut
7632*/
7633
8990e307 7634SV *
864dbfa3 7635Perl_sv_newmortal(pTHX)
8990e307
LW
7636{
7637 register SV *sv;
7638
4561caa4 7639 new_SV(sv);
8990e307 7640 SvFLAGS(sv) = SVs_TEMP;
677b06e3
GS
7641 EXTEND_MORTAL(1);
7642 PL_tmps_stack[++PL_tmps_ix] = sv;
79072805
LW
7643 return sv;
7644}
7645
954c1994
GS
7646/*
7647=for apidoc sv_2mortal
7648
d4236ebc
DM
7649Marks an existing SV as mortal. The SV will be destroyed "soon", either
7650by an explicit call to FREETMPS, or by an implicit call at places such as
37d2ac18
NC
7651statement boundaries. SvTEMP() is turned on which means that the SV's
7652string buffer can be "stolen" if this SV is copied. See also C<sv_newmortal>
7653and C<sv_mortalcopy>.
954c1994
GS
7654
7655=cut
7656*/
7657
79072805 7658SV *
864dbfa3 7659Perl_sv_2mortal(pTHX_ register SV *sv)
79072805 7660{
27da23d5 7661 dVAR;
79072805
LW
7662 if (!sv)
7663 return sv;
d689ffdd 7664 if (SvREADONLY(sv) && SvIMMORTAL(sv))
11162842 7665 return sv;
677b06e3
GS
7666 EXTEND_MORTAL(1);
7667 PL_tmps_stack[++PL_tmps_ix] = sv;
8990e307 7668 SvTEMP_on(sv);
79072805
LW
7669 return sv;
7670}
7671
954c1994
GS
7672/*
7673=for apidoc newSVpv
7674
7675Creates a new SV and copies a string into it. The reference count for the
7676SV is set to 1. If C<len> is zero, Perl will compute the length using
7677strlen(). For efficiency, consider using C<newSVpvn> instead.
7678
7679=cut
7680*/
7681
79072805 7682SV *
864dbfa3 7683Perl_newSVpv(pTHX_ const char *s, STRLEN len)
79072805 7684{
463ee0b2 7685 register SV *sv;
79072805 7686
4561caa4 7687 new_SV(sv);
616d8c9c 7688 sv_setpvn(sv,s,len ? len : strlen(s));
79072805
LW
7689 return sv;
7690}
7691
954c1994
GS
7692/*
7693=for apidoc newSVpvn
7694
7695Creates a new SV and copies a string into it. The reference count for the
1c846c1f 7696SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
954c1994 7697string. You are responsible for ensuring that the source string is at least
9e09f5f2 7698C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
954c1994
GS
7699
7700=cut
7701*/
7702
9da1e3b5 7703SV *
864dbfa3 7704Perl_newSVpvn(pTHX_ const char *s, STRLEN len)
9da1e3b5
MUN
7705{
7706 register SV *sv;
7707
7708 new_SV(sv);
9da1e3b5
MUN
7709 sv_setpvn(sv,s,len);
7710 return sv;
7711}
7712
bd08039b
NC
7713
7714/*
926f8064 7715=for apidoc newSVhek
bd08039b
NC
7716
7717Creates a new SV from the hash key structure. It will generate scalars that
5aaec2b4
NC
7718point to the shared string table where possible. Returns a new (undefined)
7719SV if the hek is NULL.
bd08039b
NC
7720
7721=cut
7722*/
7723
7724SV *
c1b02ed8 7725Perl_newSVhek(pTHX_ const HEK *hek)
bd08039b 7726{
5aaec2b4
NC
7727 if (!hek) {
7728 SV *sv;
7729
7730 new_SV(sv);
7731 return sv;
7732 }
7733
bd08039b
NC
7734 if (HEK_LEN(hek) == HEf_SVKEY) {
7735 return newSVsv(*(SV**)HEK_KEY(hek));
7736 } else {
7737 const int flags = HEK_FLAGS(hek);
7738 if (flags & HVhek_WASUTF8) {
7739 /* Trouble :-)
7740 Andreas would like keys he put in as utf8 to come back as utf8
7741 */
7742 STRLEN utf8_len = HEK_LEN(hek);
7743 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
7744 SV *sv = newSVpvn ((char*)as_utf8, utf8_len);
7745
7746 SvUTF8_on (sv);
7747 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
7748 return sv;
7749 } else if (flags & HVhek_REHASH) {
7750 /* We don't have a pointer to the hv, so we have to replicate the
7751 flag into every HEK. This hv is using custom a hasing
7752 algorithm. Hence we can't return a shared string scalar, as
7753 that would contain the (wrong) hash value, and might get passed
7754 into an hv routine with a regular hash */
7755
7756 SV *sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
7757 if (HEK_UTF8(hek))
7758 SvUTF8_on (sv);
7759 return sv;
7760 }
7761 /* This will be overwhelminly the most common case. */
7762 return newSVpvn_share(HEK_KEY(hek),
7763 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
7764 HEK_HASH(hek));
7765 }
7766}
7767
1c846c1f
NIS
7768/*
7769=for apidoc newSVpvn_share
7770
3f7c398e 7771Creates a new SV with its SvPVX_const pointing to a shared string in the string
645c22ef
DM
7772table. If the string does not already exist in the table, it is created
7773first. Turns on READONLY and FAKE. The string's hash is stored in the UV
7774slot of the SV; if the C<hash> parameter is non-zero, that value is used;
7775otherwise the hash is computed. The idea here is that as the string table
3f7c398e 7776is used for shared hash keys these strings will have SvPVX_const == HeKEY and
645c22ef 7777hash lookup will avoid string compare.
1c846c1f
NIS
7778
7779=cut
7780*/
7781
7782SV *
c3654f1a 7783Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
1c846c1f
NIS
7784{
7785 register SV *sv;
c3654f1a
IH
7786 bool is_utf8 = FALSE;
7787 if (len < 0) {
77caf834 7788 STRLEN tmplen = -len;
c3654f1a 7789 is_utf8 = TRUE;
75a54232 7790 /* See the note in hv.c:hv_fetch() --jhi */
e1ec3a88 7791 src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
75a54232
JH
7792 len = tmplen;
7793 }
1c846c1f 7794 if (!hash)
5afd6d42 7795 PERL_HASH(hash, src, len);
1c846c1f 7796 new_SV(sv);
bdd68bc3 7797 sv_upgrade(sv, SVt_PV);
f880fe2f 7798 SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
b162af07 7799 SvCUR_set(sv, len);
b162af07 7800 SvLEN_set(sv, 0);
1c846c1f
NIS
7801 SvREADONLY_on(sv);
7802 SvFAKE_on(sv);
7803 SvPOK_on(sv);
c3654f1a
IH
7804 if (is_utf8)
7805 SvUTF8_on(sv);
1c846c1f
NIS
7806 return sv;
7807}
7808
645c22ef 7809
cea2e8a9 7810#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
7811
7812/* pTHX_ magic can't cope with varargs, so this is a no-context
7813 * version of the main function, (which may itself be aliased to us).
7814 * Don't access this version directly.
7815 */
7816
46fc3d4c 7817SV *
cea2e8a9 7818Perl_newSVpvf_nocontext(const char* pat, ...)
46fc3d4c 7819{
cea2e8a9 7820 dTHX;
46fc3d4c 7821 register SV *sv;
7822 va_list args;
46fc3d4c 7823 va_start(args, pat);
c5be433b 7824 sv = vnewSVpvf(pat, &args);
46fc3d4c 7825 va_end(args);
7826 return sv;
7827}
cea2e8a9 7828#endif
46fc3d4c 7829
954c1994
GS
7830/*
7831=for apidoc newSVpvf
7832
645c22ef 7833Creates a new SV and initializes it with the string formatted like
954c1994
GS
7834C<sprintf>.
7835
7836=cut
7837*/
7838
cea2e8a9
GS
7839SV *
7840Perl_newSVpvf(pTHX_ const char* pat, ...)
7841{
7842 register SV *sv;
7843 va_list args;
cea2e8a9 7844 va_start(args, pat);
c5be433b 7845 sv = vnewSVpvf(pat, &args);
cea2e8a9
GS
7846 va_end(args);
7847 return sv;
7848}
46fc3d4c 7849
645c22ef
DM
7850/* backend for newSVpvf() and newSVpvf_nocontext() */
7851
79072805 7852SV *
c5be433b
GS
7853Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args)
7854{
7855 register SV *sv;
7856 new_SV(sv);
7857 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
7858 return sv;
7859}
7860
954c1994
GS
7861/*
7862=for apidoc newSVnv
7863
7864Creates a new SV and copies a floating point value into it.
7865The reference count for the SV is set to 1.
7866
7867=cut
7868*/
7869
c5be433b 7870SV *
65202027 7871Perl_newSVnv(pTHX_ NV n)
79072805 7872{
463ee0b2 7873 register SV *sv;
79072805 7874
4561caa4 7875 new_SV(sv);
79072805
LW
7876 sv_setnv(sv,n);
7877 return sv;
7878}
7879
954c1994
GS
7880/*
7881=for apidoc newSViv
7882
7883Creates a new SV and copies an integer into it. The reference count for the
7884SV is set to 1.
7885
7886=cut
7887*/
7888
79072805 7889SV *
864dbfa3 7890Perl_newSViv(pTHX_ IV i)
79072805 7891{
463ee0b2 7892 register SV *sv;
79072805 7893
4561caa4 7894 new_SV(sv);
79072805
LW
7895 sv_setiv(sv,i);
7896 return sv;
7897}
7898
954c1994 7899/*
1a3327fb
JH
7900=for apidoc newSVuv
7901
7902Creates a new SV and copies an unsigned integer into it.
7903The reference count for the SV is set to 1.
7904
7905=cut
7906*/
7907
7908SV *
7909Perl_newSVuv(pTHX_ UV u)
7910{
7911 register SV *sv;
7912
7913 new_SV(sv);
7914 sv_setuv(sv,u);
7915 return sv;
7916}
7917
7918/*
954c1994
GS
7919=for apidoc newRV_noinc
7920
7921Creates an RV wrapper for an SV. The reference count for the original
7922SV is B<not> incremented.
7923
7924=cut
7925*/
7926
2304df62 7927SV *
864dbfa3 7928Perl_newRV_noinc(pTHX_ SV *tmpRef)
2304df62
AD
7929{
7930 register SV *sv;
7931
4561caa4 7932 new_SV(sv);
2304df62 7933 sv_upgrade(sv, SVt_RV);
76e3520e 7934 SvTEMP_off(tmpRef);
b162af07 7935 SvRV_set(sv, tmpRef);
2304df62 7936 SvROK_on(sv);
2304df62
AD
7937 return sv;
7938}
7939
ff276b08 7940/* newRV_inc is the official function name to use now.
645c22ef
DM
7941 * newRV_inc is in fact #defined to newRV in sv.h
7942 */
7943
5f05dabc 7944SV *
864dbfa3 7945Perl_newRV(pTHX_ SV *tmpRef)
5f05dabc 7946{
5f6447b6 7947 return newRV_noinc(SvREFCNT_inc(tmpRef));
5f05dabc 7948}
5f05dabc 7949
954c1994
GS
7950/*
7951=for apidoc newSVsv
7952
7953Creates a new SV which is an exact duplicate of the original SV.
645c22ef 7954(Uses C<sv_setsv>).
954c1994
GS
7955
7956=cut
7957*/
7958
79072805 7959SV *
864dbfa3 7960Perl_newSVsv(pTHX_ register SV *old)
79072805 7961{
463ee0b2 7962 register SV *sv;
79072805
LW
7963
7964 if (!old)
7965 return Nullsv;
8990e307 7966 if (SvTYPE(old) == SVTYPEMASK) {
0453d815 7967 if (ckWARN_d(WARN_INTERNAL))
9014280d 7968 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
79072805
LW
7969 return Nullsv;
7970 }
4561caa4 7971 new_SV(sv);
e90aabeb
NC
7972 /* SV_GMAGIC is the default for sv_setv()
7973 SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games
7974 with SvTEMP_off and SvTEMP_on round a call to sv_setsv. */
7975 sv_setsv_flags(sv, old, SV_GMAGIC | SV_NOSTEAL);
463ee0b2 7976 return sv;
79072805
LW
7977}
7978
645c22ef
DM
7979/*
7980=for apidoc sv_reset
7981
7982Underlying implementation for the C<reset> Perl function.
7983Note that the perl-level function is vaguely deprecated.
7984
7985=cut
7986*/
7987
79072805 7988void
e1ec3a88 7989Perl_sv_reset(pTHX_ register const char *s, HV *stash)
79072805 7990{
27da23d5 7991 dVAR;
4802d5d7 7992 char todo[PERL_UCHAR_MAX+1];
79072805 7993
49d8d3a1
MB
7994 if (!stash)
7995 return;
7996
79072805 7997 if (!*s) { /* reset ?? searches */
8d2f4536
NC
7998 MAGIC *mg = mg_find((SV *)stash, PERL_MAGIC_symtab);
7999 if (mg) {
8000 PMOP *pm = (PMOP *) mg->mg_obj;
8001 while (pm) {
8002 pm->op_pmdynflags &= ~PMdf_USED;
8003 pm = pm->op_pmnext;
8004 }
79072805
LW
8005 }
8006 return;
8007 }
8008
8009 /* reset variables */
8010
8011 if (!HvARRAY(stash))
8012 return;
463ee0b2
LW
8013
8014 Zero(todo, 256, char);
79072805 8015 while (*s) {
b464bac0
AL
8016 I32 max;
8017 I32 i = (unsigned char)*s;
79072805
LW
8018 if (s[1] == '-') {
8019 s += 2;
8020 }
4802d5d7 8021 max = (unsigned char)*s++;
79072805 8022 for ( ; i <= max; i++) {
463ee0b2
LW
8023 todo[i] = 1;
8024 }
a0d0e21e 8025 for (i = 0; i <= (I32) HvMAX(stash); i++) {
b464bac0 8026 HE *entry;
79072805 8027 for (entry = HvARRAY(stash)[i];
9e35f4b3
GS
8028 entry;
8029 entry = HeNEXT(entry))
8030 {
b464bac0
AL
8031 register GV *gv;
8032 register SV *sv;
8033
1edc1566 8034 if (!todo[(U8)*HeKEY(entry)])
463ee0b2 8035 continue;
1edc1566 8036 gv = (GV*)HeVAL(entry);
79072805 8037 sv = GvSV(gv);
9e35f4b3
GS
8038 if (SvTHINKFIRST(sv)) {
8039 if (!SvREADONLY(sv) && SvROK(sv))
8040 sv_unref(sv);
8041 continue;
8042 }
0c34ef67 8043 SvOK_off(sv);
79072805
LW
8044 if (SvTYPE(sv) >= SVt_PV) {
8045 SvCUR_set(sv, 0);
3f7c398e 8046 if (SvPVX_const(sv) != Nullch)
463ee0b2 8047 *SvPVX(sv) = '\0';
44a8e56a 8048 SvTAINT(sv);
79072805
LW
8049 }
8050 if (GvAV(gv)) {
8051 av_clear(GvAV(gv));
8052 }
bfcb3514 8053 if (GvHV(gv) && !HvNAME_get(GvHV(gv))) {
463ee0b2 8054 hv_clear(GvHV(gv));
2f42fcb0 8055#ifndef PERL_MICRO
fa6a1c44 8056#ifdef USE_ENVIRON_ARRAY
4efc5df6
GS
8057 if (gv == PL_envgv
8058# ifdef USE_ITHREADS
8059 && PL_curinterp == aTHX
8060# endif
8061 )
8062 {
79072805 8063 environ[0] = Nullch;
4efc5df6 8064 }
a0d0e21e 8065#endif
2f42fcb0 8066#endif /* !PERL_MICRO */
79072805
LW
8067 }
8068 }
8069 }
8070 }
8071}
8072
645c22ef
DM
8073/*
8074=for apidoc sv_2io
8075
8076Using various gambits, try to get an IO from an SV: the IO slot if its a
8077GV; or the recursive result if we're an RV; or the IO slot of the symbol
8078named after the PV if we're a string.
8079
8080=cut
8081*/
8082
46fc3d4c 8083IO*
864dbfa3 8084Perl_sv_2io(pTHX_ SV *sv)
46fc3d4c 8085{
8086 IO* io;
8087 GV* gv;
8088
8089 switch (SvTYPE(sv)) {
8090 case SVt_PVIO:
8091 io = (IO*)sv;
8092 break;
8093 case SVt_PVGV:
8094 gv = (GV*)sv;
8095 io = GvIO(gv);
8096 if (!io)
cea2e8a9 8097 Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
46fc3d4c 8098 break;
8099 default:
8100 if (!SvOK(sv))
cea2e8a9 8101 Perl_croak(aTHX_ PL_no_usym, "filehandle");
46fc3d4c 8102 if (SvROK(sv))
8103 return sv_2io(SvRV(sv));
7a5fd60d 8104 gv = gv_fetchsv(sv, FALSE, SVt_PVIO);
46fc3d4c 8105 if (gv)
8106 io = GvIO(gv);
8107 else
8108 io = 0;
8109 if (!io)
35c1215d 8110 Perl_croak(aTHX_ "Bad filehandle: %"SVf, sv);
46fc3d4c 8111 break;
8112 }
8113 return io;
8114}
8115
645c22ef
DM
8116/*
8117=for apidoc sv_2cv
8118
8119Using various gambits, try to get a CV from an SV; in addition, try if
8120possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
8121
8122=cut
8123*/
8124
79072805 8125CV *
864dbfa3 8126Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref)
79072805 8127{
27da23d5 8128 dVAR;
c04a4dfe
JH
8129 GV *gv = Nullgv;
8130 CV *cv = Nullcv;
79072805
LW
8131
8132 if (!sv)
93a17b20 8133 return *gvp = Nullgv, Nullcv;
79072805 8134 switch (SvTYPE(sv)) {
79072805
LW
8135 case SVt_PVCV:
8136 *st = CvSTASH(sv);
8137 *gvp = Nullgv;
8138 return (CV*)sv;
8139 case SVt_PVHV:
8140 case SVt_PVAV:
8141 *gvp = Nullgv;
8142 return Nullcv;
8990e307
LW
8143 case SVt_PVGV:
8144 gv = (GV*)sv;
a0d0e21e 8145 *gvp = gv;
8990e307
LW
8146 *st = GvESTASH(gv);
8147 goto fix_gv;
8148
79072805 8149 default:
a0d0e21e
LW
8150 if (SvGMAGICAL(sv))
8151 mg_get(sv);
8152 if (SvROK(sv)) {
f5284f61
IZ
8153 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
8154 tryAMAGICunDEREF(to_cv);
8155
62f274bf
GS
8156 sv = SvRV(sv);
8157 if (SvTYPE(sv) == SVt_PVCV) {
8158 cv = (CV*)sv;
8159 *gvp = Nullgv;
8160 *st = CvSTASH(cv);
8161 return cv;
8162 }
8163 else if(isGV(sv))
8164 gv = (GV*)sv;
8165 else
cea2e8a9 8166 Perl_croak(aTHX_ "Not a subroutine reference");
a0d0e21e 8167 }
62f274bf 8168 else if (isGV(sv))
79072805
LW
8169 gv = (GV*)sv;
8170 else
7a5fd60d 8171 gv = gv_fetchsv(sv, lref, SVt_PVCV);
79072805
LW
8172 *gvp = gv;
8173 if (!gv)
8174 return Nullcv;
8175 *st = GvESTASH(gv);
8990e307 8176 fix_gv:
8ebc5c01 8177 if (lref && !GvCVu(gv)) {
4633a7c4 8178 SV *tmpsv;
748a9306 8179 ENTER;
4633a7c4 8180 tmpsv = NEWSV(704,0);
16660edb 8181 gv_efullname3(tmpsv, gv, Nullch);
f6ec51f7
GS
8182 /* XXX this is probably not what they think they're getting.
8183 * It has the same effect as "sub name;", i.e. just a forward
8184 * declaration! */
774d564b 8185 newSUB(start_subparse(FALSE, 0),
4633a7c4
LW
8186 newSVOP(OP_CONST, 0, tmpsv),
8187 Nullop,
8990e307 8188 Nullop);
748a9306 8189 LEAVE;
8ebc5c01 8190 if (!GvCVu(gv))
35c1215d
NC
8191 Perl_croak(aTHX_ "Unable to create sub named \"%"SVf"\"",
8192 sv);
8990e307 8193 }
8ebc5c01 8194 return GvCVu(gv);
79072805
LW
8195 }
8196}
8197
c461cf8f
JH
8198/*
8199=for apidoc sv_true
8200
8201Returns true if the SV has a true value by Perl's rules.
645c22ef
DM
8202Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
8203instead use an in-line version.
c461cf8f
JH
8204
8205=cut
8206*/
8207
79072805 8208I32
864dbfa3 8209Perl_sv_true(pTHX_ register SV *sv)
79072805 8210{
8990e307
LW
8211 if (!sv)
8212 return 0;
79072805 8213 if (SvPOK(sv)) {
e1ec3a88 8214 const register XPV* tXpv;
4e35701f 8215 if ((tXpv = (XPV*)SvANY(sv)) &&
c2f1de04 8216 (tXpv->xpv_cur > 1 ||
339049b0 8217 (tXpv->xpv_cur && *sv->sv_u.svu_pv != '0')))
79072805
LW
8218 return 1;
8219 else
8220 return 0;
8221 }
8222 else {
8223 if (SvIOK(sv))
463ee0b2 8224 return SvIVX(sv) != 0;
79072805
LW
8225 else {
8226 if (SvNOK(sv))
463ee0b2 8227 return SvNVX(sv) != 0.0;
79072805 8228 else
463ee0b2 8229 return sv_2bool(sv);
79072805
LW
8230 }
8231 }
8232}
79072805 8233
645c22ef
DM
8234/*
8235=for apidoc sv_iv
8236
8237A private implementation of the C<SvIVx> macro for compilers which can't
8238cope with complex macro expressions. Always use the macro instead.
8239
8240=cut
8241*/
8242
ff68c719 8243IV
864dbfa3 8244Perl_sv_iv(pTHX_ register SV *sv)
85e6fe83 8245{
25da4f38
IZ
8246 if (SvIOK(sv)) {
8247 if (SvIsUV(sv))
8248 return (IV)SvUVX(sv);
ff68c719 8249 return SvIVX(sv);
25da4f38 8250 }
ff68c719 8251 return sv_2iv(sv);
85e6fe83 8252}
85e6fe83 8253
645c22ef
DM
8254/*
8255=for apidoc sv_uv
8256
8257A private implementation of the C<SvUVx> macro for compilers which can't
8258cope with complex macro expressions. Always use the macro instead.
8259
8260=cut
8261*/
8262
ff68c719 8263UV
864dbfa3 8264Perl_sv_uv(pTHX_ register SV *sv)
ff68c719 8265{
25da4f38
IZ
8266 if (SvIOK(sv)) {
8267 if (SvIsUV(sv))
8268 return SvUVX(sv);
8269 return (UV)SvIVX(sv);
8270 }
ff68c719 8271 return sv_2uv(sv);
8272}
85e6fe83 8273
645c22ef
DM
8274/*
8275=for apidoc sv_nv
8276
8277A private implementation of the C<SvNVx> macro for compilers which can't
8278cope with complex macro expressions. Always use the macro instead.
8279
8280=cut
8281*/
8282
65202027 8283NV
864dbfa3 8284Perl_sv_nv(pTHX_ register SV *sv)
79072805 8285{
ff68c719 8286 if (SvNOK(sv))
8287 return SvNVX(sv);
8288 return sv_2nv(sv);
79072805 8289}
79072805 8290
09540bc3
JH
8291/* sv_pv() is now a macro using SvPV_nolen();
8292 * this function provided for binary compatibility only
8293 */
8294
8295char *
8296Perl_sv_pv(pTHX_ SV *sv)
8297{
09540bc3
JH
8298 if (SvPOK(sv))
8299 return SvPVX(sv);
8300
93524f2b 8301 return sv_2pv(sv, 0);
09540bc3
JH
8302}
8303
645c22ef
DM
8304/*
8305=for apidoc sv_pv
8306
baca2b92 8307Use the C<SvPV_nolen> macro instead
645c22ef 8308
645c22ef
DM
8309=for apidoc sv_pvn
8310
8311A private implementation of the C<SvPV> macro for compilers which can't
8312cope with complex macro expressions. Always use the macro instead.
8313
8314=cut
8315*/
8316
1fa8b10d 8317char *
864dbfa3 8318Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp)
79072805 8319{
85e6fe83
LW
8320 if (SvPOK(sv)) {
8321 *lp = SvCUR(sv);
a0d0e21e 8322 return SvPVX(sv);
85e6fe83 8323 }
463ee0b2 8324 return sv_2pv(sv, lp);
79072805 8325}
79072805 8326
6e9d1081
NC
8327
8328char *
8329Perl_sv_pvn_nomg(pTHX_ register SV *sv, STRLEN *lp)
8330{
8331 if (SvPOK(sv)) {
8332 *lp = SvCUR(sv);
8333 return SvPVX(sv);
8334 }
8335 return sv_2pv_flags(sv, lp, 0);
8336}
8337
09540bc3
JH
8338/* sv_pvn_force() is now a macro using Perl_sv_pvn_force_flags();
8339 * this function provided for binary compatibility only
8340 */
8341
8342char *
8343Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp)
8344{
8345 return sv_pvn_force_flags(sv, lp, SV_GMAGIC);
8346}
8347
c461cf8f
JH
8348/*
8349=for apidoc sv_pvn_force
8350
8351Get a sensible string out of the SV somehow.
645c22ef
DM
8352A private implementation of the C<SvPV_force> macro for compilers which
8353can't cope with complex macro expressions. Always use the macro instead.
c461cf8f 8354
8d6d96c1
HS
8355=for apidoc sv_pvn_force_flags
8356
8357Get a sensible string out of the SV somehow.
8358If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if
8359appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
8360implemented in terms of this function.
645c22ef
DM
8361You normally want to use the various wrapper macros instead: see
8362C<SvPV_force> and C<SvPV_force_nomg>
8d6d96c1
HS
8363
8364=cut
8365*/
8366
8367char *
8368Perl_sv_pvn_force_flags(pTHX_ SV *sv, STRLEN *lp, I32 flags)
8369{
a0d0e21e 8370
6fc92669 8371 if (SvTHINKFIRST(sv) && !SvROK(sv))
765f542d 8372 sv_force_normal_flags(sv, 0);
1c846c1f 8373
a0d0e21e 8374 if (SvPOK(sv)) {
13c5b33c
NC
8375 if (lp)
8376 *lp = SvCUR(sv);
a0d0e21e
LW
8377 }
8378 else {
a3b680e6 8379 char *s;
13c5b33c
NC
8380 STRLEN len;
8381
4d84ee25
NC
8382 if (SvREADONLY(sv) && !(flags & SV_MUTABLE_RETURN)) {
8383 if (PL_op)
8384 Perl_croak(aTHX_ "Can't coerce readonly %s to string in %s",
8385 sv_reftype(sv,0), OP_NAME(PL_op));
8386 else
8387 Perl_croak(aTHX_ "Can't coerce readonly %s to string",
8388 sv_reftype(sv,0));
8389 }
748a9306 8390 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
cea2e8a9 8391 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
53e06cf0 8392 OP_NAME(PL_op));
a0d0e21e 8393 }
4633a7c4 8394 else
13c5b33c
NC
8395 s = sv_2pv_flags(sv, &len, flags);
8396 if (lp)
8397 *lp = len;
8398
3f7c398e 8399 if (s != SvPVX_const(sv)) { /* Almost, but not quite, sv_setpvn() */
a0d0e21e
LW
8400 if (SvROK(sv))
8401 sv_unref(sv);
862a34c6 8402 SvUPGRADE(sv, SVt_PV); /* Never FALSE */
a0d0e21e 8403 SvGROW(sv, len + 1);
3f7c398e 8404 Move(s,SvPVX_const(sv),len,char);
a0d0e21e
LW
8405 SvCUR_set(sv, len);
8406 *SvEND(sv) = '\0';
8407 }
8408 if (!SvPOK(sv)) {
8409 SvPOK_on(sv); /* validate pointer */
8410 SvTAINT(sv);
1d7c1841 8411 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3f7c398e 8412 PTR2UV(sv),SvPVX_const(sv)));
a0d0e21e
LW
8413 }
8414 }
4d84ee25 8415 return SvPVX_mutable(sv);
a0d0e21e
LW
8416}
8417
09540bc3
JH
8418/* sv_pvbyte () is now a macro using Perl_sv_2pv_flags();
8419 * this function provided for binary compatibility only
8420 */
8421
8422char *
8423Perl_sv_pvbyte(pTHX_ SV *sv)
8424{
8425 sv_utf8_downgrade(sv,0);
8426 return sv_pv(sv);
8427}
8428
645c22ef
DM
8429/*
8430=for apidoc sv_pvbyte
8431
baca2b92 8432Use C<SvPVbyte_nolen> instead.
645c22ef 8433
645c22ef
DM
8434=for apidoc sv_pvbyten
8435
8436A private implementation of the C<SvPVbyte> macro for compilers
8437which can't cope with complex macro expressions. Always use the macro
8438instead.
8439
8440=cut
8441*/
8442
7340a771
GS
8443char *
8444Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp)
8445{
ffebcc3e 8446 sv_utf8_downgrade(sv,0);
7340a771
GS
8447 return sv_pvn(sv,lp);
8448}
8449
645c22ef
DM
8450/*
8451=for apidoc sv_pvbyten_force
8452
8453A private implementation of the C<SvPVbytex_force> macro for compilers
8454which can't cope with complex macro expressions. Always use the macro
8455instead.
8456
8457=cut
8458*/
8459
7340a771
GS
8460char *
8461Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp)
8462{
46ec2f14 8463 sv_pvn_force(sv,lp);
ffebcc3e 8464 sv_utf8_downgrade(sv,0);
46ec2f14
TS
8465 *lp = SvCUR(sv);
8466 return SvPVX(sv);
7340a771
GS
8467}
8468
09540bc3
JH
8469/* sv_pvutf8 () is now a macro using Perl_sv_2pv_flags();
8470 * this function provided for binary compatibility only
8471 */
8472
8473char *
8474Perl_sv_pvutf8(pTHX_ SV *sv)
8475{
8476 sv_utf8_upgrade(sv);
8477 return sv_pv(sv);
8478}
8479
645c22ef
DM
8480/*
8481=for apidoc sv_pvutf8
8482
baca2b92 8483Use the C<SvPVutf8_nolen> macro instead
645c22ef 8484
645c22ef
DM
8485=for apidoc sv_pvutf8n
8486
8487A private implementation of the C<SvPVutf8> macro for compilers
8488which can't cope with complex macro expressions. Always use the macro
8489instead.
8490
8491=cut
8492*/
8493
7340a771
GS
8494char *
8495Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp)
8496{
560a288e 8497 sv_utf8_upgrade(sv);
7340a771
GS
8498 return sv_pvn(sv,lp);
8499}
8500
c461cf8f
JH
8501/*
8502=for apidoc sv_pvutf8n_force
8503
645c22ef
DM
8504A private implementation of the C<SvPVutf8_force> macro for compilers
8505which can't cope with complex macro expressions. Always use the macro
8506instead.
c461cf8f
JH
8507
8508=cut
8509*/
8510
7340a771
GS
8511char *
8512Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp)
8513{
46ec2f14 8514 sv_pvn_force(sv,lp);
560a288e 8515 sv_utf8_upgrade(sv);
46ec2f14
TS
8516 *lp = SvCUR(sv);
8517 return SvPVX(sv);
7340a771
GS
8518}
8519
c461cf8f
JH
8520/*
8521=for apidoc sv_reftype
8522
8523Returns a string describing what the SV is a reference to.
8524
8525=cut
8526*/
8527
1cb0ed9b 8528char *
bfed75c6 8529Perl_sv_reftype(pTHX_ const SV *sv, int ob)
a0d0e21e 8530{
07409e01
NC
8531 /* The fact that I don't need to downcast to char * everywhere, only in ?:
8532 inside return suggests a const propagation bug in g++. */
c86bf373 8533 if (ob && SvOBJECT(sv)) {
bfcb3514 8534 char *name = HvNAME_get(SvSTASH(sv));
07409e01 8535 return name ? name : (char *) "__ANON__";
c86bf373 8536 }
a0d0e21e
LW
8537 else {
8538 switch (SvTYPE(sv)) {
8539 case SVt_NULL:
8540 case SVt_IV:
8541 case SVt_NV:
8542 case SVt_RV:
8543 case SVt_PV:
8544 case SVt_PVIV:
8545 case SVt_PVNV:
8546 case SVt_PVMG:
8547 case SVt_PVBM:
1cb0ed9b 8548 if (SvVOK(sv))
439cb1c4 8549 return "VSTRING";
a0d0e21e
LW
8550 if (SvROK(sv))
8551 return "REF";
8552 else
8553 return "SCALAR";
1cb0ed9b 8554
07409e01 8555 case SVt_PVLV: return (char *) (SvROK(sv) ? "REF"
be65207d
DM
8556 /* tied lvalues should appear to be
8557 * scalars for backwards compatitbility */
8558 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
07409e01 8559 ? "SCALAR" : "LVALUE");
a0d0e21e
LW
8560 case SVt_PVAV: return "ARRAY";
8561 case SVt_PVHV: return "HASH";
8562 case SVt_PVCV: return "CODE";
8563 case SVt_PVGV: return "GLOB";
1d2dff63 8564 case SVt_PVFM: return "FORMAT";
27f9d8f3 8565 case SVt_PVIO: return "IO";
a0d0e21e
LW
8566 default: return "UNKNOWN";
8567 }
8568 }
8569}
8570
954c1994
GS
8571/*
8572=for apidoc sv_isobject
8573
8574Returns a boolean indicating whether the SV is an RV pointing to a blessed
8575object. If the SV is not an RV, or if the object is not blessed, then this
8576will return false.
8577
8578=cut
8579*/
8580
463ee0b2 8581int
864dbfa3 8582Perl_sv_isobject(pTHX_ SV *sv)
85e6fe83 8583{
68dc0745 8584 if (!sv)
8585 return 0;
8586 if (SvGMAGICAL(sv))
8587 mg_get(sv);
85e6fe83
LW
8588 if (!SvROK(sv))
8589 return 0;
8590 sv = (SV*)SvRV(sv);
8591 if (!SvOBJECT(sv))
8592 return 0;
8593 return 1;
8594}
8595
954c1994
GS
8596/*
8597=for apidoc sv_isa
8598
8599Returns a boolean indicating whether the SV is blessed into the specified
8600class. This does not check for subtypes; use C<sv_derived_from> to verify
8601an inheritance relationship.
8602
8603=cut
8604*/
8605
85e6fe83 8606int
864dbfa3 8607Perl_sv_isa(pTHX_ SV *sv, const char *name)
463ee0b2 8608{
bfcb3514 8609 const char *hvname;
68dc0745 8610 if (!sv)
8611 return 0;
8612 if (SvGMAGICAL(sv))
8613 mg_get(sv);
ed6116ce 8614 if (!SvROK(sv))
463ee0b2 8615 return 0;
ed6116ce
LW
8616 sv = (SV*)SvRV(sv);
8617 if (!SvOBJECT(sv))
463ee0b2 8618 return 0;
bfcb3514
NC
8619 hvname = HvNAME_get(SvSTASH(sv));
8620 if (!hvname)
e27ad1f2 8621 return 0;
463ee0b2 8622
bfcb3514 8623 return strEQ(hvname, name);
463ee0b2
LW
8624}
8625
954c1994
GS
8626/*
8627=for apidoc newSVrv
8628
8629Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then
8630it will be upgraded to one. If C<classname> is non-null then the new SV will
8631be blessed in the specified package. The new SV is returned and its
8632reference count is 1.
8633
8634=cut
8635*/
8636
463ee0b2 8637SV*
864dbfa3 8638Perl_newSVrv(pTHX_ SV *rv, const char *classname)
463ee0b2 8639{
463ee0b2
LW
8640 SV *sv;
8641
4561caa4 8642 new_SV(sv);
51cf62d8 8643
765f542d 8644 SV_CHECK_THINKFIRST_COW_DROP(rv);
51cf62d8 8645 SvAMAGIC_off(rv);
51cf62d8 8646
0199fce9 8647 if (SvTYPE(rv) >= SVt_PVMG) {
a3b680e6 8648 const U32 refcnt = SvREFCNT(rv);
0199fce9
JD
8649 SvREFCNT(rv) = 0;
8650 sv_clear(rv);
8651 SvFLAGS(rv) = 0;
8652 SvREFCNT(rv) = refcnt;
8653 }
8654
51cf62d8 8655 if (SvTYPE(rv) < SVt_RV)
0199fce9
JD
8656 sv_upgrade(rv, SVt_RV);
8657 else if (SvTYPE(rv) > SVt_RV) {
8bd4d4c5 8658 SvPV_free(rv);
0199fce9
JD
8659 SvCUR_set(rv, 0);
8660 SvLEN_set(rv, 0);
8661 }
51cf62d8 8662
0c34ef67 8663 SvOK_off(rv);
b162af07 8664 SvRV_set(rv, sv);
ed6116ce 8665 SvROK_on(rv);
463ee0b2 8666
a0d0e21e
LW
8667 if (classname) {
8668 HV* stash = gv_stashpv(classname, TRUE);
8669 (void)sv_bless(rv, stash);
8670 }
8671 return sv;
8672}
8673
954c1994
GS
8674/*
8675=for apidoc sv_setref_pv
8676
8677Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
8678argument will be upgraded to an RV. That RV will be modified to point to
8679the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed
8680into the SV. The C<classname> argument indicates the package for the
8681blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
d34c2299 8682will have a reference count of 1, and the RV will be returned.
954c1994
GS
8683
8684Do not use with other Perl types such as HV, AV, SV, CV, because those
8685objects will become corrupted by the pointer copy process.
8686
8687Note that C<sv_setref_pvn> copies the string while this copies the pointer.
8688
8689=cut
8690*/
8691
a0d0e21e 8692SV*
864dbfa3 8693Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv)
a0d0e21e 8694{
189b2af5 8695 if (!pv) {
3280af22 8696 sv_setsv(rv, &PL_sv_undef);
189b2af5
GS
8697 SvSETMAGIC(rv);
8698 }
a0d0e21e 8699 else
56431972 8700 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
a0d0e21e
LW
8701 return rv;
8702}
8703
954c1994
GS
8704/*
8705=for apidoc sv_setref_iv
8706
8707Copies an integer into a new SV, optionally blessing the SV. The C<rv>
8708argument will be upgraded to an RV. That RV will be modified to point to
8709the new SV. The C<classname> argument indicates the package for the
8710blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
d34c2299 8711will have a reference count of 1, and the RV will be returned.
954c1994
GS
8712
8713=cut
8714*/
8715
a0d0e21e 8716SV*
864dbfa3 8717Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv)
a0d0e21e
LW
8718{
8719 sv_setiv(newSVrv(rv,classname), iv);
8720 return rv;
8721}
8722
954c1994 8723/*
e1c57cef
JH
8724=for apidoc sv_setref_uv
8725
8726Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
8727argument will be upgraded to an RV. That RV will be modified to point to
8728the new SV. The C<classname> argument indicates the package for the
8729blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
d34c2299 8730will have a reference count of 1, and the RV will be returned.
e1c57cef
JH
8731
8732=cut
8733*/
8734
8735SV*
8736Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv)
8737{
8738 sv_setuv(newSVrv(rv,classname), uv);
8739 return rv;
8740}
8741
8742/*
954c1994
GS
8743=for apidoc sv_setref_nv
8744
8745Copies a double into a new SV, optionally blessing the SV. The C<rv>
8746argument will be upgraded to an RV. That RV will be modified to point to
8747the new SV. The C<classname> argument indicates the package for the
8748blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
d34c2299 8749will have a reference count of 1, and the RV will be returned.
954c1994
GS
8750
8751=cut
8752*/
8753
a0d0e21e 8754SV*
65202027 8755Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv)
a0d0e21e
LW
8756{
8757 sv_setnv(newSVrv(rv,classname), nv);
8758 return rv;
8759}
463ee0b2 8760
954c1994
GS
8761/*
8762=for apidoc sv_setref_pvn
8763
8764Copies a string into a new SV, optionally blessing the SV. The length of the
8765string must be specified with C<n>. The C<rv> argument will be upgraded to
8766an RV. That RV will be modified to point to the new SV. The C<classname>
8767argument indicates the package for the blessing. Set C<classname> to
7a5fa8a2 8768C<Nullch> to avoid the blessing. The new SV will have a reference count
d34c2299 8769of 1, and the RV will be returned.
954c1994
GS
8770
8771Note that C<sv_setref_pv> copies the pointer while this copies the string.
8772
8773=cut
8774*/
8775
a0d0e21e 8776SV*
864dbfa3 8777Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n)
a0d0e21e
LW
8778{
8779 sv_setpvn(newSVrv(rv,classname), pv, n);
463ee0b2
LW
8780 return rv;
8781}
8782
954c1994
GS
8783/*
8784=for apidoc sv_bless
8785
8786Blesses an SV into a specified package. The SV must be an RV. The package
8787must be designated by its stash (see C<gv_stashpv()>). The reference count
8788of the SV is unaffected.
8789
8790=cut
8791*/
8792
a0d0e21e 8793SV*
864dbfa3 8794Perl_sv_bless(pTHX_ SV *sv, HV *stash)
a0d0e21e 8795{
76e3520e 8796 SV *tmpRef;
a0d0e21e 8797 if (!SvROK(sv))
cea2e8a9 8798 Perl_croak(aTHX_ "Can't bless non-reference value");
76e3520e
GS
8799 tmpRef = SvRV(sv);
8800 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
8801 if (SvREADONLY(tmpRef))
cea2e8a9 8802 Perl_croak(aTHX_ PL_no_modify);
76e3520e
GS
8803 if (SvOBJECT(tmpRef)) {
8804 if (SvTYPE(tmpRef) != SVt_PVIO)
3280af22 8805 --PL_sv_objcount;
76e3520e 8806 SvREFCNT_dec(SvSTASH(tmpRef));
2e3febc6 8807 }
a0d0e21e 8808 }
76e3520e
GS
8809 SvOBJECT_on(tmpRef);
8810 if (SvTYPE(tmpRef) != SVt_PVIO)
3280af22 8811 ++PL_sv_objcount;
862a34c6 8812 SvUPGRADE(tmpRef, SVt_PVMG);
b162af07 8813 SvSTASH_set(tmpRef, (HV*)SvREFCNT_inc(stash));
a0d0e21e 8814
2e3febc6
CS
8815 if (Gv_AMG(stash))
8816 SvAMAGIC_on(sv);
8817 else
8818 SvAMAGIC_off(sv);
a0d0e21e 8819
1edbfb88
AB
8820 if(SvSMAGICAL(tmpRef))
8821 if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
8822 mg_set(tmpRef);
8823
8824
ecdeb87c 8825
a0d0e21e
LW
8826 return sv;
8827}
8828
645c22ef 8829/* Downgrades a PVGV to a PVMG.
645c22ef
DM
8830 */
8831
76e3520e 8832STATIC void
cea2e8a9 8833S_sv_unglob(pTHX_ SV *sv)
a0d0e21e 8834{
850fabdf
GS
8835 void *xpvmg;
8836
a0d0e21e
LW
8837 assert(SvTYPE(sv) == SVt_PVGV);
8838 SvFAKE_off(sv);
8839 if (GvGP(sv))
1edc1566 8840 gp_free((GV*)sv);
e826b3c7
GS
8841 if (GvSTASH(sv)) {
8842 SvREFCNT_dec(GvSTASH(sv));
8843 GvSTASH(sv) = Nullhv;
8844 }
14befaf4 8845 sv_unmagic(sv, PERL_MAGIC_glob);
a0d0e21e 8846 Safefree(GvNAME(sv));
a5f75d66 8847 GvMULTI_off(sv);
850fabdf
GS
8848
8849 /* need to keep SvANY(sv) in the right arena */
8850 xpvmg = new_XPVMG();
8851 StructCopy(SvANY(sv), xpvmg, XPVMG);
8852 del_XPVGV(SvANY(sv));
8853 SvANY(sv) = xpvmg;
8854
a0d0e21e
LW
8855 SvFLAGS(sv) &= ~SVTYPEMASK;
8856 SvFLAGS(sv) |= SVt_PVMG;
8857}
8858
954c1994 8859/*
840a7b70 8860=for apidoc sv_unref_flags
954c1994
GS
8861
8862Unsets the RV status of the SV, and decrements the reference count of
8863whatever was being referenced by the RV. This can almost be thought of
840a7b70
IZ
8864as a reversal of C<newSVrv>. The C<cflags> argument can contain
8865C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
8866(otherwise the decrementing is conditional on the reference count being
8867different from one or the reference being a readonly SV).
7889fe52 8868See C<SvROK_off>.
954c1994
GS
8869
8870=cut
8871*/
8872
ed6116ce 8873void
840a7b70 8874Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags)
ed6116ce 8875{
a0d0e21e 8876 SV* rv = SvRV(sv);
810b8aa5
GS
8877
8878 if (SvWEAKREF(sv)) {
8879 sv_del_backref(sv);
8880 SvWEAKREF_off(sv);
b162af07 8881 SvRV_set(sv, NULL);
810b8aa5
GS
8882 return;
8883 }
b162af07 8884 SvRV_set(sv, NULL);
ed6116ce 8885 SvROK_off(sv);
04ca4930
NC
8886 /* You can't have a || SvREADONLY(rv) here, as $a = $$a, where $a was
8887 assigned to as BEGIN {$a = \"Foo"} will fail. */
8888 if (SvREFCNT(rv) != 1 || (flags & SV_IMMEDIATE_UNREF))
4633a7c4 8889 SvREFCNT_dec(rv);
840a7b70 8890 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
4633a7c4 8891 sv_2mortal(rv); /* Schedule for freeing later */
ed6116ce 8892}
8990e307 8893
840a7b70
IZ
8894/*
8895=for apidoc sv_unref
8896
8897Unsets the RV status of the SV, and decrements the reference count of
8898whatever was being referenced by the RV. This can almost be thought of
8899as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag>
7889fe52 8900being zero. See C<SvROK_off>.
840a7b70
IZ
8901
8902=cut
8903*/
8904
8905void
8906Perl_sv_unref(pTHX_ SV *sv)
8907{
8908 sv_unref_flags(sv, 0);
8909}
8910
645c22ef
DM
8911/*
8912=for apidoc sv_taint
8913
8914Taint an SV. Use C<SvTAINTED_on> instead.
8915=cut
8916*/
8917
bbce6d69 8918void
864dbfa3 8919Perl_sv_taint(pTHX_ SV *sv)
bbce6d69 8920{
14befaf4 8921 sv_magic((sv), Nullsv, PERL_MAGIC_taint, Nullch, 0);
bbce6d69 8922}
8923
645c22ef
DM
8924/*
8925=for apidoc sv_untaint
8926
8927Untaint an SV. Use C<SvTAINTED_off> instead.
8928=cut
8929*/
8930
bbce6d69 8931void
864dbfa3 8932Perl_sv_untaint(pTHX_ SV *sv)
bbce6d69 8933{
13f57bf8 8934 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
14befaf4 8935 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
36477c24 8936 if (mg)
565764a8 8937 mg->mg_len &= ~1;
36477c24 8938 }
bbce6d69 8939}
8940
645c22ef
DM
8941/*
8942=for apidoc sv_tainted
8943
8944Test an SV for taintedness. Use C<SvTAINTED> instead.
8945=cut
8946*/
8947
bbce6d69 8948bool
864dbfa3 8949Perl_sv_tainted(pTHX_ SV *sv)
bbce6d69 8950{
13f57bf8 8951 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
a28509cc 8952 MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
155aba94 8953 if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv)))
36477c24 8954 return TRUE;
8955 }
8956 return FALSE;
bbce6d69 8957}
8958
09540bc3
JH
8959/*
8960=for apidoc sv_setpviv
8961
8962Copies an integer into the given SV, also updating its string value.
8963Does not handle 'set' magic. See C<sv_setpviv_mg>.
8964
8965=cut
8966*/
8967
8968void
8969Perl_sv_setpviv(pTHX_ SV *sv, IV iv)
8970{
8971 char buf[TYPE_CHARS(UV)];
8972 char *ebuf;
8973 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8974
8975 sv_setpvn(sv, ptr, ebuf - ptr);
8976}
8977
8978/*
8979=for apidoc sv_setpviv_mg
8980
8981Like C<sv_setpviv>, but also handles 'set' magic.
8982
8983=cut
8984*/
8985
8986void
8987Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv)
8988{
8989 char buf[TYPE_CHARS(UV)];
8990 char *ebuf;
8991 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8992
8993 sv_setpvn(sv, ptr, ebuf - ptr);
8994 SvSETMAGIC(sv);
8995}
8996
cea2e8a9 8997#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
8998
8999/* pTHX_ magic can't cope with varargs, so this is a no-context
9000 * version of the main function, (which may itself be aliased to us).
9001 * Don't access this version directly.
9002 */
9003
cea2e8a9
GS
9004void
9005Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...)
9006{
9007 dTHX;
9008 va_list args;
9009 va_start(args, pat);
c5be433b 9010 sv_vsetpvf(sv, pat, &args);
cea2e8a9
GS
9011 va_end(args);
9012}
9013
645c22ef
DM
9014/* pTHX_ magic can't cope with varargs, so this is a no-context
9015 * version of the main function, (which may itself be aliased to us).
9016 * Don't access this version directly.
9017 */
cea2e8a9
GS
9018
9019void
9020Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...)
9021{
9022 dTHX;
9023 va_list args;
9024 va_start(args, pat);
c5be433b 9025 sv_vsetpvf_mg(sv, pat, &args);
cea2e8a9 9026 va_end(args);
cea2e8a9
GS
9027}
9028#endif
9029
954c1994
GS
9030/*
9031=for apidoc sv_setpvf
9032
bffc3d17
SH
9033Works like C<sv_catpvf> but copies the text into the SV instead of
9034appending it. Does not handle 'set' magic. See C<sv_setpvf_mg>.
954c1994
GS
9035
9036=cut
9037*/
9038
46fc3d4c 9039void
864dbfa3 9040Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...)
46fc3d4c 9041{
9042 va_list args;
46fc3d4c 9043 va_start(args, pat);
c5be433b 9044 sv_vsetpvf(sv, pat, &args);
46fc3d4c 9045 va_end(args);
9046}
9047
bffc3d17
SH
9048/*
9049=for apidoc sv_vsetpvf
9050
9051Works like C<sv_vcatpvf> but copies the text into the SV instead of
9052appending it. Does not handle 'set' magic. See C<sv_vsetpvf_mg>.
9053
9054Usually used via its frontend C<sv_setpvf>.
9055
9056=cut
9057*/
645c22ef 9058
c5be433b
GS
9059void
9060Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args)
9061{
9062 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
9063}
ef50df4b 9064
954c1994
GS
9065/*
9066=for apidoc sv_setpvf_mg
9067
9068Like C<sv_setpvf>, but also handles 'set' magic.
9069
9070=cut
9071*/
9072
ef50df4b 9073void
864dbfa3 9074Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...)
ef50df4b
GS
9075{
9076 va_list args;
ef50df4b 9077 va_start(args, pat);
c5be433b 9078 sv_vsetpvf_mg(sv, pat, &args);
ef50df4b 9079 va_end(args);
c5be433b
GS
9080}
9081
bffc3d17
SH
9082/*
9083=for apidoc sv_vsetpvf_mg
9084
9085Like C<sv_vsetpvf>, but also handles 'set' magic.
9086
9087Usually used via its frontend C<sv_setpvf_mg>.
9088
9089=cut
9090*/
645c22ef 9091
c5be433b
GS
9092void
9093Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
9094{
9095 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
ef50df4b
GS
9096 SvSETMAGIC(sv);
9097}
9098
cea2e8a9 9099#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
9100
9101/* pTHX_ magic can't cope with varargs, so this is a no-context
9102 * version of the main function, (which may itself be aliased to us).
9103 * Don't access this version directly.
9104 */
9105
cea2e8a9
GS
9106void
9107Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...)
9108{
9109 dTHX;
9110 va_list args;
9111 va_start(args, pat);
c5be433b 9112 sv_vcatpvf(sv, pat, &args);
cea2e8a9
GS
9113 va_end(args);
9114}
9115
645c22ef
DM
9116/* pTHX_ magic can't cope with varargs, so this is a no-context
9117 * version of the main function, (which may itself be aliased to us).
9118 * Don't access this version directly.
9119 */
9120
cea2e8a9
GS
9121void
9122Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...)
9123{
9124 dTHX;
9125 va_list args;
9126 va_start(args, pat);
c5be433b 9127 sv_vcatpvf_mg(sv, pat, &args);
cea2e8a9 9128 va_end(args);
cea2e8a9
GS
9129}
9130#endif
9131
954c1994
GS
9132/*
9133=for apidoc sv_catpvf
9134
d5ce4a7c
GA
9135Processes its arguments like C<sprintf> and appends the formatted
9136output to an SV. If the appended data contains "wide" characters
9137(including, but not limited to, SVs with a UTF-8 PV formatted with %s,
9138and characters >255 formatted with %c), the original SV might get
bffc3d17 9139upgraded to UTF-8. Handles 'get' magic, but not 'set' magic. See
cdd94ca7
NC
9140C<sv_catpvf_mg>. If the original SV was UTF-8, the pattern should be
9141valid UTF-8; if the original SV was bytes, the pattern should be too.
954c1994 9142
d5ce4a7c 9143=cut */
954c1994 9144
46fc3d4c 9145void
864dbfa3 9146Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...)
46fc3d4c 9147{
9148 va_list args;
46fc3d4c 9149 va_start(args, pat);
c5be433b 9150 sv_vcatpvf(sv, pat, &args);
46fc3d4c 9151 va_end(args);
9152}
9153
bffc3d17
SH
9154/*
9155=for apidoc sv_vcatpvf
9156
9157Processes its arguments like C<vsprintf> and appends the formatted output
9158to an SV. Does not handle 'set' magic. See C<sv_vcatpvf_mg>.
9159
9160Usually used via its frontend C<sv_catpvf>.
9161
9162=cut
9163*/
645c22ef 9164
ef50df4b 9165void
c5be433b
GS
9166Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args)
9167{
9168 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
9169}
9170
954c1994
GS
9171/*
9172=for apidoc sv_catpvf_mg
9173
9174Like C<sv_catpvf>, but also handles 'set' magic.
9175
9176=cut
9177*/
9178
c5be433b 9179void
864dbfa3 9180Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...)
ef50df4b
GS
9181{
9182 va_list args;
ef50df4b 9183 va_start(args, pat);
c5be433b 9184 sv_vcatpvf_mg(sv, pat, &args);
ef50df4b 9185 va_end(args);
c5be433b
GS
9186}
9187
bffc3d17
SH
9188/*
9189=for apidoc sv_vcatpvf_mg
9190
9191Like C<sv_vcatpvf>, but also handles 'set' magic.
9192
9193Usually used via its frontend C<sv_catpvf_mg>.
9194
9195=cut
9196*/
645c22ef 9197
c5be433b
GS
9198void
9199Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
9200{
9201 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
ef50df4b
GS
9202 SvSETMAGIC(sv);
9203}
9204
954c1994
GS
9205/*
9206=for apidoc sv_vsetpvfn
9207
bffc3d17 9208Works like C<sv_vcatpvfn> but copies the text into the SV instead of
954c1994
GS
9209appending it.
9210
bffc3d17 9211Usually used via one of its frontends C<sv_vsetpvf> and C<sv_vsetpvf_mg>.
645c22ef 9212
954c1994
GS
9213=cut
9214*/
9215
46fc3d4c 9216void
7d5ea4e7 9217Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
46fc3d4c 9218{
9219 sv_setpvn(sv, "", 0);
7d5ea4e7 9220 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted);
46fc3d4c 9221}
9222
645c22ef
DM
9223/* private function for use in sv_vcatpvfn via the EXPECT_NUMBER macro */
9224
2d00ba3b 9225STATIC I32
9dd79c3f 9226S_expect_number(pTHX_ char** pattern)
211dfcf1
HS
9227{
9228 I32 var = 0;
9229 switch (**pattern) {
9230 case '1': case '2': case '3':
9231 case '4': case '5': case '6':
9232 case '7': case '8': case '9':
9233 while (isDIGIT(**pattern))
9234 var = var * 10 + (*(*pattern)++ - '0');
9235 }
9236 return var;
9237}
9dd79c3f 9238#define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern))
211dfcf1 9239
4151a5fe
IZ
9240static char *
9241F0convert(NV nv, char *endbuf, STRLEN *len)
9242{
a3b680e6 9243 const int neg = nv < 0;
4151a5fe 9244 UV uv;
4151a5fe
IZ
9245
9246 if (neg)
9247 nv = -nv;
9248 if (nv < UV_MAX) {
b464bac0 9249 char *p = endbuf;
4151a5fe 9250 nv += 0.5;
028f8eaa 9251 uv = (UV)nv;
4151a5fe
IZ
9252 if (uv & 1 && uv == nv)
9253 uv--; /* Round to even */
9254 do {
a3b680e6 9255 const unsigned dig = uv % 10;
4151a5fe
IZ
9256 *--p = '0' + dig;
9257 } while (uv /= 10);
9258 if (neg)
9259 *--p = '-';
9260 *len = endbuf - p;
9261 return p;
9262 }
9263 return Nullch;
9264}
9265
9266
954c1994
GS
9267/*
9268=for apidoc sv_vcatpvfn
9269
9270Processes its arguments like C<vsprintf> and appends the formatted output
9271to an SV. Uses an array of SVs if the C style variable argument list is
9272missing (NULL). When running with taint checks enabled, indicates via
9273C<maybe_tainted> if results are untrustworthy (often due to the use of
9274locales).
9275
bffc3d17 9276Usually used via one of its frontends C<sv_vcatpvf> and C<sv_vcatpvf_mg>.
645c22ef 9277
954c1994
GS
9278=cut
9279*/
9280
1ef29b0e
RGS
9281/* XXX maybe_tainted is never assigned to, so the doc above is lying. */
9282
46fc3d4c 9283void
7d5ea4e7 9284Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
46fc3d4c 9285{
9286 char *p;
9287 char *q;
a3b680e6 9288 const char *patend;
fc36a67e 9289 STRLEN origlen;
46fc3d4c 9290 I32 svix = 0;
27da23d5 9291 static const char nullstr[] = "(null)";
9c5ffd7c 9292 SV *argsv = Nullsv;
b464bac0
AL
9293 bool has_utf8 = DO_UTF8(sv); /* has the result utf8? */
9294 const bool pat_utf8 = has_utf8; /* the pattern is in utf8? */
db79b45b 9295 SV *nsv = Nullsv;
4151a5fe
IZ
9296 /* Times 4: a decimal digit takes more than 3 binary digits.
9297 * NV_DIG: mantissa takes than many decimal digits.
9298 * Plus 32: Playing safe. */
9299 char ebuf[IV_DIG * 4 + NV_DIG + 32];
9300 /* large enough for "%#.#f" --chip */
9301 /* what about long double NVs? --jhi */
db79b45b 9302
46fc3d4c 9303 /* no matter what, this is a string now */
fc36a67e 9304 (void)SvPV_force(sv, origlen);
46fc3d4c 9305
0dbb1585 9306 /* special-case "", "%s", and "%-p" (SVf) */
46fc3d4c 9307 if (patlen == 0)
9308 return;
0dbb1585 9309 if (patlen == 2 && pat[0] == '%' && pat[1] == 's') {
c635e13b 9310 if (args) {
73d840c0 9311 const char *s = va_arg(*args, char*);
c635e13b 9312 sv_catpv(sv, s ? s : nullstr);
9313 }
7e2040f0 9314 else if (svix < svmax) {
fc36a67e 9315 sv_catsv(sv, *svargs);
7e2040f0
GS
9316 if (DO_UTF8(*svargs))
9317 SvUTF8_on(sv);
9318 }
fc36a67e 9319 return;
0dbb1585
AL
9320 }
9321 if (patlen == 3 && pat[0] == '%' &&
9322 pat[1] == '-' && pat[2] == 'p') {
fc36a67e 9323 if (args) {
7e2040f0
GS
9324 argsv = va_arg(*args, SV*);
9325 sv_catsv(sv, argsv);
9326 if (DO_UTF8(argsv))
9327 SvUTF8_on(sv);
fc36a67e 9328 return;
9329 }
46fc3d4c 9330 }
9331
1d917b39 9332#ifndef USE_LONG_DOUBLE
4151a5fe
IZ
9333 /* special-case "%.<number>[gf]" */
9334 if ( patlen <= 5 && pat[0] == '%' && pat[1] == '.'
9335 && (pat[patlen-1] == 'g' || pat[patlen-1] == 'f') ) {
9336 unsigned digits = 0;
9337 const char *pp;
9338
9339 pp = pat + 2;
9340 while (*pp >= '0' && *pp <= '9')
9341 digits = 10 * digits + (*pp++ - '0');
028f8eaa 9342 if (pp - pat == (int)patlen - 1) {
4151a5fe
IZ
9343 NV nv;
9344
9345 if (args)
9346 nv = (NV)va_arg(*args, double);
9347 else if (svix < svmax)
9348 nv = SvNV(*svargs);
9349 else
9350 return;
9351 if (*pp == 'g') {
2873255c
NC
9352 /* Add check for digits != 0 because it seems that some
9353 gconverts are buggy in this case, and we don't yet have
9354 a Configure test for this. */
9355 if (digits && digits < sizeof(ebuf) - NV_DIG - 10) {
9356 /* 0, point, slack */
2e59c212 9357 Gconvert(nv, (int)digits, 0, ebuf);
4151a5fe
IZ
9358 sv_catpv(sv, ebuf);
9359 if (*ebuf) /* May return an empty string for digits==0 */
9360 return;
9361 }
9362 } else if (!digits) {
9363 STRLEN l;
9364
9365 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
9366 sv_catpvn(sv, p, l);
9367 return;
9368 }
9369 }
9370 }
9371 }
1d917b39 9372#endif /* !USE_LONG_DOUBLE */
4151a5fe 9373
2cf2cfc6 9374 if (!args && svix < svmax && DO_UTF8(*svargs))
205f51d8 9375 has_utf8 = TRUE;
2cf2cfc6 9376
46fc3d4c 9377 patend = (char*)pat + patlen;
9378 for (p = (char*)pat; p < patend; p = q) {
9379 bool alt = FALSE;
9380 bool left = FALSE;
b22c7a20 9381 bool vectorize = FALSE;
211dfcf1 9382 bool vectorarg = FALSE;
2cf2cfc6 9383 bool vec_utf8 = FALSE;
46fc3d4c 9384 char fill = ' ';
9385 char plus = 0;
9386 char intsize = 0;
9387 STRLEN width = 0;
fc36a67e 9388 STRLEN zeros = 0;
46fc3d4c 9389 bool has_precis = FALSE;
9390 STRLEN precis = 0;
58e33a90 9391 I32 osvix = svix;
2cf2cfc6 9392 bool is_utf8 = FALSE; /* is this item utf8? */
20f6aaab
AS
9393#ifdef HAS_LDBL_SPRINTF_BUG
9394 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
205f51d8 9395 with sfio - Allen <allens@cpan.org> */
20f6aaab
AS
9396 bool fix_ldbl_sprintf_bug = FALSE;
9397#endif
205f51d8 9398
46fc3d4c 9399 char esignbuf[4];
89ebb4a3 9400 U8 utf8buf[UTF8_MAXBYTES+1];
46fc3d4c 9401 STRLEN esignlen = 0;
9402
4d84ee25 9403 const char *eptr = Nullch;
fc36a67e 9404 STRLEN elen = 0;
81f715da 9405 SV *vecsv = Nullsv;
245d4a47 9406 const U8 *vecstr = Null(U8*);
b22c7a20 9407 STRLEN veclen = 0;
934abaf1 9408 char c = 0;
46fc3d4c 9409 int i;
9c5ffd7c 9410 unsigned base = 0;
8c8eb53c
RB
9411 IV iv = 0;
9412 UV uv = 0;
9e5b023a
JH
9413 /* we need a long double target in case HAS_LONG_DOUBLE but
9414 not USE_LONG_DOUBLE
9415 */
35fff930 9416#if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE
9e5b023a
JH
9417 long double nv;
9418#else
65202027 9419 NV nv;
9e5b023a 9420#endif
46fc3d4c 9421 STRLEN have;
9422 STRLEN need;
9423 STRLEN gap;
e1ec3a88 9424 const char *dotstr = ".";
b22c7a20 9425 STRLEN dotstrlen = 1;
211dfcf1 9426 I32 efix = 0; /* explicit format parameter index */
eb3fce90 9427 I32 ewix = 0; /* explicit width index */
211dfcf1
HS
9428 I32 epix = 0; /* explicit precision index */
9429 I32 evix = 0; /* explicit vector index */
eb3fce90 9430 bool asterisk = FALSE;
46fc3d4c 9431
211dfcf1 9432 /* echo everything up to the next format specification */
46fc3d4c 9433 for (q = p; q < patend && *q != '%'; ++q) ;
9434 if (q > p) {
db79b45b
JH
9435 if (has_utf8 && !pat_utf8)
9436 sv_catpvn_utf8_upgrade(sv, p, q - p, nsv);
9437 else
9438 sv_catpvn(sv, p, q - p);
46fc3d4c 9439 p = q;
9440 }
9441 if (q++ >= patend)
9442 break;
9443
211dfcf1
HS
9444/*
9445 We allow format specification elements in this order:
9446 \d+\$ explicit format parameter index
9447 [-+ 0#]+ flags
a472f209 9448 v|\*(\d+\$)?v vector with optional (optionally specified) arg
f3583277 9449 0 flag (as above): repeated to allow "v02"
211dfcf1
HS
9450 \d+|\*(\d+\$)? width using optional (optionally specified) arg
9451 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
9452 [hlqLV] size
9453 [%bcdefginopsux_DFOUX] format (mandatory)
9454*/
9455 if (EXPECT_NUMBER(q, width)) {
9456 if (*q == '$') {
9457 ++q;
9458 efix = width;
9459 } else {
9460 goto gotwidth;
9461 }
9462 }
9463
fc36a67e 9464 /* FLAGS */
9465
46fc3d4c 9466 while (*q) {
9467 switch (*q) {
9468 case ' ':
9469 case '+':
9470 plus = *q++;
9471 continue;
9472
9473 case '-':
9474 left = TRUE;
9475 q++;
9476 continue;
9477
9478 case '0':
9479 fill = *q++;
9480 continue;
9481
9482 case '#':
9483 alt = TRUE;
9484 q++;
9485 continue;
9486
fc36a67e 9487 default:
9488 break;
9489 }
9490 break;
9491 }
46fc3d4c 9492
211dfcf1 9493 tryasterisk:
eb3fce90 9494 if (*q == '*') {
211dfcf1
HS
9495 q++;
9496 if (EXPECT_NUMBER(q, ewix))
9497 if (*q++ != '$')
9498 goto unknown;
eb3fce90 9499 asterisk = TRUE;
211dfcf1
HS
9500 }
9501 if (*q == 'v') {
eb3fce90 9502 q++;
211dfcf1
HS
9503 if (vectorize)
9504 goto unknown;
9cbac4c7 9505 if ((vectorarg = asterisk)) {
211dfcf1
HS
9506 evix = ewix;
9507 ewix = 0;
9508 asterisk = FALSE;
9509 }
9510 vectorize = TRUE;
9511 goto tryasterisk;
eb3fce90
JH
9512 }
9513
211dfcf1 9514 if (!asterisk)
7a5fa8a2 9515 if( *q == '0' )
f3583277 9516 fill = *q++;
211dfcf1
HS
9517 EXPECT_NUMBER(q, width);
9518
9519 if (vectorize) {
9520 if (vectorarg) {
9521 if (args)
9522 vecsv = va_arg(*args, SV*);
9523 else
9524 vecsv = (evix ? evix <= svmax : svix < svmax) ?
3a7a539e 9525 svargs[evix ? evix-1 : svix++] : &PL_sv_undef;
245d4a47 9526 dotstr = SvPV_const(vecsv, dotstrlen);
211dfcf1 9527 if (DO_UTF8(vecsv))
2cf2cfc6 9528 is_utf8 = TRUE;
211dfcf1
HS
9529 }
9530 if (args) {
9531 vecsv = va_arg(*args, SV*);
245d4a47 9532 vecstr = (U8*)SvPV_const(vecsv,veclen);
2cf2cfc6 9533 vec_utf8 = DO_UTF8(vecsv);
eb3fce90 9534 }
211dfcf1
HS
9535 else if (efix ? efix <= svmax : svix < svmax) {
9536 vecsv = svargs[efix ? efix-1 : svix++];
245d4a47 9537 vecstr = (U8*)SvPV_const(vecsv,veclen);
2cf2cfc6 9538 vec_utf8 = DO_UTF8(vecsv);
d7aa5382 9539 /* if this is a version object, we need to return the
3f7c398e 9540 * stringified representation (which the SvPVX_const has
d7aa5382
JP
9541 * already done for us), but not vectorize the args
9542 */
9543 if ( *q == 'd' && sv_derived_from(vecsv,"version") )
9544 {
9545 q++; /* skip past the rest of the %vd format */
245d4a47 9546 eptr = (const char *) vecstr;
d7aa5382
JP
9547 elen = strlen(eptr);
9548 vectorize=FALSE;
9549 goto string;
9550 }
211dfcf1
HS
9551 }
9552 else {
9553 vecstr = (U8*)"";
9554 veclen = 0;
9555 }
eb3fce90 9556 }
fc36a67e 9557
eb3fce90 9558 if (asterisk) {
fc36a67e 9559 if (args)
9560 i = va_arg(*args, int);
9561 else
eb3fce90
JH
9562 i = (ewix ? ewix <= svmax : svix < svmax) ?
9563 SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
fc36a67e 9564 left |= (i < 0);
9565 width = (i < 0) ? -i : i;
fc36a67e 9566 }
211dfcf1 9567 gotwidth:
fc36a67e 9568
9569 /* PRECISION */
46fc3d4c 9570
fc36a67e 9571 if (*q == '.') {
9572 q++;
9573 if (*q == '*') {
211dfcf1 9574 q++;
7b8dd722
HS
9575 if (EXPECT_NUMBER(q, epix) && *q++ != '$')
9576 goto unknown;
9577 /* XXX: todo, support specified precision parameter */
9578 if (epix)
211dfcf1 9579 goto unknown;
46fc3d4c 9580 if (args)
9581 i = va_arg(*args, int);
9582 else
eb3fce90
JH
9583 i = (ewix ? ewix <= svmax : svix < svmax)
9584 ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
fc36a67e 9585 precis = (i < 0) ? 0 : i;
fc36a67e 9586 }
9587 else {
9588 precis = 0;
9589 while (isDIGIT(*q))
9590 precis = precis * 10 + (*q++ - '0');
9591 }
9592 has_precis = TRUE;
9593 }
46fc3d4c 9594
fc36a67e 9595 /* SIZE */
46fc3d4c 9596
fc36a67e 9597 switch (*q) {
c623ac67
GS
9598#ifdef WIN32
9599 case 'I': /* Ix, I32x, and I64x */
9600# ifdef WIN64
9601 if (q[1] == '6' && q[2] == '4') {
9602 q += 3;
9603 intsize = 'q';
9604 break;
9605 }
9606# endif
9607 if (q[1] == '3' && q[2] == '2') {
9608 q += 3;
9609 break;
9610 }
9611# ifdef WIN64
9612 intsize = 'q';
9613# endif
9614 q++;
9615 break;
9616#endif
9e5b023a 9617#if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
6f9bb7fd 9618 case 'L': /* Ld */
e5c81feb 9619 /* FALL THROUGH */
e5c81feb 9620#ifdef HAS_QUAD
6f9bb7fd 9621 case 'q': /* qd */
9e5b023a 9622#endif
6f9bb7fd
GS
9623 intsize = 'q';
9624 q++;
9625 break;
9626#endif
fc36a67e 9627 case 'l':
9e5b023a 9628#if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
205f51d8 9629 if (*(q + 1) == 'l') { /* lld, llf */
fc36a67e 9630 intsize = 'q';
9631 q += 2;
46fc3d4c 9632 break;
cf2093f6 9633 }
fc36a67e 9634#endif
6f9bb7fd 9635 /* FALL THROUGH */
fc36a67e 9636 case 'h':
cf2093f6 9637 /* FALL THROUGH */
fc36a67e 9638 case 'V':
9639 intsize = *q++;
46fc3d4c 9640 break;
9641 }
9642
fc36a67e 9643 /* CONVERSION */
9644
211dfcf1
HS
9645 if (*q == '%') {
9646 eptr = q++;
9647 elen = 1;
9648 goto string;
9649 }
9650
be75b157
HS
9651 if (vectorize)
9652 argsv = vecsv;
9653 else if (!args)
211dfcf1
HS
9654 argsv = (efix ? efix <= svmax : svix < svmax) ?
9655 svargs[efix ? efix-1 : svix++] : &PL_sv_undef;
9656
46fc3d4c 9657 switch (c = *q++) {
9658
9659 /* STRINGS */
9660
46fc3d4c 9661 case 'c':
be75b157 9662 uv = (args && !vectorize) ? va_arg(*args, int) : SvIVx(argsv);
1bd104fb
JH
9663 if ((uv > 255 ||
9664 (!UNI_IS_INVARIANT(uv) && SvUTF8(sv)))
0064a8a9 9665 && !IN_BYTES) {
dfe13c55 9666 eptr = (char*)utf8buf;
9041c2e3 9667 elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf;
2cf2cfc6 9668 is_utf8 = TRUE;
7e2040f0
GS
9669 }
9670 else {
9671 c = (char)uv;
9672 eptr = &c;
9673 elen = 1;
a0ed51b3 9674 }
46fc3d4c 9675 goto string;
9676
46fc3d4c 9677 case 's':
be75b157 9678 if (args && !vectorize) {
fc36a67e 9679 eptr = va_arg(*args, char*);
c635e13b 9680 if (eptr)
1d7c1841
GS
9681#ifdef MACOS_TRADITIONAL
9682 /* On MacOS, %#s format is used for Pascal strings */
9683 if (alt)
9684 elen = *eptr++;
9685 else
9686#endif
c635e13b 9687 elen = strlen(eptr);
9688 else {
27da23d5 9689 eptr = (char *)nullstr;
c635e13b 9690 elen = sizeof nullstr - 1;
9691 }
46fc3d4c 9692 }
211dfcf1 9693 else {
4d84ee25 9694 eptr = SvPVx_const(argsv, elen);
7e2040f0 9695 if (DO_UTF8(argsv)) {
a0ed51b3
LW
9696 if (has_precis && precis < elen) {
9697 I32 p = precis;
7e2040f0 9698 sv_pos_u2b(argsv, &p, 0); /* sticks at end */
a0ed51b3
LW
9699 precis = p;
9700 }
9701 if (width) { /* fudge width (can't fudge elen) */
7e2040f0 9702 width += elen - sv_len_utf8(argsv);
a0ed51b3 9703 }
2cf2cfc6 9704 is_utf8 = TRUE;
a0ed51b3
LW
9705 }
9706 }
fc36a67e 9707
46fc3d4c 9708 string:
b22c7a20 9709 vectorize = FALSE;
46fc3d4c 9710 if (has_precis && elen > precis)
9711 elen = precis;
9712 break;
9713
9714 /* INTEGERS */
9715
fc36a67e 9716 case 'p':
0dbb1585 9717 if (left && args) { /* SVf */
5df617be 9718 left = FALSE;
0dbb1585
AL
9719 if (width) {
9720 precis = width;
9721 has_precis = TRUE;
9722 width = 0;
9723 }
9724 if (vectorize)
9725 goto unknown;
9726 argsv = va_arg(*args, SV*);
4d84ee25 9727 eptr = SvPVx_const(argsv, elen);
0dbb1585
AL
9728 if (DO_UTF8(argsv))
9729 is_utf8 = TRUE;
9730 goto string;
5df617be 9731 }
be75b157 9732 if (alt || vectorize)
c2e66d9e 9733 goto unknown;
211dfcf1 9734 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
fc36a67e 9735 base = 16;
9736 goto integer;
9737
46fc3d4c 9738 case 'D':
29fe7a80 9739#ifdef IV_IS_QUAD
22f3ae8c 9740 intsize = 'q';
29fe7a80 9741#else
46fc3d4c 9742 intsize = 'l';
29fe7a80 9743#endif
46fc3d4c 9744 /* FALL THROUGH */
9745 case 'd':
9746 case 'i':
b22c7a20 9747 if (vectorize) {
ba210ebe 9748 STRLEN ulen;
211dfcf1
HS
9749 if (!veclen)
9750 continue;
2cf2cfc6
A
9751 if (vec_utf8)
9752 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
9753 UTF8_ALLOW_ANYUV);
b22c7a20 9754 else {
e83d50c9 9755 uv = *vecstr;
b22c7a20
GS
9756 ulen = 1;
9757 }
9758 vecstr += ulen;
9759 veclen -= ulen;
e83d50c9
JP
9760 if (plus)
9761 esignbuf[esignlen++] = plus;
b22c7a20
GS
9762 }
9763 else if (args) {
46fc3d4c 9764 switch (intsize) {
9765 case 'h': iv = (short)va_arg(*args, int); break;
46fc3d4c 9766 case 'l': iv = va_arg(*args, long); break;
fc36a67e 9767 case 'V': iv = va_arg(*args, IV); break;
b10c0dba 9768 default: iv = va_arg(*args, int); break;
cf2093f6
JH
9769#ifdef HAS_QUAD
9770 case 'q': iv = va_arg(*args, Quad_t); break;
9771#endif
46fc3d4c 9772 }
9773 }
9774 else {
b10c0dba 9775 IV tiv = SvIVx(argsv); /* work around GCC bug #13488 */
46fc3d4c 9776 switch (intsize) {
b10c0dba
MHM
9777 case 'h': iv = (short)tiv; break;
9778 case 'l': iv = (long)tiv; break;
9779 case 'V':
9780 default: iv = tiv; break;
cf2093f6 9781#ifdef HAS_QUAD
b10c0dba 9782 case 'q': iv = (Quad_t)tiv; break;
cf2093f6 9783#endif
46fc3d4c 9784 }
9785 }
e83d50c9
JP
9786 if ( !vectorize ) /* we already set uv above */
9787 {
9788 if (iv >= 0) {
9789 uv = iv;
9790 if (plus)
9791 esignbuf[esignlen++] = plus;
9792 }
9793 else {
9794 uv = -iv;
9795 esignbuf[esignlen++] = '-';
9796 }
46fc3d4c 9797 }
9798 base = 10;
9799 goto integer;
9800
fc36a67e 9801 case 'U':
29fe7a80 9802#ifdef IV_IS_QUAD
22f3ae8c 9803 intsize = 'q';
29fe7a80 9804#else
fc36a67e 9805 intsize = 'l';
29fe7a80 9806#endif
fc36a67e 9807 /* FALL THROUGH */
9808 case 'u':
9809 base = 10;
9810 goto uns_integer;
9811
4f19785b
WSI
9812 case 'b':
9813 base = 2;
9814 goto uns_integer;
9815
46fc3d4c 9816 case 'O':
29fe7a80 9817#ifdef IV_IS_QUAD
22f3ae8c 9818 intsize = 'q';
29fe7a80 9819#else
46fc3d4c 9820 intsize = 'l';
29fe7a80 9821#endif
46fc3d4c 9822 /* FALL THROUGH */
9823 case 'o':
9824 base = 8;
9825 goto uns_integer;
9826
9827 case 'X':
46fc3d4c 9828 case 'x':
9829 base = 16;
46fc3d4c 9830
9831 uns_integer:
b22c7a20 9832 if (vectorize) {
ba210ebe 9833 STRLEN ulen;
b22c7a20 9834 vector:
211dfcf1
HS
9835 if (!veclen)
9836 continue;
2cf2cfc6
A
9837 if (vec_utf8)
9838 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
9839 UTF8_ALLOW_ANYUV);
b22c7a20 9840 else {
a05b299f 9841 uv = *vecstr;
b22c7a20
GS
9842 ulen = 1;
9843 }
9844 vecstr += ulen;
9845 veclen -= ulen;
9846 }
9847 else if (args) {
46fc3d4c 9848 switch (intsize) {
9849 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
46fc3d4c 9850 case 'l': uv = va_arg(*args, unsigned long); break;
fc36a67e 9851 case 'V': uv = va_arg(*args, UV); break;
b10c0dba 9852 default: uv = va_arg(*args, unsigned); break;
cf2093f6 9853#ifdef HAS_QUAD
9e3321a5 9854 case 'q': uv = va_arg(*args, Uquad_t); break;
cf2093f6 9855#endif
46fc3d4c 9856 }
9857 }
9858 else {
b10c0dba 9859 UV tuv = SvUVx(argsv); /* work around GCC bug #13488 */
46fc3d4c 9860 switch (intsize) {
b10c0dba
MHM
9861 case 'h': uv = (unsigned short)tuv; break;
9862 case 'l': uv = (unsigned long)tuv; break;
9863 case 'V':
9864 default: uv = tuv; break;
cf2093f6 9865#ifdef HAS_QUAD
b10c0dba 9866 case 'q': uv = (Uquad_t)tuv; break;
cf2093f6 9867#endif
46fc3d4c 9868 }
9869 }
9870
9871 integer:
4d84ee25
NC
9872 {
9873 char *ptr = ebuf + sizeof ebuf;
9874 switch (base) {
9875 unsigned dig;
9876 case 16:
9877 if (!uv)
9878 alt = FALSE;
9879 p = (char*)((c == 'X')
9880 ? "0123456789ABCDEF" : "0123456789abcdef");
9881 do {
9882 dig = uv & 15;
9883 *--ptr = p[dig];
9884 } while (uv >>= 4);
9885 if (alt) {
9886 esignbuf[esignlen++] = '0';
9887 esignbuf[esignlen++] = c; /* 'x' or 'X' */
9888 }
9889 break;
9890 case 8:
9891 do {
9892 dig = uv & 7;
9893 *--ptr = '0' + dig;
9894 } while (uv >>= 3);
9895 if (alt && *ptr != '0')
9896 *--ptr = '0';
9897 break;
9898 case 2:
9899 do {
9900 dig = uv & 1;
9901 *--ptr = '0' + dig;
9902 } while (uv >>= 1);
9903 if (alt) {
9904 esignbuf[esignlen++] = '0';
9905 esignbuf[esignlen++] = 'b';
9906 }
9907 break;
9908 default: /* it had better be ten or less */
9909 do {
9910 dig = uv % base;
9911 *--ptr = '0' + dig;
9912 } while (uv /= base);
9913 break;
46fc3d4c 9914 }
4d84ee25
NC
9915 elen = (ebuf + sizeof ebuf) - ptr;
9916 eptr = ptr;
9917 if (has_precis) {
9918 if (precis > elen)
9919 zeros = precis - elen;
9920 else if (precis == 0 && elen == 1 && *eptr == '0')
9921 elen = 0;
eda88b6d 9922 }
c10ed8b9 9923 }
46fc3d4c 9924 break;
9925
9926 /* FLOATING POINT */
9927
fc36a67e 9928 case 'F':
9929 c = 'f'; /* maybe %F isn't supported here */
9930 /* FALL THROUGH */
46fc3d4c 9931 case 'e': case 'E':
fc36a67e 9932 case 'f':
46fc3d4c 9933 case 'g': case 'G':
9934
9935 /* This is evil, but floating point is even more evil */
9936
9e5b023a
JH
9937 /* for SV-style calling, we can only get NV
9938 for C-style calling, we assume %f is double;
9939 for simplicity we allow any of %Lf, %llf, %qf for long double
9940 */
9941 switch (intsize) {
9942 case 'V':
9943#if defined(USE_LONG_DOUBLE)
9944 intsize = 'q';
9945#endif
9946 break;
8a2e3f14 9947/* [perl #20339] - we should accept and ignore %lf rather than die */
00e17364
HS
9948 case 'l':
9949 /* FALL THROUGH */
9e5b023a
JH
9950 default:
9951#if defined(USE_LONG_DOUBLE)
9952 intsize = args ? 0 : 'q';
9953#endif
9954 break;
9955 case 'q':
9956#if defined(HAS_LONG_DOUBLE)
9957 break;
9958#else
9959 /* FALL THROUGH */
9960#endif
9961 case 'h':
9e5b023a
JH
9962 goto unknown;
9963 }
9964
9965 /* now we need (long double) if intsize == 'q', else (double) */
be75b157 9966 nv = (args && !vectorize) ?
35fff930
JH
9967#if LONG_DOUBLESIZE > DOUBLESIZE
9968 intsize == 'q' ?
205f51d8
AS
9969 va_arg(*args, long double) :
9970 va_arg(*args, double)
35fff930 9971#else
205f51d8 9972 va_arg(*args, double)
35fff930 9973#endif
9e5b023a 9974 : SvNVx(argsv);
fc36a67e 9975
9976 need = 0;
be75b157 9977 vectorize = FALSE;
fc36a67e 9978 if (c != 'e' && c != 'E') {
9979 i = PERL_INT_MIN;
9e5b023a
JH
9980 /* FIXME: if HAS_LONG_DOUBLE but not USE_LONG_DOUBLE this
9981 will cast our (long double) to (double) */
73b309ea 9982 (void)Perl_frexp(nv, &i);
fc36a67e 9983 if (i == PERL_INT_MIN)
cea2e8a9 9984 Perl_die(aTHX_ "panic: frexp");
c635e13b 9985 if (i > 0)
fc36a67e 9986 need = BIT_DIGITS(i);
9987 }
9988 need += has_precis ? precis : 6; /* known default */
20f6aaab 9989
fc36a67e 9990 if (need < width)
9991 need = width;
9992
20f6aaab
AS
9993#ifdef HAS_LDBL_SPRINTF_BUG
9994 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
205f51d8
AS
9995 with sfio - Allen <allens@cpan.org> */
9996
9997# ifdef DBL_MAX
9998# define MY_DBL_MAX DBL_MAX
9999# else /* XXX guessing! HUGE_VAL may be defined as infinity, so not using */
10000# if DOUBLESIZE >= 8
10001# define MY_DBL_MAX 1.7976931348623157E+308L
10002# else
10003# define MY_DBL_MAX 3.40282347E+38L
10004# endif
10005# endif
10006
10007# ifdef HAS_LDBL_SPRINTF_BUG_LESS1 /* only between -1L & 1L - Allen */
10008# define MY_DBL_MAX_BUG 1L
20f6aaab 10009# else
205f51d8 10010# define MY_DBL_MAX_BUG MY_DBL_MAX
20f6aaab 10011# endif
20f6aaab 10012
205f51d8
AS
10013# ifdef DBL_MIN
10014# define MY_DBL_MIN DBL_MIN
10015# else /* XXX guessing! -Allen */
10016# if DOUBLESIZE >= 8
10017# define MY_DBL_MIN 2.2250738585072014E-308L
10018# else
10019# define MY_DBL_MIN 1.17549435E-38L
10020# endif
10021# endif
20f6aaab 10022
205f51d8
AS
10023 if ((intsize == 'q') && (c == 'f') &&
10024 ((nv < MY_DBL_MAX_BUG) && (nv > -MY_DBL_MAX_BUG)) &&
10025 (need < DBL_DIG)) {
10026 /* it's going to be short enough that
10027 * long double precision is not needed */
10028
10029 if ((nv <= 0L) && (nv >= -0L))
10030 fix_ldbl_sprintf_bug = TRUE; /* 0 is 0 - easiest */
10031 else {
10032 /* would use Perl_fp_class as a double-check but not
10033 * functional on IRIX - see perl.h comments */
10034
10035 if ((nv >= MY_DBL_MIN) || (nv <= -MY_DBL_MIN)) {
10036 /* It's within the range that a double can represent */
10037#if defined(DBL_MAX) && !defined(DBL_MIN)
10038 if ((nv >= ((long double)1/DBL_MAX)) ||
10039 (nv <= (-(long double)1/DBL_MAX)))
20f6aaab 10040#endif
205f51d8 10041 fix_ldbl_sprintf_bug = TRUE;
20f6aaab 10042 }
205f51d8
AS
10043 }
10044 if (fix_ldbl_sprintf_bug == TRUE) {
10045 double temp;
10046
10047 intsize = 0;
10048 temp = (double)nv;
10049 nv = (NV)temp;
10050 }
20f6aaab 10051 }
205f51d8
AS
10052
10053# undef MY_DBL_MAX
10054# undef MY_DBL_MAX_BUG
10055# undef MY_DBL_MIN
10056
20f6aaab
AS
10057#endif /* HAS_LDBL_SPRINTF_BUG */
10058
46fc3d4c 10059 need += 20; /* fudge factor */
80252599
GS
10060 if (PL_efloatsize < need) {
10061 Safefree(PL_efloatbuf);
10062 PL_efloatsize = need + 20; /* more fudge */
10063 New(906, PL_efloatbuf, PL_efloatsize, char);
7d5ea4e7 10064 PL_efloatbuf[0] = '\0';
46fc3d4c 10065 }
10066
4151a5fe
IZ
10067 if ( !(width || left || plus || alt) && fill != '0'
10068 && has_precis && intsize != 'q' ) { /* Shortcuts */
2873255c
NC
10069 /* See earlier comment about buggy Gconvert when digits,
10070 aka precis is 0 */
10071 if ( c == 'g' && precis) {
2e59c212 10072 Gconvert((NV)nv, (int)precis, 0, PL_efloatbuf);
4151a5fe
IZ
10073 if (*PL_efloatbuf) /* May return an empty string for digits==0 */
10074 goto float_converted;
10075 } else if ( c == 'f' && !precis) {
10076 if ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
10077 break;
10078 }
10079 }
4d84ee25
NC
10080 {
10081 char *ptr = ebuf + sizeof ebuf;
10082 *--ptr = '\0';
10083 *--ptr = c;
10084 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
9e5b023a 10085#if defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
4d84ee25
NC
10086 if (intsize == 'q') {
10087 /* Copy the one or more characters in a long double
10088 * format before the 'base' ([efgEFG]) character to
10089 * the format string. */
10090 static char const prifldbl[] = PERL_PRIfldbl;
10091 char const *p = prifldbl + sizeof(prifldbl) - 3;
10092 while (p >= prifldbl) { *--ptr = *p--; }
10093 }
65202027 10094#endif
4d84ee25
NC
10095 if (has_precis) {
10096 base = precis;
10097 do { *--ptr = '0' + (base % 10); } while (base /= 10);
10098 *--ptr = '.';
10099 }
10100 if (width) {
10101 base = width;
10102 do { *--ptr = '0' + (base % 10); } while (base /= 10);
10103 }
10104 if (fill == '0')
10105 *--ptr = fill;
10106 if (left)
10107 *--ptr = '-';
10108 if (plus)
10109 *--ptr = plus;
10110 if (alt)
10111 *--ptr = '#';
10112 *--ptr = '%';
10113
10114 /* No taint. Otherwise we are in the strange situation
10115 * where printf() taints but print($float) doesn't.
10116 * --jhi */
9e5b023a 10117#if defined(HAS_LONG_DOUBLE)
4d84ee25
NC
10118 if (intsize == 'q')
10119 (void)sprintf(PL_efloatbuf, ptr, nv);
10120 else
10121 (void)sprintf(PL_efloatbuf, ptr, (double)nv);
9e5b023a 10122#else
4d84ee25 10123 (void)sprintf(PL_efloatbuf, ptr, nv);
9e5b023a 10124#endif
4d84ee25 10125 }
4151a5fe 10126 float_converted:
80252599
GS
10127 eptr = PL_efloatbuf;
10128 elen = strlen(PL_efloatbuf);
46fc3d4c 10129 break;
10130
fc36a67e 10131 /* SPECIAL */
10132
10133 case 'n':
10134 i = SvCUR(sv) - origlen;
be75b157 10135 if (args && !vectorize) {
c635e13b 10136 switch (intsize) {
10137 case 'h': *(va_arg(*args, short*)) = i; break;
10138 default: *(va_arg(*args, int*)) = i; break;
10139 case 'l': *(va_arg(*args, long*)) = i; break;
10140 case 'V': *(va_arg(*args, IV*)) = i; break;
cf2093f6
JH
10141#ifdef HAS_QUAD
10142 case 'q': *(va_arg(*args, Quad_t*)) = i; break;
10143#endif
c635e13b 10144 }
fc36a67e 10145 }
9dd79c3f 10146 else
211dfcf1 10147 sv_setuv_mg(argsv, (UV)i);
be75b157 10148 vectorize = FALSE;
fc36a67e 10149 continue; /* not "break" */
10150
10151 /* UNKNOWN */
10152
46fc3d4c 10153 default:
fc36a67e 10154 unknown:
599cee73 10155 if (!args && ckWARN(WARN_PRINTF) &&
533c011a 10156 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
c635e13b 10157 SV *msg = sv_newmortal();
35c1215d
NC
10158 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
10159 (PL_op->op_type == OP_PRTF) ? "" : "s");
0f4b6630 10160 if (c) {
0f4b6630 10161 if (isPRINT(c))
1c846c1f 10162 Perl_sv_catpvf(aTHX_ msg,
0f4b6630
JH
10163 "\"%%%c\"", c & 0xFF);
10164 else
10165 Perl_sv_catpvf(aTHX_ msg,
57def98f 10166 "\"%%\\%03"UVof"\"",
0f4b6630 10167 (UV)c & 0xFF);
0f4b6630 10168 } else
c635e13b 10169 sv_catpv(msg, "end of string");
9014280d 10170 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%"SVf, msg); /* yes, this is reentrant */
c635e13b 10171 }
fb73857a 10172
10173 /* output mangled stuff ... */
10174 if (c == '\0')
10175 --q;
46fc3d4c 10176 eptr = p;
10177 elen = q - p;
fb73857a 10178
10179 /* ... right here, because formatting flags should not apply */
10180 SvGROW(sv, SvCUR(sv) + elen + 1);
10181 p = SvEND(sv);
4459522c 10182 Copy(eptr, p, elen, char);
fb73857a 10183 p += elen;
10184 *p = '\0';
3f7c398e 10185 SvCUR_set(sv, p - SvPVX_const(sv));
58e33a90 10186 svix = osvix;
fb73857a 10187 continue; /* not "break" */
46fc3d4c 10188 }
10189
6c94ec8b
HS
10190 /* calculate width before utf8_upgrade changes it */
10191 have = esignlen + zeros + elen;
10192
d2876be5
JH
10193 if (is_utf8 != has_utf8) {
10194 if (is_utf8) {
10195 if (SvCUR(sv))
10196 sv_utf8_upgrade(sv);
10197 }
10198 else {
10199 SV *nsv = sv_2mortal(newSVpvn(eptr, elen));
10200 sv_utf8_upgrade(nsv);
93524f2b 10201 eptr = SvPVX_const(nsv);
d2876be5
JH
10202 elen = SvCUR(nsv);
10203 }
10204 SvGROW(sv, SvCUR(sv) + elen + 1);
10205 p = SvEND(sv);
10206 *p = '\0';
10207 }
6af65485 10208
46fc3d4c 10209 need = (have > width ? have : width);
10210 gap = need - have;
10211
b22c7a20 10212 SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1);
46fc3d4c 10213 p = SvEND(sv);
10214 if (esignlen && fill == '0') {
eb160463 10215 for (i = 0; i < (int)esignlen; i++)
46fc3d4c 10216 *p++ = esignbuf[i];
10217 }
10218 if (gap && !left) {
10219 memset(p, fill, gap);
10220 p += gap;
10221 }
10222 if (esignlen && fill != '0') {
eb160463 10223 for (i = 0; i < (int)esignlen; i++)
46fc3d4c 10224 *p++ = esignbuf[i];
10225 }
fc36a67e 10226 if (zeros) {
10227 for (i = zeros; i; i--)
10228 *p++ = '0';
10229 }
46fc3d4c 10230 if (elen) {
4459522c 10231 Copy(eptr, p, elen, char);
46fc3d4c 10232 p += elen;
10233 }
10234 if (gap && left) {
10235 memset(p, ' ', gap);
10236 p += gap;
10237 }
b22c7a20
GS
10238 if (vectorize) {
10239 if (veclen) {
4459522c 10240 Copy(dotstr, p, dotstrlen, char);
b22c7a20
GS
10241 p += dotstrlen;
10242 }
10243 else
10244 vectorize = FALSE; /* done iterating over vecstr */
10245 }
2cf2cfc6
A
10246 if (is_utf8)
10247 has_utf8 = TRUE;
10248 if (has_utf8)
7e2040f0 10249 SvUTF8_on(sv);
46fc3d4c 10250 *p = '\0';
3f7c398e 10251 SvCUR_set(sv, p - SvPVX_const(sv));
b22c7a20
GS
10252 if (vectorize) {
10253 esignlen = 0;
10254 goto vector;
10255 }
46fc3d4c 10256 }
10257}
51371543 10258
645c22ef
DM
10259/* =========================================================================
10260
10261=head1 Cloning an interpreter
10262
10263All the macros and functions in this section are for the private use of
10264the main function, perl_clone().
10265
10266The foo_dup() functions make an exact copy of an existing foo thinngy.
10267During the course of a cloning, a hash table is used to map old addresses
10268to new addresses. The table is created and manipulated with the
10269ptr_table_* functions.
10270
10271=cut
10272
10273============================================================================*/
10274
10275
1d7c1841
GS
10276#if defined(USE_ITHREADS)
10277
1d7c1841
GS
10278#ifndef GpREFCNT_inc
10279# define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
10280#endif
10281
10282
d2d73c3e
AB
10283#define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t))
10284#define av_dup(s,t) (AV*)sv_dup((SV*)s,t)
10285#define av_dup_inc(s,t) (AV*)SvREFCNT_inc(sv_dup((SV*)s,t))
10286#define hv_dup(s,t) (HV*)sv_dup((SV*)s,t)
10287#define hv_dup_inc(s,t) (HV*)SvREFCNT_inc(sv_dup((SV*)s,t))
10288#define cv_dup(s,t) (CV*)sv_dup((SV*)s,t)
10289#define cv_dup_inc(s,t) (CV*)SvREFCNT_inc(sv_dup((SV*)s,t))
10290#define io_dup(s,t) (IO*)sv_dup((SV*)s,t)
10291#define io_dup_inc(s,t) (IO*)SvREFCNT_inc(sv_dup((SV*)s,t))
10292#define gv_dup(s,t) (GV*)sv_dup((SV*)s,t)
10293#define gv_dup_inc(s,t) (GV*)SvREFCNT_inc(sv_dup((SV*)s,t))
1d7c1841
GS
10294#define SAVEPV(p) (p ? savepv(p) : Nullch)
10295#define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch)
8cf8f3d1 10296
d2d73c3e 10297
d2f185dc
AMS
10298/* Duplicate a regexp. Required reading: pregcomp() and pregfree() in
10299 regcomp.c. AMS 20010712 */
645c22ef 10300
1d7c1841 10301REGEXP *
a8fc9800 10302Perl_re_dup(pTHX_ REGEXP *r, CLONE_PARAMS *param)
1d7c1841 10303{
27da23d5 10304 dVAR;
d2f185dc
AMS
10305 REGEXP *ret;
10306 int i, len, npar;
10307 struct reg_substr_datum *s;
10308
10309 if (!r)
10310 return (REGEXP *)NULL;
10311
10312 if ((ret = (REGEXP *)ptr_table_fetch(PL_ptr_table, r)))
10313 return ret;
10314
10315 len = r->offsets[0];
10316 npar = r->nparens+1;
10317
10318 Newc(0, ret, sizeof(regexp) + (len+1)*sizeof(regnode), char, regexp);
10319 Copy(r->program, ret->program, len+1, regnode);
10320
10321 New(0, ret->startp, npar, I32);
10322 Copy(r->startp, ret->startp, npar, I32);
10323 New(0, ret->endp, npar, I32);
10324 Copy(r->startp, ret->startp, npar, I32);
10325
d2f185dc
AMS
10326 New(0, ret->substrs, 1, struct reg_substr_data);
10327 for (s = ret->substrs->data, i = 0; i < 3; i++, s++) {
10328 s->min_offset = r->substrs->data[i].min_offset;
10329 s->max_offset = r->substrs->data[i].max_offset;
10330 s->substr = sv_dup_inc(r->substrs->data[i].substr, param);
33b8afdf 10331 s->utf8_substr = sv_dup_inc(r->substrs->data[i].utf8_substr, param);
d2f185dc
AMS
10332 }
10333
70612e96 10334 ret->regstclass = NULL;
d2f185dc
AMS
10335 if (r->data) {
10336 struct reg_data *d;
e1ec3a88 10337 const int count = r->data->count;
d2f185dc
AMS
10338
10339 Newc(0, d, sizeof(struct reg_data) + count*sizeof(void *),
10340 char, struct reg_data);
10341 New(0, d->what, count, U8);
10342
10343 d->count = count;
10344 for (i = 0; i < count; i++) {
10345 d->what[i] = r->data->what[i];
10346 switch (d->what[i]) {
a3621e74
YO
10347 /* legal options are one of: sfpont
10348 see also regcomp.h and pregfree() */
d2f185dc
AMS
10349 case 's':
10350 d->data[i] = sv_dup_inc((SV *)r->data->data[i], param);
10351 break;
10352 case 'p':
10353 d->data[i] = av_dup_inc((AV *)r->data->data[i], param);
10354 break;
10355 case 'f':
10356 /* This is cheating. */
10357 New(0, d->data[i], 1, struct regnode_charclass_class);
10358 StructCopy(r->data->data[i], d->data[i],
10359 struct regnode_charclass_class);
70612e96 10360 ret->regstclass = (regnode*)d->data[i];
d2f185dc
AMS
10361 break;
10362 case 'o':
33773810
AMS
10363 /* Compiled op trees are readonly, and can thus be
10364 shared without duplication. */
b34c0dd4 10365 OP_REFCNT_LOCK;
9b978d73 10366 d->data[i] = (void*)OpREFCNT_inc((OP*)r->data->data[i]);
b34c0dd4 10367 OP_REFCNT_UNLOCK;
9b978d73 10368 break;
d2f185dc
AMS
10369 case 'n':
10370 d->data[i] = r->data->data[i];
10371 break;
a3621e74
YO
10372 case 't':
10373 d->data[i] = r->data->data[i];
10374 OP_REFCNT_LOCK;
10375 ((reg_trie_data*)d->data[i])->refcount++;
10376 OP_REFCNT_UNLOCK;
10377 break;
10378 default:
10379 Perl_croak(aTHX_ "panic: re_dup unknown data code '%c'", r->data->what[i]);
d2f185dc
AMS
10380 }
10381 }
10382
10383 ret->data = d;
10384 }
10385 else
10386 ret->data = NULL;
10387
10388 New(0, ret->offsets, 2*len+1, U32);
10389 Copy(r->offsets, ret->offsets, 2*len+1, U32);
10390
e01c5899 10391 ret->precomp = SAVEPVN(r->precomp, r->prelen);
d2f185dc
AMS
10392 ret->refcnt = r->refcnt;
10393 ret->minlen = r->minlen;
10394 ret->prelen = r->prelen;
10395 ret->nparens = r->nparens;
10396 ret->lastparen = r->lastparen;
10397 ret->lastcloseparen = r->lastcloseparen;
10398 ret->reganch = r->reganch;
10399
70612e96
RG
10400 ret->sublen = r->sublen;
10401
10402 if (RX_MATCH_COPIED(ret))
e01c5899 10403 ret->subbeg = SAVEPVN(r->subbeg, r->sublen);
70612e96
RG
10404 else
10405 ret->subbeg = Nullch;
f8c7b90f 10406#ifdef PERL_OLD_COPY_ON_WRITE
9a26048b
NC
10407 ret->saved_copy = Nullsv;
10408#endif
70612e96 10409
d2f185dc
AMS
10410 ptr_table_store(PL_ptr_table, r, ret);
10411 return ret;
1d7c1841
GS
10412}
10413
d2d73c3e 10414/* duplicate a file handle */
645c22ef 10415
1d7c1841 10416PerlIO *
a8fc9800 10417Perl_fp_dup(pTHX_ PerlIO *fp, char type, CLONE_PARAMS *param)
1d7c1841
GS
10418{
10419 PerlIO *ret;
73d840c0
AL
10420 (void)type;
10421
1d7c1841
GS
10422 if (!fp)
10423 return (PerlIO*)NULL;
10424
10425 /* look for it in the table first */
10426 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
10427 if (ret)
10428 return ret;
10429
10430 /* create anew and remember what it is */
ecdeb87c 10431 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
1d7c1841
GS
10432 ptr_table_store(PL_ptr_table, fp, ret);
10433 return ret;
10434}
10435
645c22ef
DM
10436/* duplicate a directory handle */
10437
1d7c1841
GS
10438DIR *
10439Perl_dirp_dup(pTHX_ DIR *dp)
10440{
10441 if (!dp)
10442 return (DIR*)NULL;
10443 /* XXX TODO */
10444 return dp;
10445}
10446
ff276b08 10447/* duplicate a typeglob */
645c22ef 10448
1d7c1841 10449GP *
a8fc9800 10450Perl_gp_dup(pTHX_ GP *gp, CLONE_PARAMS* param)
1d7c1841
GS
10451{
10452 GP *ret;
10453 if (!gp)
10454 return (GP*)NULL;
10455 /* look for it in the table first */
10456 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
10457 if (ret)
10458 return ret;
10459
10460 /* create anew and remember what it is */
10461 Newz(0, ret, 1, GP);
10462 ptr_table_store(PL_ptr_table, gp, ret);
10463
10464 /* clone */
10465 ret->gp_refcnt = 0; /* must be before any other dups! */
d2d73c3e
AB
10466 ret->gp_sv = sv_dup_inc(gp->gp_sv, param);
10467 ret->gp_io = io_dup_inc(gp->gp_io, param);
10468 ret->gp_form = cv_dup_inc(gp->gp_form, param);
10469 ret->gp_av = av_dup_inc(gp->gp_av, param);
10470 ret->gp_hv = hv_dup_inc(gp->gp_hv, param);
10471 ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
10472 ret->gp_cv = cv_dup_inc(gp->gp_cv, param);
1d7c1841
GS
10473 ret->gp_cvgen = gp->gp_cvgen;
10474 ret->gp_flags = gp->gp_flags;
10475 ret->gp_line = gp->gp_line;
10476 ret->gp_file = gp->gp_file; /* points to COP.cop_file */
10477 return ret;
10478}
10479
645c22ef
DM
10480/* duplicate a chain of magic */
10481
1d7c1841 10482MAGIC *
a8fc9800 10483Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS* param)
1d7c1841 10484{
cb359b41
JH
10485 MAGIC *mgprev = (MAGIC*)NULL;
10486 MAGIC *mgret;
1d7c1841
GS
10487 if (!mg)
10488 return (MAGIC*)NULL;
10489 /* look for it in the table first */
10490 mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg);
10491 if (mgret)
10492 return mgret;
10493
10494 for (; mg; mg = mg->mg_moremagic) {
10495 MAGIC *nmg;
10496 Newz(0, nmg, 1, MAGIC);
cb359b41 10497 if (mgprev)
1d7c1841 10498 mgprev->mg_moremagic = nmg;
cb359b41
JH
10499 else
10500 mgret = nmg;
1d7c1841
GS
10501 nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */
10502 nmg->mg_private = mg->mg_private;
10503 nmg->mg_type = mg->mg_type;
10504 nmg->mg_flags = mg->mg_flags;
14befaf4 10505 if (mg->mg_type == PERL_MAGIC_qr) {
d2f185dc 10506 nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj, param);
1d7c1841 10507 }
05bd4103 10508 else if(mg->mg_type == PERL_MAGIC_backref) {
7fc63493 10509 const AV * const av = (AV*) mg->mg_obj;
fdc9a813
AE
10510 SV **svp;
10511 I32 i;
7fc63493 10512 (void)SvREFCNT_inc(nmg->mg_obj = (SV*)newAV());
fdc9a813
AE
10513 svp = AvARRAY(av);
10514 for (i = AvFILLp(av); i >= 0; i--) {
3a81978b 10515 if (!svp[i]) continue;
fdc9a813
AE
10516 av_push((AV*)nmg->mg_obj,sv_dup(svp[i],param));
10517 }
05bd4103 10518 }
8d2f4536
NC
10519 else if (mg->mg_type == PERL_MAGIC_symtab) {
10520 nmg->mg_obj = mg->mg_obj;
10521 }
1d7c1841
GS
10522 else {
10523 nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED)
d2d73c3e
AB
10524 ? sv_dup_inc(mg->mg_obj, param)
10525 : sv_dup(mg->mg_obj, param);
1d7c1841
GS
10526 }
10527 nmg->mg_len = mg->mg_len;
10528 nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */
14befaf4 10529 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
68795e93 10530 if (mg->mg_len > 0) {
1d7c1841 10531 nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len);
14befaf4
DM
10532 if (mg->mg_type == PERL_MAGIC_overload_table &&
10533 AMT_AMAGIC((AMT*)mg->mg_ptr))
10534 {
1d7c1841
GS
10535 AMT *amtp = (AMT*)mg->mg_ptr;
10536 AMT *namtp = (AMT*)nmg->mg_ptr;
10537 I32 i;
10538 for (i = 1; i < NofAMmeth; i++) {
d2d73c3e 10539 namtp->table[i] = cv_dup_inc(amtp->table[i], param);
1d7c1841
GS
10540 }
10541 }
10542 }
10543 else if (mg->mg_len == HEf_SVKEY)
d2d73c3e 10544 nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr, param);
1d7c1841 10545 }
68795e93
NIS
10546 if ((mg->mg_flags & MGf_DUP) && mg->mg_virtual && mg->mg_virtual->svt_dup) {
10547 CALL_FPTR(nmg->mg_virtual->svt_dup)(aTHX_ nmg, param);
10548 }
1d7c1841
GS
10549 mgprev = nmg;
10550 }
10551 return mgret;
10552}
10553
645c22ef
DM
10554/* create a new pointer-mapping table */
10555
1d7c1841
GS
10556PTR_TBL_t *
10557Perl_ptr_table_new(pTHX)
10558{
10559 PTR_TBL_t *tbl;
10560 Newz(0, tbl, 1, PTR_TBL_t);
10561 tbl->tbl_max = 511;
10562 tbl->tbl_items = 0;
10563 Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
10564 return tbl;
10565}
10566
134ca3d6
DM
10567#if (PTRSIZE == 8)
10568# define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 3)
10569#else
10570# define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 2)
10571#endif
10572
32e691d0
NC
10573
10574
10575STATIC void
10576S_more_pte(pTHX)
10577{
cac9b346
NC
10578 struct ptr_tbl_ent* pte;
10579 struct ptr_tbl_ent* pteend;
c3929b72
NC
10580 New(0, pte, PERL_ARENA_SIZE/sizeof(struct ptr_tbl_ent), struct ptr_tbl_ent);
10581 pte->next = PL_pte_arenaroot;
10582 PL_pte_arenaroot = pte;
32e691d0 10583
9c17f24a 10584 pteend = &pte[PERL_ARENA_SIZE / sizeof(struct ptr_tbl_ent) - 1];
32e691d0
NC
10585 PL_pte_root = ++pte;
10586 while (pte < pteend) {
10587 pte->next = pte + 1;
10588 pte++;
10589 }
10590 pte->next = 0;
10591}
10592
10593STATIC struct ptr_tbl_ent*
10594S_new_pte(pTHX)
10595{
10596 struct ptr_tbl_ent* pte;
10597 if (!PL_pte_root)
10598 S_more_pte(aTHX);
10599 pte = PL_pte_root;
10600 PL_pte_root = pte->next;
10601 return pte;
10602}
10603
10604STATIC void
10605S_del_pte(pTHX_ struct ptr_tbl_ent*p)
10606{
10607 p->next = PL_pte_root;
10608 PL_pte_root = p;
10609}
10610
645c22ef
DM
10611/* map an existing pointer using a table */
10612
1d7c1841
GS
10613void *
10614Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv)
10615{
10616 PTR_TBL_ENT_t *tblent;
4373e329 10617 const UV hash = PTR_TABLE_HASH(sv);
1d7c1841
GS
10618 assert(tbl);
10619 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
10620 for (; tblent; tblent = tblent->next) {
10621 if (tblent->oldval == sv)
10622 return tblent->newval;
10623 }
10624 return (void*)NULL;
10625}
10626
645c22ef
DM
10627/* add a new entry to a pointer-mapping table */
10628
1d7c1841
GS
10629void
10630Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv)
10631{
10632 PTR_TBL_ENT_t *tblent, **otblent;
10633 /* XXX this may be pessimal on platforms where pointers aren't good
10634 * hash values e.g. if they grow faster in the most significant
10635 * bits */
4373e329 10636 const UV hash = PTR_TABLE_HASH(oldv);
14cade97 10637 bool empty = 1;
1d7c1841
GS
10638
10639 assert(tbl);
10640 otblent = &tbl->tbl_ary[hash & tbl->tbl_max];
14cade97 10641 for (tblent = *otblent; tblent; empty=0, tblent = tblent->next) {
1d7c1841
GS
10642 if (tblent->oldval == oldv) {
10643 tblent->newval = newv;
1d7c1841
GS
10644 return;
10645 }
10646 }
32e691d0 10647 tblent = S_new_pte(aTHX);
1d7c1841
GS
10648 tblent->oldval = oldv;
10649 tblent->newval = newv;
10650 tblent->next = *otblent;
10651 *otblent = tblent;
10652 tbl->tbl_items++;
14cade97 10653 if (!empty && tbl->tbl_items > tbl->tbl_max)
1d7c1841
GS
10654 ptr_table_split(tbl);
10655}
10656
645c22ef
DM
10657/* double the hash bucket size of an existing ptr table */
10658
1d7c1841
GS
10659void
10660Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl)
10661{
10662 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
4373e329 10663 const UV oldsize = tbl->tbl_max + 1;
1d7c1841
GS
10664 UV newsize = oldsize * 2;
10665 UV i;
10666
10667 Renew(ary, newsize, PTR_TBL_ENT_t*);
10668 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
10669 tbl->tbl_max = --newsize;
10670 tbl->tbl_ary = ary;
10671 for (i=0; i < oldsize; i++, ary++) {
10672 PTR_TBL_ENT_t **curentp, **entp, *ent;
10673 if (!*ary)
10674 continue;
10675 curentp = ary + oldsize;
10676 for (entp = ary, ent = *ary; ent; ent = *entp) {
134ca3d6 10677 if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
1d7c1841
GS
10678 *entp = ent->next;
10679 ent->next = *curentp;
10680 *curentp = ent;
10681 continue;
10682 }
10683 else
10684 entp = &ent->next;
10685 }
10686 }
10687}
10688
645c22ef
DM
10689/* remove all the entries from a ptr table */
10690
a0739874
DM
10691void
10692Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl)
10693{
10694 register PTR_TBL_ENT_t **array;
10695 register PTR_TBL_ENT_t *entry;
a0739874
DM
10696 UV riter = 0;
10697 UV max;
10698
10699 if (!tbl || !tbl->tbl_items) {
10700 return;
10701 }
10702
10703 array = tbl->tbl_ary;
10704 entry = array[0];
10705 max = tbl->tbl_max;
10706
10707 for (;;) {
10708 if (entry) {
4373e329 10709 PTR_TBL_ENT_t *oentry = entry;
a0739874 10710 entry = entry->next;
32e691d0 10711 S_del_pte(aTHX_ oentry);
a0739874
DM
10712 }
10713 if (!entry) {
10714 if (++riter > max) {
10715 break;
10716 }
10717 entry = array[riter];
10718 }
10719 }
10720
10721 tbl->tbl_items = 0;
10722}
10723
645c22ef
DM
10724/* clear and free a ptr table */
10725
a0739874
DM
10726void
10727Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl)
10728{
10729 if (!tbl) {
10730 return;
10731 }
10732 ptr_table_clear(tbl);
10733 Safefree(tbl->tbl_ary);
10734 Safefree(tbl);
10735}
10736
645c22ef
DM
10737/* attempt to make everything in the typeglob readonly */
10738
5bd07a3d 10739STATIC SV *
59b40662 10740S_gv_share(pTHX_ SV *sstr, CLONE_PARAMS *param)
5bd07a3d
DM
10741{
10742 GV *gv = (GV*)sstr;
59b40662 10743 SV *sv = &param->proto_perl->Isv_no; /* just need SvREADONLY-ness */
5bd07a3d
DM
10744
10745 if (GvIO(gv) || GvFORM(gv)) {
7fb37951 10746 GvUNIQUE_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */
5bd07a3d
DM
10747 }
10748 else if (!GvCV(gv)) {
10749 GvCV(gv) = (CV*)sv;
10750 }
10751 else {
10752 /* CvPADLISTs cannot be shared */
37e20706 10753 if (!SvREADONLY(GvCV(gv)) && !CvXSUB(GvCV(gv))) {
7fb37951 10754 GvUNIQUE_off(gv);
5bd07a3d
DM
10755 }
10756 }
10757
7fb37951 10758 if (!GvUNIQUE(gv)) {
5bd07a3d
DM
10759#if 0
10760 PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n",
bfcb3514 10761 HvNAME_get(GvSTASH(gv)), GvNAME(gv));
5bd07a3d
DM
10762#endif
10763 return Nullsv;
10764 }
10765
4411f3b6 10766 /*
5bd07a3d
DM
10767 * write attempts will die with
10768 * "Modification of a read-only value attempted"
10769 */
10770 if (!GvSV(gv)) {
10771 GvSV(gv) = sv;
10772 }
10773 else {
10774 SvREADONLY_on(GvSV(gv));
10775 }
10776
10777 if (!GvAV(gv)) {
10778 GvAV(gv) = (AV*)sv;
10779 }
10780 else {
10781 SvREADONLY_on(GvAV(gv));
10782 }
10783
10784 if (!GvHV(gv)) {
10785 GvHV(gv) = (HV*)sv;
10786 }
10787 else {
53c33732 10788 SvREADONLY_on(GvHV(gv));
5bd07a3d
DM
10789 }
10790
10791 return sstr; /* he_dup() will SvREFCNT_inc() */
10792}
10793
645c22ef
DM
10794/* duplicate an SV of any type (including AV, HV etc) */
10795
83841fad
NIS
10796void
10797Perl_rvpv_dup(pTHX_ SV *dstr, SV *sstr, CLONE_PARAMS* param)
10798{
10799 if (SvROK(sstr)) {
b162af07
SP
10800 SvRV_set(dstr, SvWEAKREF(sstr)
10801 ? sv_dup(SvRV(sstr), param)
10802 : sv_dup_inc(SvRV(sstr), param));
f880fe2f 10803
83841fad 10804 }
3f7c398e 10805 else if (SvPVX_const(sstr)) {
83841fad
NIS
10806 /* Has something there */
10807 if (SvLEN(sstr)) {
68795e93 10808 /* Normal PV - clone whole allocated space */
3f7c398e 10809 SvPV_set(dstr, SAVEPVN(SvPVX_const(sstr), SvLEN(sstr)-1));
d3d0e6f1
NC
10810 if (SvREADONLY(sstr) && SvFAKE(sstr)) {
10811 /* Not that normal - actually sstr is copy on write.
10812 But we are a true, independant SV, so: */
10813 SvREADONLY_off(dstr);
10814 SvFAKE_off(dstr);
10815 }
68795e93 10816 }
83841fad
NIS
10817 else {
10818 /* Special case - not normally malloced for some reason */
ef10be65
NC
10819 if ((SvREADONLY(sstr) && SvFAKE(sstr))) {
10820 /* A "shared" PV - clone it as "shared" PV */
10821 SvPV_set(dstr,
10822 HEK_KEY(hek_dup(SvSHARED_HEK_FROM_PV(SvPVX_const(sstr)),
10823 param)));
83841fad
NIS
10824 }
10825 else {
10826 /* Some other special case - random pointer */
f880fe2f 10827 SvPV_set(dstr, SvPVX(sstr));
d3d0e6f1 10828 }
83841fad
NIS
10829 }
10830 }
10831 else {
10832 /* Copy the Null */
f880fe2f 10833 if (SvTYPE(dstr) == SVt_RV)
b162af07 10834 SvRV_set(dstr, NULL);
f880fe2f
SP
10835 else
10836 SvPV_set(dstr, 0);
83841fad
NIS
10837 }
10838}
10839
1d7c1841 10840SV *
a8fc9800 10841Perl_sv_dup(pTHX_ SV *sstr, CLONE_PARAMS* param)
1d7c1841 10842{
27da23d5 10843 dVAR;
1d7c1841
GS
10844 SV *dstr;
10845
10846 if (!sstr || SvTYPE(sstr) == SVTYPEMASK)
10847 return Nullsv;
10848 /* look for it in the table first */
10849 dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr);
10850 if (dstr)
10851 return dstr;
10852
0405e91e
AB
10853 if(param->flags & CLONEf_JOIN_IN) {
10854 /** We are joining here so we don't want do clone
10855 something that is bad **/
bfcb3514 10856 const char *hvname;
0405e91e
AB
10857
10858 if(SvTYPE(sstr) == SVt_PVHV &&
bfcb3514 10859 (hvname = HvNAME_get(sstr))) {
0405e91e 10860 /** don't clone stashes if they already exist **/
bfcb3514 10861 HV* old_stash = gv_stashpv(hvname,0);
0405e91e
AB
10862 return (SV*) old_stash;
10863 }
10864 }
10865
1d7c1841
GS
10866 /* create anew and remember what it is */
10867 new_SV(dstr);
fd0854ff
DM
10868
10869#ifdef DEBUG_LEAKING_SCALARS
10870 dstr->sv_debug_optype = sstr->sv_debug_optype;
10871 dstr->sv_debug_line = sstr->sv_debug_line;
10872 dstr->sv_debug_inpad = sstr->sv_debug_inpad;
10873 dstr->sv_debug_cloned = 1;
10874# ifdef NETWARE
10875 dstr->sv_debug_file = savepv(sstr->sv_debug_file);
10876# else
10877 dstr->sv_debug_file = savesharedpv(sstr->sv_debug_file);
10878# endif
10879#endif
10880
1d7c1841
GS
10881 ptr_table_store(PL_ptr_table, sstr, dstr);
10882
10883 /* clone */
10884 SvFLAGS(dstr) = SvFLAGS(sstr);
10885 SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */
10886 SvREFCNT(dstr) = 0; /* must be before any other dups! */
10887
10888#ifdef DEBUGGING
3f7c398e 10889 if (SvANY(sstr) && PL_watch_pvx && SvPVX_const(sstr) == PL_watch_pvx)
1d7c1841 10890 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
3f7c398e 10891 PL_watch_pvx, SvPVX_const(sstr));
1d7c1841
GS
10892#endif
10893
9660f481
DM
10894 /* don't clone objects whose class has asked us not to */
10895 if (SvOBJECT(sstr) && ! (SvFLAGS(SvSTASH(sstr)) & SVphv_CLONEABLE)) {
10896 SvFLAGS(dstr) &= ~SVTYPEMASK;
10897 SvOBJECT_off(dstr);
10898 return dstr;
10899 }
10900
1d7c1841
GS
10901 switch (SvTYPE(sstr)) {
10902 case SVt_NULL:
10903 SvANY(dstr) = NULL;
10904 break;
10905 case SVt_IV:
339049b0 10906 SvANY(dstr) = (XPVIV*)((char*)&(dstr->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
45977657 10907 SvIV_set(dstr, SvIVX(sstr));
1d7c1841
GS
10908 break;
10909 case SVt_NV:
10910 SvANY(dstr) = new_XNV();
9d6ce603 10911 SvNV_set(dstr, SvNVX(sstr));
1d7c1841
GS
10912 break;
10913 case SVt_RV:
339049b0 10914 SvANY(dstr) = &(dstr->sv_u.svu_rv);
83841fad 10915 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10916 break;
10917 case SVt_PV:
10918 SvANY(dstr) = new_XPV();
b162af07
SP
10919 SvCUR_set(dstr, SvCUR(sstr));
10920 SvLEN_set(dstr, SvLEN(sstr));
83841fad 10921 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10922 break;
10923 case SVt_PVIV:
10924 SvANY(dstr) = new_XPVIV();
b162af07
SP
10925 SvCUR_set(dstr, SvCUR(sstr));
10926 SvLEN_set(dstr, SvLEN(sstr));
45977657 10927 SvIV_set(dstr, SvIVX(sstr));
83841fad 10928 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10929 break;
10930 case SVt_PVNV:
10931 SvANY(dstr) = new_XPVNV();
b162af07
SP
10932 SvCUR_set(dstr, SvCUR(sstr));
10933 SvLEN_set(dstr, SvLEN(sstr));
45977657 10934 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 10935 SvNV_set(dstr, SvNVX(sstr));
83841fad 10936 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10937 break;
10938 case SVt_PVMG:
10939 SvANY(dstr) = new_XPVMG();
b162af07
SP
10940 SvCUR_set(dstr, SvCUR(sstr));
10941 SvLEN_set(dstr, SvLEN(sstr));
45977657 10942 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 10943 SvNV_set(dstr, SvNVX(sstr));
b162af07
SP
10944 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10945 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10946 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10947 break;
10948 case SVt_PVBM:
10949 SvANY(dstr) = new_XPVBM();
b162af07
SP
10950 SvCUR_set(dstr, SvCUR(sstr));
10951 SvLEN_set(dstr, SvLEN(sstr));
45977657 10952 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 10953 SvNV_set(dstr, SvNVX(sstr));
b162af07
SP
10954 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10955 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10956 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10957 BmRARE(dstr) = BmRARE(sstr);
10958 BmUSEFUL(dstr) = BmUSEFUL(sstr);
10959 BmPREVIOUS(dstr)= BmPREVIOUS(sstr);
10960 break;
10961 case SVt_PVLV:
10962 SvANY(dstr) = new_XPVLV();
b162af07
SP
10963 SvCUR_set(dstr, SvCUR(sstr));
10964 SvLEN_set(dstr, SvLEN(sstr));
45977657 10965 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 10966 SvNV_set(dstr, SvNVX(sstr));
b162af07
SP
10967 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10968 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10969 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10970 LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */
10971 LvTARGLEN(dstr) = LvTARGLEN(sstr);
dd28f7bb
DM
10972 if (LvTYPE(sstr) == 't') /* for tie: unrefcnted fake (SV**) */
10973 LvTARG(dstr) = dstr;
10974 else if (LvTYPE(sstr) == 'T') /* for tie: fake HE */
10975 LvTARG(dstr) = (SV*)he_dup((HE*)LvTARG(sstr), 0, param);
10976 else
10977 LvTARG(dstr) = sv_dup_inc(LvTARG(sstr), param);
1d7c1841
GS
10978 LvTYPE(dstr) = LvTYPE(sstr);
10979 break;
10980 case SVt_PVGV:
7fb37951 10981 if (GvUNIQUE((GV*)sstr)) {
5bd07a3d 10982 SV *share;
59b40662 10983 if ((share = gv_share(sstr, param))) {
5bd07a3d
DM
10984 del_SV(dstr);
10985 dstr = share;
37e20706 10986 ptr_table_store(PL_ptr_table, sstr, dstr);
5bd07a3d
DM
10987#if 0
10988 PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n",
bfcb3514 10989 HvNAME_get(GvSTASH(share)), GvNAME(share));
5bd07a3d
DM
10990#endif
10991 break;
10992 }
10993 }
1d7c1841 10994 SvANY(dstr) = new_XPVGV();
b162af07
SP
10995 SvCUR_set(dstr, SvCUR(sstr));
10996 SvLEN_set(dstr, SvLEN(sstr));
45977657 10997 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 10998 SvNV_set(dstr, SvNVX(sstr));
b162af07
SP
10999 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
11000 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 11001 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
11002 GvNAMELEN(dstr) = GvNAMELEN(sstr);
11003 GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr));
d2d73c3e 11004 GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr), param);
1d7c1841 11005 GvFLAGS(dstr) = GvFLAGS(sstr);
d2d73c3e 11006 GvGP(dstr) = gp_dup(GvGP(sstr), param);
1d7c1841
GS
11007 (void)GpREFCNT_inc(GvGP(dstr));
11008 break;
11009 case SVt_PVIO:
11010 SvANY(dstr) = new_XPVIO();
b162af07
SP
11011 SvCUR_set(dstr, SvCUR(sstr));
11012 SvLEN_set(dstr, SvLEN(sstr));
45977657 11013 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 11014 SvNV_set(dstr, SvNVX(sstr));
b162af07
SP
11015 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
11016 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 11017 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
a8fc9800 11018 IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr), param);
1d7c1841
GS
11019 if (IoOFP(sstr) == IoIFP(sstr))
11020 IoOFP(dstr) = IoIFP(dstr);
11021 else
a8fc9800 11022 IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr), param);
1d7c1841
GS
11023 /* PL_rsfp_filters entries have fake IoDIRP() */
11024 if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP))
11025 IoDIRP(dstr) = dirp_dup(IoDIRP(sstr));
11026 else
11027 IoDIRP(dstr) = IoDIRP(sstr);
11028 IoLINES(dstr) = IoLINES(sstr);
11029 IoPAGE(dstr) = IoPAGE(sstr);
11030 IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr);
11031 IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr);
7a5fa8a2 11032 if(IoFLAGS(sstr) & IOf_FAKE_DIRP) {
5a37521b
AB
11033 /* I have no idea why fake dirp (rsfps)
11034 should be treaded differently but otherwise
11035 we end up with leaks -- sky*/
11036 IoTOP_GV(dstr) = gv_dup_inc(IoTOP_GV(sstr), param);
11037 IoFMT_GV(dstr) = gv_dup_inc(IoFMT_GV(sstr), param);
11038 IoBOTTOM_GV(dstr) = gv_dup_inc(IoBOTTOM_GV(sstr), param);
11039 } else {
11040 IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr), param);
11041 IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr), param);
11042 IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr), param);
11043 }
1d7c1841 11044 IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr));
1d7c1841 11045 IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr));
1d7c1841 11046 IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr));
1d7c1841
GS
11047 IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr);
11048 IoTYPE(dstr) = IoTYPE(sstr);
11049 IoFLAGS(dstr) = IoFLAGS(sstr);
11050 break;
11051 case SVt_PVAV:
11052 SvANY(dstr) = new_XPVAV();
b162af07
SP
11053 SvCUR_set(dstr, SvCUR(sstr));
11054 SvLEN_set(dstr, SvLEN(sstr));
b162af07
SP
11055 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
11056 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
1d7c1841
GS
11057 if (AvARRAY((AV*)sstr)) {
11058 SV **dst_ary, **src_ary;
11059 SSize_t items = AvFILLp((AV*)sstr) + 1;
11060
11061 src_ary = AvARRAY((AV*)sstr);
11062 Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*);
11063 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
f880fe2f 11064 SvPV_set(dstr, (char*)dst_ary);
1d7c1841
GS
11065 AvALLOC((AV*)dstr) = dst_ary;
11066 if (AvREAL((AV*)sstr)) {
11067 while (items-- > 0)
d2d73c3e 11068 *dst_ary++ = sv_dup_inc(*src_ary++, param);
1d7c1841
GS
11069 }
11070 else {
11071 while (items-- > 0)
d2d73c3e 11072 *dst_ary++ = sv_dup(*src_ary++, param);
1d7c1841
GS
11073 }
11074 items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr);
11075 while (items-- > 0) {
11076 *dst_ary++ = &PL_sv_undef;
11077 }
11078 }
11079 else {
f880fe2f 11080 SvPV_set(dstr, Nullch);
1d7c1841
GS
11081 AvALLOC((AV*)dstr) = (SV**)NULL;
11082 }
11083 break;
11084 case SVt_PVHV:
11085 SvANY(dstr) = new_XPVHV();
b162af07
SP
11086 SvCUR_set(dstr, SvCUR(sstr));
11087 SvLEN_set(dstr, SvLEN(sstr));
94a66813 11088 HvTOTALKEYS(dstr) = HvTOTALKEYS(sstr);
b162af07
SP
11089 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
11090 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
bfcb3514 11091 {
7423f6db 11092 HEK *hvname = 0;
bfcb3514 11093
bfcb3514
NC
11094 if (HvARRAY((HV*)sstr)) {
11095 STRLEN i = 0;
616d8c9c
AL
11096 const bool sharekeys = !!HvSHAREKEYS(sstr);
11097 XPVHV * const dxhv = (XPVHV*)SvANY(dstr);
11098 XPVHV * const sxhv = (XPVHV*)SvANY(sstr);
7b2c381c 11099 char *darray;
a1cfa1c6 11100 New(0, darray,
b79f7545
NC
11101 PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1)
11102 + (SvOOK(sstr) ? sizeof(struct xpvhv_aux) : 0), char);
7b2c381c 11103 HvARRAY(dstr) = (HE**)darray;
bfcb3514 11104 while (i <= sxhv->xhv_max) {
a1cfa1c6 11105 HE *source = HvARRAY(sstr)[i];
7b2c381c 11106 HvARRAY(dstr)[i]
a1cfa1c6 11107 = source ? he_dup(source, sharekeys, param) : 0;
bfcb3514
NC
11108 ++i;
11109 }
b79f7545
NC
11110 if (SvOOK(sstr)) {
11111 struct xpvhv_aux *saux = HvAUX(sstr);
11112 struct xpvhv_aux *daux = HvAUX(dstr);
11113 /* This flag isn't copied. */
11114 /* SvOOK_on(hv) attacks the IV flags. */
11115 SvFLAGS(dstr) |= SVf_OOK;
11116
11117 hvname = saux->xhv_name;
11118 daux->xhv_name = hvname ? hek_dup(hvname, param) : hvname;
11119
11120 daux->xhv_riter = saux->xhv_riter;
11121 daux->xhv_eiter = saux->xhv_eiter
11122 ? he_dup(saux->xhv_eiter, (bool)!!HvSHAREKEYS(sstr),
11123 param) : 0;
11124 }
bfcb3514
NC
11125 }
11126 else {
11127 SvPV_set(dstr, Nullch);
bfcb3514
NC
11128 }
11129 /* Record stashes for possible cloning in Perl_clone(). */
11130 if(hvname)
11131 av_push(param->stashes, dstr);
1d7c1841 11132 }
1d7c1841
GS
11133 break;
11134 case SVt_PVFM:
11135 SvANY(dstr) = new_XPVFM();
11136 FmLINES(dstr) = FmLINES(sstr);
11137 goto dup_pvcv;
11138 /* NOTREACHED */
11139 case SVt_PVCV:
11140 SvANY(dstr) = new_XPVCV();
d2d73c3e 11141 dup_pvcv:
b162af07
SP
11142 SvCUR_set(dstr, SvCUR(sstr));
11143 SvLEN_set(dstr, SvLEN(sstr));
45977657 11144 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 11145 SvNV_set(dstr, SvNVX(sstr));
b162af07
SP
11146 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
11147 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 11148 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
d2d73c3e 11149 CvSTASH(dstr) = hv_dup(CvSTASH(sstr), param); /* NOTE: not refcounted */
1d7c1841 11150 CvSTART(dstr) = CvSTART(sstr);
b34c0dd4 11151 OP_REFCNT_LOCK;
1d7c1841 11152 CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr));
b34c0dd4 11153 OP_REFCNT_UNLOCK;
1d7c1841
GS
11154 CvXSUB(dstr) = CvXSUB(sstr);
11155 CvXSUBANY(dstr) = CvXSUBANY(sstr);
01485f8b
DM
11156 if (CvCONST(sstr)) {
11157 CvXSUBANY(dstr).any_ptr = GvUNIQUE(CvGV(sstr)) ?
11158 SvREFCNT_inc(CvXSUBANY(sstr).any_ptr) :
8f77bfdb 11159 sv_dup_inc((SV *)CvXSUBANY(sstr).any_ptr, param);
01485f8b 11160 }
b23f1a86
DM
11161 /* don't dup if copying back - CvGV isn't refcounted, so the
11162 * duped GV may never be freed. A bit of a hack! DAPM */
11163 CvGV(dstr) = (param->flags & CLONEf_JOIN_IN) ?
11164 Nullgv : gv_dup(CvGV(sstr), param) ;
d2d73c3e
AB
11165 if (param->flags & CLONEf_COPY_STACKS) {
11166 CvDEPTH(dstr) = CvDEPTH(sstr);
11167 } else {
11168 CvDEPTH(dstr) = 0;
11169 }
dd2155a4 11170 PAD_DUP(CvPADLIST(dstr), CvPADLIST(sstr), param);
7dafbf52
DM
11171 CvOUTSIDE_SEQ(dstr) = CvOUTSIDE_SEQ(sstr);
11172 CvOUTSIDE(dstr) =
11173 CvWEAKOUTSIDE(sstr)
11174 ? cv_dup( CvOUTSIDE(sstr), param)
11175 : cv_dup_inc(CvOUTSIDE(sstr), param);
1d7c1841 11176 CvFLAGS(dstr) = CvFLAGS(sstr);
54356c7d 11177 CvFILE(dstr) = CvXSUB(sstr) ? CvFILE(sstr) : SAVEPV(CvFILE(sstr));
1d7c1841
GS
11178 break;
11179 default:
c803eecc 11180 Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(sstr));
1d7c1841
GS
11181 break;
11182 }
11183
11184 if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO)
11185 ++PL_sv_objcount;
11186
11187 return dstr;
d2d73c3e 11188 }
1d7c1841 11189
645c22ef
DM
11190/* duplicate a context */
11191
1d7c1841 11192PERL_CONTEXT *
a8fc9800 11193Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
1d7c1841
GS
11194{
11195 PERL_CONTEXT *ncxs;
11196
11197 if (!cxs)
11198 return (PERL_CONTEXT*)NULL;
11199
11200 /* look for it in the table first */
11201 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
11202 if (ncxs)
11203 return ncxs;
11204
11205 /* create anew and remember what it is */
11206 Newz(56, ncxs, max + 1, PERL_CONTEXT);
11207 ptr_table_store(PL_ptr_table, cxs, ncxs);
11208
11209 while (ix >= 0) {
11210 PERL_CONTEXT *cx = &cxs[ix];
11211 PERL_CONTEXT *ncx = &ncxs[ix];
11212 ncx->cx_type = cx->cx_type;
11213 if (CxTYPE(cx) == CXt_SUBST) {
11214 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
11215 }
11216 else {
11217 ncx->blk_oldsp = cx->blk_oldsp;
11218 ncx->blk_oldcop = cx->blk_oldcop;
1d7c1841
GS
11219 ncx->blk_oldmarksp = cx->blk_oldmarksp;
11220 ncx->blk_oldscopesp = cx->blk_oldscopesp;
11221 ncx->blk_oldpm = cx->blk_oldpm;
11222 ncx->blk_gimme = cx->blk_gimme;
11223 switch (CxTYPE(cx)) {
11224 case CXt_SUB:
11225 ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0
d2d73c3e
AB
11226 ? cv_dup_inc(cx->blk_sub.cv, param)
11227 : cv_dup(cx->blk_sub.cv,param));
1d7c1841 11228 ncx->blk_sub.argarray = (cx->blk_sub.hasargs
d2d73c3e 11229 ? av_dup_inc(cx->blk_sub.argarray, param)
1d7c1841 11230 : Nullav);
d2d73c3e 11231 ncx->blk_sub.savearray = av_dup_inc(cx->blk_sub.savearray, param);
1d7c1841
GS
11232 ncx->blk_sub.olddepth = cx->blk_sub.olddepth;
11233 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
11234 ncx->blk_sub.lval = cx->blk_sub.lval;
f39bc417 11235 ncx->blk_sub.retop = cx->blk_sub.retop;
1d7c1841
GS
11236 break;
11237 case CXt_EVAL:
11238 ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval;
11239 ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type;
b47cad08 11240 ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv, param);
1d7c1841 11241 ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root;
d2d73c3e 11242 ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text, param);
f39bc417 11243 ncx->blk_eval.retop = cx->blk_eval.retop;
1d7c1841
GS
11244 break;
11245 case CXt_LOOP:
11246 ncx->blk_loop.label = cx->blk_loop.label;
11247 ncx->blk_loop.resetsp = cx->blk_loop.resetsp;
11248 ncx->blk_loop.redo_op = cx->blk_loop.redo_op;
11249 ncx->blk_loop.next_op = cx->blk_loop.next_op;
11250 ncx->blk_loop.last_op = cx->blk_loop.last_op;
11251 ncx->blk_loop.iterdata = (CxPADLOOP(cx)
11252 ? cx->blk_loop.iterdata
d2d73c3e 11253 : gv_dup((GV*)cx->blk_loop.iterdata, param));
f3548bdc
DM
11254 ncx->blk_loop.oldcomppad
11255 = (PAD*)ptr_table_fetch(PL_ptr_table,
11256 cx->blk_loop.oldcomppad);
d2d73c3e
AB
11257 ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave, param);
11258 ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval, param);
11259 ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary, param);
1d7c1841
GS
11260 ncx->blk_loop.iterix = cx->blk_loop.iterix;
11261 ncx->blk_loop.itermax = cx->blk_loop.itermax;
11262 break;
11263 case CXt_FORMAT:
d2d73c3e
AB
11264 ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv, param);
11265 ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv, param);
11266 ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv, param);
1d7c1841 11267 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
f39bc417 11268 ncx->blk_sub.retop = cx->blk_sub.retop;
1d7c1841
GS
11269 break;
11270 case CXt_BLOCK:
11271 case CXt_NULL:
11272 break;
11273 }
11274 }
11275 --ix;
11276 }
11277 return ncxs;
11278}
11279
645c22ef
DM
11280/* duplicate a stack info structure */
11281
1d7c1841 11282PERL_SI *
a8fc9800 11283Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
1d7c1841
GS
11284{
11285 PERL_SI *nsi;
11286
11287 if (!si)
11288 return (PERL_SI*)NULL;
11289
11290 /* look for it in the table first */
11291 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
11292 if (nsi)
11293 return nsi;
11294
11295 /* create anew and remember what it is */
11296 Newz(56, nsi, 1, PERL_SI);
11297 ptr_table_store(PL_ptr_table, si, nsi);
11298
d2d73c3e 11299 nsi->si_stack = av_dup_inc(si->si_stack, param);
1d7c1841
GS
11300 nsi->si_cxix = si->si_cxix;
11301 nsi->si_cxmax = si->si_cxmax;
d2d73c3e 11302 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
1d7c1841 11303 nsi->si_type = si->si_type;
d2d73c3e
AB
11304 nsi->si_prev = si_dup(si->si_prev, param);
11305 nsi->si_next = si_dup(si->si_next, param);
1d7c1841
GS
11306 nsi->si_markoff = si->si_markoff;
11307
11308 return nsi;
11309}
11310
11311#define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
11312#define TOPINT(ss,ix) ((ss)[ix].any_i32)
11313#define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
11314#define TOPLONG(ss,ix) ((ss)[ix].any_long)
11315#define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
11316#define TOPIV(ss,ix) ((ss)[ix].any_iv)
38d8b13e
HS
11317#define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool)
11318#define TOPBOOL(ss,ix) ((ss)[ix].any_bool)
1d7c1841
GS
11319#define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
11320#define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
11321#define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
11322#define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
11323#define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
11324#define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
11325
11326/* XXXXX todo */
11327#define pv_dup_inc(p) SAVEPV(p)
11328#define pv_dup(p) SAVEPV(p)
11329#define svp_dup_inc(p,pp) any_dup(p,pp)
11330
645c22ef
DM
11331/* map any object to the new equivent - either something in the
11332 * ptr table, or something in the interpreter structure
11333 */
11334
1d7c1841
GS
11335void *
11336Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl)
11337{
11338 void *ret;
11339
11340 if (!v)
11341 return (void*)NULL;
11342
11343 /* look for it in the table first */
11344 ret = ptr_table_fetch(PL_ptr_table, v);
11345 if (ret)
11346 return ret;
11347
11348 /* see if it is part of the interpreter structure */
11349 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
acfe0abc 11350 ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
05ec9bb3 11351 else {
1d7c1841 11352 ret = v;
05ec9bb3 11353 }
1d7c1841
GS
11354
11355 return ret;
11356}
11357
645c22ef
DM
11358/* duplicate the save stack */
11359
1d7c1841 11360ANY *
a8fc9800 11361Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
1d7c1841
GS
11362{
11363 ANY *ss = proto_perl->Tsavestack;
11364 I32 ix = proto_perl->Tsavestack_ix;
11365 I32 max = proto_perl->Tsavestack_max;
11366 ANY *nss;
11367 SV *sv;
11368 GV *gv;
11369 AV *av;
11370 HV *hv;
11371 void* ptr;
11372 int intval;
11373 long longval;
11374 GP *gp;
11375 IV iv;
c4e33207 11376 char *c = NULL;
1d7c1841 11377 void (*dptr) (void*);
acfe0abc 11378 void (*dxptr) (pTHX_ void*);
e977893f 11379 OP *o;
1d7c1841
GS
11380
11381 Newz(54, nss, max, ANY);
11382
11383 while (ix > 0) {
b464bac0 11384 I32 i = POPINT(ss,ix);
1d7c1841
GS
11385 TOPINT(nss,ix) = i;
11386 switch (i) {
11387 case SAVEt_ITEM: /* normal string */
11388 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11389 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 11390 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11391 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
11392 break;
11393 case SAVEt_SV: /* scalar reference */
11394 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11395 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 11396 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 11397 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
1d7c1841 11398 break;
f4dd75d9
GS
11399 case SAVEt_GENERIC_PVREF: /* generic char* */
11400 c = (char*)POPPTR(ss,ix);
11401 TOPPTR(nss,ix) = pv_dup(c);
11402 ptr = POPPTR(ss,ix);
11403 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11404 break;
05ec9bb3
NIS
11405 case SAVEt_SHARED_PVREF: /* char* in shared space */
11406 c = (char*)POPPTR(ss,ix);
11407 TOPPTR(nss,ix) = savesharedpv(c);
11408 ptr = POPPTR(ss,ix);
11409 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11410 break;
1d7c1841
GS
11411 case SAVEt_GENERIC_SVREF: /* generic sv */
11412 case SAVEt_SVREF: /* scalar reference */
11413 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11414 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
11415 ptr = POPPTR(ss,ix);
11416 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
11417 break;
11418 case SAVEt_AV: /* array reference */
11419 av = (AV*)POPPTR(ss,ix);
d2d73c3e 11420 TOPPTR(nss,ix) = av_dup_inc(av, param);
1d7c1841 11421 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 11422 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
11423 break;
11424 case SAVEt_HV: /* hash reference */
11425 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 11426 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841 11427 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 11428 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
11429 break;
11430 case SAVEt_INT: /* int reference */
11431 ptr = POPPTR(ss,ix);
11432 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11433 intval = (int)POPINT(ss,ix);
11434 TOPINT(nss,ix) = intval;
11435 break;
11436 case SAVEt_LONG: /* long reference */
11437 ptr = POPPTR(ss,ix);
11438 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11439 longval = (long)POPLONG(ss,ix);
11440 TOPLONG(nss,ix) = longval;
11441 break;
11442 case SAVEt_I32: /* I32 reference */
11443 case SAVEt_I16: /* I16 reference */
11444 case SAVEt_I8: /* I8 reference */
11445 ptr = POPPTR(ss,ix);
11446 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11447 i = POPINT(ss,ix);
11448 TOPINT(nss,ix) = i;
11449 break;
11450 case SAVEt_IV: /* IV reference */
11451 ptr = POPPTR(ss,ix);
11452 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11453 iv = POPIV(ss,ix);
11454 TOPIV(nss,ix) = iv;
11455 break;
11456 case SAVEt_SPTR: /* SV* reference */
11457 ptr = POPPTR(ss,ix);
11458 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11459 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11460 TOPPTR(nss,ix) = sv_dup(sv, param);
1d7c1841
GS
11461 break;
11462 case SAVEt_VPTR: /* random* reference */
11463 ptr = POPPTR(ss,ix);
11464 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11465 ptr = POPPTR(ss,ix);
11466 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11467 break;
11468 case SAVEt_PPTR: /* char* reference */
11469 ptr = POPPTR(ss,ix);
11470 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11471 c = (char*)POPPTR(ss,ix);
11472 TOPPTR(nss,ix) = pv_dup(c);
11473 break;
11474 case SAVEt_HPTR: /* HV* reference */
11475 ptr = POPPTR(ss,ix);
11476 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11477 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 11478 TOPPTR(nss,ix) = hv_dup(hv, param);
1d7c1841
GS
11479 break;
11480 case SAVEt_APTR: /* AV* reference */
11481 ptr = POPPTR(ss,ix);
11482 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11483 av = (AV*)POPPTR(ss,ix);
d2d73c3e 11484 TOPPTR(nss,ix) = av_dup(av, param);
1d7c1841
GS
11485 break;
11486 case SAVEt_NSTAB:
11487 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 11488 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
11489 break;
11490 case SAVEt_GP: /* scalar reference */
11491 gp = (GP*)POPPTR(ss,ix);
d2d73c3e 11492 TOPPTR(nss,ix) = gp = gp_dup(gp, param);
1d7c1841
GS
11493 (void)GpREFCNT_inc(gp);
11494 gv = (GV*)POPPTR(ss,ix);
2ed3c8fc 11495 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
1d7c1841
GS
11496 c = (char*)POPPTR(ss,ix);
11497 TOPPTR(nss,ix) = pv_dup(c);
11498 iv = POPIV(ss,ix);
11499 TOPIV(nss,ix) = iv;
11500 iv = POPIV(ss,ix);
11501 TOPIV(nss,ix) = iv;
11502 break;
11503 case SAVEt_FREESV:
26d9b02f 11504 case SAVEt_MORTALIZESV:
1d7c1841 11505 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11506 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
11507 break;
11508 case SAVEt_FREEOP:
11509 ptr = POPPTR(ss,ix);
11510 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
11511 /* these are assumed to be refcounted properly */
11512 switch (((OP*)ptr)->op_type) {
11513 case OP_LEAVESUB:
11514 case OP_LEAVESUBLV:
11515 case OP_LEAVEEVAL:
11516 case OP_LEAVE:
11517 case OP_SCOPE:
11518 case OP_LEAVEWRITE:
e977893f
GS
11519 TOPPTR(nss,ix) = ptr;
11520 o = (OP*)ptr;
11521 OpREFCNT_inc(o);
1d7c1841
GS
11522 break;
11523 default:
11524 TOPPTR(nss,ix) = Nullop;
11525 break;
11526 }
11527 }
11528 else
11529 TOPPTR(nss,ix) = Nullop;
11530 break;
11531 case SAVEt_FREEPV:
11532 c = (char*)POPPTR(ss,ix);
11533 TOPPTR(nss,ix) = pv_dup_inc(c);
11534 break;
11535 case SAVEt_CLEARSV:
11536 longval = POPLONG(ss,ix);
11537 TOPLONG(nss,ix) = longval;
11538 break;
11539 case SAVEt_DELETE:
11540 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 11541 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841
GS
11542 c = (char*)POPPTR(ss,ix);
11543 TOPPTR(nss,ix) = pv_dup_inc(c);
11544 i = POPINT(ss,ix);
11545 TOPINT(nss,ix) = i;
11546 break;
11547 case SAVEt_DESTRUCTOR:
11548 ptr = POPPTR(ss,ix);
11549 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
11550 dptr = POPDPTR(ss,ix);
8141890a
JH
11551 TOPDPTR(nss,ix) = DPTR2FPTR(void (*)(void*),
11552 any_dup(FPTR2DPTR(void *, dptr),
11553 proto_perl));
1d7c1841
GS
11554 break;
11555 case SAVEt_DESTRUCTOR_X:
11556 ptr = POPPTR(ss,ix);
11557 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
11558 dxptr = POPDXPTR(ss,ix);
8141890a
JH
11559 TOPDXPTR(nss,ix) = DPTR2FPTR(void (*)(pTHX_ void*),
11560 any_dup(FPTR2DPTR(void *, dxptr),
11561 proto_perl));
1d7c1841
GS
11562 break;
11563 case SAVEt_REGCONTEXT:
11564 case SAVEt_ALLOC:
11565 i = POPINT(ss,ix);
11566 TOPINT(nss,ix) = i;
11567 ix -= i;
11568 break;
11569 case SAVEt_STACK_POS: /* Position on Perl stack */
11570 i = POPINT(ss,ix);
11571 TOPINT(nss,ix) = i;
11572 break;
11573 case SAVEt_AELEM: /* array element */
11574 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11575 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
11576 i = POPINT(ss,ix);
11577 TOPINT(nss,ix) = i;
11578 av = (AV*)POPPTR(ss,ix);
d2d73c3e 11579 TOPPTR(nss,ix) = av_dup_inc(av, param);
1d7c1841
GS
11580 break;
11581 case SAVEt_HELEM: /* hash element */
11582 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11583 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 11584 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11585 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 11586 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 11587 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841
GS
11588 break;
11589 case SAVEt_OP:
11590 ptr = POPPTR(ss,ix);
11591 TOPPTR(nss,ix) = ptr;
11592 break;
11593 case SAVEt_HINTS:
11594 i = POPINT(ss,ix);
11595 TOPINT(nss,ix) = i;
11596 break;
c4410b1b
GS
11597 case SAVEt_COMPPAD:
11598 av = (AV*)POPPTR(ss,ix);
58ed4fbe 11599 TOPPTR(nss,ix) = av_dup(av, param);
c4410b1b 11600 break;
c3564e5c
GS
11601 case SAVEt_PADSV:
11602 longval = (long)POPLONG(ss,ix);
11603 TOPLONG(nss,ix) = longval;
11604 ptr = POPPTR(ss,ix);
11605 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11606 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11607 TOPPTR(nss,ix) = sv_dup(sv, param);
c3564e5c 11608 break;
a1bb4754 11609 case SAVEt_BOOL:
38d8b13e 11610 ptr = POPPTR(ss,ix);
b9609c01 11611 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
38d8b13e 11612 longval = (long)POPBOOL(ss,ix);
b9609c01 11613 TOPBOOL(nss,ix) = (bool)longval;
a1bb4754 11614 break;
8bd2680e
MHM
11615 case SAVEt_SET_SVFLAGS:
11616 i = POPINT(ss,ix);
11617 TOPINT(nss,ix) = i;
11618 i = POPINT(ss,ix);
11619 TOPINT(nss,ix) = i;
11620 sv = (SV*)POPPTR(ss,ix);
11621 TOPPTR(nss,ix) = sv_dup(sv, param);
11622 break;
1d7c1841
GS
11623 default:
11624 Perl_croak(aTHX_ "panic: ss_dup inconsistency");
11625 }
11626 }
11627
11628 return nss;
11629}
11630
9660f481
DM
11631
11632/* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
11633 * flag to the result. This is done for each stash before cloning starts,
11634 * so we know which stashes want their objects cloned */
11635
11636static void
11637do_mark_cloneable_stash(pTHX_ SV *sv)
11638{
84bda14a 11639 const HEK *hvname = HvNAME_HEK((HV*)sv);
bfcb3514 11640 if (hvname) {
9660f481
DM
11641 GV* cloner = gv_fetchmethod_autoload((HV*)sv, "CLONE_SKIP", 0);
11642 SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
11643 if (cloner && GvCV(cloner)) {
11644 dSP;
11645 UV status;
11646
11647 ENTER;
11648 SAVETMPS;
11649 PUSHMARK(SP);
84bda14a 11650 XPUSHs(sv_2mortal(newSVhek(hvname)));
9660f481
DM
11651 PUTBACK;
11652 call_sv((SV*)GvCV(cloner), G_SCALAR);
11653 SPAGAIN;
11654 status = POPu;
11655 PUTBACK;
11656 FREETMPS;
11657 LEAVE;
11658 if (status)
11659 SvFLAGS(sv) &= ~SVphv_CLONEABLE;
11660 }
11661 }
11662}
11663
11664
11665
645c22ef
DM
11666/*
11667=for apidoc perl_clone
11668
11669Create and return a new interpreter by cloning the current one.
11670
4be49ee6 11671perl_clone takes these flags as parameters:
6a78b4db 11672
7a5fa8a2
NIS
11673CLONEf_COPY_STACKS - is used to, well, copy the stacks also,
11674without it we only clone the data and zero the stacks,
11675with it we copy the stacks and the new perl interpreter is
11676ready to run at the exact same point as the previous one.
11677The pseudo-fork code uses COPY_STACKS while the
6a78b4db
AB
11678threads->new doesn't.
11679
11680CLONEf_KEEP_PTR_TABLE
7a5fa8a2
NIS
11681perl_clone keeps a ptr_table with the pointer of the old
11682variable as a key and the new variable as a value,
11683this allows it to check if something has been cloned and not
11684clone it again but rather just use the value and increase the
11685refcount. If KEEP_PTR_TABLE is not set then perl_clone will kill
11686the ptr_table using the function
11687C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>,
11688reason to keep it around is if you want to dup some of your own
11689variable who are outside the graph perl scans, example of this
6a78b4db
AB
11690code is in threads.xs create
11691
11692CLONEf_CLONE_HOST
7a5fa8a2
NIS
11693This is a win32 thing, it is ignored on unix, it tells perls
11694win32host code (which is c++) to clone itself, this is needed on
11695win32 if you want to run two threads at the same time,
11696if you just want to do some stuff in a separate perl interpreter
11697and then throw it away and return to the original one,
6a78b4db
AB
11698you don't need to do anything.
11699
645c22ef
DM
11700=cut
11701*/
11702
11703/* XXX the above needs expanding by someone who actually understands it ! */
3fc56081
NK
11704EXTERN_C PerlInterpreter *
11705perl_clone_host(PerlInterpreter* proto_perl, UV flags);
645c22ef 11706
1d7c1841
GS
11707PerlInterpreter *
11708perl_clone(PerlInterpreter *proto_perl, UV flags)
11709{
27da23d5 11710 dVAR;
1d7c1841 11711#ifdef PERL_IMPLICIT_SYS
c43294b8
AB
11712
11713 /* perlhost.h so we need to call into it
11714 to clone the host, CPerlHost should have a c interface, sky */
11715
11716 if (flags & CLONEf_CLONE_HOST) {
11717 return perl_clone_host(proto_perl,flags);
11718 }
11719 return perl_clone_using(proto_perl, flags,
1d7c1841
GS
11720 proto_perl->IMem,
11721 proto_perl->IMemShared,
11722 proto_perl->IMemParse,
11723 proto_perl->IEnv,
11724 proto_perl->IStdIO,
11725 proto_perl->ILIO,
11726 proto_perl->IDir,
11727 proto_perl->ISock,
11728 proto_perl->IProc);
11729}
11730
11731PerlInterpreter *
11732perl_clone_using(PerlInterpreter *proto_perl, UV flags,
11733 struct IPerlMem* ipM, struct IPerlMem* ipMS,
11734 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
11735 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
11736 struct IPerlDir* ipD, struct IPerlSock* ipS,
11737 struct IPerlProc* ipP)
11738{
11739 /* XXX many of the string copies here can be optimized if they're
11740 * constants; they need to be allocated as common memory and just
11741 * their pointers copied. */
11742
8fc9efbd 11743 IV i;
64aa0685
GS
11744 CLONE_PARAMS clone_params;
11745 CLONE_PARAMS* param = &clone_params;
d2d73c3e 11746
1d7c1841 11747 PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
9660f481
DM
11748 /* for each stash, determine whether its objects should be cloned */
11749 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
ba869deb 11750 PERL_SET_THX(my_perl);
1d7c1841 11751
acfe0abc 11752# ifdef DEBUGGING
a4530404 11753 Poison(my_perl, 1, PerlInterpreter);
fd0854ff 11754 PL_op = Nullop;
c008732b 11755 PL_curcop = (COP *)Nullop;
1d7c1841
GS
11756 PL_markstack = 0;
11757 PL_scopestack = 0;
11758 PL_savestack = 0;
22f7c9c9
JH
11759 PL_savestack_ix = 0;
11760 PL_savestack_max = -1;
66fe0623 11761 PL_sig_pending = 0;
25596c82 11762 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
acfe0abc 11763# else /* !DEBUGGING */
1d7c1841 11764 Zero(my_perl, 1, PerlInterpreter);
acfe0abc 11765# endif /* DEBUGGING */
1d7c1841
GS
11766
11767 /* host pointers */
11768 PL_Mem = ipM;
11769 PL_MemShared = ipMS;
11770 PL_MemParse = ipMP;
11771 PL_Env = ipE;
11772 PL_StdIO = ipStd;
11773 PL_LIO = ipLIO;
11774 PL_Dir = ipD;
11775 PL_Sock = ipS;
11776 PL_Proc = ipP;
1d7c1841
GS
11777#else /* !PERL_IMPLICIT_SYS */
11778 IV i;
64aa0685
GS
11779 CLONE_PARAMS clone_params;
11780 CLONE_PARAMS* param = &clone_params;
1d7c1841 11781 PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
9660f481
DM
11782 /* for each stash, determine whether its objects should be cloned */
11783 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
ba869deb 11784 PERL_SET_THX(my_perl);
1d7c1841
GS
11785
11786# ifdef DEBUGGING
a4530404 11787 Poison(my_perl, 1, PerlInterpreter);
fd0854ff 11788 PL_op = Nullop;
c008732b 11789 PL_curcop = (COP *)Nullop;
1d7c1841
GS
11790 PL_markstack = 0;
11791 PL_scopestack = 0;
11792 PL_savestack = 0;
22f7c9c9
JH
11793 PL_savestack_ix = 0;
11794 PL_savestack_max = -1;
66fe0623 11795 PL_sig_pending = 0;
25596c82 11796 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
1d7c1841
GS
11797# else /* !DEBUGGING */
11798 Zero(my_perl, 1, PerlInterpreter);
11799# endif /* DEBUGGING */
11800#endif /* PERL_IMPLICIT_SYS */
83236556 11801 param->flags = flags;
59b40662 11802 param->proto_perl = proto_perl;
1d7c1841
GS
11803
11804 /* arena roots */
612f20c3 11805 PL_xnv_arenaroot = NULL;
1d7c1841 11806 PL_xnv_root = NULL;
612f20c3 11807 PL_xpv_arenaroot = NULL;
1d7c1841 11808 PL_xpv_root = NULL;
612f20c3 11809 PL_xpviv_arenaroot = NULL;
1d7c1841 11810 PL_xpviv_root = NULL;
612f20c3 11811 PL_xpvnv_arenaroot = NULL;
1d7c1841 11812 PL_xpvnv_root = NULL;
612f20c3 11813 PL_xpvcv_arenaroot = NULL;
1d7c1841 11814 PL_xpvcv_root = NULL;
612f20c3 11815 PL_xpvav_arenaroot = NULL;
1d7c1841 11816 PL_xpvav_root = NULL;
612f20c3 11817 PL_xpvhv_arenaroot = NULL;
1d7c1841 11818 PL_xpvhv_root = NULL;
612f20c3 11819 PL_xpvmg_arenaroot = NULL;
1d7c1841 11820 PL_xpvmg_root = NULL;
7552b40b
DM
11821 PL_xpvgv_arenaroot = NULL;
11822 PL_xpvgv_root = NULL;
612f20c3 11823 PL_xpvlv_arenaroot = NULL;
1d7c1841 11824 PL_xpvlv_root = NULL;
612f20c3 11825 PL_xpvbm_arenaroot = NULL;
1d7c1841 11826 PL_xpvbm_root = NULL;
612f20c3 11827 PL_he_arenaroot = NULL;
1d7c1841 11828 PL_he_root = NULL;
892b45be 11829#if defined(USE_ITHREADS)
32e691d0
NC
11830 PL_pte_arenaroot = NULL;
11831 PL_pte_root = NULL;
892b45be 11832#endif
1d7c1841
GS
11833 PL_nice_chunk = NULL;
11834 PL_nice_chunk_size = 0;
11835 PL_sv_count = 0;
11836 PL_sv_objcount = 0;
11837 PL_sv_root = Nullsv;
11838 PL_sv_arenaroot = Nullsv;
11839
11840 PL_debug = proto_perl->Idebug;
11841
8df990a8
NC
11842 PL_hash_seed = proto_perl->Ihash_seed;
11843 PL_rehash_seed = proto_perl->Irehash_seed;
11844
e5dd39fc 11845#ifdef USE_REENTRANT_API
68853529
SB
11846 /* XXX: things like -Dm will segfault here in perlio, but doing
11847 * PERL_SET_CONTEXT(proto_perl);
11848 * breaks too many other things
11849 */
59bd0823 11850 Perl_reentrant_init(aTHX);
e5dd39fc
AB
11851#endif
11852
1d7c1841
GS
11853 /* create SV map for pointer relocation */
11854 PL_ptr_table = ptr_table_new();
11855
11856 /* initialize these special pointers as early as possible */
11857 SvANY(&PL_sv_undef) = NULL;
11858 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
11859 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL;
11860 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
11861
1d7c1841 11862 SvANY(&PL_sv_no) = new_XPVNV();
1d7c1841 11863 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
0309f36e
NC
11864 SvFLAGS(&PL_sv_no) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
11865 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
f880fe2f 11866 SvPV_set(&PL_sv_no, SAVEPVN(PL_No, 0));
b162af07
SP
11867 SvCUR_set(&PL_sv_no, 0);
11868 SvLEN_set(&PL_sv_no, 1);
45977657 11869 SvIV_set(&PL_sv_no, 0);
9d6ce603 11870 SvNV_set(&PL_sv_no, 0);
1d7c1841
GS
11871 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
11872
1d7c1841 11873 SvANY(&PL_sv_yes) = new_XPVNV();
1d7c1841 11874 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
0309f36e
NC
11875 SvFLAGS(&PL_sv_yes) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
11876 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
f880fe2f 11877 SvPV_set(&PL_sv_yes, SAVEPVN(PL_Yes, 1));
b162af07
SP
11878 SvCUR_set(&PL_sv_yes, 1);
11879 SvLEN_set(&PL_sv_yes, 2);
45977657 11880 SvIV_set(&PL_sv_yes, 1);
9d6ce603 11881 SvNV_set(&PL_sv_yes, 1);
1d7c1841
GS
11882 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
11883
05ec9bb3 11884 /* create (a non-shared!) shared string table */
1d7c1841
GS
11885 PL_strtab = newHV();
11886 HvSHAREKEYS_off(PL_strtab);
c4a9c09d 11887 hv_ksplit(PL_strtab, HvTOTALKEYS(proto_perl->Istrtab));
1d7c1841
GS
11888 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
11889
05ec9bb3
NIS
11890 PL_compiling = proto_perl->Icompiling;
11891
11892 /* These two PVs will be free'd special way so must set them same way op.c does */
11893 PL_compiling.cop_stashpv = savesharedpv(PL_compiling.cop_stashpv);
11894 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_stashpv, PL_compiling.cop_stashpv);
11895
11896 PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file);
11897 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file);
11898
1d7c1841
GS
11899 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
11900 if (!specialWARN(PL_compiling.cop_warnings))
d2d73c3e 11901 PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings, param);
ac27b0f5 11902 if (!specialCopIO(PL_compiling.cop_io))
d2d73c3e 11903 PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io, param);
1d7c1841
GS
11904 PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl);
11905
11906 /* pseudo environmental stuff */
11907 PL_origargc = proto_perl->Iorigargc;
e2975953 11908 PL_origargv = proto_perl->Iorigargv;
d2d73c3e 11909
d2d73c3e
AB
11910 param->stashes = newAV(); /* Setup array of objects to call clone on */
11911
a1ea730d 11912#ifdef PERLIO_LAYERS
3a1ee7e8
NIS
11913 /* Clone PerlIO tables as soon as we can handle general xx_dup() */
11914 PerlIO_clone(aTHX_ proto_perl, param);
a1ea730d 11915#endif
d2d73c3e
AB
11916
11917 PL_envgv = gv_dup(proto_perl->Ienvgv, param);
11918 PL_incgv = gv_dup(proto_perl->Iincgv, param);
11919 PL_hintgv = gv_dup(proto_perl->Ihintgv, param);
1d7c1841 11920 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
d2d73c3e
AB
11921 PL_diehook = sv_dup_inc(proto_perl->Idiehook, param);
11922 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param);
1d7c1841
GS
11923
11924 /* switches */
11925 PL_minus_c = proto_perl->Iminus_c;
d2d73c3e 11926 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param);
1d7c1841
GS
11927 PL_localpatches = proto_perl->Ilocalpatches;
11928 PL_splitstr = proto_perl->Isplitstr;
11929 PL_preprocess = proto_perl->Ipreprocess;
11930 PL_minus_n = proto_perl->Iminus_n;
11931 PL_minus_p = proto_perl->Iminus_p;
11932 PL_minus_l = proto_perl->Iminus_l;
11933 PL_minus_a = proto_perl->Iminus_a;
11934 PL_minus_F = proto_perl->Iminus_F;
11935 PL_doswitches = proto_perl->Idoswitches;
11936 PL_dowarn = proto_perl->Idowarn;
11937 PL_doextract = proto_perl->Idoextract;
11938 PL_sawampersand = proto_perl->Isawampersand;
11939 PL_unsafe = proto_perl->Iunsafe;
11940 PL_inplace = SAVEPV(proto_perl->Iinplace);
d2d73c3e 11941 PL_e_script = sv_dup_inc(proto_perl->Ie_script, param);
1d7c1841
GS
11942 PL_perldb = proto_perl->Iperldb;
11943 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
1cbb0781 11944 PL_exit_flags = proto_perl->Iexit_flags;
1d7c1841
GS
11945
11946 /* magical thingies */
11947 /* XXX time(&PL_basetime) when asked for? */
11948 PL_basetime = proto_perl->Ibasetime;
d2d73c3e 11949 PL_formfeed = sv_dup(proto_perl->Iformfeed, param);
1d7c1841
GS
11950
11951 PL_maxsysfd = proto_perl->Imaxsysfd;
11952 PL_multiline = proto_perl->Imultiline;
11953 PL_statusvalue = proto_perl->Istatusvalue;
11954#ifdef VMS
11955 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
11956#endif
0a378802 11957 PL_encoding = sv_dup(proto_perl->Iencoding, param);
1d7c1841 11958
4a4c6fe3 11959 sv_setpvn(PERL_DEBUG_PAD(0), "", 0); /* For regex debugging. */
1f483ca1
JH
11960 sv_setpvn(PERL_DEBUG_PAD(1), "", 0); /* ext/re needs these */
11961 sv_setpvn(PERL_DEBUG_PAD(2), "", 0); /* even without DEBUGGING. */
4a4c6fe3 11962
d2f185dc
AMS
11963 /* Clone the regex array */
11964 PL_regex_padav = newAV();
11965 {
a3b680e6 11966 const I32 len = av_len((AV*)proto_perl->Iregex_padav);
d2f185dc 11967 SV** regexen = AvARRAY((AV*)proto_perl->Iregex_padav);
b464bac0 11968 IV i;
0f95fc41
AB
11969 av_push(PL_regex_padav,
11970 sv_dup_inc(regexen[0],param));
11971 for(i = 1; i <= len; i++) {
11972 if(SvREPADTMP(regexen[i])) {
11973 av_push(PL_regex_padav, sv_dup_inc(regexen[i], param));
8cf8f3d1 11974 } else {
0f95fc41
AB
11975 av_push(PL_regex_padav,
11976 SvREFCNT_inc(
8cf8f3d1 11977 newSViv(PTR2IV(re_dup(INT2PTR(REGEXP *,
cbfa9890 11978 SvIVX(regexen[i])), param)))
0f95fc41
AB
11979 ));
11980 }
d2f185dc
AMS
11981 }
11982 }
11983 PL_regex_pad = AvARRAY(PL_regex_padav);
1fcf4c12 11984
1d7c1841 11985 /* shortcuts to various I/O objects */
d2d73c3e
AB
11986 PL_stdingv = gv_dup(proto_perl->Istdingv, param);
11987 PL_stderrgv = gv_dup(proto_perl->Istderrgv, param);
11988 PL_defgv = gv_dup(proto_perl->Idefgv, param);
11989 PL_argvgv = gv_dup(proto_perl->Iargvgv, param);
11990 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param);
11991 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param);
1d7c1841
GS
11992
11993 /* shortcuts to regexp stuff */
d2d73c3e 11994 PL_replgv = gv_dup(proto_perl->Ireplgv, param);
1d7c1841
GS
11995
11996 /* shortcuts to misc objects */
d2d73c3e 11997 PL_errgv = gv_dup(proto_perl->Ierrgv, param);
1d7c1841
GS
11998
11999 /* shortcuts to debugging objects */
d2d73c3e
AB
12000 PL_DBgv = gv_dup(proto_perl->IDBgv, param);
12001 PL_DBline = gv_dup(proto_perl->IDBline, param);
12002 PL_DBsub = gv_dup(proto_perl->IDBsub, param);
12003 PL_DBsingle = sv_dup(proto_perl->IDBsingle, param);
12004 PL_DBtrace = sv_dup(proto_perl->IDBtrace, param);
12005 PL_DBsignal = sv_dup(proto_perl->IDBsignal, param);
06492da6 12006 PL_DBassertion = sv_dup(proto_perl->IDBassertion, param);
d2d73c3e
AB
12007 PL_lineary = av_dup(proto_perl->Ilineary, param);
12008 PL_dbargs = av_dup(proto_perl->Idbargs, param);
1d7c1841
GS
12009
12010 /* symbol tables */
d2d73c3e
AB
12011 PL_defstash = hv_dup_inc(proto_perl->Tdefstash, param);
12012 PL_curstash = hv_dup(proto_perl->Tcurstash, param);
d2d73c3e
AB
12013 PL_debstash = hv_dup(proto_perl->Idebstash, param);
12014 PL_globalstash = hv_dup(proto_perl->Iglobalstash, param);
12015 PL_curstname = sv_dup_inc(proto_perl->Icurstname, param);
12016
12017 PL_beginav = av_dup_inc(proto_perl->Ibeginav, param);
ee1c5a4e 12018 PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param);
ece599bd 12019 PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param);
d2d73c3e
AB
12020 PL_endav = av_dup_inc(proto_perl->Iendav, param);
12021 PL_checkav = av_dup_inc(proto_perl->Icheckav, param);
12022 PL_initav = av_dup_inc(proto_perl->Iinitav, param);
1d7c1841
GS
12023
12024 PL_sub_generation = proto_perl->Isub_generation;
12025
12026 /* funky return mechanisms */
12027 PL_forkprocess = proto_perl->Iforkprocess;
12028
12029 /* subprocess state */
d2d73c3e 12030 PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param);
1d7c1841
GS
12031
12032 /* internal state */
12033 PL_tainting = proto_perl->Itainting;
7135f00b 12034 PL_taint_warn = proto_perl->Itaint_warn;
1d7c1841
GS
12035 PL_maxo = proto_perl->Imaxo;
12036 if (proto_perl->Iop_mask)
12037 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
12038 else
12039 PL_op_mask = Nullch;
06492da6 12040 /* PL_asserting = proto_perl->Iasserting; */
1d7c1841
GS
12041
12042 /* current interpreter roots */
d2d73c3e 12043 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param);
1d7c1841
GS
12044 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
12045 PL_main_start = proto_perl->Imain_start;
e977893f 12046 PL_eval_root = proto_perl->Ieval_root;
1d7c1841
GS
12047 PL_eval_start = proto_perl->Ieval_start;
12048
12049 /* runtime control stuff */
12050 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
12051 PL_copline = proto_perl->Icopline;
12052
12053 PL_filemode = proto_perl->Ifilemode;
12054 PL_lastfd = proto_perl->Ilastfd;
12055 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
12056 PL_Argv = NULL;
12057 PL_Cmd = Nullch;
12058 PL_gensym = proto_perl->Igensym;
12059 PL_preambled = proto_perl->Ipreambled;
d2d73c3e 12060 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param);
1d7c1841
GS
12061 PL_laststatval = proto_perl->Ilaststatval;
12062 PL_laststype = proto_perl->Ilaststype;
12063 PL_mess_sv = Nullsv;
12064
d2d73c3e 12065 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param);
1d7c1841
GS
12066 PL_ofmt = SAVEPV(proto_perl->Iofmt);
12067
12068 /* interpreter atexit processing */
12069 PL_exitlistlen = proto_perl->Iexitlistlen;
12070 if (PL_exitlistlen) {
12071 New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
12072 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
12073 }
12074 else
12075 PL_exitlist = (PerlExitListEntry*)NULL;
d2d73c3e 12076 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param);
19e8ce8e
AB
12077 PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param);
12078 PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param);
1d7c1841
GS
12079
12080 PL_profiledata = NULL;
a8fc9800 12081 PL_rsfp = fp_dup(proto_perl->Irsfp, '<', param);
1d7c1841 12082 /* PL_rsfp_filters entries have fake IoDIRP() */
d2d73c3e 12083 PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters, param);
1d7c1841 12084
d2d73c3e 12085 PL_compcv = cv_dup(proto_perl->Icompcv, param);
dd2155a4
DM
12086
12087 PAD_CLONE_VARS(proto_perl, param);
1d7c1841
GS
12088
12089#ifdef HAVE_INTERP_INTERN
12090 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
12091#endif
12092
12093 /* more statics moved here */
12094 PL_generation = proto_perl->Igeneration;
d2d73c3e 12095 PL_DBcv = cv_dup(proto_perl->IDBcv, param);
1d7c1841
GS
12096
12097 PL_in_clean_objs = proto_perl->Iin_clean_objs;
12098 PL_in_clean_all = proto_perl->Iin_clean_all;
12099
12100 PL_uid = proto_perl->Iuid;
12101 PL_euid = proto_perl->Ieuid;
12102 PL_gid = proto_perl->Igid;
12103 PL_egid = proto_perl->Iegid;
12104 PL_nomemok = proto_perl->Inomemok;
12105 PL_an = proto_perl->Ian;
1d7c1841
GS
12106 PL_evalseq = proto_perl->Ievalseq;
12107 PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */
12108 PL_origalen = proto_perl->Iorigalen;
12109 PL_pidstatus = newHV(); /* XXX flag for cloning? */
12110 PL_osname = SAVEPV(proto_perl->Iosname);
5c728af0 12111 PL_sh_path_compat = proto_perl->Ish_path_compat; /* XXX never deallocated */
1d7c1841
GS
12112 PL_sighandlerp = proto_perl->Isighandlerp;
12113
12114
12115 PL_runops = proto_perl->Irunops;
12116
12117 Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char);
12118
12119#ifdef CSH
12120 PL_cshlen = proto_perl->Icshlen;
74f1b2b8 12121 PL_cshname = proto_perl->Icshname; /* XXX never deallocated */
1d7c1841
GS
12122#endif
12123
12124 PL_lex_state = proto_perl->Ilex_state;
12125 PL_lex_defer = proto_perl->Ilex_defer;
12126 PL_lex_expect = proto_perl->Ilex_expect;
12127 PL_lex_formbrack = proto_perl->Ilex_formbrack;
12128 PL_lex_dojoin = proto_perl->Ilex_dojoin;
12129 PL_lex_starts = proto_perl->Ilex_starts;
d2d73c3e
AB
12130 PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff, param);
12131 PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl, param);
1d7c1841
GS
12132 PL_lex_op = proto_perl->Ilex_op;
12133 PL_lex_inpat = proto_perl->Ilex_inpat;
12134 PL_lex_inwhat = proto_perl->Ilex_inwhat;
12135 PL_lex_brackets = proto_perl->Ilex_brackets;
12136 i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets);
12137 PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i);
12138 PL_lex_casemods = proto_perl->Ilex_casemods;
12139 i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods);
12140 PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i);
12141
12142 Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE);
12143 Copy(proto_perl->Inexttype, PL_nexttype, 5, I32);
12144 PL_nexttoke = proto_perl->Inexttoke;
12145
1d773130
TB
12146 /* XXX This is probably masking the deeper issue of why
12147 * SvANY(proto_perl->Ilinestr) can be NULL at this point. For test case:
12148 * http://archive.develooper.com/perl5-porters%40perl.org/msg83298.html
12149 * (A little debugging with a watchpoint on it may help.)
12150 */
389edf32
TB
12151 if (SvANY(proto_perl->Ilinestr)) {
12152 PL_linestr = sv_dup_inc(proto_perl->Ilinestr, param);
3f7c398e 12153 i = proto_perl->Ibufptr - SvPVX_const(proto_perl->Ilinestr);
389edf32 12154 PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
3f7c398e 12155 i = proto_perl->Ioldbufptr - SvPVX_const(proto_perl->Ilinestr);
389edf32 12156 PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
3f7c398e 12157 i = proto_perl->Ioldoldbufptr - SvPVX_const(proto_perl->Ilinestr);
389edf32 12158 PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
3f7c398e 12159 i = proto_perl->Ilinestart - SvPVX_const(proto_perl->Ilinestr);
389edf32
TB
12160 PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12161 }
12162 else {
12163 PL_linestr = NEWSV(65,79);
12164 sv_upgrade(PL_linestr,SVt_PVIV);
12165 sv_setpvn(PL_linestr,"",0);
12166 PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
12167 }
1d7c1841 12168 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
1d7c1841
GS
12169 PL_pending_ident = proto_perl->Ipending_ident;
12170 PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */
12171
12172 PL_expect = proto_perl->Iexpect;
12173
12174 PL_multi_start = proto_perl->Imulti_start;
12175 PL_multi_end = proto_perl->Imulti_end;
12176 PL_multi_open = proto_perl->Imulti_open;
12177 PL_multi_close = proto_perl->Imulti_close;
12178
12179 PL_error_count = proto_perl->Ierror_count;
12180 PL_subline = proto_perl->Isubline;
d2d73c3e 12181 PL_subname = sv_dup_inc(proto_perl->Isubname, param);
1d7c1841 12182
1d773130 12183 /* XXX See comment on SvANY(proto_perl->Ilinestr) above */
389edf32 12184 if (SvANY(proto_perl->Ilinestr)) {
3f7c398e 12185 i = proto_perl->Ilast_uni - SvPVX_const(proto_perl->Ilinestr);
389edf32 12186 PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
3f7c398e 12187 i = proto_perl->Ilast_lop - SvPVX_const(proto_perl->Ilinestr);
389edf32
TB
12188 PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12189 PL_last_lop_op = proto_perl->Ilast_lop_op;
12190 }
12191 else {
12192 PL_last_uni = SvPVX(PL_linestr);
12193 PL_last_lop = SvPVX(PL_linestr);
12194 PL_last_lop_op = 0;
12195 }
1d7c1841 12196 PL_in_my = proto_perl->Iin_my;
d2d73c3e 12197 PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash, param);
1d7c1841
GS
12198#ifdef FCRYPT
12199 PL_cryptseen = proto_perl->Icryptseen;
12200#endif
12201
12202 PL_hints = proto_perl->Ihints;
12203
12204 PL_amagic_generation = proto_perl->Iamagic_generation;
12205
12206#ifdef USE_LOCALE_COLLATE
12207 PL_collation_ix = proto_perl->Icollation_ix;
12208 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
12209 PL_collation_standard = proto_perl->Icollation_standard;
12210 PL_collxfrm_base = proto_perl->Icollxfrm_base;
12211 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
12212#endif /* USE_LOCALE_COLLATE */
12213
12214#ifdef USE_LOCALE_NUMERIC
12215 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
12216 PL_numeric_standard = proto_perl->Inumeric_standard;
12217 PL_numeric_local = proto_perl->Inumeric_local;
d2d73c3e 12218 PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param);
1d7c1841
GS
12219#endif /* !USE_LOCALE_NUMERIC */
12220
12221 /* utf8 character classes */
d2d73c3e
AB
12222 PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum, param);
12223 PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc, param);
12224 PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii, param);
12225 PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha, param);
12226 PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space, param);
12227 PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl, param);
12228 PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph, param);
12229 PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit, param);
12230 PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper, param);
12231 PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower, param);
12232 PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print, param);
12233 PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct, param);
12234 PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit, param);
12235 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param);
12236 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param);
12237 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param);
12238 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param);
b4e400f9 12239 PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param);
82686b01
JH
12240 PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param);
12241 PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param);
1d7c1841 12242
6c3182a5 12243 /* Did the locale setup indicate UTF-8? */
9769094f 12244 PL_utf8locale = proto_perl->Iutf8locale;
6c3182a5
JH
12245 /* Unicode features (see perlrun/-C) */
12246 PL_unicode = proto_perl->Iunicode;
12247
12248 /* Pre-5.8 signals control */
12249 PL_signals = proto_perl->Isignals;
12250
12251 /* times() ticks per second */
12252 PL_clocktick = proto_perl->Iclocktick;
12253
12254 /* Recursion stopper for PerlIO_find_layer */
12255 PL_in_load_module = proto_perl->Iin_load_module;
12256
12257 /* sort() routine */
12258 PL_sort_RealCmp = proto_perl->Isort_RealCmp;
12259
57c6e6d2
JH
12260 /* Not really needed/useful since the reenrant_retint is "volatile",
12261 * but do it for consistency's sake. */
12262 PL_reentrant_retint = proto_perl->Ireentrant_retint;
12263
15a5279a
JH
12264 /* Hooks to shared SVs and locks. */
12265 PL_sharehook = proto_perl->Isharehook;
12266 PL_lockhook = proto_perl->Ilockhook;
12267 PL_unlockhook = proto_perl->Iunlockhook;
12268 PL_threadhook = proto_perl->Ithreadhook;
12269
bce260cd
JH
12270 PL_runops_std = proto_perl->Irunops_std;
12271 PL_runops_dbg = proto_perl->Irunops_dbg;
12272
12273#ifdef THREADS_HAVE_PIDS
12274 PL_ppid = proto_perl->Ippid;
12275#endif
12276
1d7c1841
GS
12277 /* swatch cache */
12278 PL_last_swash_hv = Nullhv; /* reinits on demand */
12279 PL_last_swash_klen = 0;
12280 PL_last_swash_key[0]= '\0';
12281 PL_last_swash_tmps = (U8*)NULL;
12282 PL_last_swash_slen = 0;
12283
1d7c1841
GS
12284 PL_glob_index = proto_perl->Iglob_index;
12285 PL_srand_called = proto_perl->Isrand_called;
12286 PL_uudmap['M'] = 0; /* reinits on demand */
12287 PL_bitcount = Nullch; /* reinits on demand */
12288
66fe0623
NIS
12289 if (proto_perl->Ipsig_pend) {
12290 Newz(0, PL_psig_pend, SIG_SIZE, int);
9dd79c3f 12291 }
66fe0623
NIS
12292 else {
12293 PL_psig_pend = (int*)NULL;
12294 }
12295
1d7c1841 12296 if (proto_perl->Ipsig_ptr) {
76d3c696
JH
12297 Newz(0, PL_psig_ptr, SIG_SIZE, SV*);
12298 Newz(0, PL_psig_name, SIG_SIZE, SV*);
76d3c696 12299 for (i = 1; i < SIG_SIZE; i++) {
d2d73c3e
AB
12300 PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i], param);
12301 PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i], param);
1d7c1841
GS
12302 }
12303 }
12304 else {
12305 PL_psig_ptr = (SV**)NULL;
12306 PL_psig_name = (SV**)NULL;
12307 }
12308
12309 /* thrdvar.h stuff */
12310
a0739874 12311 if (flags & CLONEf_COPY_STACKS) {
1d7c1841
GS
12312 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
12313 PL_tmps_ix = proto_perl->Ttmps_ix;
12314 PL_tmps_max = proto_perl->Ttmps_max;
12315 PL_tmps_floor = proto_perl->Ttmps_floor;
12316 Newz(50, PL_tmps_stack, PL_tmps_max, SV*);
12317 i = 0;
12318 while (i <= PL_tmps_ix) {
d2d73c3e 12319 PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i], param);
1d7c1841
GS
12320 ++i;
12321 }
12322
12323 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
12324 i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack;
12325 Newz(54, PL_markstack, i, I32);
12326 PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max
12327 - proto_perl->Tmarkstack);
12328 PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr
12329 - proto_perl->Tmarkstack);
12330 Copy(proto_perl->Tmarkstack, PL_markstack,
12331 PL_markstack_ptr - PL_markstack + 1, I32);
12332
12333 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
12334 * NOTE: unlike the others! */
12335 PL_scopestack_ix = proto_perl->Tscopestack_ix;
12336 PL_scopestack_max = proto_perl->Tscopestack_max;
12337 Newz(54, PL_scopestack, PL_scopestack_max, I32);
12338 Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32);
12339
1d7c1841 12340 /* NOTE: si_dup() looks at PL_markstack */
d2d73c3e 12341 PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo, param);
1d7c1841
GS
12342
12343 /* PL_curstack = PL_curstackinfo->si_stack; */
d2d73c3e
AB
12344 PL_curstack = av_dup(proto_perl->Tcurstack, param);
12345 PL_mainstack = av_dup(proto_perl->Tmainstack, param);
1d7c1841
GS
12346
12347 /* next PUSHs() etc. set *(PL_stack_sp+1) */
12348 PL_stack_base = AvARRAY(PL_curstack);
12349 PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp
12350 - proto_perl->Tstack_base);
12351 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
12352
12353 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
12354 * NOTE: unlike the others! */
12355 PL_savestack_ix = proto_perl->Tsavestack_ix;
12356 PL_savestack_max = proto_perl->Tsavestack_max;
12357 /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/
d2d73c3e 12358 PL_savestack = ss_dup(proto_perl, param);
1d7c1841
GS
12359 }
12360 else {
12361 init_stacks();
985e7056 12362 ENTER; /* perl_destruct() wants to LEAVE; */
1d7c1841
GS
12363 }
12364
12365 PL_start_env = proto_perl->Tstart_env; /* XXXXXX */
12366 PL_top_env = &PL_start_env;
12367
12368 PL_op = proto_perl->Top;
12369
12370 PL_Sv = Nullsv;
12371 PL_Xpv = (XPV*)NULL;
12372 PL_na = proto_perl->Tna;
12373
12374 PL_statbuf = proto_perl->Tstatbuf;
12375 PL_statcache = proto_perl->Tstatcache;
d2d73c3e
AB
12376 PL_statgv = gv_dup(proto_perl->Tstatgv, param);
12377 PL_statname = sv_dup_inc(proto_perl->Tstatname, param);
1d7c1841
GS
12378#ifdef HAS_TIMES
12379 PL_timesbuf = proto_perl->Ttimesbuf;
12380#endif
12381
12382 PL_tainted = proto_perl->Ttainted;
12383 PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */
d2d73c3e
AB
12384 PL_rs = sv_dup_inc(proto_perl->Trs, param);
12385 PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv, param);
12386 PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv, param);
12387 PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv, param);
1d7c1841 12388 PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */
d2d73c3e
AB
12389 PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget, param);
12390 PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget, param);
12391 PL_formtarget = sv_dup(proto_perl->Tformtarget, param);
1d7c1841
GS
12392
12393 PL_restartop = proto_perl->Trestartop;
12394 PL_in_eval = proto_perl->Tin_eval;
12395 PL_delaymagic = proto_perl->Tdelaymagic;
12396 PL_dirty = proto_perl->Tdirty;
12397 PL_localizing = proto_perl->Tlocalizing;
12398
d2d73c3e 12399 PL_errors = sv_dup_inc(proto_perl->Terrors, param);
dd28f7bb 12400 PL_hv_fetch_ent_mh = Nullhe;
1d7c1841
GS
12401 PL_modcount = proto_perl->Tmodcount;
12402 PL_lastgotoprobe = Nullop;
12403 PL_dumpindent = proto_perl->Tdumpindent;
12404
12405 PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl);
d2d73c3e
AB
12406 PL_sortstash = hv_dup(proto_perl->Tsortstash, param);
12407 PL_firstgv = gv_dup(proto_perl->Tfirstgv, param);
12408 PL_secondgv = gv_dup(proto_perl->Tsecondgv, param);
1d7c1841
GS
12409 PL_sortcxix = proto_perl->Tsortcxix;
12410 PL_efloatbuf = Nullch; /* reinits on demand */
12411 PL_efloatsize = 0; /* reinits on demand */
12412
12413 /* regex stuff */
12414
12415 PL_screamfirst = NULL;
12416 PL_screamnext = NULL;
12417 PL_maxscream = -1; /* reinits on demand */
12418 PL_lastscream = Nullsv;
12419
12420 PL_watchaddr = NULL;
12421 PL_watchok = Nullch;
12422
12423 PL_regdummy = proto_perl->Tregdummy;
1d7c1841
GS
12424 PL_regprecomp = Nullch;
12425 PL_regnpar = 0;
12426 PL_regsize = 0;
1d7c1841
GS
12427 PL_colorset = 0; /* reinits PL_colors[] */
12428 /*PL_colors[6] = {0,0,0,0,0,0};*/
1d7c1841
GS
12429 PL_reginput = Nullch;
12430 PL_regbol = Nullch;
12431 PL_regeol = Nullch;
12432 PL_regstartp = (I32*)NULL;
12433 PL_regendp = (I32*)NULL;
12434 PL_reglastparen = (U32*)NULL;
2d862feb 12435 PL_reglastcloseparen = (U32*)NULL;
1d7c1841 12436 PL_regtill = Nullch;
1d7c1841
GS
12437 PL_reg_start_tmp = (char**)NULL;
12438 PL_reg_start_tmpl = 0;
12439 PL_regdata = (struct reg_data*)NULL;
12440 PL_bostr = Nullch;
12441 PL_reg_flags = 0;
12442 PL_reg_eval_set = 0;
12443 PL_regnarrate = 0;
12444 PL_regprogram = (regnode*)NULL;
12445 PL_regindent = 0;
12446 PL_regcc = (CURCUR*)NULL;
12447 PL_reg_call_cc = (struct re_cc_state*)NULL;
12448 PL_reg_re = (regexp*)NULL;
12449 PL_reg_ganch = Nullch;
12450 PL_reg_sv = Nullsv;
53c4c00c 12451 PL_reg_match_utf8 = FALSE;
1d7c1841
GS
12452 PL_reg_magic = (MAGIC*)NULL;
12453 PL_reg_oldpos = 0;
12454 PL_reg_oldcurpm = (PMOP*)NULL;
12455 PL_reg_curpm = (PMOP*)NULL;
12456 PL_reg_oldsaved = Nullch;
12457 PL_reg_oldsavedlen = 0;
f8c7b90f 12458#ifdef PERL_OLD_COPY_ON_WRITE
504cff3b 12459 PL_nrs = Nullsv;
ed252734 12460#endif
1d7c1841
GS
12461 PL_reg_maxiter = 0;
12462 PL_reg_leftiter = 0;
12463 PL_reg_poscache = Nullch;
12464 PL_reg_poscache_size= 0;
12465
12466 /* RE engine - function pointers */
12467 PL_regcompp = proto_perl->Tregcompp;
12468 PL_regexecp = proto_perl->Tregexecp;
12469 PL_regint_start = proto_perl->Tregint_start;
12470 PL_regint_string = proto_perl->Tregint_string;
12471 PL_regfree = proto_perl->Tregfree;
12472
12473 PL_reginterp_cnt = 0;
12474 PL_reg_starttry = 0;
12475
a2efc822
SC
12476 /* Pluggable optimizer */
12477 PL_peepp = proto_perl->Tpeepp;
12478
081fc587
AB
12479 PL_stashcache = newHV();
12480
a0739874
DM
12481 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
12482 ptr_table_free(PL_ptr_table);
12483 PL_ptr_table = NULL;
12484 }
8cf8f3d1 12485
f284b03f
AMS
12486 /* Call the ->CLONE method, if it exists, for each of the stashes
12487 identified by sv_dup() above.
12488 */
d2d73c3e
AB
12489 while(av_len(param->stashes) != -1) {
12490 HV* stash = (HV*) av_shift(param->stashes);
f284b03f
AMS
12491 GV* cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
12492 if (cloner && GvCV(cloner)) {
12493 dSP;
12494 ENTER;
12495 SAVETMPS;
12496 PUSHMARK(SP);
84bda14a 12497 XPUSHs(sv_2mortal(newSVhek(HvNAME_HEK(stash))));
f284b03f
AMS
12498 PUTBACK;
12499 call_sv((SV*)GvCV(cloner), G_DISCARD);
12500 FREETMPS;
12501 LEAVE;
12502 }
4a09accc 12503 }
a0739874 12504
dc507217 12505 SvREFCNT_dec(param->stashes);
dc507217 12506
6d26897e
DM
12507 /* orphaned? eg threads->new inside BEGIN or use */
12508 if (PL_compcv && ! SvREFCNT(PL_compcv)) {
a3b680e6 12509 (void)SvREFCNT_inc(PL_compcv);
6d26897e
DM
12510 SAVEFREESV(PL_compcv);
12511 }
12512
1d7c1841 12513 return my_perl;
1d7c1841
GS
12514}
12515
1d7c1841 12516#endif /* USE_ITHREADS */
a0ae6670 12517
9f4817db 12518/*
ccfc67b7
JH
12519=head1 Unicode Support
12520
9f4817db
JH
12521=for apidoc sv_recode_to_utf8
12522
5d170f3a
JH
12523The encoding is assumed to be an Encode object, on entry the PV
12524of the sv is assumed to be octets in that encoding, and the sv
12525will be converted into Unicode (and UTF-8).
9f4817db 12526
5d170f3a
JH
12527If the sv already is UTF-8 (or if it is not POK), or if the encoding
12528is not a reference, nothing is done to the sv. If the encoding is not
1768d7eb
JH
12529an C<Encode::XS> Encoding object, bad things will happen.
12530(See F<lib/encoding.pm> and L<Encode>).
9f4817db 12531
5d170f3a 12532The PV of the sv is returned.
9f4817db 12533
5d170f3a
JH
12534=cut */
12535
12536char *
12537Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
12538{
27da23d5 12539 dVAR;
220e2d4e 12540 if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
d0063567
DK
12541 SV *uni;
12542 STRLEN len;
93524f2b 12543 const char *s;
d0063567
DK
12544 dSP;
12545 ENTER;
12546 SAVETMPS;
220e2d4e 12547 save_re_context();
d0063567
DK
12548 PUSHMARK(sp);
12549 EXTEND(SP, 3);
12550 XPUSHs(encoding);
12551 XPUSHs(sv);
7a5fa8a2 12552/*
f9893866
NIS
12553 NI-S 2002/07/09
12554 Passing sv_yes is wrong - it needs to be or'ed set of constants
7a5fa8a2 12555 for Encode::XS, while UTf-8 decode (currently) assumes a true value means
f9893866
NIS
12556 remove converted chars from source.
12557
12558 Both will default the value - let them.
7a5fa8a2 12559
d0063567 12560 XPUSHs(&PL_sv_yes);
f9893866 12561*/
d0063567
DK
12562 PUTBACK;
12563 call_method("decode", G_SCALAR);
12564 SPAGAIN;
12565 uni = POPs;
12566 PUTBACK;
93524f2b 12567 s = SvPV_const(uni, len);
3f7c398e 12568 if (s != SvPVX_const(sv)) {
d0063567 12569 SvGROW(sv, len + 1);
93524f2b 12570 Move(s, SvPVX(sv), len + 1, char);
d0063567 12571 SvCUR_set(sv, len);
d0063567
DK
12572 }
12573 FREETMPS;
12574 LEAVE;
d0063567 12575 SvUTF8_on(sv);
95899a2a 12576 return SvPVX(sv);
f9893866 12577 }
95899a2a 12578 return SvPOKp(sv) ? SvPVX(sv) : NULL;
9f4817db
JH
12579}
12580
220e2d4e
IH
12581/*
12582=for apidoc sv_cat_decode
12583
12584The encoding is assumed to be an Encode object, the PV of the ssv is
12585assumed to be octets in that encoding and decoding the input starts
12586from the position which (PV + *offset) pointed to. The dsv will be
12587concatenated the decoded UTF-8 string from ssv. Decoding will terminate
12588when the string tstr appears in decoding output or the input ends on
12589the PV of the ssv. The value which the offset points will be modified
12590to the last input position on the ssv.
68795e93 12591
220e2d4e
IH
12592Returns TRUE if the terminator was found, else returns FALSE.
12593
12594=cut */
12595
12596bool
12597Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
12598 SV *ssv, int *offset, char *tstr, int tlen)
12599{
27da23d5 12600 dVAR;
a73e8557 12601 bool ret = FALSE;
220e2d4e 12602 if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding) && offset) {
220e2d4e
IH
12603 SV *offsv;
12604 dSP;
12605 ENTER;
12606 SAVETMPS;
12607 save_re_context();
12608 PUSHMARK(sp);
12609 EXTEND(SP, 6);
12610 XPUSHs(encoding);
12611 XPUSHs(dsv);
12612 XPUSHs(ssv);
12613 XPUSHs(offsv = sv_2mortal(newSViv(*offset)));
12614 XPUSHs(sv_2mortal(newSVpvn(tstr, tlen)));
12615 PUTBACK;
12616 call_method("cat_decode", G_SCALAR);
12617 SPAGAIN;
12618 ret = SvTRUE(TOPs);
12619 *offset = SvIV(offsv);
12620 PUTBACK;
12621 FREETMPS;
12622 LEAVE;
220e2d4e 12623 }
a73e8557
JH
12624 else
12625 Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
12626 return ret;
220e2d4e 12627}
f9893866 12628
241d1a3b
NC
12629/*
12630 * Local variables:
12631 * c-indentation-style: bsd
12632 * c-basic-offset: 4
12633 * indent-tabs-mode: t
12634 * End:
12635 *
37442d52
RGS
12636 * ex: set ts=8 sts=4 sw=4 noet:
12637 */