This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / sv.c
CommitLineData
a0d0e21e 1/* sv.c
79072805 2 *
e6906430 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
583439ab 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
645c22ef 26
47185da9
NC
27#ifdef __Lynx__
28/* Missing proto on LynxOS */
29 char *gconvert(double, int, int, char *);
30#endif
31
d6218095
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
645c22ef
DM
50/* ============================================================================
51
52=head1 Allocation and deallocation of SVs.
53
5e045b90
AMS
54An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv,
55av, hv...) contains type and reference count information, as well as a
56pointer to the body (struct xrv, xpv, xpviv...), which contains fields
57specific to each type.
58
8298e346
NC
59Normally, this allocation is done using arenas, which by default are
60approximately 4K chunks of memory parcelled up into N heads or bodies. The
61first slot in each arena is reserved, and is used to hold a link to the next
62arena. In the case of heads, the unused first slot also contains some flags
63and a note of the number of slots. Snaked through each arena chain is a
5e045b90 64linked list of free items; when this becomes empty, an extra arena is
8298e346 65allocated and divided up into N items which are threaded into the free list.
645c22ef
DM
66
67The following global variables are associated with arenas:
68
69 PL_sv_arenaroot pointer to list of SV arenas
70 PL_sv_root pointer to list of free SV structures
71
72 PL_foo_arenaroot pointer to list of foo arenas,
73 PL_foo_root pointer to list of free foo bodies
74 ... for foo in xiv, xnv, xrv, xpv etc.
75
76Note that some of the larger and more rarely used body types (eg xpvio)
77are not allocated using arenas, but are instead just malloc()/free()ed as
78required. Also, if PURIFY is defined, arenas are abandoned altogether,
79with all items individually malloc()ed. In addition, a few SV heads are
80not allocated from an arena, but are instead directly created as static
8298e346
NC
81or auto variables, eg PL_sv_undef. The size of arenas can be changed from
82the default by setting PERL_ARENA_SIZE appropriately at compile time.
645c22ef
DM
83
84The SV arena serves the secondary purpose of allowing still-live SVs
85to be located and destroyed during final cleanup.
86
87At the lowest level, the macros new_SV() and del_SV() grab and free
88an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
89to return the SV to the free list with error checking.) new_SV() calls
90more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
91SVs in the free list have their SvTYPE field set to all ones.
92
93Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
94that allocate and return individual body types. Normally these are mapped
ff276b08
RG
95to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be
96instead mapped directly to malloc()/free() if PURIFY is defined. The
645c22ef
DM
97new/del functions remove from, or add to, the appropriate PL_foo_root
98list, and call more_xiv() etc to add a new arena if the list is empty.
99
ff276b08 100At the time of very final cleanup, sv_free_arenas() is called from
645c22ef
DM
101perl_destruct() to physically free all the arenas allocated since the
102start of the interpreter. Note that this also clears PL_he_arenaroot,
103which is otherwise dealt with in hv.c.
104
105Manipulation of any of the PL_*root pointers is protected by enclosing
106LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
107if threads are enabled.
108
109The function visit() scans the SV arenas list, and calls a specified
110function for each SV it finds which is still live - ie which has an SvTYPE
111other than all 1's, and a non-zero SvREFCNT. visit() is used by the
112following functions (specified as [function that calls visit()] / [function
113called by visit() for each SV]):
114
115 sv_report_used() / do_report_used()
116 dump all remaining SVs (debugging aid)
117
118 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
119 Attempt to free all objects pointed to by RVs,
120 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
121 try to do the same for all objects indirectly
122 referenced by typeglobs too. Called once from
123 perl_destruct(), prior to calling sv_clean_all()
124 below.
125
126 sv_clean_all() / do_clean_all()
127 SvREFCNT_dec(sv) each remaining SV, possibly
128 triggering an sv_free(). It also sets the
129 SVf_BREAK flag on the SV to indicate that the
130 refcnt has been artificially lowered, and thus
131 stopping sv_free() from giving spurious warnings
132 about SVs which unexpectedly have a refcnt
133 of zero. called repeatedly from perl_destruct()
134 until there are no SVs left.
135
136=head2 Summary
137
138Private API to rest of sv.c
139
140 new_SV(), del_SV(),
141
142 new_XIV(), del_XIV(),
143 new_XNV(), del_XNV(),
144 etc
145
146Public API:
147
8cf8f3d1 148 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
645c22ef
DM
149
150
151=cut
152
153============================================================================ */
154
155
51371543 156
4561caa4
CS
157/*
158 * "A time to plant, and a time to uproot what was planted..."
159 */
160
69ddb3b9 161
053fc874
GS
162#define plant_SV(p) \
163 STMT_START { \
164 SvANY(p) = (void *)PL_sv_root; \
165 SvFLAGS(p) = SVTYPEMASK; \
166 PL_sv_root = (p); \
167 --PL_sv_count; \
168 } STMT_END
a0d0e21e 169
fba3b22e 170/* sv_mutex must be held while calling uproot_SV() */
053fc874
GS
171#define uproot_SV(p) \
172 STMT_START { \
173 (p) = PL_sv_root; \
174 PL_sv_root = (SV*)SvANY(p); \
175 ++PL_sv_count; \
176 } STMT_END
177
645c22ef 178
69ddb3b9
NC
179/* make some more SVs by adding another arena */
180
181/* sv_mutex must be held while calling more_sv() */
182STATIC SV*
183S_more_sv(pTHX)
184{
185 SV* sv;
186
187 if (PL_nice_chunk) {
188 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
189 PL_nice_chunk = Nullch;
190 PL_nice_chunk_size = 0;
191 }
192 else {
193 char *chunk; /* must use New here to match call to */
194 New(704,chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
195 sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
196 }
197 uproot_SV(sv);
198 return sv;
199}
200
645c22ef
DM
201/* new_SV(): return a new, empty SV head */
202
c240c76d
JH
203#ifdef DEBUG_LEAKING_SCALARS
204/* provide a real function for a debugger to play with */
205STATIC SV*
206S_new_SV(pTHX)
207{
208 SV* sv;
209
210 LOCK_SV_MUTEX;
211 if (PL_sv_root)
212 uproot_SV(sv);
213 else
69ddb3b9 214 sv = S_more_sv(aTHX);
c240c76d
JH
215 UNLOCK_SV_MUTEX;
216 SvANY(sv) = 0;
217 SvREFCNT(sv) = 1;
218 SvFLAGS(sv) = 0;
219 return sv;
220}
221# define new_SV(p) (p)=S_new_SV(aTHX)
222
223#else
224# define new_SV(p) \
053fc874
GS
225 STMT_START { \
226 LOCK_SV_MUTEX; \
227 if (PL_sv_root) \
228 uproot_SV(p); \
229 else \
69ddb3b9 230 (p) = S_more_sv(aTHX); \
053fc874
GS
231 UNLOCK_SV_MUTEX; \
232 SvANY(p) = 0; \
233 SvREFCNT(p) = 1; \
234 SvFLAGS(p) = 0; \
235 } STMT_END
c240c76d 236#endif
463ee0b2 237
645c22ef
DM
238
239/* del_SV(): return an empty SV head to the free list */
240
a0d0e21e 241#ifdef DEBUGGING
4561caa4 242
053fc874
GS
243#define del_SV(p) \
244 STMT_START { \
245 LOCK_SV_MUTEX; \
aea4f609 246 if (DEBUG_D_TEST) \
053fc874
GS
247 del_sv(p); \
248 else \
249 plant_SV(p); \
250 UNLOCK_SV_MUTEX; \
251 } STMT_END
a0d0e21e 252
76e3520e 253STATIC void
cea2e8a9 254S_del_sv(pTHX_ SV *p)
463ee0b2 255{
aea4f609 256 if (DEBUG_D_TEST) {
4633a7c4 257 SV* sva;
8c18bf38 258 bool ok = 0;
3280af22 259 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
8c18bf38
AL
260 SV *sv = sva + 1;
261 SV *svend = &sva[SvREFCNT(sva)];
2a8de9e2 262 if (p >= sv && p < svend) {
a0d0e21e 263 ok = 1;
2a8de9e2
AL
264 break;
265 }
a0d0e21e
LW
266 }
267 if (!ok) {
0453d815 268 if (ckWARN_d(WARN_INTERNAL))
9014280d 269 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
b035a42e
NC
270 "Attempt to free non-arena SV: 0x%"UVxf
271 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
a0d0e21e
LW
272 return;
273 }
274 }
4561caa4 275 plant_SV(p);
463ee0b2 276}
a0d0e21e 277
4561caa4
CS
278#else /* ! DEBUGGING */
279
280#define del_SV(p) plant_SV(p)
281
282#endif /* DEBUGGING */
463ee0b2 283
645c22ef
DM
284
285/*
ccfc67b7
JH
286=head1 SV Manipulation Functions
287
645c22ef
DM
288=for apidoc sv_add_arena
289
290Given a chunk of memory, link it to the head of the list of arenas,
291and split it into a list of free SVs.
292
293=cut
294*/
295
4633a7c4 296void
864dbfa3 297Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
463ee0b2 298{
4633a7c4 299 SV* sva = (SV*)ptr;
463ee0b2
LW
300 register SV* sv;
301 register SV* svend;
4633a7c4
LW
302
303 /* The first SV in an arena isn't an SV. */
3280af22 304 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
4633a7c4
LW
305 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
306 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
307
3280af22
NIS
308 PL_sv_arenaroot = sva;
309 PL_sv_root = sva + 1;
4633a7c4
LW
310
311 svend = &sva[SvREFCNT(sva) - 1];
312 sv = sva + 1;
463ee0b2 313 while (sv < svend) {
a0d0e21e 314 SvANY(sv) = (void *)(SV*)(sv + 1);
2a8de9e2 315#ifdef DEBUGGING
8b3a4b74 316 SvREFCNT(sv) = 0;
2a8de9e2
AL
317#endif
318 /* Must always set typemask because it's awlays checked in on cleanup
319 when the arenas are walked looking for objects. */
8990e307 320 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
321 sv++;
322 }
323 SvANY(sv) = 0;
2a8de9e2
AL
324#ifdef DEBUGGING
325 SvREFCNT(sv) = 0;
326#endif
4633a7c4
LW
327 SvFLAGS(sv) = SVTYPEMASK;
328}
329
b035a42e
NC
330/* visit(): call the named function for each non-free SV in the arenas
331 * whose flags field matches the flags/mask args. */
645c22ef 332
5226ed68 333STATIC I32
b035a42e 334S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
8990e307 335{
4633a7c4 336 SV* sva;
5226ed68 337 I32 visited = 0;
8990e307 338
3280af22 339 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
8c18bf38
AL
340 register SV * const svend = &sva[SvREFCNT(sva)];
341 register SV* sv;
4561caa4 342 for (sv = sva + 1; sv < svend; ++sv) {
b035a42e
NC
343 if (SvTYPE(sv) != SVTYPEMASK
344 && (sv->sv_flags & mask) == flags
345 && SvREFCNT(sv))
346 {
acfe0abc 347 (FCALL)(aTHX_ sv);
5226ed68
JH
348 ++visited;
349 }
8990e307
LW
350 }
351 }
5226ed68 352 return visited;
8990e307
LW
353}
354
758a08c3
JH
355#ifdef DEBUGGING
356
645c22ef
DM
357/* called by sv_report_used() for each live SV */
358
359static void
acfe0abc 360do_report_used(pTHX_ SV *sv)
645c22ef
DM
361{
362 if (SvTYPE(sv) != SVTYPEMASK) {
363 PerlIO_printf(Perl_debug_log, "****\n");
364 sv_dump(sv);
365 }
366}
758a08c3 367#endif
645c22ef
DM
368
369/*
370=for apidoc sv_report_used
371
372Dump the contents of all SVs not yet freed. (Debugging aid).
373
374=cut
375*/
376
8990e307 377void
864dbfa3 378Perl_sv_report_used(pTHX)
4561caa4 379{
ff270d3a 380#ifdef DEBUGGING
b035a42e 381 visit(do_report_used, 0, 0);
ff270d3a 382#endif
4561caa4
CS
383}
384
645c22ef
DM
385/* called by sv_clean_objs() for each live SV */
386
387static void
acfe0abc 388do_clean_objs(pTHX_ SV *sv)
645c22ef
DM
389{
390 SV* rv;
391
392 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
393 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv)));
394 if (SvWEAKREF(sv)) {
395 sv_del_backref(sv);
396 SvWEAKREF_off(sv);
a8dc4fe8 397 SvRV_set(sv, NULL);
645c22ef
DM
398 } else {
399 SvROK_off(sv);
a8dc4fe8 400 SvRV_set(sv, NULL);
645c22ef
DM
401 SvREFCNT_dec(rv);
402 }
403 }
404
405 /* XXX Might want to check arrays, etc. */
406}
407
408/* called by sv_clean_objs() for each live SV */
409
410#ifndef DISABLE_DESTRUCTOR_KLUDGE
411static void
acfe0abc 412do_clean_named_objs(pTHX_ SV *sv)
645c22ef
DM
413{
414 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
415 if ( SvOBJECT(GvSV(sv)) ||
416 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
417 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
418 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
419 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
420 {
421 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
1f26b251 422 SvFLAGS(sv) |= SVf_BREAK;
645c22ef
DM
423 SvREFCNT_dec(sv);
424 }
425 }
426}
427#endif
428
429/*
430=for apidoc sv_clean_objs
431
432Attempt to destroy all objects not yet freed
433
434=cut
435*/
436
4561caa4 437void
864dbfa3 438Perl_sv_clean_objs(pTHX)
4561caa4 439{
3280af22 440 PL_in_clean_objs = TRUE;
b035a42e 441 visit(do_clean_objs, SVf_ROK, SVf_ROK);
4561caa4 442#ifndef DISABLE_DESTRUCTOR_KLUDGE
2d0f3c12 443 /* some barnacles may yet remain, clinging to typeglobs */
b035a42e 444 visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
4561caa4 445#endif
3280af22 446 PL_in_clean_objs = FALSE;
4561caa4
CS
447}
448
645c22ef
DM
449/* called by sv_clean_all() for each live SV */
450
451static void
acfe0abc 452do_clean_all(pTHX_ SV *sv)
645c22ef
DM
453{
454 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
455 SvFLAGS(sv) |= SVf_BREAK;
456 SvREFCNT_dec(sv);
457}
458
459/*
460=for apidoc sv_clean_all
461
462Decrement the refcnt of each remaining SV, possibly triggering a
463cleanup. This function may have to be called multiple times to free
ff276b08 464SVs which are in complex self-referential hierarchies.
645c22ef
DM
465
466=cut
467*/
468
5226ed68 469I32
864dbfa3 470Perl_sv_clean_all(pTHX)
8990e307 471{
5226ed68 472 I32 cleaned;
3280af22 473 PL_in_clean_all = TRUE;
b035a42e 474 cleaned = visit(do_clean_all, 0,0);
3280af22 475 PL_in_clean_all = FALSE;
5226ed68 476 return cleaned;
8990e307 477}
463ee0b2 478
645c22ef
DM
479/*
480=for apidoc sv_free_arenas
481
482Deallocate the memory used by all arenas. Note that all the individual SV
483heads and bodies within the arenas must already have been freed.
484
485=cut
486*/
487
4633a7c4 488void
864dbfa3 489Perl_sv_free_arenas(pTHX)
4633a7c4
LW
490{
491 SV* sva;
492 SV* svanext;
612f20c3 493 XPV *arena, *arenanext;
4633a7c4
LW
494
495 /* Free arenas here, but be careful about fake ones. (We assume
496 contiguity of the fake ones with the corresponding real ones.) */
497
3280af22 498 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
4633a7c4
LW
499 svanext = (SV*) SvANY(sva);
500 while (svanext && SvFAKE(svanext))
501 svanext = (SV*) SvANY(svanext);
502
503 if (!SvFAKE(sva))
228fe6e6 504 Safefree(sva);
4633a7c4 505 }
5f05dabc 506
612f20c3
GS
507 for (arena = PL_xiv_arenaroot; arena; arena = arenanext) {
508 arenanext = (XPV*)arena->xpv_pv;
509 Safefree(arena);
510 }
511 PL_xiv_arenaroot = 0;
d5aea225 512 PL_xiv_root = 0;
612f20c3
GS
513
514 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
515 arenanext = (XPV*)arena->xpv_pv;
516 Safefree(arena);
517 }
518 PL_xnv_arenaroot = 0;
d5aea225 519 PL_xnv_root = 0;
612f20c3
GS
520
521 for (arena = PL_xrv_arenaroot; arena; arena = arenanext) {
522 arenanext = (XPV*)arena->xpv_pv;
523 Safefree(arena);
524 }
525 PL_xrv_arenaroot = 0;
d5aea225 526 PL_xrv_root = 0;
612f20c3
GS
527
528 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
529 arenanext = (XPV*)arena->xpv_pv;
530 Safefree(arena);
531 }
532 PL_xpv_arenaroot = 0;
d5aea225 533 PL_xpv_root = 0;
612f20c3
GS
534
535 for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) {
536 arenanext = (XPV*)arena->xpv_pv;
537 Safefree(arena);
538 }
539 PL_xpviv_arenaroot = 0;
d5aea225 540 PL_xpviv_root = 0;
612f20c3
GS
541
542 for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) {
543 arenanext = (XPV*)arena->xpv_pv;
544 Safefree(arena);
545 }
546 PL_xpvnv_arenaroot = 0;
d5aea225 547 PL_xpvnv_root = 0;
612f20c3
GS
548
549 for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) {
550 arenanext = (XPV*)arena->xpv_pv;
551 Safefree(arena);
552 }
553 PL_xpvcv_arenaroot = 0;
d5aea225 554 PL_xpvcv_root = 0;
612f20c3
GS
555
556 for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) {
557 arenanext = (XPV*)arena->xpv_pv;
558 Safefree(arena);
559 }
560 PL_xpvav_arenaroot = 0;
d5aea225 561 PL_xpvav_root = 0;
612f20c3
GS
562
563 for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) {
564 arenanext = (XPV*)arena->xpv_pv;
565 Safefree(arena);
566 }
567 PL_xpvhv_arenaroot = 0;
d5aea225 568 PL_xpvhv_root = 0;
612f20c3
GS
569
570 for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) {
571 arenanext = (XPV*)arena->xpv_pv;
572 Safefree(arena);
573 }
574 PL_xpvmg_arenaroot = 0;
d5aea225 575 PL_xpvmg_root = 0;
612f20c3 576
69ddb3b9
NC
577 for (arena = (XPV*)PL_xpvgv_arenaroot; arena; arena = arenanext) {
578 arenanext = (XPV*)arena->xpv_pv;
579 Safefree(arena);
580 }
581 PL_xpvgv_arenaroot = 0;
582 PL_xpvgv_root = 0;
583
612f20c3
GS
584 for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) {
585 arenanext = (XPV*)arena->xpv_pv;
586 Safefree(arena);
587 }
588 PL_xpvlv_arenaroot = 0;
d5aea225 589 PL_xpvlv_root = 0;
612f20c3
GS
590
591 for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) {
592 arenanext = (XPV*)arena->xpv_pv;
593 Safefree(arena);
594 }
595 PL_xpvbm_arenaroot = 0;
d5aea225 596 PL_xpvbm_root = 0;
612f20c3
GS
597
598 for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) {
599 arenanext = (XPV*)arena->xpv_pv;
600 Safefree(arena);
601 }
602 PL_he_arenaroot = 0;
d5aea225 603 PL_he_root = 0;
612f20c3 604
72c9d3b6
NC
605#if defined(USE_ITHREADS)
606 for (arena = (XPV*)PL_pte_arenaroot; arena; arena = arenanext) {
607 arenanext = (XPV*)arena->xpv_pv;
608 Safefree(arena);
609 }
610 PL_pte_arenaroot = 0;
611 PL_pte_root = 0;
612#endif
613
3280af22
NIS
614 if (PL_nice_chunk)
615 Safefree(PL_nice_chunk);
616 PL_nice_chunk = Nullch;
617 PL_nice_chunk_size = 0;
618 PL_sv_arenaroot = 0;
619 PL_sv_root = 0;
4633a7c4
LW
620}
621
645c22ef
DM
622/*
623=for apidoc report_uninit
624
625Print appropriate "Use of uninitialized variable" warning
626
627=cut
628*/
629
1d7c1841
GS
630void
631Perl_report_uninit(pTHX)
632{
633 if (PL_op)
9014280d 634 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
53e06cf0 635 " in ", OP_DESC(PL_op));
1d7c1841 636 else
9014280d 637 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit, "", "");
1d7c1841
GS
638}
639
645c22ef 640
69ddb3b9 641/* allocate another arena's worth of struct xrv */
645c22ef 642
76e3520e 643STATIC void
69ddb3b9 644S_more_xrv(pTHX)
463ee0b2 645{
69ddb3b9
NC
646 XRV* xrv;
647 XRV* xrvend;
648 XPV *ptr;
649 New(712, ptr, PERL_ARENA_SIZE/sizeof(XPV), XPV);
650 ptr->xpv_pv = (char*)PL_xrv_arenaroot;
651 PL_xrv_arenaroot = ptr;
652
653 xrv = (XRV*) ptr;
654 xrvend = &xrv[PERL_ARENA_SIZE / sizeof(XRV) - 1];
655 xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1;
656 PL_xrv_root = xrv;
657 while (xrv < xrvend) {
658 xrv->xrv_rv = (SV*)(xrv + 1);
659 xrv++;
660 }
661 xrv->xrv_rv = 0;
463ee0b2
LW
662}
663
645c22ef
DM
664/* allocate another arena's worth of IV bodies */
665
cbe51380 666STATIC void
cea2e8a9 667S_more_xiv(pTHX)
463ee0b2 668{
69ddb3b9
NC
669 IV* xiv;
670 IV* xivend;
8c52afec 671 XPV* ptr;
72c9d3b6 672 New(705, ptr, PERL_ARENA_SIZE/sizeof(XPV), XPV);
645c22ef 673 ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */
3280af22 674 PL_xiv_arenaroot = ptr; /* to keep Purify happy */
a0d0e21e 675
ea7c11a3 676 xiv = (IV*) ptr;
72c9d3b6 677 xivend = &xiv[PERL_ARENA_SIZE / sizeof(IV) - 1];
645c22ef 678 xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */
3280af22 679 PL_xiv_root = xiv;
463ee0b2 680 while (xiv < xivend) {
ea7c11a3 681 *(IV**)xiv = (IV *)(xiv + 1);
463ee0b2
LW
682 xiv++;
683 }
ea7c11a3 684 *(IV**)xiv = 0;
463ee0b2
LW
685}
686
69ddb3b9 687/* allocate another arena's worth of NV bodies */
645c22ef 688
69ddb3b9
NC
689STATIC void
690S_more_xnv(pTHX)
463ee0b2 691{
65202027 692 NV* xnv;
69ddb3b9
NC
693 NV* xnvend;
694 XPV *ptr;
695 New(711, ptr, PERL_ARENA_SIZE/sizeof(XPV), XPV);
696 ptr->xpv_pv = (char*)PL_xnv_arenaroot;
697 PL_xnv_arenaroot = ptr;
698
699 xnv = (NV*) ptr;
700 xnvend = &xnv[PERL_ARENA_SIZE / sizeof(NV) - 1];
701 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
702 PL_xnv_root = xnv;
703 while (xnv < xnvend) {
704 *(NV**)xnv = (NV*)(xnv + 1);
705 xnv++;
706 }
707 *(NV**)xnv = 0;
463ee0b2
LW
708}
709
69ddb3b9 710/* allocate another arena's worth of struct xpv */
645c22ef 711
76e3520e 712STATIC void
69ddb3b9 713S_more_xpv(pTHX)
463ee0b2 714{
69ddb3b9
NC
715 XPV* xpv;
716 XPV* xpvend;
717 New(713, xpv, PERL_ARENA_SIZE/sizeof(XPV), XPV);
718 xpv->xpv_pv = (char*)PL_xpv_arenaroot;
719 PL_xpv_arenaroot = xpv;
720
721 xpvend = &xpv[PERL_ARENA_SIZE / sizeof(XPV) - 1];
722 PL_xpv_root = ++xpv;
723 while (xpv < xpvend) {
724 xpv->xpv_pv = (char*)(xpv + 1);
725 xpv++;
726 }
727 xpv->xpv_pv = 0;
463ee0b2
LW
728}
729
69ddb3b9 730/* allocate another arena's worth of struct xpviv */
645c22ef 731
cbe51380 732STATIC void
69ddb3b9 733S_more_xpviv(pTHX)
463ee0b2 734{
69ddb3b9
NC
735 XPVIV* xpviv;
736 XPVIV* xpvivend;
737 New(714, xpviv, PERL_ARENA_SIZE/sizeof(XPVIV), XPVIV);
738 xpviv->xpv_pv = (char*)PL_xpviv_arenaroot;
739 PL_xpviv_arenaroot = xpviv;
740
741 xpvivend = &xpviv[PERL_ARENA_SIZE / sizeof(XPVIV) - 1];
742 PL_xpviv_root = ++xpviv;
743 while (xpviv < xpvivend) {
744 xpviv->xpv_pv = (char*)(xpviv + 1);
745 xpviv++;
746 }
747 xpviv->xpv_pv = 0;
748}
749
750/* allocate another arena's worth of struct xpvnv */
751
752STATIC void
753S_more_xpvnv(pTHX)
754{
755 XPVNV* xpvnv;
756 XPVNV* xpvnvend;
757 New(715, xpvnv, PERL_ARENA_SIZE/sizeof(XPVNV), XPVNV);
758 xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot;
759 PL_xpvnv_arenaroot = xpvnv;
760
761 xpvnvend = &xpvnv[PERL_ARENA_SIZE / sizeof(XPVNV) - 1];
762 PL_xpvnv_root = ++xpvnv;
763 while (xpvnv < xpvnvend) {
764 xpvnv->xpv_pv = (char*)(xpvnv + 1);
765 xpvnv++;
766 }
767 xpvnv->xpv_pv = 0;
768}
769
770/* allocate another arena's worth of struct xpvcv */
771
772STATIC void
773S_more_xpvcv(pTHX)
774{
775 XPVCV* xpvcv;
776 XPVCV* xpvcvend;
777 New(716, xpvcv, PERL_ARENA_SIZE/sizeof(XPVCV), XPVCV);
778 xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot;
779 PL_xpvcv_arenaroot = xpvcv;
780
781 xpvcvend = &xpvcv[PERL_ARENA_SIZE / sizeof(XPVCV) - 1];
782 PL_xpvcv_root = ++xpvcv;
783 while (xpvcv < xpvcvend) {
784 xpvcv->xpv_pv = (char*)(xpvcv + 1);
785 xpvcv++;
786 }
787 xpvcv->xpv_pv = 0;
788}
789
790/* allocate another arena's worth of struct xpvav */
791
792STATIC void
793S_more_xpvav(pTHX)
794{
795 XPVAV* xpvav;
796 XPVAV* xpvavend;
797 New(717, xpvav, PERL_ARENA_SIZE/sizeof(XPVAV), XPVAV);
798 xpvav->xav_array = (char*)PL_xpvav_arenaroot;
799 PL_xpvav_arenaroot = xpvav;
800
801 xpvavend = &xpvav[PERL_ARENA_SIZE / sizeof(XPVAV) - 1];
802 PL_xpvav_root = ++xpvav;
803 while (xpvav < xpvavend) {
804 xpvav->xav_array = (char*)(xpvav + 1);
805 xpvav++;
806 }
807 xpvav->xav_array = 0;
808}
809
810/* allocate another arena's worth of struct xpvhv */
811
812STATIC void
813S_more_xpvhv(pTHX)
814{
815 XPVHV* xpvhv;
816 XPVHV* xpvhvend;
817 New(718, xpvhv, PERL_ARENA_SIZE/sizeof(XPVHV), XPVHV);
818 xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot;
819 PL_xpvhv_arenaroot = xpvhv;
820
821 xpvhvend = &xpvhv[PERL_ARENA_SIZE / sizeof(XPVHV) - 1];
822 PL_xpvhv_root = ++xpvhv;
823 while (xpvhv < xpvhvend) {
824 xpvhv->xhv_array = (char*)(xpvhv + 1);
825 xpvhv++;
826 }
827 xpvhv->xhv_array = 0;
828}
829
830/* allocate another arena's worth of struct xpvmg */
831
832STATIC void
833S_more_xpvmg(pTHX)
834{
835 XPVMG* xpvmg;
836 XPVMG* xpvmgend;
837 New(719, xpvmg, PERL_ARENA_SIZE/sizeof(XPVMG), XPVMG);
838 xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot;
839 PL_xpvmg_arenaroot = xpvmg;
840
841 xpvmgend = &xpvmg[PERL_ARENA_SIZE / sizeof(XPVMG) - 1];
842 PL_xpvmg_root = ++xpvmg;
843 while (xpvmg < xpvmgend) {
844 xpvmg->xpv_pv = (char*)(xpvmg + 1);
845 xpvmg++;
846 }
847 xpvmg->xpv_pv = 0;
848}
849
850/* allocate another arena's worth of struct xpvgv */
851
852STATIC void
853S_more_xpvgv(pTHX)
854{
855 XPVGV* xpvgv;
856 XPVGV* xpvgvend;
857 New(720, xpvgv, PERL_ARENA_SIZE/sizeof(XPVGV), XPVGV);
858 xpvgv->xpv_pv = (char*)PL_xpvgv_arenaroot;
859 PL_xpvgv_arenaroot = xpvgv;
860
861 xpvgvend = &xpvgv[PERL_ARENA_SIZE / sizeof(XPVGV) - 1];
862 PL_xpvgv_root = ++xpvgv;
863 while (xpvgv < xpvgvend) {
864 xpvgv->xpv_pv = (char*)(xpvgv + 1);
865 xpvgv++;
866 }
867 xpvgv->xpv_pv = 0;
868}
869
870/* allocate another arena's worth of struct xpvlv */
871
872STATIC void
873S_more_xpvlv(pTHX)
874{
875 XPVLV* xpvlv;
876 XPVLV* xpvlvend;
877 New(720, xpvlv, PERL_ARENA_SIZE/sizeof(XPVLV), XPVLV);
878 xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot;
879 PL_xpvlv_arenaroot = xpvlv;
880
881 xpvlvend = &xpvlv[PERL_ARENA_SIZE / sizeof(XPVLV) - 1];
882 PL_xpvlv_root = ++xpvlv;
883 while (xpvlv < xpvlvend) {
884 xpvlv->xpv_pv = (char*)(xpvlv + 1);
885 xpvlv++;
886 }
887 xpvlv->xpv_pv = 0;
888}
889
890/* allocate another arena's worth of struct xpvbm */
891
892STATIC void
893S_more_xpvbm(pTHX)
894{
895 XPVBM* xpvbm;
896 XPVBM* xpvbmend;
897 New(721, xpvbm, PERL_ARENA_SIZE/sizeof(XPVBM), XPVBM);
898 xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot;
899 PL_xpvbm_arenaroot = xpvbm;
612f20c3 900
69ddb3b9
NC
901 xpvbmend = &xpvbm[PERL_ARENA_SIZE / sizeof(XPVBM) - 1];
902 PL_xpvbm_root = ++xpvbm;
903 while (xpvbm < xpvbmend) {
904 xpvbm->xpv_pv = (char*)(xpvbm + 1);
905 xpvbm++;
463ee0b2 906 }
69ddb3b9 907 xpvbm->xpv_pv = 0;
463ee0b2
LW
908}
909
645c22ef
DM
910/* grab a new struct xrv from the free list, allocating more if necessary */
911
76e3520e 912STATIC XRV*
cea2e8a9 913S_new_xrv(pTHX)
ed6116ce
LW
914{
915 XRV* xrv;
cbe51380
GS
916 LOCK_SV_MUTEX;
917 if (!PL_xrv_root)
69ddb3b9 918 S_more_xrv(aTHX);
cbe51380
GS
919 xrv = PL_xrv_root;
920 PL_xrv_root = (XRV*)xrv->xrv_rv;
921 UNLOCK_SV_MUTEX;
922 return xrv;
ed6116ce
LW
923}
924
645c22ef
DM
925/* return a struct xrv to the free list */
926
76e3520e 927STATIC void
cea2e8a9 928S_del_xrv(pTHX_ XRV *p)
ed6116ce 929{
cbe51380 930 LOCK_SV_MUTEX;
3280af22
NIS
931 p->xrv_rv = (SV*)PL_xrv_root;
932 PL_xrv_root = p;
cbe51380 933 UNLOCK_SV_MUTEX;
ed6116ce
LW
934}
935
69ddb3b9
NC
936/* grab a new IV body from the free list, allocating more if necessary */
937
938STATIC XPVIV*
939S_new_xiv(pTHX)
940{
941 IV* xiv;
942 LOCK_SV_MUTEX;
943 if (!PL_xiv_root)
944 S_more_xiv(aTHX);
945 xiv = PL_xiv_root;
946 /*
947 * See comment in more_xiv() -- RAM.
948 */
949 PL_xiv_root = *(IV**)xiv;
950 UNLOCK_SV_MUTEX;
951 return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv));
952}
953
954/* return an IV body to the free list */
645c22ef 955
cbe51380 956STATIC void
69ddb3b9 957S_del_xiv(pTHX_ XPVIV *p)
ed6116ce 958{
69ddb3b9
NC
959 IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv));
960 LOCK_SV_MUTEX;
961 *(IV**)xiv = PL_xiv_root;
962 PL_xiv_root = xiv;
963 UNLOCK_SV_MUTEX;
964}
612f20c3 965
69ddb3b9
NC
966/* grab a new NV body from the free list, allocating more if necessary */
967
968STATIC XPVNV*
969S_new_xnv(pTHX)
970{
971 NV* xnv;
972 LOCK_SV_MUTEX;
973 if (!PL_xnv_root)
974 S_more_xnv(aTHX);
975 xnv = PL_xnv_root;
976 PL_xnv_root = *(NV**)xnv;
977 UNLOCK_SV_MUTEX;
978 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
979}
980
981/* return an NV body to the free list */
982
983STATIC void
984S_del_xnv(pTHX_ XPVNV *p)
985{
986 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
987 LOCK_SV_MUTEX;
988 *(NV**)xnv = PL_xnv_root;
989 PL_xnv_root = xnv;
990 UNLOCK_SV_MUTEX;
ed6116ce
LW
991}
992
645c22ef
DM
993/* grab a new struct xpv from the free list, allocating more if necessary */
994
76e3520e 995STATIC XPV*
cea2e8a9 996S_new_xpv(pTHX)
463ee0b2
LW
997{
998 XPV* xpv;
cbe51380
GS
999 LOCK_SV_MUTEX;
1000 if (!PL_xpv_root)
69ddb3b9 1001 S_more_xpv(aTHX);
cbe51380
GS
1002 xpv = PL_xpv_root;
1003 PL_xpv_root = (XPV*)xpv->xpv_pv;
1004 UNLOCK_SV_MUTEX;
1005 return xpv;
463ee0b2
LW
1006}
1007
645c22ef
DM
1008/* return a struct xpv to the free list */
1009
76e3520e 1010STATIC void
cea2e8a9 1011S_del_xpv(pTHX_ XPV *p)
463ee0b2 1012{
cbe51380 1013 LOCK_SV_MUTEX;
3280af22
NIS
1014 p->xpv_pv = (char*)PL_xpv_root;
1015 PL_xpv_root = p;
cbe51380 1016 UNLOCK_SV_MUTEX;
463ee0b2
LW
1017}
1018
645c22ef
DM
1019/* grab a new struct xpviv from the free list, allocating more if necessary */
1020
932e9ff9
VB
1021STATIC XPVIV*
1022S_new_xpviv(pTHX)
1023{
1024 XPVIV* xpviv;
1025 LOCK_SV_MUTEX;
1026 if (!PL_xpviv_root)
69ddb3b9 1027 S_more_xpviv(aTHX);
932e9ff9
VB
1028 xpviv = PL_xpviv_root;
1029 PL_xpviv_root = (XPVIV*)xpviv->xpv_pv;
1030 UNLOCK_SV_MUTEX;
1031 return xpviv;
1032}
1033
645c22ef
DM
1034/* return a struct xpviv to the free list */
1035
932e9ff9
VB
1036STATIC void
1037S_del_xpviv(pTHX_ XPVIV *p)
1038{
1039 LOCK_SV_MUTEX;
1040 p->xpv_pv = (char*)PL_xpviv_root;
1041 PL_xpviv_root = p;
1042 UNLOCK_SV_MUTEX;
1043}
1044
645c22ef
DM
1045/* grab a new struct xpvnv from the free list, allocating more if necessary */
1046
932e9ff9
VB
1047STATIC XPVNV*
1048S_new_xpvnv(pTHX)
1049{
1050 XPVNV* xpvnv;
1051 LOCK_SV_MUTEX;
1052 if (!PL_xpvnv_root)
69ddb3b9 1053 S_more_xpvnv(aTHX);
932e9ff9
VB
1054 xpvnv = PL_xpvnv_root;
1055 PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv;
1056 UNLOCK_SV_MUTEX;
1057 return xpvnv;
1058}
1059
645c22ef
DM
1060/* return a struct xpvnv to the free list */
1061
932e9ff9
VB
1062STATIC void
1063S_del_xpvnv(pTHX_ XPVNV *p)
1064{
1065 LOCK_SV_MUTEX;
1066 p->xpv_pv = (char*)PL_xpvnv_root;
1067 PL_xpvnv_root = p;
1068 UNLOCK_SV_MUTEX;
1069}
1070
645c22ef
DM
1071/* grab a new struct xpvcv from the free list, allocating more if necessary */
1072
932e9ff9
VB
1073STATIC XPVCV*
1074S_new_xpvcv(pTHX)
1075{
1076 XPVCV* xpvcv;
1077 LOCK_SV_MUTEX;
1078 if (!PL_xpvcv_root)
69ddb3b9 1079 S_more_xpvcv(aTHX);
932e9ff9
VB
1080 xpvcv = PL_xpvcv_root;
1081 PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv;
1082 UNLOCK_SV_MUTEX;
1083 return xpvcv;
1084}
1085
645c22ef
DM
1086/* return a struct xpvcv to the free list */
1087
932e9ff9
VB
1088STATIC void
1089S_del_xpvcv(pTHX_ XPVCV *p)
1090{
1091 LOCK_SV_MUTEX;
1092 p->xpv_pv = (char*)PL_xpvcv_root;
1093 PL_xpvcv_root = p;
1094 UNLOCK_SV_MUTEX;
1095}
1096
645c22ef
DM
1097/* grab a new struct xpvav from the free list, allocating more if necessary */
1098
932e9ff9
VB
1099STATIC XPVAV*
1100S_new_xpvav(pTHX)
1101{
1102 XPVAV* xpvav;
1103 LOCK_SV_MUTEX;
1104 if (!PL_xpvav_root)
69ddb3b9 1105 S_more_xpvav(aTHX);
932e9ff9
VB
1106 xpvav = PL_xpvav_root;
1107 PL_xpvav_root = (XPVAV*)xpvav->xav_array;
1108 UNLOCK_SV_MUTEX;
1109 return xpvav;
1110}
1111
645c22ef
DM
1112/* return a struct xpvav to the free list */
1113
932e9ff9
VB
1114STATIC void
1115S_del_xpvav(pTHX_ XPVAV *p)
1116{
1117 LOCK_SV_MUTEX;
1118 p->xav_array = (char*)PL_xpvav_root;
1119 PL_xpvav_root = p;
1120 UNLOCK_SV_MUTEX;
1121}
1122
645c22ef
DM
1123/* grab a new struct xpvhv from the free list, allocating more if necessary */
1124
932e9ff9
VB
1125STATIC XPVHV*
1126S_new_xpvhv(pTHX)
1127{
1128 XPVHV* xpvhv;
1129 LOCK_SV_MUTEX;
1130 if (!PL_xpvhv_root)
69ddb3b9 1131 S_more_xpvhv(aTHX);
932e9ff9
VB
1132 xpvhv = PL_xpvhv_root;
1133 PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array;
1134 UNLOCK_SV_MUTEX;
1135 return xpvhv;
1136}
1137
645c22ef
DM
1138/* return a struct xpvhv to the free list */
1139
932e9ff9
VB
1140STATIC void
1141S_del_xpvhv(pTHX_ XPVHV *p)
1142{
1143 LOCK_SV_MUTEX;
1144 p->xhv_array = (char*)PL_xpvhv_root;
1145 PL_xpvhv_root = p;
1146 UNLOCK_SV_MUTEX;
1147}
1148
645c22ef
DM
1149/* grab a new struct xpvmg from the free list, allocating more if necessary */
1150
932e9ff9
VB
1151STATIC XPVMG*
1152S_new_xpvmg(pTHX)
1153{
1154 XPVMG* xpvmg;
1155 LOCK_SV_MUTEX;
1156 if (!PL_xpvmg_root)
69ddb3b9 1157 S_more_xpvmg(aTHX);
932e9ff9
VB
1158 xpvmg = PL_xpvmg_root;
1159 PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv;
1160 UNLOCK_SV_MUTEX;
1161 return xpvmg;
1162}
1163
645c22ef
DM
1164/* return a struct xpvmg to the free list */
1165
932e9ff9
VB
1166STATIC void
1167S_del_xpvmg(pTHX_ XPVMG *p)
1168{
1169 LOCK_SV_MUTEX;
1170 p->xpv_pv = (char*)PL_xpvmg_root;
1171 PL_xpvmg_root = p;
1172 UNLOCK_SV_MUTEX;
1173}
1174
69ddb3b9 1175/* grab a new struct xpvgv from the free list, allocating more if necessary */
645c22ef 1176
69ddb3b9
NC
1177STATIC XPVGV*
1178S_new_xpvgv(pTHX)
932e9ff9 1179{
69ddb3b9
NC
1180 XPVGV* xpvgv;
1181 LOCK_SV_MUTEX;
1182 if (!PL_xpvgv_root)
1183 S_more_xpvgv(aTHX);
1184 xpvgv = PL_xpvgv_root;
1185 PL_xpvgv_root = (XPVGV*)xpvgv->xpv_pv;
1186 UNLOCK_SV_MUTEX;
1187 return xpvgv;
1188}
612f20c3 1189
69ddb3b9
NC
1190/* return a struct xpvgv to the free list */
1191
1192STATIC void
1193S_del_xpvgv(pTHX_ XPVGV *p)
1194{
1195 LOCK_SV_MUTEX;
1196 p->xpv_pv = (char*)PL_xpvgv_root;
1197 PL_xpvgv_root = p;
1198 UNLOCK_SV_MUTEX;
932e9ff9
VB
1199}
1200
645c22ef
DM
1201/* grab a new struct xpvlv from the free list, allocating more if necessary */
1202
932e9ff9
VB
1203STATIC XPVLV*
1204S_new_xpvlv(pTHX)
1205{
1206 XPVLV* xpvlv;
1207 LOCK_SV_MUTEX;
1208 if (!PL_xpvlv_root)
69ddb3b9 1209 S_more_xpvlv(aTHX);
932e9ff9
VB
1210 xpvlv = PL_xpvlv_root;
1211 PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv;
1212 UNLOCK_SV_MUTEX;
1213 return xpvlv;
1214}
1215
645c22ef
DM
1216/* return a struct xpvlv to the free list */
1217
932e9ff9
VB
1218STATIC void
1219S_del_xpvlv(pTHX_ XPVLV *p)
1220{
1221 LOCK_SV_MUTEX;
1222 p->xpv_pv = (char*)PL_xpvlv_root;
1223 PL_xpvlv_root = p;
1224 UNLOCK_SV_MUTEX;
1225}
1226
645c22ef
DM
1227/* grab a new struct xpvbm from the free list, allocating more if necessary */
1228
932e9ff9
VB
1229STATIC XPVBM*
1230S_new_xpvbm(pTHX)
1231{
1232 XPVBM* xpvbm;
1233 LOCK_SV_MUTEX;
1234 if (!PL_xpvbm_root)
69ddb3b9 1235 S_more_xpvbm(aTHX);
932e9ff9
VB
1236 xpvbm = PL_xpvbm_root;
1237 PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv;
1238 UNLOCK_SV_MUTEX;
1239 return xpvbm;
1240}
1241
645c22ef
DM
1242/* return a struct xpvbm to the free list */
1243
932e9ff9
VB
1244STATIC void
1245S_del_xpvbm(pTHX_ XPVBM *p)
1246{
1247 LOCK_SV_MUTEX;
1248 p->xpv_pv = (char*)PL_xpvbm_root;
1249 PL_xpvbm_root = p;
1250 UNLOCK_SV_MUTEX;
1251}
1252
2fe0f101
JH
1253#define my_safemalloc(s) (void*)safemalloc(s)
1254#define my_safefree(p) safefree((char*)p)
463ee0b2 1255
d33b2eba 1256#ifdef PURIFY
463ee0b2 1257
d33b2eba
GS
1258#define new_XIV() my_safemalloc(sizeof(XPVIV))
1259#define del_XIV(p) my_safefree(p)
ed6116ce 1260
d33b2eba
GS
1261#define new_XNV() my_safemalloc(sizeof(XPVNV))
1262#define del_XNV(p) my_safefree(p)
463ee0b2 1263
d33b2eba
GS
1264#define new_XRV() my_safemalloc(sizeof(XRV))
1265#define del_XRV(p) my_safefree(p)
8c52afec 1266
d33b2eba
GS
1267#define new_XPV() my_safemalloc(sizeof(XPV))
1268#define del_XPV(p) my_safefree(p)
9b94d1dd 1269
d33b2eba
GS
1270#define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1271#define del_XPVIV(p) my_safefree(p)
932e9ff9 1272
d33b2eba
GS
1273#define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1274#define del_XPVNV(p) my_safefree(p)
932e9ff9 1275
d33b2eba
GS
1276#define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1277#define del_XPVCV(p) my_safefree(p)
932e9ff9 1278
d33b2eba
GS
1279#define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1280#define del_XPVAV(p) my_safefree(p)
1281
1282#define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1283#define del_XPVHV(p) my_safefree(p)
1c846c1f 1284
d33b2eba
GS
1285#define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1286#define del_XPVMG(p) my_safefree(p)
1287
69ddb3b9
NC
1288#define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1289#define del_XPVGV(p) my_safefree(p)
1290
d33b2eba
GS
1291#define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1292#define del_XPVLV(p) my_safefree(p)
1293
1294#define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1295#define del_XPVBM(p) my_safefree(p)
1296
1297#else /* !PURIFY */
1298
1299#define new_XIV() (void*)new_xiv()
1300#define del_XIV(p) del_xiv((XPVIV*) p)
1301
1302#define new_XNV() (void*)new_xnv()
1303#define del_XNV(p) del_xnv((XPVNV*) p)
9b94d1dd 1304
d33b2eba
GS
1305#define new_XRV() (void*)new_xrv()
1306#define del_XRV(p) del_xrv((XRV*) p)
9b94d1dd 1307
d33b2eba
GS
1308#define new_XPV() (void*)new_xpv()
1309#define del_XPV(p) del_xpv((XPV *)p)
1310
1311#define new_XPVIV() (void*)new_xpviv()
1312#define del_XPVIV(p) del_xpviv((XPVIV *)p)
1313
1314#define new_XPVNV() (void*)new_xpvnv()
1315#define del_XPVNV(p) del_xpvnv((XPVNV *)p)
1316
1317#define new_XPVCV() (void*)new_xpvcv()
1318#define del_XPVCV(p) del_xpvcv((XPVCV *)p)
1319
1320#define new_XPVAV() (void*)new_xpvav()
1321#define del_XPVAV(p) del_xpvav((XPVAV *)p)
1322
1323#define new_XPVHV() (void*)new_xpvhv()
1324#define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1c846c1f 1325
d33b2eba
GS
1326#define new_XPVMG() (void*)new_xpvmg()
1327#define del_XPVMG(p) del_xpvmg((XPVMG *)p)
1328
69ddb3b9
NC
1329#define new_XPVGV() (void*)new_xpvgv()
1330#define del_XPVGV(p) del_xpvgv((XPVGV *)p)
1331
d33b2eba
GS
1332#define new_XPVLV() (void*)new_xpvlv()
1333#define del_XPVLV(p) del_xpvlv((XPVLV *)p)
1334
1335#define new_XPVBM() (void*)new_xpvbm()
1336#define del_XPVBM(p) del_xpvbm((XPVBM *)p)
1337
1338#endif /* PURIFY */
9b94d1dd 1339
d33b2eba
GS
1340#define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1341#define del_XPVFM(p) my_safefree(p)
1c846c1f 1342
d33b2eba
GS
1343#define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1344#define del_XPVIO(p) my_safefree(p)
8990e307 1345
954c1994
GS
1346/*
1347=for apidoc sv_upgrade
1348
ff276b08 1349Upgrade an SV to a more complex form. Generally adds a new body type to the
645c22ef 1350SV, then copies across as much information as possible from the old body.
ff276b08 1351You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
954c1994
GS
1352
1353=cut
1354*/
1355
79072805 1356bool
864dbfa3 1357Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
79072805 1358{
1cb3760b 1359
9081f4f7
NC
1360 char* pv;
1361 U32 cur;
1362 U32 len;
1363 IV iv;
1364 NV nv;
1365 MAGIC* magic;
1366 HV* stash;
79072805 1367
f130fd45
NIS
1368 if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) {
1369 sv_force_normal(sv);
1370 }
1371
79072805
LW
1372 if (SvTYPE(sv) == mt)
1373 return TRUE;
1374
a5f75d66
AD
1375 if (mt < SVt_PVIV)
1376 (void)SvOOK_off(sv);
1377
9081f4f7
NC
1378 pv = NULL;
1379 cur = 0;
1380 len = 0;
1381 iv = 0;
1382 nv = 0.0;
1383 magic = NULL;
1384 stash = Nullhv;
1385
79072805
LW
1386 switch (SvTYPE(sv)) {
1387 case SVt_NULL:
79072805 1388 break;
79072805 1389 case SVt_IV:
463ee0b2 1390 iv = SvIVX(sv);
79072805 1391 del_XIV(SvANY(sv));
ed6116ce 1392 if (mt == SVt_NV)
463ee0b2 1393 mt = SVt_PVNV;
ed6116ce
LW
1394 else if (mt < SVt_PVIV)
1395 mt = SVt_PVIV;
79072805
LW
1396 break;
1397 case SVt_NV:
463ee0b2 1398 nv = SvNVX(sv);
79072805 1399 del_XNV(SvANY(sv));
ed6116ce 1400 if (mt < SVt_PVNV)
79072805
LW
1401 mt = SVt_PVNV;
1402 break;
ed6116ce
LW
1403 case SVt_RV:
1404 pv = (char*)SvRV(sv);
ed6116ce 1405 del_XRV(SvANY(sv));
ed6116ce 1406 break;
79072805 1407 case SVt_PV:
463ee0b2 1408 pv = SvPVX(sv);
79072805
LW
1409 cur = SvCUR(sv);
1410 len = SvLEN(sv);
79072805 1411 del_XPV(SvANY(sv));
748a9306
LW
1412 if (mt <= SVt_IV)
1413 mt = SVt_PVIV;
1414 else if (mt == SVt_NV)
1415 mt = SVt_PVNV;
79072805
LW
1416 break;
1417 case SVt_PVIV:
463ee0b2 1418 pv = SvPVX(sv);
79072805
LW
1419 cur = SvCUR(sv);
1420 len = SvLEN(sv);
463ee0b2 1421 iv = SvIVX(sv);
79072805
LW
1422 del_XPVIV(SvANY(sv));
1423 break;
1424 case SVt_PVNV:
463ee0b2 1425 pv = SvPVX(sv);
79072805
LW
1426 cur = SvCUR(sv);
1427 len = SvLEN(sv);
463ee0b2
LW
1428 iv = SvIVX(sv);
1429 nv = SvNVX(sv);
79072805
LW
1430 del_XPVNV(SvANY(sv));
1431 break;
1432 case SVt_PVMG:
2a8de9e2
AL
1433 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1434 there's no way that it can be safely upgraded, because perl.c
1435 expects to Safefree(SvANY(PL_mess_sv)) */
1436 assert(sv != PL_mess_sv);
2098fb77
NC
1437 /* This flag bit is used to mean other things in other scalar types.
1438 Given that it only has meaning inside the pad, it shouldn't be set
1439 on anything that can get upgraded. */
1440 assert((SvFLAGS(sv) & SVpad_TYPED) == 0);
463ee0b2 1441 pv = SvPVX(sv);
79072805
LW
1442 cur = SvCUR(sv);
1443 len = SvLEN(sv);
463ee0b2
LW
1444 iv = SvIVX(sv);
1445 nv = SvNVX(sv);
79072805
LW
1446 magic = SvMAGIC(sv);
1447 stash = SvSTASH(sv);
1448 del_XPVMG(SvANY(sv));
1449 break;
1450 default:
cea2e8a9 1451 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
79072805
LW
1452 }
1453
0da6cfda
SP
1454 SvFLAGS(sv) &= ~SVTYPEMASK;
1455 SvFLAGS(sv) |= mt;
1456
79072805
LW
1457 switch (mt) {
1458 case SVt_NULL:
cea2e8a9 1459 Perl_croak(aTHX_ "Can't upgrade to undef");
79072805
LW
1460 case SVt_IV:
1461 SvANY(sv) = new_XIV();
0da6cfda 1462 SvIV_set(sv, iv);
79072805
LW
1463 break;
1464 case SVt_NV:
1465 SvANY(sv) = new_XNV();
0da6cfda 1466 SvNV_set(sv, nv);
79072805 1467 break;
ed6116ce
LW
1468 case SVt_RV:
1469 SvANY(sv) = new_XRV();
a8dc4fe8 1470 SvRV_set(sv, (SV*)pv);
ed6116ce 1471 break;
79072805
LW
1472 case SVt_PV:
1473 SvANY(sv) = new_XPV();
0da6cfda 1474 SvPV_set(sv, pv);
a8dc4fe8
SP
1475 SvCUR_set(sv, cur);
1476 SvLEN_set(sv, len);
79072805
LW
1477 break;
1478 case SVt_PVIV:
1479 SvANY(sv) = new_XPVIV();
0da6cfda 1480 SvPV_set(sv, pv);
a8dc4fe8
SP
1481 SvCUR_set(sv, cur);
1482 SvLEN_set(sv, len);
0da6cfda 1483 SvIV_set(sv, iv);
79072805 1484 if (SvNIOK(sv))
a0d0e21e 1485 (void)SvIOK_on(sv);
79072805
LW
1486 SvNOK_off(sv);
1487 break;
1488 case SVt_PVNV:
1489 SvANY(sv) = new_XPVNV();
0da6cfda 1490 SvPV_set(sv, pv);
a8dc4fe8
SP
1491 SvCUR_set(sv, cur);
1492 SvLEN_set(sv, len);
0da6cfda
SP
1493 SvIV_set(sv, iv);
1494 SvNV_set(sv, nv);
79072805
LW
1495 break;
1496 case SVt_PVMG:
1497 SvANY(sv) = new_XPVMG();
0da6cfda 1498 SvPV_set(sv, pv);
a8dc4fe8
SP
1499 SvCUR_set(sv, cur);
1500 SvLEN_set(sv, len);
0da6cfda
SP
1501 SvIV_set(sv, iv);
1502 SvNV_set(sv, nv);
a8dc4fe8
SP
1503 SvMAGIC_set(sv, magic);
1504 SvSTASH_set(sv, stash);
79072805
LW
1505 break;
1506 case SVt_PVLV:
1507 SvANY(sv) = new_XPVLV();
0da6cfda 1508 SvPV_set(sv, pv);
a8dc4fe8
SP
1509 SvCUR_set(sv, cur);
1510 SvLEN_set(sv, len);
0da6cfda
SP
1511 SvIV_set(sv, iv);
1512 SvNV_set(sv, nv);
a8dc4fe8
SP
1513 SvMAGIC_set(sv, magic);
1514 SvSTASH_set(sv, stash);
79072805
LW
1515 LvTARGOFF(sv) = 0;
1516 LvTARGLEN(sv) = 0;
1517 LvTARG(sv) = 0;
1518 LvTYPE(sv) = 0;
1519 break;
1520 case SVt_PVAV:
1521 SvANY(sv) = new_XPVAV();
463ee0b2
LW
1522 if (pv)
1523 Safefree(pv);
0da6cfda 1524 SvPV_set(sv, (char*)0);
d1bf51dd 1525 AvMAX(sv) = -1;
93965878 1526 AvFILLp(sv) = -1;
0da6cfda
SP
1527 SvIV_set(sv, 0);
1528 SvNV_set(sv, 0.0);
a8dc4fe8
SP
1529 SvMAGIC_set(sv, magic);
1530 SvSTASH_set(sv, stash);
463ee0b2 1531 AvALLOC(sv) = 0;
79072805 1532 AvARYLEN(sv) = 0;
1cb3760b 1533 AvFLAGS(sv) = AVf_REAL;
79072805
LW
1534 break;
1535 case SVt_PVHV:
1536 SvANY(sv) = new_XPVHV();
463ee0b2
LW
1537 if (pv)
1538 Safefree(pv);
0da6cfda 1539 SvPV_set(sv, (char*)0);
463ee0b2
LW
1540 HvFILL(sv) = 0;
1541 HvMAX(sv) = 0;
8aacddc1 1542 HvTOTALKEYS(sv) = 0;
c3f2d5da 1543 HvPLACEHOLDERS_set(sv, 0);
a8dc4fe8
SP
1544 SvMAGIC_set(sv, magic);
1545 SvSTASH_set(sv, stash);
79072805
LW
1546 HvRITER(sv) = 0;
1547 HvEITER(sv) = 0;
1548 HvPMROOT(sv) = 0;
1549 HvNAME(sv) = 0;
79072805
LW
1550 break;
1551 case SVt_PVCV:
1552 SvANY(sv) = new_XPVCV();
748a9306 1553 Zero(SvANY(sv), 1, XPVCV);
0da6cfda 1554 SvPV_set(sv, pv);
a8dc4fe8
SP
1555 SvCUR_set(sv, cur);
1556 SvLEN_set(sv, len);
0da6cfda
SP
1557 SvIV_set(sv, iv);
1558 SvNV_set(sv, nv);
a8dc4fe8
SP
1559 SvMAGIC_set(sv, magic);
1560 SvSTASH_set(sv, stash);
79072805
LW
1561 break;
1562 case SVt_PVGV:
1563 SvANY(sv) = new_XPVGV();
0da6cfda 1564 SvPV_set(sv, pv);
a8dc4fe8
SP
1565 SvCUR_set(sv, cur);
1566 SvLEN_set(sv, len);
0da6cfda
SP
1567 SvIV_set(sv, iv);
1568 SvNV_set(sv, nv);
a8dc4fe8
SP
1569 SvMAGIC_set(sv, magic);
1570 SvSTASH_set(sv, stash);
93a17b20 1571 GvGP(sv) = 0;
79072805
LW
1572 GvNAME(sv) = 0;
1573 GvNAMELEN(sv) = 0;
1574 GvSTASH(sv) = 0;
a5f75d66 1575 GvFLAGS(sv) = 0;
79072805
LW
1576 break;
1577 case SVt_PVBM:
1578 SvANY(sv) = new_XPVBM();
0da6cfda 1579 SvPV_set(sv, pv);
a8dc4fe8
SP
1580 SvCUR_set(sv, cur);
1581 SvLEN_set(sv, len);
0da6cfda
SP
1582 SvIV_set(sv, iv);
1583 SvNV_set(sv, nv);
a8dc4fe8
SP
1584 SvMAGIC_set(sv, magic);
1585 SvSTASH_set(sv, stash);
79072805
LW
1586 BmRARE(sv) = 0;
1587 BmUSEFUL(sv) = 0;
1588 BmPREVIOUS(sv) = 0;
1589 break;
1590 case SVt_PVFM:
1591 SvANY(sv) = new_XPVFM();
748a9306 1592 Zero(SvANY(sv), 1, XPVFM);
0da6cfda 1593 SvPV_set(sv, pv);
a8dc4fe8
SP
1594 SvCUR_set(sv, cur);
1595 SvLEN_set(sv, len);
0da6cfda
SP
1596 SvIV_set(sv, iv);
1597 SvNV_set(sv, nv);
a8dc4fe8
SP
1598 SvMAGIC_set(sv, magic);
1599 SvSTASH_set(sv, stash);
79072805 1600 break;
8990e307
LW
1601 case SVt_PVIO:
1602 SvANY(sv) = new_XPVIO();
748a9306 1603 Zero(SvANY(sv), 1, XPVIO);
0da6cfda 1604 SvPV_set(sv, pv);
a8dc4fe8
SP
1605 SvCUR_set(sv, cur);
1606 SvLEN_set(sv, len);
0da6cfda
SP
1607 SvIV_set(sv, iv);
1608 SvNV_set(sv, nv);
a8dc4fe8
SP
1609 SvMAGIC_set(sv, magic);
1610 SvSTASH_set(sv, stash);
85e6fe83 1611 IoPAGE_LEN(sv) = 60;
8990e307
LW
1612 break;
1613 }
79072805
LW
1614 return TRUE;
1615}
1616
645c22ef
DM
1617/*
1618=for apidoc sv_backoff
1619
1620Remove any string offset. You should normally use the C<SvOOK_off> macro
1621wrapper instead.
1622
1623=cut
1624*/
1625
79072805 1626int
864dbfa3 1627Perl_sv_backoff(pTHX_ register SV *sv)
79072805
LW
1628{
1629 assert(SvOOK(sv));
463ee0b2 1630 if (SvIVX(sv)) {
fdac8c4b 1631 const char *s = SvPVX_const(sv);
a8dc4fe8 1632 SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
0da6cfda 1633 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
79072805 1634 SvIV_set(sv, 0);
463ee0b2 1635 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
1636 }
1637 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 1638 return 0;
79072805
LW
1639}
1640
954c1994
GS
1641/*
1642=for apidoc sv_grow
1643
645c22ef
DM
1644Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1645upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1646Use the C<SvGROW> wrapper instead.
954c1994
GS
1647
1648=cut
1649*/
1650
79072805 1651char *
864dbfa3 1652Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
79072805
LW
1653{
1654 register char *s;
1655
54f0641b
NIS
1656
1657
55497cff 1658#ifdef HAS_64K_LIMIT
79072805 1659 if (newlen >= 0x10000) {
1d7c1841
GS
1660 PerlIO_printf(Perl_debug_log,
1661 "Allocation too large: %"UVxf"\n", (UV)newlen);
79072805
LW
1662 my_exit(1);
1663 }
55497cff 1664#endif /* HAS_64K_LIMIT */
a0d0e21e
LW
1665 if (SvROK(sv))
1666 sv_unref(sv);
79072805
LW
1667 if (SvTYPE(sv) < SVt_PV) {
1668 sv_upgrade(sv, SVt_PV);
463ee0b2 1669 s = SvPVX(sv);
79072805
LW
1670 }
1671 else if (SvOOK(sv)) { /* pv is offset? */
1672 sv_backoff(sv);
463ee0b2 1673 s = SvPVX(sv);
79072805
LW
1674 if (newlen > SvLEN(sv))
1675 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
c6f8c383
GA
1676#ifdef HAS_64K_LIMIT
1677 if (newlen >= 0x10000)
1678 newlen = 0xFFFF;
1679#endif
79072805
LW
1680 }
1681 else
463ee0b2 1682 s = SvPVX(sv);
54f0641b 1683
79072805 1684 if (newlen > SvLEN(sv)) { /* need more room? */
9eaeeaca 1685 newlen = PERL_STRLEN_ROUNDUP(newlen);
8d6dde3e 1686 if (SvLEN(sv) && s) {
2fe0f101 1687#ifdef MYMALLOC
8c18bf38 1688 const STRLEN l = malloced_size((void*)SvPVX(sv));
8d6dde3e
IZ
1689 if (newlen <= l) {
1690 SvLEN_set(sv, l);
1691 return s;
1692 } else
c70c8a0a 1693#endif
9eaeeaca 1694 s = saferealloc(s, newlen);
8d6dde3e 1695 }
a00f3e00 1696 else {
ee5f0761
AMS
1697 /* sv_force_normal_flags() must not try to unshare the new
1698 PVX we allocate below. AMS 20010713 */
4e83176d 1699 if (SvREADONLY(sv) && SvFAKE(sv)) {
4e83176d
AMS
1700 SvFAKE_off(sv);
1701 SvREADONLY_off(sv);
4e83176d 1702 }
9eaeeaca 1703 s = safemalloc(newlen);
fdac8c4b
SP
1704 if (SvPVX_const(sv) && SvCUR(sv)) {
1705 Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
40565179 1706 }
4e83176d 1707 }
79072805 1708 SvPV_set(sv, s);
c05e0e2f 1709 SvLEN_set(sv, newlen);
79072805
LW
1710 }
1711 return s;
1712}
1713
954c1994
GS
1714/*
1715=for apidoc sv_setiv
1716
645c22ef
DM
1717Copies an integer into the given SV, upgrading first if necessary.
1718Does not handle 'set' magic. See also C<sv_setiv_mg>.
954c1994
GS
1719
1720=cut
1721*/
1722
79072805 1723void
864dbfa3 1724Perl_sv_setiv(pTHX_ register SV *sv, IV i)
79072805 1725{
2213622d 1726 SV_CHECK_THINKFIRST(sv);
463ee0b2
LW
1727 switch (SvTYPE(sv)) {
1728 case SVt_NULL:
79072805 1729 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
1730 break;
1731 case SVt_NV:
1732 sv_upgrade(sv, SVt_PVNV);
1733 break;
ed6116ce 1734 case SVt_RV:
463ee0b2 1735 case SVt_PV:
79072805 1736 sv_upgrade(sv, SVt_PVIV);
463ee0b2 1737 break;
a0d0e21e
LW
1738
1739 case SVt_PVGV:
a0d0e21e
LW
1740 case SVt_PVAV:
1741 case SVt_PVHV:
1742 case SVt_PVCV:
1743 case SVt_PVFM:
1744 case SVt_PVIO:
411caa50 1745 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
53e06cf0 1746 OP_DESC(PL_op));
463ee0b2 1747 }
a0d0e21e 1748 (void)SvIOK_only(sv); /* validate number */
0da6cfda 1749 SvIV_set(sv, i);
463ee0b2 1750 SvTAINT(sv);
79072805
LW
1751}
1752
954c1994
GS
1753/*
1754=for apidoc sv_setiv_mg
1755
1756Like C<sv_setiv>, but also handles 'set' magic.
1757
1758=cut
1759*/
1760
79072805 1761void
864dbfa3 1762Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
ef50df4b
GS
1763{
1764 sv_setiv(sv,i);
1765 SvSETMAGIC(sv);
1766}
1767
954c1994
GS
1768/*
1769=for apidoc sv_setuv
1770
645c22ef
DM
1771Copies an unsigned integer into the given SV, upgrading first if necessary.
1772Does not handle 'set' magic. See also C<sv_setuv_mg>.
954c1994
GS
1773
1774=cut
1775*/
1776
ef50df4b 1777void
864dbfa3 1778Perl_sv_setuv(pTHX_ register SV *sv, UV u)
55497cff 1779{
55ada374
NC
1780 /* With these two if statements:
1781 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 1782
55ada374
NC
1783 without
1784 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 1785
55ada374
NC
1786 If you wish to remove them, please benchmark to see what the effect is
1787 */
28e5dec8
JH
1788 if (u <= (UV)IV_MAX) {
1789 sv_setiv(sv, (IV)u);
1790 return;
1791 }
25da4f38
IZ
1792 sv_setiv(sv, 0);
1793 SvIsUV_on(sv);
0da6cfda 1794 SvUV_set(sv, u);
55497cff 1795}
1796
954c1994
GS
1797/*
1798=for apidoc sv_setuv_mg
1799
1800Like C<sv_setuv>, but also handles 'set' magic.
1801
1802=cut
1803*/
1804
55497cff 1805void
864dbfa3 1806Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
ef50df4b 1807{
55ada374
NC
1808 /* With these two if statements:
1809 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 1810
55ada374
NC
1811 without
1812 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 1813
55ada374
NC
1814 If you wish to remove them, please benchmark to see what the effect is
1815 */
28e5dec8
JH
1816 if (u <= (UV)IV_MAX) {
1817 sv_setiv(sv, (IV)u);
1818 } else {
1819 sv_setiv(sv, 0);
1820 SvIsUV_on(sv);
1821 sv_setuv(sv,u);
1822 }
ef50df4b
GS
1823 SvSETMAGIC(sv);
1824}
1825
954c1994
GS
1826/*
1827=for apidoc sv_setnv
1828
645c22ef
DM
1829Copies a double into the given SV, upgrading first if necessary.
1830Does not handle 'set' magic. See also C<sv_setnv_mg>.
954c1994
GS
1831
1832=cut
1833*/
1834
ef50df4b 1835void
65202027 1836Perl_sv_setnv(pTHX_ register SV *sv, NV num)
79072805 1837{
2213622d 1838 SV_CHECK_THINKFIRST(sv);
a0d0e21e
LW
1839 switch (SvTYPE(sv)) {
1840 case SVt_NULL:
1841 case SVt_IV:
79072805 1842 sv_upgrade(sv, SVt_NV);
a0d0e21e 1843 break;
a0d0e21e
LW
1844 case SVt_RV:
1845 case SVt_PV:
1846 case SVt_PVIV:
79072805 1847 sv_upgrade(sv, SVt_PVNV);
a0d0e21e 1848 break;
827b7e14 1849
a0d0e21e 1850 case SVt_PVGV:
a0d0e21e
LW
1851 case SVt_PVAV:
1852 case SVt_PVHV:
1853 case SVt_PVCV:
1854 case SVt_PVFM:
1855 case SVt_PVIO:
411caa50 1856 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
53e06cf0 1857 OP_NAME(PL_op));
79072805 1858 }
0da6cfda 1859 SvNV_set(sv, num);
a0d0e21e 1860 (void)SvNOK_only(sv); /* validate number */
463ee0b2 1861 SvTAINT(sv);
79072805
LW
1862}
1863
954c1994
GS
1864/*
1865=for apidoc sv_setnv_mg
1866
1867Like C<sv_setnv>, but also handles 'set' magic.
1868
1869=cut
1870*/
1871
ef50df4b 1872void
65202027 1873Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
ef50df4b
GS
1874{
1875 sv_setnv(sv,num);
1876 SvSETMAGIC(sv);
1877}
1878
645c22ef
DM
1879/* Print an "isn't numeric" warning, using a cleaned-up,
1880 * printable version of the offending string
1881 */
1882
76e3520e 1883STATIC void
cea2e8a9 1884S_not_a_number(pTHX_ SV *sv)
a0d0e21e 1885{
94463019
JH
1886 SV *dsv;
1887 char tmpbuf[64];
1888 char *pv;
1889
1890 if (DO_UTF8(sv)) {
1891 dsv = sv_2mortal(newSVpv("", 0));
1892 pv = sv_uni_display(dsv, sv, 10, 0);
1893 } else {
1894 char *d = tmpbuf;
1895 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1896 /* each *s can expand to 4 chars + "...\0",
1897 i.e. need room for 8 chars */
ecdeb87c 1898
94463019
JH
1899 char *s, *end;
1900 for (s = SvPVX(sv), end = s + SvCUR(sv); s < end && d < limit; s++) {
1901 int ch = *s & 0xFF;
1902 if (ch & 128 && !isPRINT_LC(ch)) {
1903 *d++ = 'M';
1904 *d++ = '-';
1905 ch &= 127;
1906 }
1907 if (ch == '\n') {
1908 *d++ = '\\';
1909 *d++ = 'n';
1910 }
1911 else if (ch == '\r') {
1912 *d++ = '\\';
1913 *d++ = 'r';
1914 }
1915 else if (ch == '\f') {
1916 *d++ = '\\';
1917 *d++ = 'f';
1918 }
1919 else if (ch == '\\') {
1920 *d++ = '\\';
1921 *d++ = '\\';
1922 }
1923 else if (ch == '\0') {
1924 *d++ = '\\';
1925 *d++ = '0';
1926 }
1927 else if (isPRINT_LC(ch))
1928 *d++ = ch;
1929 else {
1930 *d++ = '^';
1931 *d++ = toCTRL(ch);
1932 }
1933 }
1934 if (s < end) {
1935 *d++ = '.';
1936 *d++ = '.';
1937 *d++ = '.';
1938 }
1939 *d = '\0';
1940 pv = tmpbuf;
a0d0e21e 1941 }
a0d0e21e 1942
533c011a 1943 if (PL_op)
9014280d 1944 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019
JH
1945 "Argument \"%s\" isn't numeric in %s", pv,
1946 OP_DESC(PL_op));
a0d0e21e 1947 else
9014280d 1948 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019 1949 "Argument \"%s\" isn't numeric", pv);
a0d0e21e
LW
1950}
1951
c2988b20
NC
1952/*
1953=for apidoc looks_like_number
1954
645c22ef
DM
1955Test if the content of an SV looks like a number (or is a number).
1956C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1957non-numeric warning), even if your atof() doesn't grok them.
c2988b20
NC
1958
1959=cut
1960*/
1961
1962I32
1963Perl_looks_like_number(pTHX_ SV *sv)
1964{
8c18bf38 1965 register const char *sbegin;
c2988b20
NC
1966 STRLEN len;
1967
1968 if (SvPOK(sv)) {
fdac8c4b 1969 sbegin = SvPVX_const(sv);
c2988b20
NC
1970 len = SvCUR(sv);
1971 }
1972 else if (SvPOKp(sv))
1973 sbegin = SvPV(sv, len);
1974 else
fc3d430d 1975 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
c2988b20
NC
1976 return grok_number(sbegin, len, NULL);
1977}
25da4f38
IZ
1978
1979/* Actually, ISO C leaves conversion of UV to IV undefined, but
1980 until proven guilty, assume that things are not that bad... */
1981
645c22ef
DM
1982/*
1983 NV_PRESERVES_UV:
1984
1985 As 64 bit platforms often have an NV that doesn't preserve all bits of
28e5dec8
JH
1986 an IV (an assumption perl has been based on to date) it becomes necessary
1987 to remove the assumption that the NV always carries enough precision to
1988 recreate the IV whenever needed, and that the NV is the canonical form.
1989 Instead, IV/UV and NV need to be given equal rights. So as to not lose
645c22ef 1990 precision as a side effect of conversion (which would lead to insanity
28e5dec8
JH
1991 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1992 1) to distinguish between IV/UV/NV slots that have cached a valid
1993 conversion where precision was lost and IV/UV/NV slots that have a
1994 valid conversion which has lost no precision
645c22ef 1995 2) to ensure that if a numeric conversion to one form is requested that
28e5dec8
JH
1996 would lose precision, the precise conversion (or differently
1997 imprecise conversion) is also performed and cached, to prevent
1998 requests for different numeric formats on the same SV causing
1999 lossy conversion chains. (lossless conversion chains are perfectly
2000 acceptable (still))
2001
2002
2003 flags are used:
2004 SvIOKp is true if the IV slot contains a valid value
2005 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
2006 SvNOKp is true if the NV slot contains a valid value
2007 SvNOK is true only if the NV value is accurate
2008
2009 so
645c22ef 2010 while converting from PV to NV, check to see if converting that NV to an
28e5dec8
JH
2011 IV(or UV) would lose accuracy over a direct conversion from PV to
2012 IV(or UV). If it would, cache both conversions, return NV, but mark
2013 SV as IOK NOKp (ie not NOK).
2014
645c22ef 2015 While converting from PV to IV, check to see if converting that IV to an
28e5dec8
JH
2016 NV would lose accuracy over a direct conversion from PV to NV. If it
2017 would, cache both conversions, flag similarly.
2018
2019 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
2020 correctly because if IV & NV were set NV *always* overruled.
645c22ef
DM
2021 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
2022 changes - now IV and NV together means that the two are interchangeable:
28e5dec8 2023 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
d460ef45 2024
645c22ef
DM
2025 The benefit of this is that operations such as pp_add know that if
2026 SvIOK is true for both left and right operands, then integer addition
2027 can be used instead of floating point (for cases where the result won't
2028 overflow). Before, floating point was always used, which could lead to
28e5dec8
JH
2029 loss of precision compared with integer addition.
2030
2031 * making IV and NV equal status should make maths accurate on 64 bit
2032 platforms
2033 * may speed up maths somewhat if pp_add and friends start to use
645c22ef 2034 integers when possible instead of fp. (Hopefully the overhead in
28e5dec8
JH
2035 looking for SvIOK and checking for overflow will not outweigh the
2036 fp to integer speedup)
2037 * will slow down integer operations (callers of SvIV) on "inaccurate"
2038 values, as the change from SvIOK to SvIOKp will cause a call into
2039 sv_2iv each time rather than a macro access direct to the IV slot
2040 * should speed up number->string conversion on integers as IV is
645c22ef 2041 favoured when IV and NV are equally accurate
28e5dec8
JH
2042
2043 ####################################################################
645c22ef
DM
2044 You had better be using SvIOK_notUV if you want an IV for arithmetic:
2045 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
2046 On the other hand, SvUOK is true iff UV.
28e5dec8
JH
2047 ####################################################################
2048
645c22ef 2049 Your mileage will vary depending your CPU's relative fp to integer
28e5dec8
JH
2050 performance ratio.
2051*/
2052
2053#ifndef NV_PRESERVES_UV
645c22ef
DM
2054# define IS_NUMBER_UNDERFLOW_IV 1
2055# define IS_NUMBER_UNDERFLOW_UV 2
2056# define IS_NUMBER_IV_AND_UV 2
2057# define IS_NUMBER_OVERFLOW_IV 4
2058# define IS_NUMBER_OVERFLOW_UV 5
2059
2060/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
28e5dec8
JH
2061
2062/* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2063STATIC int
645c22ef 2064S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
28e5dec8 2065{
fdac8c4b 2066 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
2067 if (SvNVX(sv) < (NV)IV_MIN) {
2068 (void)SvIOKp_on(sv);
2069 (void)SvNOK_on(sv);
0da6cfda 2070 SvIV_set(sv, IV_MIN);
28e5dec8
JH
2071 return IS_NUMBER_UNDERFLOW_IV;
2072 }
2073 if (SvNVX(sv) > (NV)UV_MAX) {
2074 (void)SvIOKp_on(sv);
2075 (void)SvNOK_on(sv);
2076 SvIsUV_on(sv);
0da6cfda 2077 SvUV_set(sv, UV_MAX);
28e5dec8
JH
2078 return IS_NUMBER_OVERFLOW_UV;
2079 }
c2988b20
NC
2080 (void)SvIOKp_on(sv);
2081 (void)SvNOK_on(sv);
2082 /* Can't use strtol etc to convert this string. (See truth table in
2083 sv_2iv */
2084 if (SvNVX(sv) <= (UV)IV_MAX) {
0da6cfda 2085 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2086 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2087 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2088 } else {
2089 /* Integer is imprecise. NOK, IOKp */
2090 }
2091 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2092 }
2093 SvIsUV_on(sv);
0da6cfda 2094 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2095 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2096 if (SvUVX(sv) == UV_MAX) {
2097 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2098 possibly be preserved by NV. Hence, it must be overflow.
2099 NOK, IOKp */
2100 return IS_NUMBER_OVERFLOW_UV;
2101 }
2102 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2103 } else {
2104 /* Integer is imprecise. NOK, IOKp */
28e5dec8 2105 }
c2988b20 2106 return IS_NUMBER_OVERFLOW_IV;
28e5dec8 2107}
645c22ef
DM
2108#endif /* !NV_PRESERVES_UV*/
2109
2110/*
2111=for apidoc sv_2iv
2112
2113Return the integer value of an SV, doing any necessary string conversion,
2114magic etc. Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2115
2116=cut
2117*/
28e5dec8 2118
a0d0e21e 2119IV
864dbfa3 2120Perl_sv_2iv(pTHX_ register SV *sv)
79072805
LW
2121{
2122 if (!sv)
2123 return 0;
8990e307 2124 if (SvGMAGICAL(sv)) {
463ee0b2
LW
2125 mg_get(sv);
2126 if (SvIOKp(sv))
2127 return SvIVX(sv);
748a9306 2128 if (SvNOKp(sv)) {
25da4f38 2129 return I_V(SvNVX(sv));
748a9306 2130 }
36477c24 2131 if (SvPOKp(sv) && SvLEN(sv))
2132 return asIV(sv);
3fe9a6f1 2133 if (!SvROK(sv)) {
d008e5eb 2134 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2135 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2136 report_uninit();
c6ee37c5 2137 }
36477c24 2138 return 0;
3fe9a6f1 2139 }
463ee0b2 2140 }
ed6116ce 2141 if (SvTHINKFIRST(sv)) {
a0d0e21e 2142 if (SvROK(sv)) {
a0d0e21e 2143 SV* tmpstr;
1554e226 2144 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2145 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2146 return SvIV(tmpstr);
56431972 2147 return PTR2IV(SvRV(sv));
a0d0e21e 2148 }
47deb5e7
NIS
2149 if (SvREADONLY(sv) && SvFAKE(sv)) {
2150 sv_force_normal(sv);
2151 }
0336b60e 2152 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2153 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 2154 report_uninit();
ed6116ce
LW
2155 return 0;
2156 }
79072805 2157 }
25da4f38
IZ
2158 if (SvIOKp(sv)) {
2159 if (SvIsUV(sv)) {
2160 return (IV)(SvUVX(sv));
2161 }
2162 else {
2163 return SvIVX(sv);
2164 }
463ee0b2 2165 }
748a9306 2166 if (SvNOKp(sv)) {
28e5dec8
JH
2167 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2168 * without also getting a cached IV/UV from it at the same time
2169 * (ie PV->NV conversion should detect loss of accuracy and cache
2170 * IV or UV at same time to avoid this. NWC */
25da4f38
IZ
2171
2172 if (SvTYPE(sv) == SVt_NV)
2173 sv_upgrade(sv, SVt_PVNV);
2174
28e5dec8
JH
2175 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2176 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2177 certainly cast into the IV range at IV_MAX, whereas the correct
2178 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2179 cases go to UV */
2180 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
0da6cfda 2181 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2182 if (SvNVX(sv) == (NV) SvIVX(sv)
2183#ifndef NV_PRESERVES_UV
2184 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2185 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2186 /* Don't flag it as "accurately an integer" if the number
2187 came from a (by definition imprecise) NV operation, and
2188 we're outside the range of NV integer precision */
2189#endif
2190 ) {
2191 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2192 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2193 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2194 PTR2UV(sv),
2195 SvNVX(sv),
2196 SvIVX(sv)));
2197
2198 } else {
2199 /* IV not precise. No need to convert from PV, as NV
2200 conversion would already have cached IV if it detected
2201 that PV->IV would be better than PV->NV->IV
2202 flags already correct - don't set public IOK. */
2203 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2204 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2205 PTR2UV(sv),
2206 SvNVX(sv),
2207 SvIVX(sv)));
2208 }
2209 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2210 but the cast (NV)IV_MIN rounds to a the value less (more
2211 negative) than IV_MIN which happens to be equal to SvNVX ??
2212 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2213 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2214 (NV)UVX == NVX are both true, but the values differ. :-(
2215 Hopefully for 2s complement IV_MIN is something like
2216 0x8000000000000000 which will be exact. NWC */
d460ef45 2217 }
25da4f38 2218 else {
0da6cfda 2219 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2220 if (
2221 (SvNVX(sv) == (NV) SvUVX(sv))
2222#ifndef NV_PRESERVES_UV
2223 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2224 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2225 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2226 /* Don't flag it as "accurately an integer" if the number
2227 came from a (by definition imprecise) NV operation, and
2228 we're outside the range of NV integer precision */
2229#endif
2230 )
2231 SvIOK_on(sv);
25da4f38
IZ
2232 SvIsUV_on(sv);
2233 ret_iv_max:
1c846c1f 2234 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 2235 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 2236 PTR2UV(sv),
57def98f
JH
2237 SvUVX(sv),
2238 SvUVX(sv)));
25da4f38
IZ
2239 return (IV)SvUVX(sv);
2240 }
748a9306
LW
2241 }
2242 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2243 UV value;
547d29e4 2244 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
25da4f38
IZ
2245 /* We want to avoid a possible problem when we cache an IV which
2246 may be later translated to an NV, and the resulting NV is not
c2988b20
NC
2247 the same as the direct translation of the initial string
2248 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2249 be careful to ensure that the value with the .456 is around if the
2250 NV value is requested in the future).
1c846c1f 2251
25da4f38
IZ
2252 This means that if we cache such an IV, we need to cache the
2253 NV as well. Moreover, we trade speed for space, and do not
28e5dec8 2254 cache the NV if we are sure it's not needed.
25da4f38 2255 */
16b7a9a4 2256
c2988b20
NC
2257 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2258 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2259 == IS_NUMBER_IN_UV) {
5e045b90 2260 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8
JH
2261 if (SvTYPE(sv) < SVt_PVIV)
2262 sv_upgrade(sv, SVt_PVIV);
f7bbb42a 2263 (void)SvIOK_on(sv);
c2988b20
NC
2264 } else if (SvTYPE(sv) < SVt_PVNV)
2265 sv_upgrade(sv, SVt_PVNV);
28e5dec8 2266
c2988b20
NC
2267 /* If NV preserves UV then we only use the UV value if we know that
2268 we aren't going to call atof() below. If NVs don't preserve UVs
2269 then the value returned may have more precision than atof() will
2270 return, even though value isn't perfectly accurate. */
2271 if ((numtype & (IS_NUMBER_IN_UV
2272#ifdef NV_PRESERVES_UV
2273 | IS_NUMBER_NOT_INT
2274#endif
2275 )) == IS_NUMBER_IN_UV) {
2276 /* This won't turn off the public IOK flag if it was set above */
2277 (void)SvIOKp_on(sv);
2278
2279 if (!(numtype & IS_NUMBER_NEG)) {
2280 /* positive */;
2281 if (value <= (UV)IV_MAX) {
0da6cfda 2282 SvIV_set(sv, (IV)value);
c2988b20 2283 } else {
0da6cfda 2284 SvUV_set(sv, value);
c2988b20
NC
2285 SvIsUV_on(sv);
2286 }
2287 } else {
2288 /* 2s complement assumption */
2289 if (value <= (UV)IV_MIN) {
0da6cfda 2290 SvIV_set(sv, -(IV)value);
c2988b20
NC
2291 } else {
2292 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2293 I'm assuming it will be rare. */
c2988b20
NC
2294 if (SvTYPE(sv) < SVt_PVNV)
2295 sv_upgrade(sv, SVt_PVNV);
2296 SvNOK_on(sv);
2297 SvIOK_off(sv);
2298 SvIOKp_on(sv);
0da6cfda
SP
2299 SvNV_set(sv, -(NV)value);
2300 SvIV_set(sv, IV_MIN);
c2988b20
NC
2301 }
2302 }
2303 }
2304 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2305 will be in the previous block to set the IV slot, and the next
2306 block to set the NV slot. So no else here. */
2307
2308 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2309 != IS_NUMBER_IN_UV) {
2310 /* It wasn't an (integer that doesn't overflow the UV). */
fdac8c4b 2311 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8 2312
c2988b20
NC
2313 if (! numtype && ckWARN(WARN_NUMERIC))
2314 not_a_number(sv);
28e5dec8 2315
65202027 2316#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2317 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2318 PTR2UV(sv), SvNVX(sv)));
65202027 2319#else
1779d84d 2320 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
c2988b20 2321 PTR2UV(sv), SvNVX(sv)));
65202027 2322#endif
28e5dec8
JH
2323
2324
2325#ifdef NV_PRESERVES_UV
c2988b20
NC
2326 (void)SvIOKp_on(sv);
2327 (void)SvNOK_on(sv);
2328 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
0da6cfda 2329 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2330 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2331 SvIOK_on(sv);
28e5dec8 2332 } else {
c2988b20
NC
2333 /* Integer is imprecise. NOK, IOKp */
2334 }
2335 /* UV will not work better than IV */
2336 } else {
2337 if (SvNVX(sv) > (NV)UV_MAX) {
2338 SvIsUV_on(sv);
2339 /* Integer is inaccurate. NOK, IOKp, is UV */
0da6cfda 2340 SvUV_set(sv, UV_MAX);
c2988b20
NC
2341 SvIsUV_on(sv);
2342 } else {
0da6cfda 2343 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2344 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2345 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2346 SvIOK_on(sv);
28e5dec8
JH
2347 SvIsUV_on(sv);
2348 } else {
c2988b20
NC
2349 /* Integer is imprecise. NOK, IOKp, is UV */
2350 SvIsUV_on(sv);
28e5dec8 2351 }
28e5dec8 2352 }
c2988b20
NC
2353 goto ret_iv_max;
2354 }
28e5dec8 2355#else /* NV_PRESERVES_UV */
c2988b20
NC
2356 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2357 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2358 /* The IV slot will have been set from value returned by
2359 grok_number above. The NV slot has just been set using
2360 Atof. */
560b0c46 2361 SvNOK_on(sv);
c2988b20
NC
2362 assert (SvIOKp(sv));
2363 } else {
2364 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2365 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2366 /* Small enough to preserve all bits. */
2367 (void)SvIOKp_on(sv);
2368 SvNOK_on(sv);
0da6cfda 2369 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2370 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2371 SvIOK_on(sv);
2372 /* Assumption: first non-preserved integer is < IV_MAX,
2373 this NV is in the preserved range, therefore: */
2374 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2375 < (UV)IV_MAX)) {
6b6e5432 2376 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
2377 }
2378 } else {
2379 /* IN_UV NOT_INT
2380 0 0 already failed to read UV.
2381 0 1 already failed to read UV.
2382 1 0 you won't get here in this case. IV/UV
2383 slot set, public IOK, Atof() unneeded.
2384 1 1 already read UV.
2385 so there's no point in sv_2iuv_non_preserve() attempting
2386 to use atol, strtol, strtoul etc. */
2387 if (sv_2iuv_non_preserve (sv, numtype)
2388 >= IS_NUMBER_OVERFLOW_IV)
2389 goto ret_iv_max;
2390 }
2391 }
28e5dec8 2392#endif /* NV_PRESERVES_UV */
25da4f38 2393 }
28e5dec8 2394 } else {
599cee73 2395 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1d7c1841 2396 report_uninit();
25da4f38
IZ
2397 if (SvTYPE(sv) < SVt_IV)
2398 /* Typically the caller expects that sv_any is not NULL now. */
2399 sv_upgrade(sv, SVt_IV);
a0d0e21e 2400 return 0;
79072805 2401 }
1d7c1841
GS
2402 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2403 PTR2UV(sv),SvIVX(sv)));
25da4f38 2404 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
2405}
2406
645c22ef
DM
2407/*
2408=for apidoc sv_2uv
2409
2410Return the unsigned integer value of an SV, doing any necessary string
2411conversion, magic etc. Normally used via the C<SvUV(sv)> and C<SvUVx(sv)>
2412macros.
2413
2414=cut
2415*/
2416
ff68c719 2417UV
864dbfa3 2418Perl_sv_2uv(pTHX_ register SV *sv)
ff68c719 2419{
2420 if (!sv)
2421 return 0;
2422 if (SvGMAGICAL(sv)) {
2423 mg_get(sv);
2424 if (SvIOKp(sv))
2425 return SvUVX(sv);
2426 if (SvNOKp(sv))
2427 return U_V(SvNVX(sv));
36477c24 2428 if (SvPOKp(sv) && SvLEN(sv))
2429 return asUV(sv);
3fe9a6f1 2430 if (!SvROK(sv)) {
d008e5eb 2431 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2432 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2433 report_uninit();
c6ee37c5 2434 }
36477c24 2435 return 0;
3fe9a6f1 2436 }
ff68c719 2437 }
2438 if (SvTHINKFIRST(sv)) {
2439 if (SvROK(sv)) {
ff68c719 2440 SV* tmpstr;
1554e226 2441 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2442 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2443 return SvUV(tmpstr);
56431972 2444 return PTR2UV(SvRV(sv));
ff68c719 2445 }
8a818333
NIS
2446 if (SvREADONLY(sv) && SvFAKE(sv)) {
2447 sv_force_normal(sv);
2448 }
0336b60e 2449 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2450 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 2451 report_uninit();
ff68c719 2452 return 0;
2453 }
2454 }
25da4f38
IZ
2455 if (SvIOKp(sv)) {
2456 if (SvIsUV(sv)) {
2457 return SvUVX(sv);
2458 }
2459 else {
2460 return (UV)SvIVX(sv);
2461 }
ff68c719 2462 }
2463 if (SvNOKp(sv)) {
28e5dec8
JH
2464 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2465 * without also getting a cached IV/UV from it at the same time
2466 * (ie PV->NV conversion should detect loss of accuracy and cache
2467 * IV or UV at same time to avoid this. */
2468 /* IV-over-UV optimisation - choose to cache IV if possible */
2469
25da4f38
IZ
2470 if (SvTYPE(sv) == SVt_NV)
2471 sv_upgrade(sv, SVt_PVNV);
28e5dec8
JH
2472
2473 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2474 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
0da6cfda 2475 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2476 if (SvNVX(sv) == (NV) SvIVX(sv)
2477#ifndef NV_PRESERVES_UV
2478 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2479 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2480 /* Don't flag it as "accurately an integer" if the number
2481 came from a (by definition imprecise) NV operation, and
2482 we're outside the range of NV integer precision */
2483#endif
2484 ) {
2485 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2486 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2487 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2488 PTR2UV(sv),
2489 SvNVX(sv),
2490 SvIVX(sv)));
2491
2492 } else {
2493 /* IV not precise. No need to convert from PV, as NV
2494 conversion would already have cached IV if it detected
2495 that PV->IV would be better than PV->NV->IV
2496 flags already correct - don't set public IOK. */
2497 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2498 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2499 PTR2UV(sv),
2500 SvNVX(sv),
2501 SvIVX(sv)));
2502 }
2503 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2504 but the cast (NV)IV_MIN rounds to a the value less (more
2505 negative) than IV_MIN which happens to be equal to SvNVX ??
2506 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2507 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2508 (NV)UVX == NVX are both true, but the values differ. :-(
2509 Hopefully for 2s complement IV_MIN is something like
2510 0x8000000000000000 which will be exact. NWC */
d460ef45 2511 }
28e5dec8 2512 else {
0da6cfda 2513 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2514 if (
2515 (SvNVX(sv) == (NV) SvUVX(sv))
2516#ifndef NV_PRESERVES_UV
2517 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2518 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2519 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2520 /* Don't flag it as "accurately an integer" if the number
2521 came from a (by definition imprecise) NV operation, and
2522 we're outside the range of NV integer precision */
2523#endif
2524 )
2525 SvIOK_on(sv);
2526 SvIsUV_on(sv);
1c846c1f 2527 DEBUG_c(PerlIO_printf(Perl_debug_log,
28e5dec8 2528 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
57def98f 2529 PTR2UV(sv),
28e5dec8
JH
2530 SvUVX(sv),
2531 SvUVX(sv)));
25da4f38 2532 }
ff68c719 2533 }
2534 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2535 UV value;
547d29e4 2536 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
25da4f38
IZ
2537
2538 /* We want to avoid a possible problem when we cache a UV which
2539 may be later translated to an NV, and the resulting NV is not
2540 the translation of the initial data.
1c846c1f 2541
25da4f38
IZ
2542 This means that if we cache such a UV, we need to cache the
2543 NV as well. Moreover, we trade speed for space, and do not
2544 cache the NV if not needed.
2545 */
16b7a9a4 2546
c2988b20
NC
2547 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2548 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2549 == IS_NUMBER_IN_UV) {
5e045b90 2550 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8 2551 if (SvTYPE(sv) < SVt_PVIV)
f7bbb42a
JH
2552 sv_upgrade(sv, SVt_PVIV);
2553 (void)SvIOK_on(sv);
c2988b20
NC
2554 } else if (SvTYPE(sv) < SVt_PVNV)
2555 sv_upgrade(sv, SVt_PVNV);
d460ef45 2556
c2988b20
NC
2557 /* If NV preserves UV then we only use the UV value if we know that
2558 we aren't going to call atof() below. If NVs don't preserve UVs
2559 then the value returned may have more precision than atof() will
2560 return, even though it isn't accurate. */
2561 if ((numtype & (IS_NUMBER_IN_UV
2562#ifdef NV_PRESERVES_UV
2563 | IS_NUMBER_NOT_INT
2564#endif
2565 )) == IS_NUMBER_IN_UV) {
2566 /* This won't turn off the public IOK flag if it was set above */
2567 (void)SvIOKp_on(sv);
2568
2569 if (!(numtype & IS_NUMBER_NEG)) {
2570 /* positive */;
2571 if (value <= (UV)IV_MAX) {
0da6cfda 2572 SvIV_set(sv, (IV)value);
28e5dec8
JH
2573 } else {
2574 /* it didn't overflow, and it was positive. */
0da6cfda 2575 SvUV_set(sv, value);
28e5dec8
JH
2576 SvIsUV_on(sv);
2577 }
c2988b20
NC
2578 } else {
2579 /* 2s complement assumption */
2580 if (value <= (UV)IV_MIN) {
0da6cfda 2581 SvIV_set(sv, -(IV)value);
c2988b20
NC
2582 } else {
2583 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2584 I'm assuming it will be rare. */
c2988b20
NC
2585 if (SvTYPE(sv) < SVt_PVNV)
2586 sv_upgrade(sv, SVt_PVNV);
2587 SvNOK_on(sv);
2588 SvIOK_off(sv);
2589 SvIOKp_on(sv);
0da6cfda
SP
2590 SvNV_set(sv, -(NV)value);
2591 SvIV_set(sv, IV_MIN);
c2988b20
NC
2592 }
2593 }
2594 }
2595
2596 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2597 != IS_NUMBER_IN_UV) {
2598 /* It wasn't an integer, or it overflowed the UV. */
fdac8c4b 2599 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8 2600
c2988b20 2601 if (! numtype && ckWARN(WARN_NUMERIC))
28e5dec8
JH
2602 not_a_number(sv);
2603
2604#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2605 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
2606 PTR2UV(sv), SvNVX(sv)));
28e5dec8 2607#else
1779d84d 2608 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
c2988b20 2609 PTR2UV(sv), SvNVX(sv)));
28e5dec8
JH
2610#endif
2611
2612#ifdef NV_PRESERVES_UV
c2988b20
NC
2613 (void)SvIOKp_on(sv);
2614 (void)SvNOK_on(sv);
2615 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
0da6cfda 2616 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2617 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2618 SvIOK_on(sv);
2619 } else {
2620 /* Integer is imprecise. NOK, IOKp */
2621 }
2622 /* UV will not work better than IV */
2623 } else {
2624 if (SvNVX(sv) > (NV)UV_MAX) {
2625 SvIsUV_on(sv);
2626 /* Integer is inaccurate. NOK, IOKp, is UV */
0da6cfda 2627 SvUV_set(sv, UV_MAX);
c2988b20
NC
2628 SvIsUV_on(sv);
2629 } else {
0da6cfda 2630 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2631 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2632 NV preservse UV so can do correct comparison. */
2633 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2634 SvIOK_on(sv);
2635 SvIsUV_on(sv);
2636 } else {
2637 /* Integer is imprecise. NOK, IOKp, is UV */
2638 SvIsUV_on(sv);
2639 }
2640 }
2641 }
28e5dec8 2642#else /* NV_PRESERVES_UV */
c2988b20
NC
2643 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2644 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2645 /* The UV slot will have been set from value returned by
2646 grok_number above. The NV slot has just been set using
2647 Atof. */
560b0c46 2648 SvNOK_on(sv);
c2988b20
NC
2649 assert (SvIOKp(sv));
2650 } else {
2651 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2652 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2653 /* Small enough to preserve all bits. */
2654 (void)SvIOKp_on(sv);
2655 SvNOK_on(sv);
0da6cfda 2656 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2657 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2658 SvIOK_on(sv);
2659 /* Assumption: first non-preserved integer is < IV_MAX,
2660 this NV is in the preserved range, therefore: */
2661 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2662 < (UV)IV_MAX)) {
6b6e5432 2663 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
2664 }
2665 } else
2666 sv_2iuv_non_preserve (sv, numtype);
2667 }
28e5dec8 2668#endif /* NV_PRESERVES_UV */
f7bbb42a 2669 }
ff68c719 2670 }
2671 else {
d008e5eb 2672 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2673 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2674 report_uninit();
c6ee37c5 2675 }
25da4f38
IZ
2676 if (SvTYPE(sv) < SVt_IV)
2677 /* Typically the caller expects that sv_any is not NULL now. */
2678 sv_upgrade(sv, SVt_IV);
ff68c719 2679 return 0;
2680 }
25da4f38 2681
1d7c1841
GS
2682 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2683 PTR2UV(sv),SvUVX(sv)));
25da4f38 2684 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719 2685}
2686
645c22ef
DM
2687/*
2688=for apidoc sv_2nv
2689
2690Return the num value of an SV, doing any necessary string or integer
2691conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
2692macros.
2693
2694=cut
2695*/
2696
65202027 2697NV
864dbfa3 2698Perl_sv_2nv(pTHX_ register SV *sv)
79072805
LW
2699{
2700 if (!sv)
2701 return 0.0;
8990e307 2702 if (SvGMAGICAL(sv)) {
463ee0b2
LW
2703 mg_get(sv);
2704 if (SvNOKp(sv))
2705 return SvNVX(sv);
a0d0e21e 2706 if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2707 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
547d29e4 2708 !grok_number(SvPVX_const(sv), SvCUR(sv), NULL))
a0d0e21e 2709 not_a_number(sv);
fdac8c4b 2710 return Atof(SvPVX_const(sv));
a0d0e21e 2711 }
25da4f38 2712 if (SvIOKp(sv)) {
1c846c1f 2713 if (SvIsUV(sv))
65202027 2714 return (NV)SvUVX(sv);
25da4f38 2715 else
65202027 2716 return (NV)SvIVX(sv);
25da4f38 2717 }
16d20bd9 2718 if (!SvROK(sv)) {
d008e5eb 2719 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2720 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2721 report_uninit();
c6ee37c5 2722 }
16d20bd9
AD
2723 return 0;
2724 }
463ee0b2 2725 }
ed6116ce 2726 if (SvTHINKFIRST(sv)) {
a0d0e21e 2727 if (SvROK(sv)) {
a0d0e21e 2728 SV* tmpstr;
1554e226 2729 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2730 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2731 return SvNV(tmpstr);
56431972 2732 return PTR2NV(SvRV(sv));
a0d0e21e 2733 }
8a818333
NIS
2734 if (SvREADONLY(sv) && SvFAKE(sv)) {
2735 sv_force_normal(sv);
2736 }
0336b60e 2737 if (SvREADONLY(sv) && !SvOK(sv)) {
599cee73 2738 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 2739 report_uninit();
ed6116ce
LW
2740 return 0.0;
2741 }
79072805
LW
2742 }
2743 if (SvTYPE(sv) < SVt_NV) {
463ee0b2
LW
2744 if (SvTYPE(sv) == SVt_IV)
2745 sv_upgrade(sv, SVt_PVNV);
2746 else
2747 sv_upgrade(sv, SVt_NV);
906f284f 2748#ifdef USE_LONG_DOUBLE
097ee67d 2749 DEBUG_c({
f93f4e46 2750 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2751 PerlIO_printf(Perl_debug_log,
2752 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2753 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2754 RESTORE_NUMERIC_LOCAL();
2755 });
65202027 2756#else
572bbb43 2757 DEBUG_c({
f93f4e46 2758 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2759 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
1d7c1841 2760 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2761 RESTORE_NUMERIC_LOCAL();
2762 });
572bbb43 2763#endif
79072805
LW
2764 }
2765 else if (SvTYPE(sv) < SVt_PVNV)
2766 sv_upgrade(sv, SVt_PVNV);
59d8ce62
NC
2767 if (SvNOKp(sv)) {
2768 return SvNVX(sv);
61604483 2769 }
59d8ce62 2770 if (SvIOKp(sv)) {
0da6cfda 2771 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
28e5dec8
JH
2772#ifdef NV_PRESERVES_UV
2773 SvNOK_on(sv);
2774#else
2775 /* Only set the public NV OK flag if this NV preserves the IV */
2776 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2777 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2778 : (SvIVX(sv) == I_V(SvNVX(sv))))
2779 SvNOK_on(sv);
2780 else
2781 SvNOKp_on(sv);
2782#endif
93a17b20 2783 }
748a9306 2784 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20 2785 UV value;
fdac8c4b 2786 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
c2988b20 2787 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
a0d0e21e 2788 not_a_number(sv);
28e5dec8 2789#ifdef NV_PRESERVES_UV
c2988b20
NC
2790 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2791 == IS_NUMBER_IN_UV) {
5e045b90 2792 /* It's definitely an integer */
0da6cfda 2793 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
c2988b20 2794 } else
fdac8c4b 2795 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
2796 SvNOK_on(sv);
2797#else
fdac8c4b 2798 SvNV_set(sv, Atof(SvPVX_const(sv)));
28e5dec8
JH
2799 /* Only set the public NV OK flag if this NV preserves the value in
2800 the PV at least as well as an IV/UV would.
2801 Not sure how to do this 100% reliably. */
2802 /* if that shift count is out of range then Configure's test is
2803 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2804 UV_BITS */
2805 if (((UV)1 << NV_PRESERVES_UV_BITS) >
c2988b20 2806 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
28e5dec8 2807 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
c2988b20
NC
2808 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2809 /* Can't use strtol etc to convert this string, so don't try.
2810 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2811 SvNOK_on(sv);
2812 } else {
2813 /* value has been set. It may not be precise. */
2814 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
2815 /* 2s complement assumption for (UV)IV_MIN */
2816 SvNOK_on(sv); /* Integer is too negative. */
2817 } else {
2818 SvNOKp_on(sv);
2819 SvIOKp_on(sv);
6fa402ec 2820
c2988b20 2821 if (numtype & IS_NUMBER_NEG) {
0da6cfda 2822 SvIV_set(sv, -(IV)value);
c2988b20 2823 } else if (value <= (UV)IV_MAX) {
0da6cfda 2824 SvIV_set(sv, (IV)value);
c2988b20 2825 } else {
0da6cfda 2826 SvUV_set(sv, value);
c2988b20
NC
2827 SvIsUV_on(sv);
2828 }
2829
2830 if (numtype & IS_NUMBER_NOT_INT) {
2831 /* I believe that even if the original PV had decimals,
2832 they are lost beyond the limit of the FP precision.
2833 However, neither is canonical, so both only get p
2834 flags. NWC, 2000/11/25 */
2835 /* Both already have p flags, so do nothing */
2836 } else {
2837 NV nv = SvNVX(sv);
2838 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2839 if (SvIVX(sv) == I_V(nv)) {
2840 SvNOK_on(sv);
2841 SvIOK_on(sv);
2842 } else {
2843 SvIOK_on(sv);
2844 /* It had no "." so it must be integer. */
2845 }
2846 } else {
2847 /* between IV_MAX and NV(UV_MAX).
2848 Could be slightly > UV_MAX */
6fa402ec 2849
c2988b20
NC
2850 if (numtype & IS_NUMBER_NOT_INT) {
2851 /* UV and NV both imprecise. */
2852 } else {
2853 UV nv_as_uv = U_V(nv);
2854
2855 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2856 SvNOK_on(sv);
2857 SvIOK_on(sv);
2858 } else {
2859 SvIOK_on(sv);
2860 }
2861 }
2862 }
2863 }
2864 }
2865 }
28e5dec8 2866#endif /* NV_PRESERVES_UV */
93a17b20 2867 }
79072805 2868 else {
599cee73 2869 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1d7c1841 2870 report_uninit();
25da4f38
IZ
2871 if (SvTYPE(sv) < SVt_NV)
2872 /* Typically the caller expects that sv_any is not NULL now. */
28e5dec8
JH
2873 /* XXX Ilya implies that this is a bug in callers that assume this
2874 and ideally should be fixed. */
25da4f38 2875 sv_upgrade(sv, SVt_NV);
a0d0e21e 2876 return 0.0;
79072805 2877 }
572bbb43 2878#if defined(USE_LONG_DOUBLE)
097ee67d 2879 DEBUG_c({
f93f4e46 2880 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2881 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2882 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2883 RESTORE_NUMERIC_LOCAL();
2884 });
65202027 2885#else
572bbb43 2886 DEBUG_c({
f93f4e46 2887 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 2888 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
1d7c1841 2889 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2890 RESTORE_NUMERIC_LOCAL();
2891 });
572bbb43 2892#endif
463ee0b2 2893 return SvNVX(sv);
79072805
LW
2894}
2895
645c22ef
DM
2896/* asIV(): extract an integer from the string value of an SV.
2897 * Caller must validate PVX */
2898
76e3520e 2899STATIC IV
cea2e8a9 2900S_asIV(pTHX_ SV *sv)
36477c24 2901{
c2988b20 2902 UV value;
fdac8c4b 2903 int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
c2988b20
NC
2904
2905 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2906 == IS_NUMBER_IN_UV) {
645c22ef 2907 /* It's definitely an integer */
c2988b20
NC
2908 if (numtype & IS_NUMBER_NEG) {
2909 if (value < (UV)IV_MIN)
2910 return -(IV)value;
2911 } else {
2912 if (value < (UV)IV_MAX)
2913 return (IV)value;
2914 }
2915 }
d008e5eb 2916 if (!numtype) {
d008e5eb
GS
2917 if (ckWARN(WARN_NUMERIC))
2918 not_a_number(sv);
2919 }
fdac8c4b 2920 return I_V(Atof(SvPVX_const(sv)));
36477c24 2921}
2922
645c22ef
DM
2923/* asUV(): extract an unsigned integer from the string value of an SV
2924 * Caller must validate PVX */
2925
76e3520e 2926STATIC UV
cea2e8a9 2927S_asUV(pTHX_ SV *sv)
36477c24 2928{
c2988b20 2929 UV value;
547d29e4 2930 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
36477c24 2931
c2988b20
NC
2932 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2933 == IS_NUMBER_IN_UV) {
645c22ef 2934 /* It's definitely an integer */
6fa402ec 2935 if (!(numtype & IS_NUMBER_NEG))
c2988b20
NC
2936 return value;
2937 }
d008e5eb 2938 if (!numtype) {
d008e5eb
GS
2939 if (ckWARN(WARN_NUMERIC))
2940 not_a_number(sv);
2941 }
fdac8c4b 2942 return U_V(Atof(SvPVX_const(sv)));
36477c24 2943}
2944
645c22ef
DM
2945/*
2946=for apidoc sv_2pv_nolen
2947
2948Like C<sv_2pv()>, but doesn't return the length too. You should usually
2949use the macro wrapper C<SvPV_nolen(sv)> instead.
2950=cut
2951*/
2952
79072805 2953char *
864dbfa3 2954Perl_sv_2pv_nolen(pTHX_ register SV *sv)
1fa8b10d
JD
2955{
2956 STRLEN n_a;
2957 return sv_2pv(sv, &n_a);
2958}
2959
645c22ef
DM
2960/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2961 * UV as a string towards the end of buf, and return pointers to start and
2962 * end of it.
2963 *
2964 * We assume that buf is at least TYPE_CHARS(UV) long.
2965 */
2966
864dbfa3 2967static char *
25da4f38
IZ
2968uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
2969{
25da4f38
IZ
2970 char *ptr = buf + TYPE_CHARS(UV);
2971 char *ebuf = ptr;
2972 int sign;
25da4f38
IZ
2973
2974 if (is_uv)
2975 sign = 0;
2976 else if (iv >= 0) {
2977 uv = iv;
2978 sign = 0;
2979 } else {
2980 uv = -iv;
2981 sign = 1;
2982 }
2983 do {
eb160463 2984 *--ptr = '0' + (char)(uv % 10);
25da4f38
IZ
2985 } while (uv /= 10);
2986 if (sign)
2987 *--ptr = '-';
2988 *peob = ebuf;
2989 return ptr;
2990}
2991
d34f9d2e
JH
2992/* sv_2pv() is now a macro using Perl_sv_2pv_flags();
2993 * this function provided for binary compatibility only
2994 */
2995
2996char *
2997Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
2998{
2999 return sv_2pv_flags(sv, lp, SV_GMAGIC);
3000}
3001
645c22ef
DM
3002/*
3003=for apidoc sv_2pv_flags
3004
ff276b08 3005Returns a pointer to the string value of an SV, and sets *lp to its length.
645c22ef
DM
3006If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
3007if necessary.
3008Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
3009usually end up here too.
3010
3011=cut
3012*/
3013
8d6d96c1
HS
3014char *
3015Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
3016{
79072805
LW
3017 register char *s;
3018 int olderrno;
5b7ea690 3019 SV *tsv, *origsv;
25da4f38
IZ
3020 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
3021 char *tmpbuf = tbuf;
79072805 3022
463ee0b2
LW
3023 if (!sv) {
3024 *lp = 0;
e2b56717 3025 return (char *)"";
463ee0b2 3026 }
8990e307 3027 if (SvGMAGICAL(sv)) {
8d6d96c1
HS
3028 if (flags & SV_GMAGIC)
3029 mg_get(sv);
463ee0b2
LW
3030 if (SvPOKp(sv)) {
3031 *lp = SvCUR(sv);
3032 return SvPVX(sv);
3033 }
cf2093f6 3034 if (SvIOKp(sv)) {
1c846c1f 3035 if (SvIsUV(sv))
57def98f 3036 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
cf2093f6 3037 else
57def98f 3038 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
46fc3d4c 3039 tsv = Nullsv;
a0d0e21e 3040 goto tokensave;
463ee0b2
LW
3041 }
3042 if (SvNOKp(sv)) {
2d4389e4 3043 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
46fc3d4c 3044 tsv = Nullsv;
a0d0e21e 3045 goto tokensave;
463ee0b2 3046 }
16d20bd9 3047 if (!SvROK(sv)) {
d008e5eb 3048 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 3049 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 3050 report_uninit();
c6ee37c5 3051 }
16d20bd9 3052 *lp = 0;
e2b56717 3053 return (char *)"";
16d20bd9 3054 }
463ee0b2 3055 }
ed6116ce
LW
3056 if (SvTHINKFIRST(sv)) {
3057 if (SvROK(sv)) {
a0d0e21e 3058 SV* tmpstr;
c05e0e2f 3059 register const char *typestr;
1554e226 3060 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
b4b9a328 3061 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
5b7ea690
JH
3062 char *pv = SvPV(tmpstr, *lp);
3063 if (SvUTF8(tmpstr))
3064 SvUTF8_on(sv);
3065 else
3066 SvUTF8_off(sv);
3067 return pv;
3068 }
3069 origsv = sv;
ed6116ce
LW
3070 sv = (SV*)SvRV(sv);
3071 if (!sv)
c05e0e2f 3072 typestr = "NULLREF";
ed6116ce 3073 else {
f9277f47
IZ
3074 MAGIC *mg;
3075
ed6116ce 3076 switch (SvTYPE(sv)) {
f9277f47
IZ
3077 case SVt_PVMG:
3078 if ( ((SvFLAGS(sv) &
1c846c1f 3079 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
5835a535 3080 == (SVs_OBJECT|SVs_SMG))
14befaf4 3081 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
c05e0e2f 3082 const regexp *re = (regexp *)mg->mg_obj;
1bd3ad17 3083
2cd61cdb 3084 if (!mg->mg_ptr) {
c05e0e2f 3085 const char *fptr = "msix";
8782bef2
GB
3086 char reflags[6];
3087 char ch;
3088 int left = 0;
3089 int right = 4;
ff385a1b 3090 char need_newline = 0;
eb160463 3091 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
8782bef2 3092
155aba94 3093 while((ch = *fptr++)) {
8782bef2
GB
3094 if(reganch & 1) {
3095 reflags[left++] = ch;
3096 }
3097 else {
3098 reflags[right--] = ch;
3099 }
3100 reganch >>= 1;
3101 }
3102 if(left != 4) {
3103 reflags[left] = '-';
3104 left = 5;
3105 }
3106
3107 mg->mg_len = re->prelen + 4 + left;
ff385a1b
JF
3108 /*
3109 * If /x was used, we have to worry about a regex
3110 * ending with a comment later being embedded
3111 * within another regex. If so, we don't want this
3112 * regex's "commentization" to leak out to the
3113 * right part of the enclosing regex, we must cap
3114 * it with a newline.
3115 *
3116 * So, if /x was used, we scan backwards from the
3117 * end of the regex. If we find a '#' before we
3118 * find a newline, we need to add a newline
3119 * ourself. If we find a '\n' first (or if we
3120 * don't find '#' or '\n'), we don't need to add
3121 * anything. -jfriedl
3122 */
3123 if (PMf_EXTENDED & re->reganch)
3124 {
c05e0e2f 3125 const char *endptr = re->precomp + re->prelen;
ff385a1b
JF
3126 while (endptr >= re->precomp)
3127 {
c05e0e2f 3128 const char c = *(endptr--);
ff385a1b
JF
3129 if (c == '\n')
3130 break; /* don't need another */
3131 if (c == '#') {
3132 /* we end while in a comment, so we
3133 need a newline */
3134 mg->mg_len++; /* save space for it */
3135 need_newline = 1; /* note to add it */
5b7ea690 3136 break;
ff385a1b
JF
3137 }
3138 }
3139 }
3140
8782bef2
GB
3141 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3142 Copy("(?", mg->mg_ptr, 2, char);
3143 Copy(reflags, mg->mg_ptr+2, left, char);
3144 Copy(":", mg->mg_ptr+left+2, 1, char);
3145 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
ff385a1b
JF
3146 if (need_newline)
3147 mg->mg_ptr[mg->mg_len - 2] = '\n';
1bd3ad17
IZ
3148 mg->mg_ptr[mg->mg_len - 1] = ')';
3149 mg->mg_ptr[mg->mg_len] = 0;
3150 }
3280af22 3151 PL_reginterp_cnt += re->program[0].next_off;
5b7ea690
JH
3152
3153 if (re->reganch & ROPT_UTF8)
3154 SvUTF8_on(origsv);
3155 else
3156 SvUTF8_off(origsv);
1bd3ad17
IZ
3157 *lp = mg->mg_len;
3158 return mg->mg_ptr;
f9277f47
IZ
3159 }
3160 /* Fall through */
ed6116ce
LW
3161 case SVt_NULL:
3162 case SVt_IV:
3163 case SVt_NV:
3164 case SVt_RV:
3165 case SVt_PV:
3166 case SVt_PVIV:
3167 case SVt_PVNV:
c05e0e2f
AL
3168 case SVt_PVBM: typestr = SvROK(sv) ? "REF" : "SCALAR"; break;
3169 case SVt_PVLV: typestr = SvROK(sv) ? "REF"
ac388100
JH
3170 /* tied lvalues should appear to be
3171 * scalars for backwards compatitbility */
3172 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
3173 ? "SCALAR" : "LVALUE"; break;
c05e0e2f
AL
3174 case SVt_PVAV: typestr = "ARRAY"; break;
3175 case SVt_PVHV: typestr = "HASH"; break;
3176 case SVt_PVCV: typestr = "CODE"; break;
3177 case SVt_PVGV: typestr = "GLOB"; break;
3178 case SVt_PVFM: typestr = "FORMAT"; break;
3179 case SVt_PVIO: typestr = "IO"; break;
3180 default: typestr = "UNKNOWN"; break;
ed6116ce 3181 }
46fc3d4c 3182 tsv = NEWSV(0,0);
3c71ae1e 3183 if (SvOBJECT(sv)) {
26ab6a78 3184 const char *name = HvNAME_get(SvSTASH(sv));
3c71ae1e 3185 Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
c05e0e2f 3186 name ? name : "__ANON__" , typestr, PTR2UV(sv));
3c71ae1e 3187 }
ed6116ce 3188 else
c05e0e2f 3189 Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv));
a0d0e21e 3190 goto tokensaveref;
463ee0b2 3191 }
c05e0e2f 3192 *lp = strlen(typestr);
e2b56717 3193 return (char *)typestr;
79072805 3194 }
0336b60e 3195 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 3196 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 3197 report_uninit();
ed6116ce 3198 *lp = 0;
e2b56717 3199 return (char *)"";
79072805 3200 }
79072805 3201 }
28e5dec8
JH
3202 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3203 /* I'm assuming that if both IV and NV are equally valid then
3204 converting the IV is going to be more efficient */
c05e0e2f
AL
3205 const U32 isIOK = SvIOK(sv);
3206 const U32 isUIOK = SvIsUV(sv);
28e5dec8
JH
3207 char buf[TYPE_CHARS(UV)];
3208 char *ebuf, *ptr;
3209
3210 if (SvTYPE(sv) < SVt_PVIV)
3211 sv_upgrade(sv, SVt_PVIV);
3212 if (isUIOK)
3213 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3214 else
3215 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
eb160463 3216 SvGROW(sv, (STRLEN)(ebuf - ptr + 1)); /* inlined from sv_setpvn */
28e5dec8
JH
3217 Move(ptr,SvPVX(sv),ebuf - ptr,char);
3218 SvCUR_set(sv, ebuf - ptr);
3219 s = SvEND(sv);
3220 *s = '\0';
3221 if (isIOK)
3222 SvIOK_on(sv);
3223 else
3224 SvIOKp_on(sv);
3225 if (isUIOK)
3226 SvIsUV_on(sv);
3227 }
3228 else if (SvNOKp(sv)) {
79072805
LW
3229 if (SvTYPE(sv) < SVt_PVNV)
3230 sv_upgrade(sv, SVt_PVNV);
1c846c1f 3231 /* The +20 is pure guesswork. Configure test needed. --jhi */
59155cc0 3232 SvGROW(sv, NV_DIG + 20);
463ee0b2 3233 s = SvPVX(sv);
79072805 3234 olderrno = errno; /* some Xenix systems wipe out errno here */
79072805 3235#ifdef apollo
463ee0b2 3236 if (SvNVX(sv) == 0.0)
79072805
LW
3237 (void)strcpy(s,"0");
3238 else
3239#endif /*apollo*/
bbce6d69 3240 {
2d4389e4 3241 Gconvert(SvNVX(sv), NV_DIG, 0, s);
bbce6d69 3242 }
79072805 3243 errno = olderrno;
a0d0e21e
LW
3244#ifdef FIXNEGATIVEZERO
3245 if (*s == '-' && s[1] == '0' && !s[2])
3246 strcpy(s,"0");
3247#endif
79072805
LW
3248 while (*s) s++;
3249#ifdef hcx
3250 if (s[-1] == '.')
46fc3d4c 3251 *--s = '\0';
79072805
LW
3252#endif
3253 }
79072805 3254 else {
0336b60e
IZ
3255 if (ckWARN(WARN_UNINITIALIZED)
3256 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1d7c1841 3257 report_uninit();
a0d0e21e 3258 *lp = 0;
25da4f38
IZ
3259 if (SvTYPE(sv) < SVt_PV)
3260 /* Typically the caller expects that sv_any is not NULL now. */
3261 sv_upgrade(sv, SVt_PV);
e2b56717 3262 return (char *)"";
79072805 3263 }
fdac8c4b 3264 *lp = s - SvPVX_const(sv);
463ee0b2 3265 SvCUR_set(sv, *lp);
79072805 3266 SvPOK_on(sv);
1d7c1841 3267 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
fdac8c4b 3268 PTR2UV(sv),SvPVX_const(sv)));
463ee0b2 3269 return SvPVX(sv);
a0d0e21e
LW
3270
3271 tokensave:
3272 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3273 /* Sneaky stuff here */
3274
3275 tokensaveref:
46fc3d4c 3276 if (!tsv)
96827780 3277 tsv = newSVpv(tmpbuf, 0);
46fc3d4c 3278 sv_2mortal(tsv);
3279 *lp = SvCUR(tsv);
3280 return SvPVX(tsv);
a0d0e21e
LW
3281 }
3282 else {
3283 STRLEN len;
e2b56717 3284 const char *t;
46fc3d4c 3285
3286 if (tsv) {
3287 sv_2mortal(tsv);
fdac8c4b 3288 t = SvPVX_const(tsv);
46fc3d4c 3289 len = SvCUR(tsv);
3290 }
3291 else {
96827780
MB
3292 t = tmpbuf;
3293 len = strlen(tmpbuf);
46fc3d4c 3294 }
a0d0e21e 3295#ifdef FIXNEGATIVEZERO
46fc3d4c 3296 if (len == 2 && t[0] == '-' && t[1] == '0') {
3297 t = "0";
3298 len = 1;
3299 }
a0d0e21e
LW
3300#endif
3301 (void)SvUPGRADE(sv, SVt_PV);
46fc3d4c 3302 *lp = len;
a0d0e21e
LW
3303 s = SvGROW(sv, len + 1);
3304 SvCUR_set(sv, len);
6bf554b4 3305 SvPOKp_on(sv);
735fe74b 3306 return strcpy(s, t);
a0d0e21e 3307 }
463ee0b2
LW
3308}
3309
645c22ef 3310/*
6050d10e
JP
3311=for apidoc sv_copypv
3312
3313Copies a stringified representation of the source SV into the
3314destination SV. Automatically performs any necessary mg_get and
54f0641b 3315coercion of numeric values into strings. Guaranteed to preserve
6050d10e 3316UTF-8 flag even from overloaded objects. Similar in nature to
54f0641b
NIS
3317sv_2pv[_flags] but operates directly on an SV instead of just the
3318string. Mostly uses sv_2pv_flags to do its work, except when that
6050d10e
JP
3319would lose the UTF-8'ness of the PV.
3320
3321=cut
3322*/
3323
3324void
3325Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3326{
5b7ea690
JH
3327 STRLEN len;
3328 char *s;
3329 s = SvPV(ssv,len);
3330 sv_setpvn(dsv,s,len);
3331 if (SvUTF8(ssv))
3332 SvUTF8_on(dsv);
3333 else
3334 SvUTF8_off(dsv);
6050d10e
JP
3335}
3336
3337/*
645c22ef
DM
3338=for apidoc sv_2pvbyte_nolen
3339
3340Return a pointer to the byte-encoded representation of the SV.
cd458e05 3341May cause the SV to be downgraded from UTF-8 as a side-effect.
645c22ef
DM
3342
3343Usually accessed via the C<SvPVbyte_nolen> macro.
3344
3345=cut
3346*/
3347
7340a771
GS
3348char *
3349Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3350{
560a288e
GS
3351 STRLEN n_a;
3352 return sv_2pvbyte(sv, &n_a);
7340a771
GS
3353}
3354
645c22ef
DM
3355/*
3356=for apidoc sv_2pvbyte
3357
3358Return a pointer to the byte-encoded representation of the SV, and set *lp
cd458e05 3359to its length. May cause the SV to be downgraded from UTF-8 as a
645c22ef
DM
3360side-effect.
3361
3362Usually accessed via the C<SvPVbyte> macro.
3363
3364=cut
3365*/
3366
7340a771
GS
3367char *
3368Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3369{
0875d2fe
NIS
3370 sv_utf8_downgrade(sv,0);
3371 return SvPV(sv,*lp);
7340a771
GS
3372}
3373
645c22ef
DM
3374/*
3375=for apidoc sv_2pvutf8_nolen
3376
cd458e05
JH
3377Return a pointer to the UTF-8-encoded representation of the SV.
3378May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3379
3380Usually accessed via the C<SvPVutf8_nolen> macro.
3381
3382=cut
3383*/
3384
7340a771
GS
3385char *
3386Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3387{
560a288e
GS
3388 STRLEN n_a;
3389 return sv_2pvutf8(sv, &n_a);
7340a771
GS
3390}
3391
645c22ef
DM
3392/*
3393=for apidoc sv_2pvutf8
3394
cd458e05
JH
3395Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3396to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3397
3398Usually accessed via the C<SvPVutf8> macro.
3399
3400=cut
3401*/
3402
7340a771
GS
3403char *
3404Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3405{
560a288e 3406 sv_utf8_upgrade(sv);
7d59b7e4 3407 return SvPV(sv,*lp);
7340a771 3408}
1c846c1f 3409
645c22ef
DM
3410/*
3411=for apidoc sv_2bool
3412
3413This function is only called on magical items, and is only used by
8cf8f3d1 3414sv_true() or its macro equivalent.
645c22ef
DM
3415
3416=cut
3417*/
3418
463ee0b2 3419bool
864dbfa3 3420Perl_sv_2bool(pTHX_ register SV *sv)
463ee0b2 3421{
8990e307 3422 if (SvGMAGICAL(sv))
463ee0b2
LW
3423 mg_get(sv);
3424
a0d0e21e
LW
3425 if (!SvOK(sv))
3426 return 0;
3427 if (SvROK(sv)) {
a0d0e21e 3428 SV* tmpsv;
1554e226 3429 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
9e3013b1 3430 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
8a31060d 3431 return (bool)SvTRUE(tmpsv);
a0d0e21e
LW
3432 return SvRV(sv) != 0;
3433 }
463ee0b2 3434 if (SvPOKp(sv)) {
11343788
MB
3435 register XPV* Xpvtmp;
3436 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
3437 (*Xpvtmp->xpv_pv > '0' ||
3438 Xpvtmp->xpv_cur > 1 ||
3439 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
463ee0b2
LW
3440 return 1;
3441 else
3442 return 0;
3443 }
3444 else {
3445 if (SvIOKp(sv))
3446 return SvIVX(sv) != 0;
3447 else {
3448 if (SvNOKp(sv))
3449 return SvNVX(sv) != 0.0;
3450 else
3451 return FALSE;
3452 }
3453 }
79072805
LW
3454}
3455
d34f9d2e
JH
3456/* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3457 * this function provided for binary compatibility only
3458 */
3459
3460
3461STRLEN
3462Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3463{
3464 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3465}
3466
c461cf8f
JH
3467/*
3468=for apidoc sv_utf8_upgrade
3469
a48bc54f 3470Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3471Forces the SV to string form if it is not already.
4411f3b6
NIS
3472Always sets the SvUTF8 flag to avoid future validity checks even
3473if all the bytes have hibit clear.
c461cf8f 3474
13a6c0e0
JH
3475This is not as a general purpose byte encoding to Unicode interface:
3476use the Encode extension for that.
3477
8d6d96c1
HS
3478=for apidoc sv_utf8_upgrade_flags
3479
a48bc54f 3480Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3481Forces the SV to string form if it is not already.
8d6d96c1
HS
3482Always sets the SvUTF8 flag to avoid future validity checks even
3483if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3484will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3485C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3486
13a6c0e0
JH
3487This is not as a general purpose byte encoding to Unicode interface:
3488use the Encode extension for that.
3489
8d6d96c1
HS
3490=cut
3491*/
3492
3493STRLEN
3494Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3495{
d9835d7b
NC
3496 if (sv == &PL_sv_undef)
3497 return 0;
e0e62c2a
NIS
3498 if (!SvPOK(sv)) {
3499 STRLEN len = 0;
d6e8a192
NC
3500 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3501 (void) sv_2pv_flags(sv,&len, flags);
3502 if (SvUTF8(sv))
3503 return len;
3504 } else {
3505 (void) SvPV_force(sv,len);
3506 }
e0e62c2a 3507 }
4411f3b6 3508
548f1cb8 3509 if (SvUTF8(sv)) {
4411f3b6 3510 return SvCUR(sv);
548f1cb8 3511 }
560a288e 3512
db42d148
NIS
3513 if (SvREADONLY(sv) && SvFAKE(sv)) {
3514 sv_force_normal(sv);
3515 }
3516
6563884d 3517 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
799ef3cb 3518 sv_recode_to_utf8(sv, PL_encoding);
9f4817db 3519 else { /* Assume Latin-1/EBCDIC */
676a626c
NC
3520 /* This function could be much more efficient if we
3521 * had a FLAG in SVs to signal if there are any hibit
3522 * chars in the PV. Given that there isn't such a flag
3523 * make the loop as fast as possible. */
3524 U8 *s = (U8 *) SvPVX(sv);
3525 U8 *e = (U8 *) SvEND(sv);
3526 U8 *t = s;
3527 int hibit = 0;
3528
3529 while (t < e) {
3530 U8 ch = *t++;
3531 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
3532 break;
3533 }
3534 if (hibit) {
3535 STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */
3536 s = bytes_to_utf8((U8*)s, &len);
3537
3538 SvPV_free(sv); /* No longer using what was there before. */
3539
3540 SvPV_set(sv, (char*)s);
3541 SvCUR_set(sv, len - 1);
3542 SvLEN_set(sv, len); /* No longer know the real size. */
3543 }
3544 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3545 SvUTF8_on(sv);
560a288e 3546 }
4411f3b6 3547 return SvCUR(sv);
560a288e
GS
3548}
3549
c461cf8f
JH
3550/*
3551=for apidoc sv_utf8_downgrade
3552
a48bc54f
TS
3553Attempts to convert the PV of an SV from characters to bytes.
3554If the PV contains a character beyond byte, this conversion will fail;
3555in this case, either returns false or, if C<fail_ok> is not
c461cf8f
JH
3556true, croaks.
3557
13a6c0e0
JH
3558This is not as a general purpose Unicode to byte encoding interface:
3559use the Encode extension for that.
3560
c461cf8f
JH
3561=cut
3562*/
3563
560a288e
GS
3564bool
3565Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3566{
a48bc54f 3567 if (SvPOKp(sv) && SvUTF8(sv)) {
fa301091 3568 if (SvCUR(sv)) {
03cfe0ae 3569 U8 *s;
652088fc 3570 STRLEN len;
fa301091 3571
652088fc
JH
3572 if (SvREADONLY(sv) && SvFAKE(sv))
3573 sv_force_normal(sv);
03cfe0ae
NIS
3574 s = (U8 *) SvPV(sv, len);
3575 if (!utf8_to_bytes(s, &len)) {
fa301091
JH
3576 if (fail_ok)
3577 return FALSE;
3578 else {
3579 if (PL_op)
3580 Perl_croak(aTHX_ "Wide character in %s",
53e06cf0 3581 OP_DESC(PL_op));
fa301091
JH
3582 else
3583 Perl_croak(aTHX_ "Wide character");
3584 }
4b3603a4 3585 }
a8dc4fe8 3586 SvCUR_set(sv, len);
67e989fb 3587 }
560a288e 3588 }
ffebcc3e 3589 SvUTF8_off(sv);
560a288e
GS
3590 return TRUE;
3591}
3592
c461cf8f
JH
3593/*
3594=for apidoc sv_utf8_encode
3595
a48bc54f
TS
3596Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3597flag off so that it looks like octets again.
c461cf8f
JH
3598
3599=cut
3600*/
3601
560a288e
GS
3602void
3603Perl_sv_utf8_encode(pTHX_ register SV *sv)
3604{
4411f3b6 3605 (void) sv_utf8_upgrade(sv);
71410450
NC
3606 if (SvIsCOW(sv)) {
3607 sv_force_normal_flags(sv, 0);
3608 }
3609 if (SvREADONLY(sv)) {
3610 Perl_croak(aTHX_ PL_no_modify);
3611 }
560a288e
GS
3612 SvUTF8_off(sv);
3613}
3614
4411f3b6
NIS
3615/*
3616=for apidoc sv_utf8_decode
3617
a48bc54f
TS
3618If the PV of the SV is an octet sequence in UTF-8
3619and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3620so that it looks like a character. If the PV contains only single-byte
3621characters, the C<SvUTF8> flag stays being off.
3622Scans PV for validity and returns false if the PV is invalid UTF-8.
4411f3b6
NIS
3623
3624=cut
3625*/
3626
560a288e
GS
3627bool
3628Perl_sv_utf8_decode(pTHX_ register SV *sv)
3629{
a48bc54f 3630 if (SvPOKp(sv)) {
63cd0674
NIS
3631 U8 *c;
3632 U8 *e;
9cbac4c7 3633
645c22ef
DM
3634 /* The octets may have got themselves encoded - get them back as
3635 * bytes
3636 */
3637 if (!sv_utf8_downgrade(sv, TRUE))
560a288e
GS
3638 return FALSE;
3639
3640 /* it is actually just a matter of turning the utf8 flag on, but
3641 * we want to make sure everything inside is valid utf8 first.
3642 */
63cd0674
NIS
3643 c = (U8 *) SvPVX(sv);
3644 if (!is_utf8_string(c, SvCUR(sv)+1))
67e989fb 3645 return FALSE;
63cd0674 3646 e = (U8 *) SvEND(sv);
511c2ff0 3647 while (c < e) {
c4d5f83a
NIS
3648 U8 ch = *c++;
3649 if (!UTF8_IS_INVARIANT(ch)) {
67e989fb
JH
3650 SvUTF8_on(sv);
3651 break;
3652 }
560a288e 3653 }
560a288e
GS
3654 }
3655 return TRUE;
3656}
3657
d34f9d2e
JH
3658/* sv_setsv() is now a macro using Perl_sv_setsv_flags();
3659 * this function provided for binary compatibility only
3660 */
3661
3662void
3663Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
3664{
3665 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
3666}
3667
954c1994
GS
3668/*
3669=for apidoc sv_setsv
3670
645c22ef
DM
3671Copies the contents of the source SV C<ssv> into the destination SV
3672C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3673function if the source SV needs to be reused. Does not handle 'set' magic.
3674Loosely speaking, it performs a copy-by-value, obliterating any previous
3675content of the destination.
3676
3677You probably want to use one of the assortment of wrappers, such as
3678C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3679C<SvSetMagicSV_nosteal>.
3680
8d6d96c1
HS
3681=for apidoc sv_setsv_flags
3682
645c22ef
DM
3683Copies the contents of the source SV C<ssv> into the destination SV
3684C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3685function if the source SV needs to be reused. Does not handle 'set' magic.
3686Loosely speaking, it performs a copy-by-value, obliterating any previous
3687content of the destination.
3688If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
65daad90
NC
3689C<ssv> if appropriate, else not. If the C<flags> parameter has the
3690C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
3691and C<sv_setsv_nomg> are implemented in terms of this function.
645c22ef
DM
3692
3693You probably want to use one of the assortment of wrappers, such as
3694C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3695C<SvSetMagicSV_nosteal>.
3696
3697This is the primary function for copying scalars, and most other
3698copy-ish functions and macros use this underneath.
8d6d96c1
HS
3699
3700=cut
3701*/
3702
3703void
3704Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
3705{
8990e307
LW
3706 register U32 sflags;
3707 register int dtype;
3708 register int stype;
463ee0b2 3709
79072805
LW
3710 if (sstr == dstr)
3711 return;
2213622d 3712 SV_CHECK_THINKFIRST(dstr);
79072805 3713 if (!sstr)
3280af22 3714 sstr = &PL_sv_undef;
8990e307
LW
3715 stype = SvTYPE(sstr);
3716 dtype = SvTYPE(dstr);
79072805 3717
a0d0e21e 3718 SvAMAGIC_off(dstr);
7d7ce6cc
JH
3719 if ( SvVOK(dstr) )
3720 {
3721 /* need to nuke the magic */
3722 mg_free(dstr);
3723 SvRMAGICAL_off(dstr);
3724 }
9e7bc3e8 3725
463ee0b2 3726 /* There's a lot of redundancy below but we're going for speed here */
79072805 3727
8990e307 3728 switch (stype) {
79072805 3729 case SVt_NULL:
aece5585 3730 undef_sstr:
20408e3c
GS
3731 if (dtype != SVt_PVGV) {
3732 (void)SvOK_off(dstr);
3733 return;
3734 }
3735 break;
463ee0b2 3736 case SVt_IV:
aece5585
GA
3737 if (SvIOK(sstr)) {
3738 switch (dtype) {
3739 case SVt_NULL:
8990e307 3740 sv_upgrade(dstr, SVt_IV);
aece5585
GA
3741 break;
3742 case SVt_NV:
8990e307 3743 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
3744 break;
3745 case SVt_RV:
3746 case SVt_PV:
a0d0e21e 3747 sv_upgrade(dstr, SVt_PVIV);
aece5585
GA
3748 break;
3749 }
3750 (void)SvIOK_only(dstr);
0da6cfda 3751 SvIV_set(dstr, SvIVX(sstr));
25da4f38
IZ
3752 if (SvIsUV(sstr))
3753 SvIsUV_on(dstr);
27c9684d
AP
3754 if (SvTAINTED(sstr))
3755 SvTAINT(dstr);
aece5585 3756 return;
8990e307 3757 }
aece5585
GA
3758 goto undef_sstr;
3759
463ee0b2 3760 case SVt_NV:
aece5585
GA
3761 if (SvNOK(sstr)) {
3762 switch (dtype) {
3763 case SVt_NULL:
3764 case SVt_IV:
8990e307 3765 sv_upgrade(dstr, SVt_NV);
aece5585
GA
3766 break;
3767 case SVt_RV:
3768 case SVt_PV:
3769 case SVt_PVIV:
a0d0e21e 3770 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
3771 break;
3772 }
0da6cfda 3773 SvNV_set(dstr, SvNVX(sstr));
aece5585 3774 (void)SvNOK_only(dstr);
27c9684d
AP
3775 if (SvTAINTED(sstr))
3776 SvTAINT(dstr);
aece5585 3777 return;
8990e307 3778 }
aece5585
GA
3779 goto undef_sstr;
3780
ed6116ce 3781 case SVt_RV:
8990e307 3782 if (dtype < SVt_RV)
ed6116ce 3783 sv_upgrade(dstr, SVt_RV);
c07a80fd 3784 else if (dtype == SVt_PVGV &&
5e8f8cda 3785 SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) {
c07a80fd 3786 sstr = SvRV(sstr);
a5f75d66 3787 if (sstr == dstr) {
1d7c1841
GS
3788 if (GvIMPORTED(dstr) != GVf_IMPORTED
3789 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3790 {
a5f75d66 3791 GvIMPORTED_on(dstr);
1d7c1841 3792 }
a5f75d66
AD
3793 GvMULTI_on(dstr);
3794 return;
3795 }
c07a80fd 3796 goto glob_assign;
3797 }
ed6116ce 3798 break;
463ee0b2 3799 case SVt_PV:
fc36a67e 3800 case SVt_PVFM:
8990e307 3801 if (dtype < SVt_PV)
463ee0b2 3802 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
3803 break;
3804 case SVt_PVIV:
8990e307 3805 if (dtype < SVt_PVIV)
463ee0b2 3806 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
3807 break;
3808 case SVt_PVNV:
8990e307 3809 if (dtype < SVt_PVNV)
463ee0b2 3810 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 3811 break;
4633a7c4
LW
3812 case SVt_PVAV:
3813 case SVt_PVHV:
3814 case SVt_PVCV:
4633a7c4 3815 case SVt_PVIO:
8c18bf38
AL
3816 {
3817 const char * const type = sv_reftype(sstr,0);
533c011a 3818 if (PL_op)
8c18bf38 3819 Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_NAME(PL_op));
4633a7c4 3820 else
8c18bf38
AL
3821 Perl_croak(aTHX_ "Bizarre copy of %s", type);
3822 }
4633a7c4
LW
3823 break;
3824
79072805 3825 case SVt_PVGV:
8990e307 3826 if (dtype <= SVt_PVGV) {
c07a80fd 3827 glob_assign:
a5f75d66 3828 if (dtype != SVt_PVGV) {
8c18bf38
AL
3829 const char * const name = GvNAME(sstr);
3830 const STRLEN len = GvNAMELEN(sstr);
463ee0b2 3831 sv_upgrade(dstr, SVt_PVGV);
14befaf4 3832 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
85aff577 3833 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
a0d0e21e
LW
3834 GvNAME(dstr) = savepvn(name, len);
3835 GvNAMELEN(dstr) = len;
3836 SvFAKE_on(dstr); /* can coerce to non-glob */
3837 }
7bac28a0 3838 /* ahem, death to those who redefine active sort subs */
3280af22
NIS
3839 else if (PL_curstackinfo->si_type == PERLSI_SORT
3840 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
cea2e8a9 3841 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
7bac28a0 3842 GvNAME(dstr));
5bd07a3d 3843
7fb37951
AMS
3844#ifdef GV_UNIQUE_CHECK
3845 if (GvUNIQUE((GV*)dstr)) {
5bd07a3d
DM
3846 Perl_croak(aTHX_ PL_no_modify);
3847 }
3848#endif
3849
a0d0e21e 3850 (void)SvOK_off(dstr);
a5f75d66 3851 GvINTRO_off(dstr); /* one-shot flag */
1edc1566 3852 gp_free((GV*)dstr);
79072805 3853 GvGP(dstr) = gp_ref(GvGP(sstr));
27c9684d
AP
3854 if (SvTAINTED(sstr))
3855 SvTAINT(dstr);
1d7c1841
GS
3856 if (GvIMPORTED(dstr) != GVf_IMPORTED
3857 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3858 {
a5f75d66 3859 GvIMPORTED_on(dstr);
1d7c1841 3860 }
a5f75d66 3861 GvMULTI_on(dstr);
79072805
LW
3862 return;
3863 }
3864 /* FALL THROUGH */
3865
3866 default:
8d6d96c1 3867 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
973f89ab 3868 mg_get(sstr);
eb160463 3869 if ((int)SvTYPE(sstr) != stype) {
973f89ab
CS
3870 stype = SvTYPE(sstr);
3871 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
3872 goto glob_assign;
3873 }
3874 }
ded42b9f 3875 if (stype == SVt_PVLV)
6fc92669 3876 (void)SvUPGRADE(dstr, SVt_PVNV);
ded42b9f 3877 else
eb160463 3878 (void)SvUPGRADE(dstr, (U32)stype);
79072805
LW
3879 }
3880
8990e307
LW
3881 sflags = SvFLAGS(sstr);
3882
3883 if (sflags & SVf_ROK) {
3884 if (dtype >= SVt_PV) {
3885 if (dtype == SVt_PVGV) {
3886 SV *sref = SvREFCNT_inc(SvRV(sstr));
3887 SV *dref = 0;
8c18bf38 3888 const int intro = GvINTRO(dstr);
a0d0e21e 3889
7fb37951
AMS
3890#ifdef GV_UNIQUE_CHECK
3891 if (GvUNIQUE((GV*)dstr)) {
5bd07a3d
DM
3892 Perl_croak(aTHX_ PL_no_modify);
3893 }
3894#endif
3895
a0d0e21e 3896 if (intro) {
a5f75d66 3897 GvINTRO_off(dstr); /* one-shot flag */
1d7c1841 3898 GvLINE(dstr) = CopLINE(PL_curcop);
1edc1566 3899 GvEGV(dstr) = (GV*)dstr;
a0d0e21e 3900 }
a5f75d66 3901 GvMULTI_on(dstr);
8990e307
LW
3902 switch (SvTYPE(sref)) {
3903 case SVt_PVAV:
a0d0e21e 3904 if (intro)
9b1c5f7e 3905 SAVEGENERICSV(GvAV(dstr));
a0d0e21e
LW
3906 else
3907 dref = (SV*)GvAV(dstr);
8990e307 3908 GvAV(dstr) = (AV*)sref;
39bac7f7 3909 if (!GvIMPORTED_AV(dstr)
1d7c1841
GS
3910 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3911 {
a5f75d66 3912 GvIMPORTED_AV_on(dstr);
1d7c1841 3913 }
8990e307
LW
3914 break;
3915 case SVt_PVHV:
a0d0e21e 3916 if (intro)
9b1c5f7e 3917 SAVEGENERICSV(GvHV(dstr));
a0d0e21e
LW
3918 else
3919 dref = (SV*)GvHV(dstr);
8990e307 3920 GvHV(dstr) = (HV*)sref;
39bac7f7 3921 if (!GvIMPORTED_HV(dstr)
1d7c1841
GS
3922 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3923 {
a5f75d66 3924 GvIMPORTED_HV_on(dstr);
1d7c1841 3925 }
8990e307
LW
3926 break;
3927 case SVt_PVCV:
8ebc5c01 3928 if (intro) {
3929 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
3930 SvREFCNT_dec(GvCV(dstr));
3931 GvCV(dstr) = Nullcv;
68dc0745 3932 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3280af22 3933 PL_sub_generation++;
8ebc5c01 3934 }
9b1c5f7e 3935 SAVEGENERICSV(GvCV(dstr));
8ebc5c01 3936 }
68dc0745 3937 else
3938 dref = (SV*)GvCV(dstr);
3939 if (GvCV(dstr) != (CV*)sref) {
748a9306 3940 CV* cv = GvCV(dstr);
4633a7c4 3941 if (cv) {
68dc0745 3942 if (!GvCVGEN((GV*)dstr) &&
3943 (CvROOT(cv) || CvXSUB(cv)))
3944 {
7bac28a0 3945 /* ahem, death to those who redefine
3946 * active sort subs */
3280af22
NIS
3947 if (PL_curstackinfo->si_type == PERLSI_SORT &&
3948 PL_sortcop == CvSTART(cv))
1c846c1f 3949 Perl_croak(aTHX_
7bac28a0 3950 "Can't redefine active sort subroutine %s",
3951 GvENAME((GV*)dstr));
beab0874
JT
3952 /* Redefining a sub - warning is mandatory if
3953 it was a const and its value changed. */
3954 if (ckWARN(WARN_REDEFINE)
3955 || (CvCONST(cv)
3956 && (!CvCONST((CV*)sref)
3957 || sv_cmp(cv_const_sv(cv),
3958 cv_const_sv((CV*)sref)))))
3959 {
9014280d 3960 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
beab0874 3961 CvCONST(cv)
910764e6
RGS
3962 ? "Constant subroutine %s::%s redefined"
3963 : "Subroutine %s::%s redefined",
26ab6a78 3964 HvNAME_get(GvSTASH((GV*)dstr)),
beab0874
JT
3965 GvENAME((GV*)dstr));
3966 }
9607fc9c 3967 }
fb24441d
RGS
3968 if (!intro)
3969 cv_ckproto(cv, (GV*)dstr,
3970 SvPOK(sref) ? SvPVX(sref) : Nullch);
4633a7c4 3971 }
a5f75d66 3972 GvCV(dstr) = (CV*)sref;
7a4c00b4 3973 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
a5f75d66 3974 GvASSUMECV_on(dstr);
3280af22 3975 PL_sub_generation++;
a5f75d66 3976 }
39bac7f7 3977 if (!GvIMPORTED_CV(dstr)
1d7c1841
GS
3978 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3979 {
a5f75d66 3980 GvIMPORTED_CV_on(dstr);
1d7c1841 3981 }
8990e307 3982 break;
91bba347
LW
3983 case SVt_PVIO:
3984 if (intro)
9b1c5f7e 3985 SAVEGENERICSV(GvIOp(dstr));
91bba347
LW
3986 else
3987 dref = (SV*)GvIOp(dstr);
3988 GvIOp(dstr) = (IO*)sref;
3989 break;
f4d13ee9
JH
3990 case SVt_PVFM:
3991 if (intro)
9b1c5f7e 3992 SAVEGENERICSV(GvFORM(dstr));
f4d13ee9
JH
3993 else
3994 dref = (SV*)GvFORM(dstr);
3995 GvFORM(dstr) = (CV*)sref;
3996 break;
8990e307 3997 default:
a0d0e21e 3998 if (intro)
9b1c5f7e 3999 SAVEGENERICSV(GvSV(dstr));
a0d0e21e
LW
4000 else
4001 dref = (SV*)GvSV(dstr);
8990e307 4002 GvSV(dstr) = sref;
39bac7f7 4003 if (!GvIMPORTED_SV(dstr)
1d7c1841
GS
4004 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4005 {
a5f75d66 4006 GvIMPORTED_SV_on(dstr);
1d7c1841 4007 }
8990e307
LW
4008 break;
4009 }
4010 if (dref)
4011 SvREFCNT_dec(dref);
27c9684d
AP
4012 if (SvTAINTED(sstr))
4013 SvTAINT(dstr);
8990e307
LW
4014 return;
4015 }
fdac8c4b 4016 if (SvPVX_const(dstr)) {
676a626c 4017 SvPV_free(dstr);
a8dc4fe8
SP
4018 SvLEN_set(dstr, 0);
4019 SvCUR_set(dstr, 0);
a0d0e21e 4020 }
8990e307 4021 }
a0d0e21e 4022 (void)SvOK_off(dstr);
a8dc4fe8 4023 SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
ed6116ce 4024 SvROK_on(dstr);
8990e307 4025 if (sflags & SVp_NOK) {
3332b3c1
JH
4026 SvNOKp_on(dstr);
4027 /* Only set the public OK flag if the source has public OK. */
4028 if (sflags & SVf_NOK)
4029 SvFLAGS(dstr) |= SVf_NOK;
0da6cfda 4030 SvNV_set(dstr, SvNVX(sstr));
ed6116ce 4031 }
8990e307 4032 if (sflags & SVp_IOK) {
3332b3c1
JH
4033 (void)SvIOKp_on(dstr);
4034 if (sflags & SVf_IOK)
4035 SvFLAGS(dstr) |= SVf_IOK;
2b1c7e3e 4036 if (sflags & SVf_IVisUV)
25da4f38 4037 SvIsUV_on(dstr);
0da6cfda 4038 SvIV_set(dstr, SvIVX(sstr));
ed6116ce 4039 }
a0d0e21e
LW
4040 if (SvAMAGIC(sstr)) {
4041 SvAMAGIC_on(dstr);
4042 }
ed6116ce 4043 }
8990e307 4044 else if (sflags & SVp_POK) {
79072805
LW
4045
4046 /*
4047 * Check to see if we can just swipe the string. If so, it's a
4048 * possible small lose on short strings, but a big win on long ones.
fdac8c4b
SP
4049 * It might even be a win on short strings if SvPVX_const(dstr)
4050 * has to be allocated and SvPVX_const(sstr) has to be freed.
79072805
LW
4051 */
4052
ff68c719 4053 if (SvTEMP(sstr) && /* slated for free anyway? */
01b73108 4054 SvREFCNT(sstr) == 1 && /* and no other references to it? */
65daad90 4055 (!(flags & SV_NOSTEAL)) && /* and we're allowed to steal temps */
1c846c1f 4056 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4c8f17b9 4057 SvLEN(sstr) && /* and really is a string */
645c22ef
DM
4058 /* and won't be needed again, potentially */
4059 !(PL_op && PL_op->op_type == OP_AASSIGN))
a5f75d66 4060 {
fdac8c4b 4061 if (SvPVX_const(dstr)) { /* we know that dtype >= SVt_PV */
a5f75d66
AD
4062 if (SvOOK(dstr)) {
4063 SvFLAGS(dstr) &= ~SVf_OOK;
fdac8c4b 4064 Safefree(SvPVX_const(dstr) - SvIVX(dstr));
a5f75d66 4065 }
50483b2c 4066 else if (SvLEN(dstr))
fdac8c4b 4067 Safefree(SvPVX_const(dstr));
79072805 4068 }
a5f75d66 4069 (void)SvPOK_only(dstr);
463ee0b2 4070 SvPV_set(dstr, SvPVX(sstr));
79072805
LW
4071 SvLEN_set(dstr, SvLEN(sstr));
4072 SvCUR_set(dstr, SvCUR(sstr));
f4e86e0f 4073
79072805 4074 SvTEMP_off(dstr);
645c22ef 4075 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
79072805
LW
4076 SvPV_set(sstr, Nullch);
4077 SvLEN_set(sstr, 0);
a5f75d66
AD
4078 SvCUR_set(sstr, 0);
4079 SvTEMP_off(sstr);
79072805 4080 }
645c22ef 4081 else { /* have to copy actual string */
8990e307 4082 STRLEN len = SvCUR(sstr);
645c22ef 4083 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
fdac8c4b 4084 Move(SvPVX_const(sstr),SvPVX(dstr),len,char);
8990e307
LW
4085 SvCUR_set(dstr, len);
4086 *SvEND(dstr) = '\0';
a0d0e21e 4087 (void)SvPOK_only(dstr);
79072805 4088 }
9aa983d2 4089 if (sflags & SVf_UTF8)
a7cb1f99 4090 SvUTF8_on(dstr);
79072805 4091 /*SUPPRESS 560*/
8990e307 4092 if (sflags & SVp_NOK) {
3332b3c1
JH
4093 SvNOKp_on(dstr);
4094 if (sflags & SVf_NOK)
4095 SvFLAGS(dstr) |= SVf_NOK;
0da6cfda 4096 SvNV_set(dstr, SvNVX(sstr));
79072805 4097 }
8990e307 4098 if (sflags & SVp_IOK) {
3332b3c1
JH
4099 (void)SvIOKp_on(dstr);
4100 if (sflags & SVf_IOK)
4101 SvFLAGS(dstr) |= SVf_IOK;
2b1c7e3e 4102 if (sflags & SVf_IVisUV)
25da4f38 4103 SvIsUV_on(dstr);
0da6cfda 4104 SvIV_set(dstr, SvIVX(sstr));
79072805 4105 }
7d7ce6cc
JH
4106 if ( SvVOK(sstr) ) {
4107 MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring);
4108 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4109 smg->mg_ptr, smg->mg_len);
4110 SvRMAGICAL_on(dstr);
4111 }
79072805 4112 }
8990e307 4113 else if (sflags & SVp_IOK) {
3332b3c1
JH
4114 if (sflags & SVf_IOK)
4115 (void)SvIOK_only(dstr);
4116 else {
9cbac4c7
DM
4117 (void)SvOK_off(dstr);
4118 (void)SvIOKp_on(dstr);
3332b3c1
JH
4119 }
4120 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
2b1c7e3e 4121 if (sflags & SVf_IVisUV)
25da4f38 4122 SvIsUV_on(dstr);
0da6cfda 4123 SvIV_set(dstr, SvIVX(sstr));
3332b3c1
JH
4124 if (sflags & SVp_NOK) {
4125 if (sflags & SVf_NOK)
4126 (void)SvNOK_on(dstr);
4127 else
4128 (void)SvNOKp_on(dstr);
0da6cfda 4129 SvNV_set(dstr, SvNVX(sstr));
3332b3c1
JH
4130 }
4131 }
4132 else if (sflags & SVp_NOK) {
4133 if (sflags & SVf_NOK)
4134 (void)SvNOK_only(dstr);
4135 else {
9cbac4c7 4136 (void)SvOK_off(dstr);
3332b3c1
JH
4137 SvNOKp_on(dstr);
4138 }
0da6cfda 4139 SvNV_set(dstr, SvNVX(sstr));
79072805
LW
4140 }
4141 else {
20408e3c 4142 if (dtype == SVt_PVGV) {
e476b1b5 4143 if (ckWARN(WARN_MISC))
9014280d 4144 Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
20408e3c
GS
4145 }
4146 else
4147 (void)SvOK_off(dstr);
a0d0e21e 4148 }
27c9684d
AP
4149 if (SvTAINTED(sstr))
4150 SvTAINT(dstr);
79072805
LW
4151}
4152
954c1994
GS
4153/*
4154=for apidoc sv_setsv_mg
4155
4156Like C<sv_setsv>, but also handles 'set' magic.
4157
4158=cut
4159*/
4160
79072805 4161void
864dbfa3 4162Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
ef50df4b
GS
4163{
4164 sv_setsv(dstr,sstr);
4165 SvSETMAGIC(dstr);
4166}
4167
954c1994
GS
4168/*
4169=for apidoc sv_setpvn
4170
4171Copies a string into an SV. The C<len> parameter indicates the number of
611e9550
NC
4172bytes to be copied. If the C<ptr> argument is NULL the SV will become
4173undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>.
954c1994
GS
4174
4175=cut
4176*/
4177
ef50df4b 4178void
864dbfa3 4179Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
79072805 4180{
c6f8c383 4181 register char *dptr;
22c522df 4182
2213622d 4183 SV_CHECK_THINKFIRST(sv);
463ee0b2 4184 if (!ptr) {
a0d0e21e 4185 (void)SvOK_off(sv);
463ee0b2
LW
4186 return;
4187 }
22c522df
JH
4188 else {
4189 /* len is STRLEN which is unsigned, need to copy to signed */
8c18bf38 4190 const IV iv = len;
9c5ffd7c
JH
4191 if (iv < 0)
4192 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
22c522df 4193 }
6fc92669 4194 (void)SvUPGRADE(sv, SVt_PV);
c6f8c383 4195
79072805 4196 SvGROW(sv, len + 1);
c6f8c383
GA
4197 dptr = SvPVX(sv);
4198 Move(ptr,dptr,len,char);
4199 dptr[len] = '\0';
79072805 4200 SvCUR_set(sv, len);
1aa99e6b 4201 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4202 SvTAINT(sv);
79072805
LW
4203}
4204
954c1994
GS
4205/*
4206=for apidoc sv_setpvn_mg
4207
4208Like C<sv_setpvn>, but also handles 'set' magic.
4209
4210=cut
4211*/
4212
79072805 4213void
864dbfa3 4214Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
4215{
4216 sv_setpvn(sv,ptr,len);
4217 SvSETMAGIC(sv);
4218}
4219
954c1994
GS
4220/*
4221=for apidoc sv_setpv
4222
4223Copies a string into an SV. The string must be null-terminated. Does not
4224handle 'set' magic. See C<sv_setpv_mg>.
4225
4226=cut
4227*/
4228
ef50df4b 4229void
864dbfa3 4230Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
4231{
4232 register STRLEN len;
4233
2213622d 4234 SV_CHECK_THINKFIRST(sv);
463ee0b2 4235 if (!ptr) {
a0d0e21e 4236 (void)SvOK_off(sv);
463ee0b2
LW
4237 return;
4238 }
79072805 4239 len = strlen(ptr);
6fc92669 4240 (void)SvUPGRADE(sv, SVt_PV);
c6f8c383 4241
79072805 4242 SvGROW(sv, len + 1);
463ee0b2 4243 Move(ptr,SvPVX(sv),len+1,char);
79072805 4244 SvCUR_set(sv, len);
1aa99e6b 4245 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2
LW
4246 SvTAINT(sv);
4247}
4248
954c1994
GS
4249/*
4250=for apidoc sv_setpv_mg
4251
4252Like C<sv_setpv>, but also handles 'set' magic.
4253
4254=cut
4255*/
4256
463ee0b2 4257void
864dbfa3 4258Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b
GS
4259{
4260 sv_setpv(sv,ptr);
4261 SvSETMAGIC(sv);
4262}
4263
954c1994
GS
4264/*
4265=for apidoc sv_usepvn
4266
4267Tells an SV to use C<ptr> to find its string value. Normally the string is
1c846c1f 4268stored inside the SV but sv_usepvn allows the SV to use an outside string.
954c1994
GS
4269The C<ptr> should point to memory that was allocated by C<malloc>. The
4270string length, C<len>, must be supplied. This function will realloc the
4271memory pointed to by C<ptr>, so that pointer should not be freed or used by
4272the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
4273See C<sv_usepvn_mg>.
4274
4275=cut
4276*/
4277
ef50df4b 4278void
864dbfa3 4279Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
463ee0b2 4280{
9eaeeaca 4281 STRLEN allocate;
2213622d 4282 SV_CHECK_THINKFIRST(sv);
c6f8c383 4283 (void)SvUPGRADE(sv, SVt_PV);
463ee0b2 4284 if (!ptr) {
a0d0e21e 4285 (void)SvOK_off(sv);
463ee0b2
LW
4286 return;
4287 }
fdac8c4b 4288 if (SvPVX_const(sv))
676a626c 4289 SvPV_free(sv);
9eaeeaca
NC
4290
4291 allocate = PERL_STRLEN_ROUNDUP(len + 1);
4292 ptr = saferealloc (ptr, allocate);
0da6cfda 4293 SvPV_set(sv, ptr);
463ee0b2 4294 SvCUR_set(sv, len);
9eaeeaca 4295 SvLEN_set(sv, allocate);
463ee0b2 4296 *SvEND(sv) = '\0';
1aa99e6b 4297 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4298 SvTAINT(sv);
79072805
LW
4299}
4300
954c1994
GS
4301/*
4302=for apidoc sv_usepvn_mg
4303
4304Like C<sv_usepvn>, but also handles 'set' magic.
4305
4306=cut
4307*/
4308
ef50df4b 4309void
864dbfa3 4310Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
ef50df4b 4311{
51c1089b 4312 sv_usepvn(sv,ptr,len);
ef50df4b
GS
4313 SvSETMAGIC(sv);
4314}
4315
645c22ef
DM
4316/*
4317=for apidoc sv_force_normal_flags
4318
4319Undo various types of fakery on an SV: if the PV is a shared string, make
4320a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4321an xpvmg. The C<flags> parameter gets passed to C<sv_unref_flags()>
4322when unrefing. C<sv_force_normal> calls this function with flags set to 0.
4323
4324=cut
4325*/
4326
6fc92669 4327void
840a7b70 4328Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
0f15f207 4329{
2213622d 4330 if (SvREADONLY(sv)) {
1c846c1f 4331 if (SvFAKE(sv)) {
fdac8c4b 4332 char *pvx = SvPVX_const(sv);
1c846c1f
NIS
4333 STRLEN len = SvCUR(sv);
4334 U32 hash = SvUVX(sv);
1624793d
NC
4335 SvFAKE_off(sv);
4336 SvREADONLY_off(sv);
1c846c1f 4337 SvGROW(sv, len + 1);
fdac8c4b 4338 Move(pvx,SvPVX_const(sv),len,char);
1c846c1f 4339 *SvEND(sv) = '\0';
25716404 4340 unsharepvn(pvx, SvUTF8(sv) ? -(I32)len : len, hash);
1c846c1f 4341 }
ef7b71f0 4342 else if (IN_PERL_RUNTIME)
cea2e8a9 4343 Perl_croak(aTHX_ PL_no_modify);
0f15f207 4344 }
2213622d 4345 if (SvROK(sv))
840a7b70 4346 sv_unref_flags(sv, flags);
6fc92669
GS
4347 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
4348 sv_unglob(sv);
0f15f207 4349}
1c846c1f 4350
645c22ef
DM
4351/*
4352=for apidoc sv_force_normal
4353
4354Undo various types of fakery on an SV: if the PV is a shared string, make
4355a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4356an xpvmg. See also C<sv_force_normal_flags>.
4357
4358=cut
4359*/
4360
840a7b70
IZ
4361void
4362Perl_sv_force_normal(pTHX_ register SV *sv)
4363{
4364 sv_force_normal_flags(sv, 0);
4365}
4366
954c1994
GS
4367/*
4368=for apidoc sv_chop
4369
1c846c1f 4370Efficient removal of characters from the beginning of the string buffer.
954c1994
GS
4371SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
4372the string buffer. The C<ptr> becomes the first character of the adjusted
645c22ef 4373string. Uses the "OOK hack".
fdac8c4b 4374Beware: after this function returns, C<ptr> and SvPVX_const(sv) may no longer
b9219079 4375refer to the same chunk of data.
954c1994
GS
4376
4377=cut
4378*/
4379
79072805 4380void
645c22ef 4381Perl_sv_chop(pTHX_ register SV *sv, register char *ptr)
79072805
LW
4382{
4383 register STRLEN delta;
a0d0e21e 4384 if (!ptr || !SvPOKp(sv))
79072805 4385 return;
fdac8c4b 4386 delta = ptr - SvPVX_const(sv);
2213622d 4387 SV_CHECK_THINKFIRST(sv);
79072805
LW
4388 if (SvTYPE(sv) < SVt_PVIV)
4389 sv_upgrade(sv,SVt_PVIV);
4390
4391 if (!SvOOK(sv)) {
50483b2c 4392 if (!SvLEN(sv)) { /* make copy of shared string */
fdac8c4b 4393 const char *pvx = SvPVX_const(sv);
50483b2c
JD
4394 STRLEN len = SvCUR(sv);
4395 SvGROW(sv, len + 1);
fdac8c4b 4396 Move(pvx,SvPVX_const(sv),len,char);
50483b2c
JD
4397 *SvEND(sv) = '\0';
4398 }
0da6cfda 4399 SvIV_set(sv, 0);
e33c26d0
JH
4400 /* Same SvOOK_on but SvOOK_on does a SvIOK_off
4401 and we do that anyway inside the SvNIOK_off
4402 */
4403 SvFLAGS(sv) |= SVf_OOK;
79072805 4404 }
e33c26d0 4405 SvNIOK_off(sv);
a8dc4fe8
SP
4406 SvLEN_set(sv, SvLEN(sv) - delta);
4407 SvCUR_set(sv, SvCUR(sv) - delta);
0da6cfda
SP
4408 SvPV_set(sv, SvPVX(sv) + delta);
4409 SvIV_set(sv, SvIVX(sv) + delta);
79072805
LW
4410}
4411
d34f9d2e
JH
4412/* sv_catpvn() is now a macro using Perl_sv_catpvn_flags();
4413 * this function provided for binary compatibility only
4414 */
4415
4416void
4417Perl_sv_catpvn(pTHX_ SV *dsv, const char* sstr, STRLEN slen)
4418{
4419 sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC);
4420}
4421
954c1994
GS
4422/*
4423=for apidoc sv_catpvn
4424
4425Concatenates the string onto the end of the string which is in the SV. The
cd458e05
JH
4426C<len> indicates number of bytes to copy. If the SV has the UTF-8
4427status set, then the bytes appended should be valid UTF-8.
d5ce4a7c 4428Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>.
954c1994 4429
8d6d96c1
HS
4430=for apidoc sv_catpvn_flags
4431
4432Concatenates the string onto the end of the string which is in the SV. The
cd458e05
JH
4433C<len> indicates number of bytes to copy. If the SV has the UTF-8
4434status set, then the bytes appended should be valid UTF-8.
8d6d96c1
HS
4435If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if
4436appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented
4437in terms of this function.
4438
4439=cut
4440*/
4441
4442void
4443Perl_sv_catpvn_flags(pTHX_ register SV *dsv, register const char *sstr, register STRLEN slen, I32 flags)
4444{
4445 STRLEN dlen;
24c2fff4 4446 const char *dstr = SvPV_force_flags(dsv, dlen, flags);
8d6d96c1 4447
8d6d96c1
HS
4448 SvGROW(dsv, dlen + slen + 1);
4449 if (sstr == dstr)
fdac8c4b 4450 sstr = SvPVX_const(dsv);
8d6d96c1 4451 Move(sstr, SvPVX(dsv) + dlen, slen, char);
a8dc4fe8 4452 SvCUR_set(dsv, SvCUR(dsv) + slen);
8d6d96c1
HS
4453 *SvEND(dsv) = '\0';
4454 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
4455 SvTAINT(dsv);
79072805
LW
4456}
4457
954c1994
GS
4458/*
4459=for apidoc sv_catpvn_mg
4460
4461Like C<sv_catpvn>, but also handles 'set' magic.
4462
4463=cut
4464*/
4465
79072805 4466void
864dbfa3 4467Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
4468{
4469 sv_catpvn(sv,ptr,len);
4470 SvSETMAGIC(sv);
4471}
4472
d34f9d2e
JH
4473/* sv_catsv() is now a macro using Perl_sv_catsv_flags();
4474 * this function provided for binary compatibility only
4475 */
4476
4477void
4478Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
4479{
4480 sv_catsv_flags(dstr, sstr, SV_GMAGIC);
4481}
4482
954c1994
GS
4483/*
4484=for apidoc sv_catsv
4485
13e8c8e3
JH
4486Concatenates the string from SV C<ssv> onto the end of the string in
4487SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
4488not 'set' magic. See C<sv_catsv_mg>.
954c1994 4489
8d6d96c1
HS
4490=for apidoc sv_catsv_flags
4491
4492Concatenates the string from SV C<ssv> onto the end of the string in
4493SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC>
4494bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv>
4495and C<sv_catsv_nomg> are implemented in terms of this function.
4496
4497=cut */
4498
ef50df4b 4499void
8d6d96c1 4500Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags)
79072805 4501{
13e8c8e3
JH
4502 char *spv;
4503 STRLEN slen;
46199a12 4504 if (!ssv)
79072805 4505 return;
46199a12 4506 if ((spv = SvPV(ssv, slen))) {
4fd84b44
AD
4507 /* sutf8 and dutf8 were type bool, but under USE_ITHREADS,
4508 gcc version 2.95.2 20000220 (Debian GNU/Linux) for
8cf8f3d1
NIS
4509 Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously
4510 get dutf8 = 0x20000000, (i.e. SVf_UTF8) even though
4fd84b44
AD
4511 dsv->sv_flags doesn't have that bit set.
4512 Andy Dougherty 12 Oct 2001
4513 */
4514 I32 sutf8 = DO_UTF8(ssv);
4515 I32 dutf8;
13e8c8e3 4516
8d6d96c1
HS
4517 if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC))
4518 mg_get(dsv);
4519 dutf8 = DO_UTF8(dsv);
4520
4521 if (dutf8 != sutf8) {
13e8c8e3 4522 if (dutf8) {
46199a12 4523 /* Not modifying source SV, so taking a temporary copy. */
8d6d96c1 4524 SV* csv = sv_2mortal(newSVpvn(spv, slen));
13e8c8e3 4525
46199a12 4526 sv_utf8_upgrade(csv);
8d6d96c1 4527 spv = SvPV(csv, slen);
13e8c8e3 4528 }
8d6d96c1
HS
4529 else
4530 sv_utf8_upgrade_nomg(dsv);
e84ff256 4531 }
8d6d96c1 4532 sv_catpvn_nomg(dsv, spv, slen);
560a288e 4533 }
79072805
LW
4534}
4535
954c1994
GS
4536/*
4537=for apidoc sv_catsv_mg
4538
4539Like C<sv_catsv>, but also handles 'set' magic.
4540
4541=cut
4542*/
4543
79072805 4544void
46199a12 4545Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv)
ef50df4b 4546{
46199a12
JH
4547 sv_catsv(dsv,ssv);
4548 SvSETMAGIC(dsv);
ef50df4b
GS
4549}
4550
954c1994
GS
4551/*
4552=for apidoc sv_catpv
4553
4554Concatenates the string onto the end of the string which is in the SV.
cd458e05
JH
4555If the SV has the UTF-8 status set, then the bytes appended should be
4556valid UTF-8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
954c1994 4557
d5ce4a7c 4558=cut */
954c1994 4559
ef50df4b 4560void
0c981600 4561Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
4562{
4563 register STRLEN len;
463ee0b2 4564 STRLEN tlen;
748a9306 4565 char *junk;
79072805 4566
0c981600 4567 if (!ptr)
79072805 4568 return;
748a9306 4569 junk = SvPV_force(sv, tlen);
0c981600 4570 len = strlen(ptr);
463ee0b2 4571 SvGROW(sv, tlen + len + 1);
0c981600 4572 if (ptr == junk)
fdac8c4b 4573 ptr = SvPVX_const(sv);
0c981600 4574 Move(ptr,SvPVX(sv)+tlen,len+1,char);
a8dc4fe8 4575 SvCUR_set(sv, SvCUR(sv) + len);
d41ff1b8 4576 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4577 SvTAINT(sv);
79072805
LW
4578}
4579
954c1994
GS
4580/*
4581=for apidoc sv_catpv_mg
4582
4583Like C<sv_catpv>, but also handles 'set' magic.
4584
4585=cut
4586*/
4587
ef50df4b 4588void
0c981600 4589Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b 4590{
0c981600 4591 sv_catpv(sv,ptr);
ef50df4b
GS
4592 SvSETMAGIC(sv);
4593}
4594
645c22ef
DM
4595/*
4596=for apidoc newSV
4597
4598Create a new null SV, or if len > 0, create a new empty SVt_PV type SV
4599with an initial PV allocation of len+1. Normally accessed via the C<NEWSV>
4600macro.
4601
4602=cut
4603*/
4604
79072805 4605SV *
864dbfa3 4606Perl_newSV(pTHX_ STRLEN len)
79072805
LW
4607{
4608 register SV *sv;
1c846c1f 4609
4561caa4 4610 new_SV(sv);
79072805
LW
4611 if (len) {
4612 sv_upgrade(sv, SVt_PV);
4613 SvGROW(sv, len + 1);
4614 }
4615 return sv;
4616}
954c1994 4617/*
92110913 4618=for apidoc sv_magicext
954c1994 4619
68795e93 4620Adds magic to an SV, upgrading it if necessary. Applies the
af70ddd4 4621supplied vtable and returns a pointer to the magic added.
92110913 4622
af70ddd4
NC
4623Note that C<sv_magicext> will allow things that C<sv_magic> will not.
4624In particular, you can add magic to SvREADONLY SVs, and add more than
4625one instance of the same 'how'.
645c22ef 4626
af70ddd4
NC
4627If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
4628stored, if C<namlen> is zero then C<name> is stored as-is and - as another
4629special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
4630to contain an C<SV*> and is stored as-is with its REFCNT incremented.
92110913 4631
af70ddd4 4632(This is now used as a subroutine by C<sv_magic>.)
954c1994
GS
4633
4634=cut
4635*/
92110913
NIS
4636MAGIC *
4637Perl_sv_magicext(pTHX_ SV* sv, SV* obj, int how, MGVTBL *vtable,
4638 const char* name, I32 namlen)
79072805
LW
4639{
4640 MAGIC* mg;
68795e93 4641
92110913
NIS
4642 if (SvTYPE(sv) < SVt_PVMG) {
4643 (void)SvUPGRADE(sv, SVt_PVMG);
463ee0b2 4644 }
79072805
LW
4645 Newz(702,mg, 1, MAGIC);
4646 mg->mg_moremagic = SvMAGIC(sv);
a8dc4fe8 4647 SvMAGIC_set(sv, mg);
75f9d97a 4648
0df18620
NC
4649 /* Sometimes a magic contains a reference loop, where the sv and
4650 object refer to each other. To prevent a reference loop that
4651 would prevent such objects being freed, we look for such loops
4652 and if we find one we avoid incrementing the object refcount.
87f0b213
JH
4653
4654 Note we cannot do this to avoid self-tie loops as intervening RV must
8ccffa6a 4655 have its REFCNT incremented to keep it in existence.
87f0b213
JH
4656
4657 */
14befaf4
DM
4658 if (!obj || obj == sv ||
4659 how == PERL_MAGIC_arylen ||
4660 how == PERL_MAGIC_qr ||
75f9d97a
JH
4661 (SvTYPE(obj) == SVt_PVGV &&
4662 (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv ||
4663 GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv ||
2628be26 4664 GvFORM(obj) == (CV*)sv)))
75f9d97a 4665 {
8990e307 4666 mg->mg_obj = obj;
75f9d97a 4667 }
85e6fe83 4668 else {
8990e307 4669 mg->mg_obj = SvREFCNT_inc(obj);
85e6fe83
LW
4670 mg->mg_flags |= MGf_REFCOUNTED;
4671 }
8ccffa6a
YST
4672
4673 /* Normal self-ties simply pass a null object, and instead of
4674 using mg_obj directly, use the SvTIED_obj macro to produce a
4675 new RV as needed. For glob "self-ties", we are tieing the PVIO
4676 with an RV obj pointing to the glob containing the PVIO. In
4677 this case, to avoid a reference loop, we need to weaken the
4678 reference.
4679 */
4680
4681 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
4682 obj && SvROK(obj) && GvIO(SvRV(obj)) == (IO*)sv)
4683 {
4684 sv_rvweaken(obj);
4685 }
4686
79072805 4687 mg->mg_type = how;
565764a8 4688 mg->mg_len = namlen;
9cbac4c7 4689 if (name) {
92110913 4690 if (namlen > 0)
1edc1566 4691 mg->mg_ptr = savepvn(name, namlen);
c6ee37c5 4692 else if (namlen == HEf_SVKEY)
1edc1566 4693 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
68795e93 4694 else
92110913 4695 mg->mg_ptr = (char *) name;
9cbac4c7 4696 }
92110913 4697 mg->mg_virtual = vtable;
68795e93 4698
92110913
NIS
4699 mg_magical(sv);
4700 if (SvGMAGICAL(sv))
4701 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
4702 return mg;
4703}
4704
4705/*
4706=for apidoc sv_magic
1c846c1f 4707
92110913
NIS
4708Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary,
4709then adds a new magic item of type C<how> to the head of the magic list.
4710
af70ddd4
NC
4711See C<sv_magicext> (which C<sv_magic> now calls) for a description of the
4712handling of the C<name> and C<namlen> arguments.
4713
0df18620
NC
4714You need to use C<sv_magicext> to add magic to SvREADONLY SVs and also
4715to add more than one instance of the same 'how'.
4716
92110913
NIS
4717=cut
4718*/
4719
4720void
4721Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
68795e93 4722{
c05e0e2f 4723 const MGVTBL *vtable = 0;
92110913 4724 MAGIC* mg;
92110913
NIS
4725
4726 if (SvREADONLY(sv)) {
ef7b71f0 4727 if (IN_PERL_RUNTIME
92110913
NIS
4728 && how != PERL_MAGIC_regex_global
4729 && how != PERL_MAGIC_bm
4730 && how != PERL_MAGIC_fm
4731 && how != PERL_MAGIC_sv
6125ae81 4732 && how != PERL_MAGIC_backref
92110913
NIS
4733 )
4734 {
4735 Perl_croak(aTHX_ PL_no_modify);
4736 }
4737 }
4738 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
4739 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
68795e93
NIS
4740 /* sv_magic() refuses to add a magic of the same 'how' as an
4741 existing one
92110913
NIS
4742 */
4743 if (how == PERL_MAGIC_taint)
4744 mg->mg_len |= 1;
4745 return;
4746 }
4747 }
68795e93 4748
79072805 4749 switch (how) {
14befaf4 4750 case PERL_MAGIC_sv:
92110913 4751 vtable = &PL_vtbl_sv;
79072805 4752 break;
14befaf4 4753 case PERL_MAGIC_overload:
92110913 4754 vtable = &PL_vtbl_amagic;
a0d0e21e 4755 break;
14befaf4 4756 case PERL_MAGIC_overload_elem:
92110913 4757 vtable = &PL_vtbl_amagicelem;
a0d0e21e 4758 break;
14befaf4 4759 case PERL_MAGIC_overload_table:
92110913 4760 vtable = &PL_vtbl_ovrld;
a0d0e21e 4761 break;
14befaf4 4762 case PERL_MAGIC_bm:
92110913 4763 vtable = &PL_vtbl_bm;
79072805 4764 break;
14befaf4 4765 case PERL_MAGIC_regdata:
92110913 4766 vtable = &PL_vtbl_regdata;
6cef1e77 4767 break;
14befaf4 4768 case PERL_MAGIC_regdatum:
92110913 4769 vtable = &PL_vtbl_regdatum;
6cef1e77 4770 break;
14befaf4 4771 case PERL_MAGIC_env:
92110913 4772 vtable = &PL_vtbl_env;
79072805 4773 break;
14befaf4 4774 case PERL_MAGIC_fm:
92110913 4775 vtable = &PL_vtbl_fm;
55497cff 4776 break;
14befaf4 4777 case PERL_MAGIC_envelem:
92110913 4778 vtable = &PL_vtbl_envelem;
79072805 4779 break;
14befaf4 4780 case PERL_MAGIC_regex_global:
92110913 4781 vtable = &PL_vtbl_mglob;
93a17b20 4782 break;
14befaf4 4783 case PERL_MAGIC_isa:
92110913 4784 vtable = &PL_vtbl_isa;
463ee0b2 4785 break;
14befaf4 4786 case PERL_MAGIC_isaelem:
92110913 4787 vtable = &PL_vtbl_isaelem;
463ee0b2 4788 break;
14befaf4 4789 case PERL_MAGIC_nkeys:
92110913 4790 vtable = &PL_vtbl_nkeys;
16660edb 4791 break;
14befaf4 4792 case PERL_MAGIC_dbfile:
92110913 4793 vtable = 0;
93a17b20 4794 break;
14befaf4 4795 case PERL_MAGIC_dbline:
92110913 4796 vtable = &PL_vtbl_dbline;
79072805 4797 break;
4d1ff10f 4798#ifdef USE_5005THREADS
14befaf4 4799 case PERL_MAGIC_mutex:
92110913 4800 vtable = &PL_vtbl_mutex;
f93b4edd 4801 break;
4d1ff10f 4802#endif /* USE_5005THREADS */
36477c24 4803#ifdef USE_LOCALE_COLLATE
14befaf4 4804 case PERL_MAGIC_collxfrm:
92110913 4805 vtable = &PL_vtbl_collxfrm;
bbce6d69 4806 break;
36477c24 4807#endif /* USE_LOCALE_COLLATE */
14befaf4 4808 case PERL_MAGIC_tied:
92110913 4809 vtable = &PL_vtbl_pack;
463ee0b2 4810 break;
14befaf4
DM
4811 case PERL_MAGIC_tiedelem:
4812 case PERL_MAGIC_tiedscalar:
92110913 4813 vtable = &PL_vtbl_packelem;
463ee0b2 4814 break;
14befaf4 4815 case PERL_MAGIC_qr:
92110913 4816 vtable = &PL_vtbl_regexp;
c277df42 4817 break;
14befaf4 4818 case PERL_MAGIC_sig:
92110913 4819 vtable = &PL_vtbl_sig;
79072805 4820 break;
14befaf4 4821 case PERL_MAGIC_sigelem:
92110913 4822 vtable = &PL_vtbl_sigelem;
79072805 4823 break;
14befaf4 4824 case PERL_MAGIC_taint:
92110913 4825 vtable = &PL_vtbl_taint;
463ee0b2 4826 break;
14befaf4 4827 case PERL_MAGIC_uvar:
92110913 4828 vtable = &PL_vtbl_uvar;
79072805 4829 break;
14befaf4 4830 case PERL_MAGIC_vec:
92110913 4831 vtable = &PL_vtbl_vec;
79072805 4832 break;
7d7ce6cc
JH
4833 case PERL_MAGIC_vstring:
4834 vtable = 0;
4835 break;
323eb6b5
JH
4836 case PERL_MAGIC_utf8:
4837 vtable = &PL_vtbl_utf8;
4838 break;
14befaf4 4839 case PERL_MAGIC_substr:
92110913 4840 vtable = &PL_vtbl_substr;
79072805 4841 break;
14befaf4 4842 case PERL_MAGIC_defelem:
92110913 4843 vtable = &PL_vtbl_defelem;
5f05dabc 4844 break;
14befaf4 4845 case PERL_MAGIC_glob:
92110913 4846 vtable = &PL_vtbl_glob;
79072805 4847 break;
14befaf4 4848 case PERL_MAGIC_arylen:
92110913 4849 vtable = &PL_vtbl_arylen;
79072805 4850 break;
14befaf4 4851 case PERL_MAGIC_pos:
92110913 4852 vtable = &PL_vtbl_pos;
a0d0e21e 4853 break;
14befaf4 4854 case PERL_MAGIC_backref:
92110913 4855 vtable = &PL_vtbl_backref;
810b8aa5 4856 break;
14befaf4
DM
4857 case PERL_MAGIC_ext:
4858 /* Reserved for use by extensions not perl internals. */
4633a7c4
LW
4859 /* Useful for attaching extension internal data to perl vars. */
4860 /* Note that multiple extensions may clash if magical scalars */
4861 /* etc holding private data from one are passed to another. */
a0d0e21e 4862 break;
79072805 4863 default:
14befaf4 4864 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
463ee0b2 4865 }
68795e93 4866
92110913
NIS
4867 /* Rest of work is done else where */
4868 mg = sv_magicext(sv,obj,how,vtable,name,namlen);
68795e93 4869
92110913
NIS
4870 switch (how) {
4871 case PERL_MAGIC_taint:
4872 mg->mg_len = 1;
4873 break;
4874 case PERL_MAGIC_ext:
4875 case PERL_MAGIC_dbfile:
4876 SvRMAGICAL_on(sv);
4877 break;
4878 }
463ee0b2
LW
4879}
4880
c461cf8f
JH
4881/*
4882=for apidoc sv_unmagic
4883
645c22ef 4884Removes all magic of type C<type> from an SV.
c461cf8f
JH
4885
4886=cut
4887*/
4888
463ee0b2 4889int
864dbfa3 4890Perl_sv_unmagic(pTHX_ SV *sv, int type)
463ee0b2
LW
4891{
4892 MAGIC* mg;
4893 MAGIC** mgp;
91bba347 4894 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
463ee0b2
LW
4895 return 0;
4896 mgp = &SvMAGIC(sv);
4897 for (mg = *mgp; mg; mg = *mgp) {
4898 if (mg->mg_type == type) {
c05e0e2f 4899 const MGVTBL* const vtbl = mg->mg_virtual;
463ee0b2 4900 *mgp = mg->mg_moremagic;
1d7c1841 4901 if (vtbl && vtbl->svt_free)
fc0dc3b3 4902 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
14befaf4 4903 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
92110913 4904 if (mg->mg_len > 0)
1edc1566 4905 Safefree(mg->mg_ptr);
565764a8 4906 else if (mg->mg_len == HEf_SVKEY)
1edc1566 4907 SvREFCNT_dec((SV*)mg->mg_ptr);
323eb6b5
JH
4908 else if (mg->mg_type == PERL_MAGIC_utf8 && mg->mg_ptr)
4909 Safefree(mg->mg_ptr);
9cbac4c7 4910 }
a0d0e21e
LW
4911 if (mg->mg_flags & MGf_REFCOUNTED)
4912 SvREFCNT_dec(mg->mg_obj);
463ee0b2
LW
4913 Safefree(mg);
4914 }
4915 else
4916 mgp = &mg->mg_moremagic;
79072805 4917 }
91bba347 4918 if (!SvMAGIC(sv)) {
463ee0b2 4919 SvMAGICAL_off(sv);
06759ea0 4920 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
463ee0b2
LW
4921 }
4922
4923 return 0;
79072805
LW
4924}
4925
c461cf8f
JH
4926/*
4927=for apidoc sv_rvweaken
4928
645c22ef
DM
4929Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
4930referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
4931push a back-reference to this RV onto the array of backreferences
4932associated with that magic.
c461cf8f
JH
4933
4934=cut
4935*/
4936
810b8aa5 4937SV *
864dbfa3 4938Perl_sv_rvweaken(pTHX_ SV *sv)
810b8aa5
GS
4939{
4940 SV *tsv;
4941 if (!SvOK(sv)) /* let undefs pass */
4942 return sv;
4943 if (!SvROK(sv))
cea2e8a9 4944 Perl_croak(aTHX_ "Can't weaken a nonreference");
810b8aa5 4945 else if (SvWEAKREF(sv)) {
810b8aa5 4946 if (ckWARN(WARN_MISC))
9014280d 4947 Perl_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
810b8aa5
GS
4948 return sv;
4949 }
4950 tsv = SvRV(sv);
4951 sv_add_backref(tsv, sv);
4952 SvWEAKREF_on(sv);
1c846c1f 4953 SvREFCNT_dec(tsv);
810b8aa5
GS
4954 return sv;
4955}
4956
645c22ef
DM
4957/* Give tsv backref magic if it hasn't already got it, then push a
4958 * back-reference to sv onto the array associated with the backref magic.
4959 */
4960
810b8aa5 4961STATIC void
cea2e8a9 4962S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
810b8aa5
GS
4963{
4964 AV *av;
4965 MAGIC *mg;
14befaf4 4966 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, PERL_MAGIC_backref)))
810b8aa5
GS
4967 av = (AV*)mg->mg_obj;
4968 else {
4969 av = newAV();
14befaf4 4970 sv_magic(tsv, (SV*)av, PERL_MAGIC_backref, NULL, 0);
219ef941
JH
4971 /* av now has a refcnt of 2, which avoids it getting freed
4972 * before us during global cleanup. The extra ref is removed
4973 * by magic_killbackrefs() when tsv is being freed */
810b8aa5 4974 }
f8cf5370 4975 if (AvFILLp(av) >= AvMAX(av)) {
f8cf5370
JH
4976 av_extend(av, AvFILLp(av)+1);
4977 }
4978 AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
810b8aa5
GS
4979}
4980
645c22ef
DM
4981/* delete a back-reference to ourselves from the backref magic associated
4982 * with the SV we point to.
4983 */
4984
1c846c1f 4985STATIC void
cea2e8a9 4986S_sv_del_backref(pTHX_ SV *sv)
810b8aa5
GS
4987{
4988 AV *av;
4989 SV **svp;
4990 I32 i;
4991 SV *tsv = SvRV(sv);
c04a4dfe 4992 MAGIC *mg = NULL;
14befaf4 4993 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref)))
cea2e8a9 4994 Perl_croak(aTHX_ "panic: del_backref");
810b8aa5
GS
4995 av = (AV *)mg->mg_obj;
4996 svp = AvARRAY(av);
0fa9aa63
NC
4997 /* We shouldn't be in here more than once, but for paranoia reasons lets
4998 not assume this. */
4999 for (i = AvFILLp(av); i >= 0; i--) {
5000 if (svp[i] == sv) {
5001 const SSize_t fill = AvFILLp(av);
5002 if (i != fill) {
5003 /* We weren't the last entry.
5004 An unordered list has this property that you can take the
5005 last element off the end to fill the hole, and it's still
5006 an unordered list :-)
5007 */
5008 svp[i] = svp[fill];
5009 }
5010 svp[fill] = Nullsv;
5011 AvFILLp(av) = fill - 1;
5012 }
5013 }
810b8aa5
GS
5014}
5015
954c1994
GS
5016/*
5017=for apidoc sv_insert
5018
5019Inserts a string at the specified offset/length within the SV. Similar to
5020the Perl substr() function.
5021
5022=cut
5023*/
5024
79072805 5025void
864dbfa3 5026Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen)
79072805
LW
5027{
5028 register char *big;
5029 register char *mid;
5030 register char *midend;
5031 register char *bigend;
5032 register I32 i;
6ff81951 5033 STRLEN curlen;
1c846c1f 5034
79072805 5035
8990e307 5036 if (!bigstr)
cea2e8a9 5037 Perl_croak(aTHX_ "Can't modify non-existent substring");
6ff81951 5038 SvPV_force(bigstr, curlen);
60fa28ff 5039 (void)SvPOK_only_UTF8(bigstr);
6ff81951
GS
5040 if (offset + len > curlen) {
5041 SvGROW(bigstr, offset+len+1);
fdac8c4b 5042 Zero(SvPVX_const(bigstr)+curlen, offset+len-curlen, char);
6ff81951
GS
5043 SvCUR_set(bigstr, offset+len);
5044 }
79072805 5045
69b47968 5046 SvTAINT(bigstr);
79072805
LW
5047 i = littlelen - len;
5048 if (i > 0) { /* string might grow */
a0d0e21e 5049 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
79072805
LW
5050 mid = big + offset + len;
5051 midend = bigend = big + SvCUR(bigstr);
5052 bigend += i;
5053 *bigend = '\0';
5054 while (midend > mid) /* shove everything down */
5055 *--bigend = *--midend;
5056 Move(little,big+offset,littlelen,char);
a8dc4fe8 5057 SvCUR_set(bigstr, SvCUR(bigstr) + i);
79072805
LW
5058 SvSETMAGIC(bigstr);
5059 return;
5060 }
5061 else if (i == 0) {
463ee0b2 5062 Move(little,SvPVX(bigstr)+offset,len,char);
79072805
LW
5063 SvSETMAGIC(bigstr);
5064 return;
5065 }
5066
463ee0b2 5067 big = SvPVX(bigstr);
79072805
LW
5068 mid = big + offset;
5069 midend = mid + len;
5070 bigend = big + SvCUR(bigstr);
5071
5072 if (midend > bigend)
cea2e8a9 5073 Perl_croak(aTHX_ "panic: sv_insert");
79072805
LW
5074
5075 if (mid - big > bigend - midend) { /* faster to shorten from end */
5076 if (littlelen) {
5077 Move(little, mid, littlelen,char);
5078 mid += littlelen;
5079 }
5080 i = bigend - midend;
5081 if (i > 0) {
5082 Move(midend, mid, i,char);
5083 mid += i;
5084 }
5085 *mid = '\0';
5086 SvCUR_set(bigstr, mid - big);
5087 }
5088 /*SUPPRESS 560*/
155aba94 5089 else if ((i = mid - big)) { /* faster from front */
79072805
LW
5090 midend -= littlelen;
5091 mid = midend;
5092 sv_chop(bigstr,midend-i);
5093 big += i;
5094 while (i--)
5095 *--midend = *--big;
5096 if (littlelen)
5097 Move(little, mid, littlelen,char);
5098 }
5099 else if (littlelen) {
5100 midend -= littlelen;
5101 sv_chop(bigstr,midend);
5102 Move(little,midend,littlelen,char);
5103 }
5104 else {
5105 sv_chop(bigstr,midend);
5106 }
5107 SvSETMAGIC(bigstr);
5108}
5109
c461cf8f
JH
5110/*
5111=for apidoc sv_replace
5112
5113Make the first argument a copy of the second, then delete the original.
645c22ef
DM
5114The target SV physically takes over ownership of the body of the source SV
5115and inherits its flags; however, the target keeps any magic it owns,
5116and any magic in the source is discarded.
ff276b08 5117Note that this is a rather specialist SV copying operation; most of the
645c22ef 5118time you'll want to use C<sv_setsv> or one of its many macro front-ends.
c461cf8f
JH
5119
5120=cut
5121*/
79072805
LW
5122
5123void
864dbfa3 5124Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
79072805 5125{
8c18bf38 5126 const U32 refcnt = SvREFCNT(sv);
2213622d 5127 SV_CHECK_THINKFIRST(sv);
0453d815 5128 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
9014280d 5129 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Reference miscount in sv_replace()");
93a17b20 5130 if (SvMAGICAL(sv)) {
a0d0e21e
LW
5131 if (SvMAGICAL(nsv))
5132 mg_free(nsv);
5133 else
5134 sv_upgrade(nsv, SVt_PVMG);
a8dc4fe8 5135 SvMAGIC_set(nsv, SvMAGIC(sv));
a0d0e21e 5136 SvFLAGS(nsv) |= SvMAGICAL(sv);
93a17b20 5137 SvMAGICAL_off(sv);
a8dc4fe8 5138 SvMAGIC_set(sv, NULL);
93a17b20 5139 }
79072805
LW
5140 SvREFCNT(sv) = 0;
5141 sv_clear(sv);
477f5d66 5142 assert(!SvREFCNT(sv));
79072805
LW
5143 StructCopy(nsv,sv,SV);
5144 SvREFCNT(sv) = refcnt;
1edc1566 5145 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
007262b0 5146 SvREFCNT(nsv) = 0;
463ee0b2 5147 del_SV(nsv);
79072805
LW
5148}
5149
c461cf8f
JH
5150/*
5151=for apidoc sv_clear
5152
645c22ef
DM
5153Clear an SV: call any destructors, free up any memory used by the body,
5154and free the body itself. The SV's head is I<not> freed, although
5155its type is set to all 1's so that it won't inadvertently be assumed
5156to be live during global destruction etc.
5157This function should only be called when REFCNT is zero. Most of the time
5158you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
5159instead.
c461cf8f
JH
5160
5161=cut
5162*/
5163
79072805 5164void
864dbfa3 5165Perl_sv_clear(pTHX_ register SV *sv)
79072805 5166{
ec12f114 5167 HV* stash;
79072805
LW
5168 assert(sv);
5169 assert(SvREFCNT(sv) == 0);
5170
ed6116ce 5171 if (SvOBJECT(sv)) {
3280af22 5172 if (PL_defstash) { /* Still have a symbol table? */
39644a26 5173 dSP;
32251b26 5174 CV* destructor;
a0d0e21e 5175
3e5ba712 5176
8ebc5c01 5177
d460ef45 5178 do {
4e8e7886 5179 stash = SvSTASH(sv);
32251b26 5180 destructor = StashHANDLER(stash,DESTROY);
4e8e7886 5181 if (destructor) {
3e5ba712
JH
5182 SV* tmpref = newRV(sv);
5183 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
4e8e7886 5184 ENTER;
e788e7d3 5185 PUSHSTACKi(PERLSI_DESTROY);
4e8e7886
GS
5186 EXTEND(SP, 2);
5187 PUSHMARK(SP);
3e5ba712 5188 PUSHs(tmpref);
4e8e7886 5189 PUTBACK;
44389ee9 5190 call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
3e5ba712
JH
5191
5192
d3acc0f7 5193 POPSTACK;
3095d977 5194 SPAGAIN;
4e8e7886 5195 LEAVE;
3e5ba712
JH
5196 if(SvREFCNT(tmpref) < 2) {
5197 /* tmpref is not kept alive! */
5198 SvREFCNT(sv)--;
a8dc4fe8 5199 SvRV_set(tmpref, NULL);
3e5ba712
JH
5200 SvROK_off(tmpref);
5201 }
5202 SvREFCNT_dec(tmpref);
4e8e7886
GS
5203 }
5204 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
8ebc5c01 5205
6f44e0a4
JP
5206
5207 if (SvREFCNT(sv)) {
5208 if (PL_in_clean_objs)
cea2e8a9 5209 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
26ab6a78 5210 HvNAME_get(stash));
6f44e0a4
JP
5211 /* DESTROY gave object new lease on life */
5212 return;
5213 }
a0d0e21e 5214 }
4e8e7886 5215
a0d0e21e 5216 if (SvOBJECT(sv)) {
4e8e7886 5217 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
a0d0e21e
LW
5218 SvOBJECT_off(sv); /* Curse the object. */
5219 if (SvTYPE(sv) != SVt_PVIO)
3280af22 5220 --PL_sv_objcount; /* XXX Might want something more general */
a0d0e21e 5221 }
463ee0b2 5222 }
524189f1
JH
5223 if (SvTYPE(sv) >= SVt_PVMG) {
5224 if (SvMAGIC(sv))
5225 mg_free(sv);
2098fb77 5226 if (SvTYPE(sv) == SVt_PVMG && SvFLAGS(sv) & SVpad_TYPED)
524189f1
JH
5227 SvREFCNT_dec(SvSTASH(sv));
5228 }
ec12f114 5229 stash = NULL;
79072805 5230 switch (SvTYPE(sv)) {
8990e307 5231 case SVt_PVIO:
df0bd2f4
GS
5232 if (IoIFP(sv) &&
5233 IoIFP(sv) != PerlIO_stdin() &&
5f05dabc 5234 IoIFP(sv) != PerlIO_stdout() &&
5235 IoIFP(sv) != PerlIO_stderr())
93578b34 5236 {
f2b5be74 5237 io_close((IO*)sv, FALSE);
93578b34 5238 }
1d7c1841 5239 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
1236053a 5240 PerlDir_close(IoDIRP(sv));
1d7c1841 5241 IoDIRP(sv) = (DIR*)NULL;
8990e307
LW
5242 Safefree(IoTOP_NAME(sv));
5243 Safefree(IoFMT_NAME(sv));
5244 Safefree(IoBOTTOM_NAME(sv));
a0d0e21e 5245 /* FALL THROUGH */
79072805 5246 case SVt_PVBM:
a0d0e21e 5247 goto freescalar;
79072805 5248 case SVt_PVCV:
748a9306 5249 case SVt_PVFM:
85e6fe83 5250 cv_undef((CV*)sv);
a0d0e21e 5251 goto freescalar;
79072805 5252 case SVt_PVHV:
85e6fe83 5253 hv_undef((HV*)sv);
a0d0e21e 5254 break;
79072805 5255 case SVt_PVAV:
85e6fe83 5256 av_undef((AV*)sv);
a0d0e21e 5257 break;
02270b4e 5258 case SVt_PVLV:
73c86719
JH
5259 if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
5260 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
5261 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
5262 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
5263 }
5264 else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */
5265 SvREFCNT_dec(LvTARG(sv));
02270b4e 5266 goto freescalar;
a0d0e21e 5267 case SVt_PVGV:
1edc1566 5268 gp_free((GV*)sv);
a0d0e21e 5269 Safefree(GvNAME(sv));
ec12f114
JPC
5270 /* cannot decrease stash refcount yet, as we might recursively delete
5271 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
5272 of stash until current sv is completely gone.
5273 -- JohnPC, 27 Mar 1998 */
5274 stash = GvSTASH(sv);
a0d0e21e 5275 /* FALL THROUGH */
79072805 5276 case SVt_PVMG:
79072805
LW
5277 case SVt_PVNV:
5278 case SVt_PVIV:
a0d0e21e 5279 freescalar:
2a8de9e2
AL
5280 /* Don't bother with SvOOK_off(sv); as we're only going to free it. */
5281 if (SvOOK(sv)) {
5282 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
5283 /* Don't even bother with turning off the OOK flag. */
5284 }
79072805
LW
5285 /* FALL THROUGH */
5286 case SVt_PV:
a0d0e21e 5287 case SVt_RV:
810b8aa5
GS
5288 if (SvROK(sv)) {
5289 if (SvWEAKREF(sv))
5290 sv_del_backref(sv);
5291 else
5292 SvREFCNT_dec(SvRV(sv));
5293 }
fdac8c4b
SP
5294 else if (SvPVX_const(sv) && SvLEN(sv))
5295 Safefree(SvPVX_const(sv));
5296 else if (SvPVX_const(sv) && SvREADONLY(sv) && SvFAKE(sv)) {
5297 unsharepvn(SvPVX_const(sv),
25716404
GS
5298 SvUTF8(sv) ? -(I32)SvCUR(sv) : SvCUR(sv),
5299 SvUVX(sv));
1c846c1f
NIS
5300 SvFAKE_off(sv);
5301 }
79072805 5302 break;
a0d0e21e 5303/*
79072805 5304 case SVt_NV:
79072805 5305 case SVt_IV:
79072805
LW
5306 case SVt_NULL:
5307 break;
a0d0e21e 5308*/
79072805
LW
5309 }
5310
5311 switch (SvTYPE(sv)) {
5312 case SVt_NULL:
5313 break;
79072805
LW
5314 case SVt_IV:
5315 del_XIV(SvANY(sv));
5316 break;
5317 case SVt_NV:
5318 del_XNV(SvANY(sv));
5319 break;
ed6116ce
LW
5320 case SVt_RV:
5321 del_XRV(SvANY(sv));
5322 break;
79072805
LW
5323 case SVt_PV:
5324 del_XPV(SvANY(sv));
5325 break;
5326 case SVt_PVIV:
5327 del_XPVIV(SvANY(sv));
5328 break;
5329 case SVt_PVNV:
5330 del_XPVNV(SvANY(sv));
5331 break;
5332 case SVt_PVMG:
5333 del_XPVMG(SvANY(sv));
5334 break;
5335 case SVt_PVLV:
5336 del_XPVLV(SvANY(sv));
5337 break;
5338 case SVt_PVAV:
5339 del_XPVAV(SvANY(sv));
5340 break;
5341 case SVt_PVHV:
5342 del_XPVHV(SvANY(sv));
5343 break;
5344 case SVt_PVCV:
5345 del_XPVCV(SvANY(sv));
5346 break;
5347 case SVt_PVGV:
5348 del_XPVGV(SvANY(sv));
ec12f114
JPC
5349 /* code duplication for increased performance. */
5350 SvFLAGS(sv) &= SVf_BREAK;
5351 SvFLAGS(sv) |= SVTYPEMASK;
5352 /* decrease refcount of the stash that owns this GV, if any */
5353 if (stash)
5354 SvREFCNT_dec(stash);
5355 return; /* not break, SvFLAGS reset already happened */
79072805
LW
5356 case SVt_PVBM:
5357 del_XPVBM(SvANY(sv));
5358 break;
5359 case SVt_PVFM:
5360 del_XPVFM(SvANY(sv));
5361 break;
8990e307
LW
5362 case SVt_PVIO:
5363 del_XPVIO(SvANY(sv));
5364 break;
79072805 5365 }
a0d0e21e 5366 SvFLAGS(sv) &= SVf_BREAK;
8990e307 5367 SvFLAGS(sv) |= SVTYPEMASK;
79072805
LW
5368}
5369
645c22ef
DM
5370/*
5371=for apidoc sv_newref
5372
5373Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
5374instead.
5375
5376=cut
5377*/
5378
79072805 5379SV *
864dbfa3 5380Perl_sv_newref(pTHX_ SV *sv)
79072805 5381{
463ee0b2 5382 if (sv)
dce16143 5383 ATOMIC_INC(SvREFCNT(sv));
79072805
LW
5384 return sv;
5385}
5386
c461cf8f
JH
5387/*
5388=for apidoc sv_free
5389
645c22ef
DM
5390Decrement an SV's reference count, and if it drops to zero, call
5391C<sv_clear> to invoke destructors and free up any memory used by
5392the body; finally, deallocate the SV's head itself.
5393Normally called via a wrapper macro C<SvREFCNT_dec>.
c461cf8f
JH
5394
5395=cut
5396*/
5397
79072805 5398void
864dbfa3 5399Perl_sv_free(pTHX_ SV *sv)
79072805 5400{
dce16143
MB
5401 int refcount_is_zero;
5402
79072805
LW
5403 if (!sv)
5404 return;
a0d0e21e
LW
5405 if (SvREFCNT(sv) == 0) {
5406 if (SvFLAGS(sv) & SVf_BREAK)
645c22ef
DM
5407 /* this SV's refcnt has been artificially decremented to
5408 * trigger cleanup */
a0d0e21e 5409 return;
3280af22 5410 if (PL_in_clean_all) /* All is fair */
1edc1566 5411 return;
d689ffdd
JP
5412 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
5413 /* make sure SvREFCNT(sv)==0 happens very seldom */
5414 SvREFCNT(sv) = (~(U32)0)/2;
5415 return;
5416 }
0453d815 5417 if (ckWARN_d(WARN_INTERNAL))
14347439 5418 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
b035a42e
NC
5419 "Attempt to free unreferenced scalar: SV 0x%"UVxf
5420 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
79072805
LW
5421 return;
5422 }
dce16143 5423 ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv));
b881518d 5424 if (!refcount_is_zero)
8990e307 5425 return;
463ee0b2
LW
5426#ifdef DEBUGGING
5427 if (SvTEMP(sv)) {
0453d815 5428 if (ckWARN_d(WARN_DEBUGGING))
9014280d 5429 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
b035a42e
NC
5430 "Attempt to free temp prematurely: SV 0x%"UVxf
5431 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
79072805 5432 return;
79072805 5433 }
463ee0b2 5434#endif
d689ffdd
JP
5435 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
5436 /* make sure SvREFCNT(sv)==0 happens very seldom */
5437 SvREFCNT(sv) = (~(U32)0)/2;
5438 return;
5439 }
79072805 5440 sv_clear(sv);
477f5d66
CS
5441 if (! SvREFCNT(sv))
5442 del_SV(sv);
79072805
LW
5443}
5444
954c1994
GS
5445/*
5446=for apidoc sv_len
5447
645c22ef
DM
5448Returns the length of the string in the SV. Handles magic and type
5449coercion. See also C<SvCUR>, which gives raw access to the xpv_cur slot.
954c1994
GS
5450
5451=cut
5452*/
5453
79072805 5454STRLEN
864dbfa3 5455Perl_sv_len(pTHX_ register SV *sv)
79072805 5456{
463ee0b2 5457 STRLEN len;
79072805
LW
5458
5459 if (!sv)
5460 return 0;
5461
8990e307 5462 if (SvGMAGICAL(sv))
565764a8 5463 len = mg_length(sv);
8990e307 5464 else
497b47a8 5465 (void)SvPV(sv, len);
463ee0b2 5466 return len;
79072805
LW
5467}
5468
c461cf8f
JH
5469/*
5470=for apidoc sv_len_utf8
5471
5472Returns the number of characters in the string in an SV, counting wide
cd458e05 5473UTF-8 bytes as a single character. Handles magic and type coercion.
c461cf8f
JH
5474
5475=cut
5476*/
5477
323eb6b5
JH
5478/*
5479 * The length is cached in PERL_UTF8_magic, in the mg_len field. Also the
5480 * mg_ptr is used, by sv_pos_u2b(), see the comments of S_utf8_mg_pos_init().
5481 * (Note that the mg_len is not the length of the mg_ptr field.)
5482 *
5483 */
5484
a0ed51b3 5485STRLEN
864dbfa3 5486Perl_sv_len_utf8(pTHX_ register SV *sv)
a0ed51b3 5487{
a0ed51b3
LW
5488 if (!sv)
5489 return 0;
5490
a0ed51b3 5491 if (SvGMAGICAL(sv))
b76347f2 5492 return mg_length(sv);
a0ed51b3 5493 else
b76347f2 5494 {
323eb6b5 5495 STRLEN len, ulen;
8c18bf38 5496 const U8 *s = (U8*)SvPV(sv, len);
323eb6b5
JH
5497 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : 0;
5498
d6218095 5499 if (mg && mg->mg_len != -1 && (mg->mg_len > 0 || len == 0)) {
323eb6b5 5500 ulen = mg->mg_len;
d6218095
JH
5501#ifdef PERL_UTF8_CACHE_ASSERT
5502 assert(ulen == Perl_utf8_length(aTHX_ s, s + len));
5503#endif
5504 }
323eb6b5
JH
5505 else {
5506 ulen = Perl_utf8_length(aTHX_ s, s + len);
5507 if (!mg && !SvREADONLY(sv)) {
5508 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
5509 mg = mg_find(sv, PERL_MAGIC_utf8);
5510 assert(mg);
5511 }
5512 if (mg)
5513 mg->mg_len = ulen;
5514 }
5515 return ulen;
5516 }
5517}
5518
5519/* S_utf8_mg_pos_init() is used to initialize the mg_ptr field of
5520 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
5521 * between UTF-8 and byte offsets. There are two (substr offset and substr
5522 * length, the i offset, PERL_MAGIC_UTF8_CACHESIZE) times two (UTF-8 offset
5523 * and byte offset) cache positions.
5524 *
5525 * The mg_len field is used by sv_len_utf8(), see its comments.
5526 * Note that the mg_len is not the length of the mg_ptr field.
5527 *
5528 */
5529STATIC bool
8c18bf38 5530S_utf8_mg_pos_init(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 offsetp, U8 *s, U8 *start)
323eb6b5
JH
5531{
5532 bool found = FALSE;
5533
5534 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
1fce4797
NC
5535 if (!*mgp)
5536 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, &PL_vtbl_utf8, 0, 0);
5537 assert(*mgp);
b76347f2 5538
323eb6b5
JH
5539 if ((*mgp)->mg_ptr)
5540 *cachep = (STRLEN *) (*mgp)->mg_ptr;
5541 else {
5542 Newz(0, *cachep, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
5543 (*mgp)->mg_ptr = (char *) *cachep;
5544 }
5545 assert(*cachep);
5546
8c18bf38
AL
5547 (*cachep)[i] = offsetp;
5548 (*cachep)[i+1] = s - start;
5549 found = TRUE;
323eb6b5
JH
5550 }
5551
5552 return found;
5553}
5554
5555/*
5556 * S_utf8_mg_pos() is used to query and update mg_ptr field of
5557 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
5558 * between UTF-8 and byte offsets. See also the comments of
5559 * S_utf8_mg_pos_init().
5560 *
5561 */
5562STATIC bool
91eb8809 5563S_utf8_mg_pos(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, I32 uoff, U8 **sp, U8 *start, U8 *send)
323eb6b5
JH
5564{
5565 bool found = FALSE;
5566
5567 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
5568 if (!*mgp)
5569 *mgp = mg_find(sv, PERL_MAGIC_utf8);
5570 if (*mgp && (*mgp)->mg_ptr) {
5571 *cachep = (STRLEN *) (*mgp)->mg_ptr;
d6218095 5572 ASSERT_UTF8_CACHE(*cachep);
91eb8809 5573 if ((*cachep)[i] == (STRLEN)uoff) /* An exact match. */
323eb6b5
JH
5574 found = TRUE;
5575 else { /* We will skip to the right spot. */
5576 STRLEN forw = 0;
5577 STRLEN backw = 0;
8c18bf38 5578 const U8* p = NULL;
323eb6b5
JH
5579
5580 /* The assumption is that going backward is half
5581 * the speed of going forward (that's where the
5582 * 2 * backw in the below comes from). (The real
5583 * figure of course depends on the UTF-8 data.) */
5584
91eb8809 5585 if ((*cachep)[i] > (STRLEN)uoff) {
323eb6b5 5586 forw = uoff;
91eb8809 5587 backw = (*cachep)[i] - (STRLEN)uoff;
323eb6b5
JH
5588
5589 if (forw < 2 * backw)
5590 p = start;
5591 else
5592 p = start + (*cachep)[i+1];
5593 }
5594 /* Try this only for the substr offset (i == 0),
5595 * not for the substr length (i == 2). */
5596 else if (i == 0) { /* (*cachep)[i] < uoff */
8c18bf38 5597 const STRLEN ulen = sv_len_utf8(sv);
323eb6b5 5598
91eb8809
JH
5599 if ((STRLEN)uoff < ulen) {
5600 forw = (STRLEN)uoff - (*cachep)[i];
5601 backw = ulen - (STRLEN)uoff;
323eb6b5
JH
5602
5603 if (forw < 2 * backw)
5604 p = start + (*cachep)[i+1];
5605 else
5606 p = send;
5607 }
5608
5609 /* If the string is not long enough for uoff,
5610 * we could extend it, but not at this low a level. */
5611 }
5612
5613 if (p) {
5614 if (forw < 2 * backw) {
5615 while (forw--)
5616 p += UTF8SKIP(p);
5617 }
5618 else {
5619 while (backw--) {
5620 p--;
5621 while (UTF8_IS_CONTINUATION(*p))
5622 p--;
5623 }
5624 }
5625
5626 /* Update the cache. */
91eb8809 5627 (*cachep)[i] = (STRLEN)uoff;
323eb6b5
JH
5628 (*cachep)[i+1] = p - start;
5629
1fce4797
NC
5630 /* Drop the stale "length" cache */
5631 if (i == 0) {
5632 (*cachep)[2] = 0;
5633 (*cachep)[3] = 0;
5634 }
5635
323eb6b5
JH
5636 found = TRUE;
5637 }
5638 }
5639 if (found) { /* Setup the return values. */
5640 *offsetp = (*cachep)[i+1];
5641 *sp = start + *offsetp;
5642 if (*sp >= send) {
5643 *sp = send;
5644 *offsetp = send - start;
5645 }
5646 else if (*sp < start) {
5647 *sp = start;
5648 *offsetp = 0;
5649 }
5650 }
d6218095
JH
5651 }
5652#ifdef PERL_UTF8_CACHE_ASSERT
5653 if (found) {
5654 U8 *s = start;
5655 I32 n = uoff;
5656
5657 while (n-- && s < send)
5658 s += UTF8SKIP(s);
5659
5660 if (i == 0) {
5661 assert(*offsetp == s - start);
5662 assert((*cachep)[0] == (STRLEN)uoff);
5663 assert((*cachep)[1] == *offsetp);
5664 }
5665 ASSERT_UTF8_CACHE(*cachep);
5666 }
5667#endif
a0ed51b3 5668 }
323eb6b5
JH
5669
5670 return found;
a0ed51b3
LW
5671}
5672
645c22ef
DM
5673/*
5674=for apidoc sv_pos_u2b
5675
cd458e05 5676Converts the value pointed to by offsetp from a count of UTF-8 chars from
645c22ef
DM
5677the start of the string, to a count of the equivalent number of bytes; if
5678lenp is non-zero, it does the same to lenp, but this time starting from
5679the offset, rather than from the start of the string. Handles magic and
5680type coercion.
5681
5682=cut
5683*/
5684
323eb6b5
JH
5685/*
5686 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
5687 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
5688 * byte offsets. See also the comments of S_utf8_mg_pos().
5689 *
5690 */
5691
a0ed51b3 5692void
864dbfa3 5693Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
a0ed51b3 5694{
dfe13c55
GS
5695 U8 *start;
5696 U8 *s;
a0ed51b3 5697 STRLEN len;
323eb6b5
JH
5698 STRLEN *cache = 0;
5699 STRLEN boffset = 0;
a0ed51b3
LW
5700
5701 if (!sv)
5702 return;
5703
dfe13c55 5704 start = s = (U8*)SvPV(sv, len);
323eb6b5
JH
5705 if (len) {
5706 I32 uoffset = *offsetp;
5707 U8 *send = s + len;
5708 MAGIC *mg = 0;
5709 bool found = FALSE;
5710
5eed253f 5711 if (utf8_mg_pos(sv, &mg, &cache, 0, offsetp, *offsetp, &s, start, send))
323eb6b5
JH
5712 found = TRUE;
5713 if (!found && uoffset > 0) {
5714 while (s < send && uoffset--)
5715 s += UTF8SKIP(s);
5716 if (s >= send)
5717 s = send;
8c18bf38 5718 if (utf8_mg_pos_init(sv, &mg, &cache, 0, *offsetp, s, start))
323eb6b5
JH
5719 boffset = cache[1];
5720 *offsetp = s - start;
5721 }
5722 if (lenp) {
5723 found = FALSE;
5724 start = s;
9648d1c1 5725 if (utf8_mg_pos(sv, &mg, &cache, 2, lenp, *lenp, &s, start, send)) {
323eb6b5
JH
5726 *lenp -= boffset;
5727 found = TRUE;
5728 }
5729 if (!found && *lenp > 0) {
5730 I32 ulen = *lenp;
5731 if (ulen > 0)
5732 while (s < send && ulen--)
5733 s += UTF8SKIP(s);
5734 if (s >= send)
5735 s = send;
8c18bf38 5736 utf8_mg_pos_init(sv, &mg, &cache, 2, *lenp, s, start);
323eb6b5
JH
5737 }
5738 *lenp = s - start;
5739 }
d6218095 5740 ASSERT_UTF8_CACHE(cache);
323eb6b5
JH
5741 }
5742 else {
5743 *offsetp = 0;
5744 if (lenp)
5745 *lenp = 0;
a0ed51b3 5746 }
d6218095 5747
a0ed51b3
LW
5748 return;
5749}
5750
645c22ef
DM
5751/*
5752=for apidoc sv_pos_b2u
5753
5754Converts the value pointed to by offsetp from a count of bytes from the
cd458e05 5755start of the string, to a count of the equivalent number of UTF-8 chars.
645c22ef
DM
5756Handles magic and type coercion.
5757
5758=cut
5759*/
5760
323eb6b5
JH
5761/*
5762 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
5763 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
5764 * byte offsets. See also the comments of S_utf8_mg_pos().
5765 *
5766 */
5767
a0ed51b3 5768void
323eb6b5 5769Perl_sv_pos_b2u(pTHX_ register SV* sv, I32* offsetp)
a0ed51b3 5770{
323eb6b5 5771 U8* s;
a0ed51b3
LW
5772 STRLEN len;
5773
5774 if (!sv)
5775 return;
5776
dfe13c55 5777 s = (U8*)SvPV(sv, len);
eb160463 5778 if ((I32)len < *offsetp)
a0dbb045 5779 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset");
323eb6b5 5780 else {
4a9f3d2e
NC
5781 U8* send = s + *offsetp;
5782 MAGIC* mg = NULL;
5783 STRLEN *cache = NULL;
323eb6b5 5784
4a9f3d2e 5785 len = 0;
323eb6b5 5786
4a9f3d2e
NC
5787 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
5788 mg = mg_find(sv, PERL_MAGIC_utf8);
5789 if (mg && mg->mg_ptr) {
5790 cache = (STRLEN *) mg->mg_ptr;
5791 if (cache[1] == (STRLEN)*offsetp) {
5792 /* An exact match. */
5793 *offsetp = cache[0];
323eb6b5 5794
4a9f3d2e
NC
5795 return;
5796 }
5797 else if (cache[1] < (STRLEN)*offsetp) {
5798 /* We already know part of the way. */
5799 len = cache[0];
5800 s += cache[1];
5801 /* Let the below loop do the rest. */
5802 }
5803 else { /* cache[1] > *offsetp */
5804 /* We already know all of the way, now we may
5805 * be able to walk back. The same assumption
5806 * is made as in S_utf8_mg_pos(), namely that
5807 * walking backward is twice slower than
5808 * walking forward. */
5809 STRLEN forw = *offsetp;
5810 STRLEN backw = cache[1] - *offsetp;
5811
5812 if (!(forw < 2 * backw)) {
5813 U8 *p = s + cache[1];
5814 STRLEN ubackw = 0;
323eb6b5 5815
4a9f3d2e 5816 cache[1] -= backw;
338501c1 5817
4a9f3d2e 5818 while (backw--) {
b9219079
JH
5819 p--;
5820 while (UTF8_IS_CONTINUATION(*p)) {
5821 p--;
5822 backw--;
5823 }
5824 ubackw++;
323eb6b5 5825 }
b9219079
JH
5826
5827 cache[0] -= ubackw;
5828 *offsetp = cache[0];
b035a42e
NC
5829
5830 /* Drop the stale "length" cache */
5831 cache[2] = 0;
5832 cache[3] = 0;
5833
b9219079
JH
5834 return;
5835 }
5836 }
5837 }
d6218095 5838 ASSERT_UTF8_CACHE(cache);
323eb6b5
JH
5839 }
5840
4a9f3d2e
NC
5841 while (s < send) {
5842 STRLEN n = 1;
323eb6b5 5843
4a9f3d2e
NC
5844 /* Call utf8n_to_uvchr() to validate the sequence
5845 * (unless a simple non-UTF character) */
5846 if (!UTF8_IS_INVARIANT(*s))
5847 utf8n_to_uvchr(s, UTF8SKIP(s), &n, 0);
5848 if (n > 0) {
5849 s += n;
5850 len++;
5851 }
5852 else
5853 break;
5854 }
323eb6b5 5855
4a9f3d2e
NC
5856 if (!SvREADONLY(sv)) {
5857 if (!mg) {
5858 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
5859 mg = mg_find(sv, PERL_MAGIC_utf8);
5860 }
5861 assert(mg);
323eb6b5 5862
4a9f3d2e
NC
5863 if (!mg->mg_ptr) {
5864 Newz(0, cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
5865 mg->mg_ptr = (char *) cache;
5866 }
5867 assert(cache);
323eb6b5 5868
4a9f3d2e
NC
5869 cache[0] = len;
5870 cache[1] = *offsetp;
b035a42e
NC
5871 /* Drop the stale "length" cache */
5872 cache[2] = 0;
5873 cache[3] = 0;
4a9f3d2e 5874 }
323eb6b5 5875
4a9f3d2e 5876 *offsetp = len;
a0ed51b3 5877 }
323eb6b5 5878
a0ed51b3
LW
5879 return;
5880}
5881
954c1994
GS
5882/*
5883=for apidoc sv_eq
5884
5885Returns a boolean indicating whether the strings in the two SVs are
645c22ef
DM
5886identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will
5887coerce its args to strings if necessary.
954c1994
GS
5888
5889=cut
5890*/
5891
79072805 5892I32
e01b9e88 5893Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2)
79072805 5894{
c05e0e2f 5895 const char *pv1;
463ee0b2 5896 STRLEN cur1;
c05e0e2f 5897 const char *pv2;
463ee0b2 5898 STRLEN cur2;
e01b9e88 5899 I32 eq = 0;
553e1bcc
AT
5900 char *tpv = Nullch;
5901 SV* svrecode = Nullsv;
79072805 5902
e01b9e88 5903 if (!sv1) {
79072805
LW
5904 pv1 = "";
5905 cur1 = 0;
5906 }
463ee0b2 5907 else
e01b9e88 5908 pv1 = SvPV(sv1, cur1);
79072805 5909
e01b9e88
SC
5910 if (!sv2){
5911 pv2 = "";
5912 cur2 = 0;
92d29cee 5913 }
e01b9e88
SC
5914 else
5915 pv2 = SvPV(sv2, cur2);
79072805 5916
cf48d248 5917 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
799ef3cb
JH
5918 /* Differing utf8ness.
5919 * Do not UTF8size the comparands as a side-effect. */
5920 if (PL_encoding) {
5921 if (SvUTF8(sv1)) {
553e1bcc
AT
5922 svrecode = newSVpvn(pv2, cur2);
5923 sv_recode_to_utf8(svrecode, PL_encoding);
5924 pv2 = SvPV(svrecode, cur2);
799ef3cb
JH
5925 }
5926 else {
553e1bcc
AT
5927 svrecode = newSVpvn(pv1, cur1);
5928 sv_recode_to_utf8(svrecode, PL_encoding);
5929 pv1 = SvPV(svrecode, cur1);
799ef3cb
JH
5930 }
5931 /* Now both are in UTF-8. */
60e3e0e4
NC
5932 if (cur1 != cur2) {
5933 SvREFCNT_dec(svrecode);
799ef3cb 5934 return FALSE;
60e3e0e4 5935 }
799ef3cb
JH
5936 }
5937 else {
5938 bool is_utf8 = TRUE;
5939
5940 if (SvUTF8(sv1)) {
5941 /* sv1 is the UTF-8 one,
5942 * if is equal it must be downgrade-able */
c05e0e2f 5943 char *pv = (char*)bytes_from_utf8((const U8*)pv1,
799ef3cb
JH
5944 &cur1, &is_utf8);
5945 if (pv != pv1)
553e1bcc 5946 pv1 = tpv = pv;
799ef3cb
JH
5947 }
5948 else {
5949 /* sv2 is the UTF-8 one,
5950 * if is equal it must be downgrade-able */
c05e0e2f 5951 char *pv = (char *)bytes_from_utf8((const U8*)pv2,
799ef3cb
JH
5952 &cur2, &is_utf8);
5953 if (pv != pv2)
553e1bcc 5954 pv2 = tpv = pv;
799ef3cb
JH
5955 }
5956 if (is_utf8) {
5957 /* Downgrade not possible - cannot be eq */
5958 return FALSE;
5959 }
5960 }
cf48d248
JH
5961 }
5962
5963 if (cur1 == cur2)
5964 eq = memEQ(pv1, pv2, cur1);
e01b9e88 5965
553e1bcc
AT
5966 if (svrecode)
5967 SvREFCNT_dec(svrecode);
799ef3cb 5968
553e1bcc
AT
5969 if (tpv)
5970 Safefree(tpv);
cf48d248 5971
e01b9e88 5972 return eq;
79072805
LW
5973}
5974
954c1994
GS
5975/*
5976=for apidoc sv_cmp
5977
5978Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
5979string in C<sv1> is less than, equal to, or greater than the string in
645c22ef
DM
5980C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will
5981coerce its args to strings if necessary. See also C<sv_cmp_locale>.
954c1994
GS
5982
5983=cut
5984*/
5985
79072805 5986I32
e01b9e88 5987Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2)
79072805 5988{
560a288e 5989 STRLEN cur1, cur2;
c05e0e2f
AL
5990 const char *pv1, *pv2;
5991 char *tpv = Nullch;
cf48d248 5992 I32 cmp;
553e1bcc 5993 SV *svrecode = Nullsv;
560a288e 5994
e01b9e88
SC
5995 if (!sv1) {
5996 pv1 = "";
560a288e
GS
5997 cur1 = 0;
5998 }
e01b9e88
SC
5999 else
6000 pv1 = SvPV(sv1, cur1);
560a288e 6001
553e1bcc 6002 if (!sv2) {
e01b9e88 6003 pv2 = "";
560a288e
GS
6004 cur2 = 0;
6005 }
e01b9e88
SC
6006 else
6007 pv2 = SvPV(sv2, cur2);
79072805 6008
cf48d248 6009 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
799ef3cb
JH
6010 /* Differing utf8ness.
6011 * Do not UTF8size the comparands as a side-effect. */
cf48d248 6012 if (SvUTF8(sv1)) {
799ef3cb 6013 if (PL_encoding) {
553e1bcc
AT
6014 svrecode = newSVpvn(pv2, cur2);
6015 sv_recode_to_utf8(svrecode, PL_encoding);
6016 pv2 = SvPV(svrecode, cur2);
799ef3cb
JH
6017 }
6018 else {
c05e0e2f 6019 pv2 = tpv = (char*)bytes_to_utf8((const U8*)pv2, &cur2);
799ef3cb 6020 }
cf48d248
JH
6021 }
6022 else {
799ef3cb 6023 if (PL_encoding) {
553e1bcc
AT
6024 svrecode = newSVpvn(pv1, cur1);
6025 sv_recode_to_utf8(svrecode, PL_encoding);
6026 pv1 = SvPV(svrecode, cur1);
799ef3cb
JH
6027 }
6028 else {
c05e0e2f 6029 pv1 = tpv = (char*)bytes_to_utf8((const U8*)pv1, &cur1);
799ef3cb 6030 }
cf48d248
JH
6031 }
6032 }
6033
e01b9e88 6034 if (!cur1) {
cf48d248 6035 cmp = cur2 ? -1 : 0;
e01b9e88 6036 } else if (!cur2) {
cf48d248
JH
6037 cmp = 1;
6038 } else {
c05e0e2f 6039 const I32 retval = memcmp((const void*)pv1, (const void*)pv2, cur1 < cur2 ? cur1 : cur2);
e01b9e88
SC
6040
6041 if (retval) {
cf48d248 6042 cmp = retval < 0 ? -1 : 1;
e01b9e88 6043 } else if (cur1 == cur2) {
cf48d248
JH
6044 cmp = 0;
6045 } else {
6046 cmp = cur1 < cur2 ? -1 : 1;
e01b9e88 6047 }
cf48d248 6048 }
16660edb 6049
553e1bcc
AT
6050 if (svrecode)
6051 SvREFCNT_dec(svrecode);
799ef3cb 6052
553e1bcc
AT
6053 if (tpv)
6054 Safefree(tpv);
cf48d248
JH
6055
6056 return cmp;
bbce6d69 6057}
16660edb 6058
c461cf8f
JH
6059/*
6060=for apidoc sv_cmp_locale
6061
645c22ef
DM
6062Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
6063'use bytes' aware, handles get magic, and will coerce its args to strings
6064if necessary. See also C<sv_cmp_locale>. See also C<sv_cmp>.
c461cf8f
JH
6065
6066=cut
6067*/
6068
bbce6d69 6069I32
864dbfa3 6070Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
bbce6d69 6071{
36477c24 6072#ifdef USE_LOCALE_COLLATE
16660edb 6073
bbce6d69 6074 char *pv1, *pv2;
6075 STRLEN len1, len2;
6076 I32 retval;
16660edb 6077
3280af22 6078 if (PL_collation_standard)
bbce6d69 6079 goto raw_compare;
16660edb 6080
bbce6d69 6081 len1 = 0;
8ac85365 6082 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
bbce6d69 6083 len2 = 0;
8ac85365 6084 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
16660edb 6085
bbce6d69 6086 if (!pv1 || !len1) {
6087 if (pv2 && len2)
6088 return -1;
6089 else
6090 goto raw_compare;
6091 }
6092 else {
6093 if (!pv2 || !len2)
6094 return 1;
6095 }
16660edb 6096
bbce6d69 6097 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
16660edb 6098
bbce6d69 6099 if (retval)
16660edb 6100 return retval < 0 ? -1 : 1;
6101
bbce6d69 6102 /*
6103 * When the result of collation is equality, that doesn't mean
6104 * that there are no differences -- some locales exclude some
6105 * characters from consideration. So to avoid false equalities,
6106 * we use the raw string as a tiebreaker.
6107 */
16660edb 6108
bbce6d69 6109 raw_compare:
6110 /* FALL THROUGH */
16660edb 6111
36477c24 6112#endif /* USE_LOCALE_COLLATE */
16660edb 6113
bbce6d69 6114 return sv_cmp(sv1, sv2);
6115}
79072805 6116
645c22ef 6117
36477c24 6118#ifdef USE_LOCALE_COLLATE
645c22ef 6119
7a4c00b4 6120/*
645c22ef
DM
6121=for apidoc sv_collxfrm
6122
6123Add Collate Transform magic to an SV if it doesn't already have it.
6124
6125Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the
6126scalar data of the variable, but transformed to such a format that a normal
6127memory comparison can be used to compare the data according to the locale
6128settings.
6129
6130=cut
6131*/
6132
bbce6d69 6133char *
864dbfa3 6134Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp)
bbce6d69 6135{
7a4c00b4 6136 MAGIC *mg;
16660edb 6137
14befaf4 6138 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
3280af22 6139 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
bbce6d69 6140 char *s, *xf;
6141 STRLEN len, xlen;
6142
7a4c00b4 6143 if (mg)
6144 Safefree(mg->mg_ptr);
bbce6d69 6145 s = SvPV(sv, len);
6146 if ((xf = mem_collxfrm(s, len, &xlen))) {
ff0cee69 6147 if (SvREADONLY(sv)) {
6148 SAVEFREEPV(xf);
6149 *nxp = xlen;
3280af22 6150 return xf + sizeof(PL_collation_ix);
ff0cee69 6151 }
7a4c00b4 6152 if (! mg) {
14befaf4
DM
6153 sv_magic(sv, 0, PERL_MAGIC_collxfrm, 0, 0);
6154 mg = mg_find(sv, PERL_MAGIC_collxfrm);
7a4c00b4 6155 assert(mg);
bbce6d69 6156 }
7a4c00b4 6157 mg->mg_ptr = xf;
565764a8 6158 mg->mg_len = xlen;
7a4c00b4 6159 }
6160 else {
ff0cee69 6161 if (mg) {
6162 mg->mg_ptr = NULL;
565764a8 6163 mg->mg_len = -1;
ff0cee69 6164 }
bbce6d69 6165 }
6166 }
7a4c00b4 6167 if (mg && mg->mg_ptr) {
565764a8 6168 *nxp = mg->mg_len;
3280af22 6169 return mg->mg_ptr + sizeof(PL_collation_ix);
bbce6d69 6170 }
6171 else {
6172 *nxp = 0;
6173 return NULL;
16660edb 6174 }
79072805
LW
6175}
6176
36477c24 6177#endif /* USE_LOCALE_COLLATE */
bbce6d69 6178
c461cf8f
JH
6179/*
6180=for apidoc sv_gets
6181
6182Get a line from the filehandle and store it into the SV, optionally
6183appending to the currently-stored string.
6184
6185=cut
6186*/
6187
79072805 6188char *
864dbfa3 6189Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append)
79072805 6190{
c05e0e2f 6191 const char *rsptr;
c07a80fd 6192 STRLEN rslen;
6193 register STDCHAR rslast;
6194 register STDCHAR *bp;
6195 register I32 cnt;
9c5ffd7c 6196 I32 i = 0;
8bfdd7d9 6197 I32 rspara = 0;
fe1d0b35 6198 I32 recsize;
c07a80fd 6199
1624793d
NC
6200 if (SvTHINKFIRST(sv))
6201 sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
6202 /* XXX. If you make this PVIV, then copy on write can copy scalars read
6203 from <>.
6204 However, perlbench says it's slower, because the existing swipe code
6205 is faster than copy on write.
6206 Swings and roundabouts. */
6fc92669 6207 (void)SvUPGRADE(sv, SVt_PV);
99491443 6208
ff68c719 6209 SvSCREAM_off(sv);
251c53ad
AE
6210
6211 if (append) {
6212 if (PerlIO_isutf8(fp)) {
6213 if (!SvUTF8(sv)) {
6214 sv_utf8_upgrade_nomg(sv);
6215 sv_pos_u2b(sv,&append,0);
6216 }
6217 } else if (SvUTF8(sv)) {
6218 SV *tsv = NEWSV(0,0);
6219 sv_gets(tsv, fp, 0);
6220 sv_utf8_upgrade_nomg(tsv);
6221 SvCUR_set(sv,append);
6222 sv_catsv(sv,tsv);
6223 sv_free(tsv);
6224 goto return_string_or_null;
6225 }
6226 }
6227
6228 SvPOK_only(sv);
6229 if (PerlIO_isutf8(fp))
6230 SvUTF8_on(sv);
c07a80fd 6231
ef7b71f0 6232 if (IN_PERL_COMPILETIME) {
8bfdd7d9
HS
6233 /* we always read code in line mode */
6234 rsptr = "\n";
6235 rslen = 1;
6236 }
6237 else if (RsSNARF(PL_rs)) {
338501c1
JH
6238 /* If it is a regular disk file use size from stat() as estimate
6239 of amount we are going to read - may result in malloc-ing
6240 more memory than we realy need if layers bellow reduce
6241 size we read (e.g. CRLF or a gzip layer)
6242 */
fe1d0b35 6243 Stat_t st;
338501c1 6244 if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode)) {
24c2fff4 6245 const Off_t offset = PerlIO_tell(fp);
bfd7eeef 6246 if (offset != (Off_t) -1 && st.st_size + append > offset) {
338501c1
JH
6247 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
6248 }
6249 }
c07a80fd 6250 rsptr = NULL;
6251 rslen = 0;
6252 }
3280af22 6253 else if (RsRECORD(PL_rs)) {
fe1d0b35 6254 I32 bytesread;
5b2b9c68
HM
6255 char *buffer;
6256
6257 /* Grab the size of the record we're getting */
3280af22 6258 recsize = SvIV(SvRV(PL_rs));
fe1d0b35 6259 buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
5b2b9c68
HM
6260 /* Go yank in */
6261#ifdef VMS
6262 /* VMS wants read instead of fread, because fread doesn't respect */
6263 /* RMS record boundaries. This is not necessarily a good thing to be */
338501c1
JH
6264 /* doing, but we've got no other real choice - except avoid stdio
6265 as implementation - perhaps write a :vms layer ?
6266 */
5b2b9c68
HM
6267 bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
6268#else
6269 bytesread = PerlIO_read(fp, buffer, recsize);
6270#endif
e3b9cd39
JH
6271 if (bytesread < 0)
6272 bytesread = 0;
fe1d0b35 6273 SvCUR_set(sv, bytesread += append);
e670df4e 6274 buffer[bytesread] = '\0';
251c53ad 6275 goto return_string_or_null;
5b2b9c68 6276 }
3280af22 6277 else if (RsPARA(PL_rs)) {
c07a80fd 6278 rsptr = "\n\n";
6279 rslen = 2;
8bfdd7d9 6280 rspara = 1;
c07a80fd 6281 }
7d59b7e4
NIS
6282 else {
6283 /* Get $/ i.e. PL_rs into same encoding as stream wants */
6284 if (PerlIO_isutf8(fp)) {
6285 rsptr = SvPVutf8(PL_rs, rslen);
6286 }
6287 else {
6288 if (SvUTF8(PL_rs)) {
6289 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
6290 Perl_croak(aTHX_ "Wide character in $/");
6291 }
6292 }
6293 rsptr = SvPV(PL_rs, rslen);
6294 }
6295 }
6296
c07a80fd 6297 rslast = rslen ? rsptr[rslen - 1] : '\0';
6298
8bfdd7d9 6299 if (rspara) { /* have to do this both before and after */
79072805 6300 do { /* to make sure file boundaries work right */
760ac839 6301 if (PerlIO_eof(fp))
a0d0e21e 6302 return 0;
760ac839 6303 i = PerlIO_getc(fp);
79072805 6304 if (i != '\n') {
a0d0e21e
LW
6305 if (i == -1)
6306 return 0;
760ac839 6307 PerlIO_ungetc(fp,i);
79072805
LW
6308 break;
6309 }
6310 } while (i != EOF);
6311 }
c07a80fd 6312
760ac839
LW
6313 /* See if we know enough about I/O mechanism to cheat it ! */
6314
6315 /* This used to be #ifdef test - it is made run-time test for ease
1c846c1f 6316 of abstracting out stdio interface. One call should be cheap
760ac839
LW
6317 enough here - and may even be a macro allowing compile
6318 time optimization.
6319 */
6320
6321 if (PerlIO_fast_gets(fp)) {
6322
6323 /*
6324 * We're going to steal some values from the stdio struct
6325 * and put EVERYTHING in the innermost loop into registers.
6326 */
6327 register STDCHAR *ptr;
6328 STRLEN bpx;
6329 I32 shortbuffered;
6330
16660edb 6331#if defined(VMS) && defined(PERLIO_IS_STDIO)
6332 /* An ungetc()d char is handled separately from the regular
6333 * buffer, so we getc() it back out and stuff it in the buffer.
6334 */
6335 i = PerlIO_getc(fp);
6336 if (i == EOF) return 0;
6337 *(--((*fp)->_ptr)) = (unsigned char) i;
6338 (*fp)->_cnt++;
6339#endif
c07a80fd 6340
c2960299 6341 /* Here is some breathtakingly efficient cheating */
c07a80fd 6342
a20bf0c3 6343 cnt = PerlIO_get_cnt(fp); /* get count into register */
338501c1
JH
6344 /* make sure we have the room */
6345 if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
6346 /* Not room for all of it
6347 if we are looking for a separator and room for some
6348 */
6349 if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
6350 /* just process what we have room for */
79072805
LW
6351 shortbuffered = cnt - SvLEN(sv) + append + 1;
6352 cnt -= shortbuffered;
6353 }
6354 else {
6355 shortbuffered = 0;
bbce6d69 6356 /* remember that cnt can be negative */
eb160463 6357 SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
79072805
LW
6358 }
6359 }
338501c1 6360 else
79072805 6361 shortbuffered = 0;
fdac8c4b 6362 bp = (STDCHAR*)SvPVX_const(sv) + append; /* move these two too to registers */
a20bf0c3 6363 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
16660edb 6364 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6365 "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
16660edb 6366 DEBUG_P(PerlIO_printf(Perl_debug_log,
ba7abf9d 6367 "Screamer: entering: PerlIO * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 6368 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 6369 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
79072805
LW
6370 for (;;) {
6371 screamer:
93a17b20 6372 if (cnt > 0) {
c07a80fd 6373 if (rslen) {
760ac839
LW
6374 while (cnt > 0) { /* this | eat */
6375 cnt--;
c07a80fd 6376 if ((*bp++ = *ptr++) == rslast) /* really | dust */
6377 goto thats_all_folks; /* screams | sed :-) */
6378 }
6379 }
6380 else {
1c846c1f
NIS
6381 Copy(ptr, bp, cnt, char); /* this | eat */
6382 bp += cnt; /* screams | dust */
c07a80fd 6383 ptr += cnt; /* louder | sed :-) */
a5f75d66 6384 cnt = 0;
93a17b20 6385 }
79072805
LW
6386 }
6387
748a9306 6388 if (shortbuffered) { /* oh well, must extend */
79072805
LW
6389 cnt = shortbuffered;
6390 shortbuffered = 0;
fdac8c4b 6391 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
79072805
LW
6392 SvCUR_set(sv, bpx);
6393 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
fdac8c4b 6394 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
79072805
LW
6395 continue;
6396 }
6397
16660edb 6398 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841
GS
6399 "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n",
6400 PTR2UV(ptr),(long)cnt));
cc00df79 6401 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
ba7abf9d 6402#if 0
16660edb 6403 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6404 "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 6405 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 6406 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
ba7abf9d 6407#endif
1c846c1f 6408 /* This used to call 'filbuf' in stdio form, but as that behaves like
774d564b 6409 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
6410 another abstraction. */
760ac839 6411 i = PerlIO_getc(fp); /* get more characters */
ba7abf9d 6412#if 0
16660edb 6413 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6414 "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 6415 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 6416 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
ba7abf9d 6417#endif
a20bf0c3
JH
6418 cnt = PerlIO_get_cnt(fp);
6419 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
16660edb 6420 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6421 "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
79072805 6422
748a9306
LW
6423 if (i == EOF) /* all done for ever? */
6424 goto thats_really_all_folks;
6425
fdac8c4b 6426 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
79072805
LW
6427 SvCUR_set(sv, bpx);
6428 SvGROW(sv, bpx + cnt + 2);
fdac8c4b 6429 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
c07a80fd 6430
eb160463 6431 *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */
79072805 6432
c07a80fd 6433 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
79072805 6434 goto thats_all_folks;
79072805
LW
6435 }
6436
6437thats_all_folks:
fdac8c4b 6438 if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX_const(sv)) < rslen) ||
36477c24 6439 memNE((char*)bp - rslen, rsptr, rslen))
760ac839 6440 goto screamer; /* go back to the fray */
79072805
LW
6441thats_really_all_folks:
6442 if (shortbuffered)
6443 cnt += shortbuffered;
16660edb 6444 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6445 "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
cc00df79 6446 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */
16660edb 6447 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 6448 "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 6449 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 6450 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
79072805 6451 *bp = '\0';
fdac8c4b 6452 SvCUR_set(sv, bp - (STDCHAR*)SvPVX_const(sv)); /* set length */
16660edb 6453 DEBUG_P(PerlIO_printf(Perl_debug_log,
fb73857a 6454 "Screamer: done, len=%ld, string=|%.*s|\n",
fdac8c4b 6455 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX_const(sv)));
760ac839
LW
6456 }
6457 else
79072805 6458 {
2ee2a917
JH
6459 /*The big, slow, and stupid way. */
6460
6461 /* Any stack-challenged places. */
f73bace3 6462#if defined(EPOC)
2ee2a917
JH
6463 /* EPOC: need to work around SDK features. *
6464 * On WINS: MS VC5 generates calls to _chkstk, *
6465 * if a "large" stack frame is allocated. *
6466 * gcc on MARM does not generate calls like these. */
6467# define USEHEAPINSTEADOFSTACK
6468#endif
6469
6470#ifdef USEHEAPINSTEADOFSTACK
6471 STDCHAR *buf = 0;
6472 New(0, buf, 8192, STDCHAR);
6473 assert(buf);
4d2c4e07 6474#else
2ee2a917 6475 STDCHAR buf[8192];
4d2c4e07 6476#endif
79072805 6477
760ac839 6478screamer2:
c07a80fd 6479 if (rslen) {
c501bbfe 6480 const register STDCHAR *bpe = buf + sizeof(buf);
760ac839 6481 bp = buf;
eb160463 6482 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
760ac839
LW
6483 ; /* keep reading */
6484 cnt = bp - buf;
c07a80fd 6485 }
6486 else {
760ac839 6487 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
16660edb 6488 /* Accomodate broken VAXC compiler, which applies U8 cast to
6489 * both args of ?: operator, causing EOF to change into 255
6490 */
37be0adf 6491 if (cnt > 0)
cbe9e203
JH
6492 i = (U8)buf[cnt - 1];
6493 else
37be0adf 6494 i = EOF;
c07a80fd 6495 }
79072805 6496
cbe9e203
JH
6497 if (cnt < 0)
6498 cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
6499 if (append)
6500 sv_catpvn(sv, (char *) buf, cnt);
6501 else
6502 sv_setpvn(sv, (char *) buf, cnt);
c07a80fd 6503
6504 if (i != EOF && /* joy */
6505 (!rslen ||
6506 SvCUR(sv) < rslen ||
fdac8c4b 6507 memNE(SvPVX_const(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
79072805
LW
6508 {
6509 append = -1;
63e4d877
CS
6510 /*
6511 * If we're reading from a TTY and we get a short read,
6512 * indicating that the user hit his EOF character, we need
6513 * to notice it now, because if we try to read from the TTY
6514 * again, the EOF condition will disappear.
6515 *
6516 * The comparison of cnt to sizeof(buf) is an optimization
6517 * that prevents unnecessary calls to feof().
6518 *
6519 * - jik 9/25/96
6520 */
6521 if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
6522 goto screamer2;
79072805 6523 }
2ee2a917
JH
6524
6525#ifdef USEHEAPINSTEADOFSTACK
6526 Safefree(buf);
6527#endif
79072805
LW
6528 }
6529
8bfdd7d9 6530 if (rspara) { /* have to do this both before and after */
c07a80fd 6531 while (i != EOF) { /* to make sure file boundaries work right */
760ac839 6532 i = PerlIO_getc(fp);
79072805 6533 if (i != '\n') {
760ac839 6534 PerlIO_ungetc(fp,i);
79072805
LW
6535 break;
6536 }
6537 }
6538 }
c07a80fd 6539
251c53ad 6540return_string_or_null:
c07a80fd 6541 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
79072805
LW
6542}
6543
954c1994
GS
6544/*
6545=for apidoc sv_inc
6546
645c22ef
DM
6547Auto-increment of the value in the SV, doing string to numeric conversion
6548if necessary. Handles 'get' magic.
954c1994
GS
6549
6550=cut
6551*/
6552
79072805 6553void
864dbfa3 6554Perl_sv_inc(pTHX_ register SV *sv)
79072805
LW
6555{
6556 register char *d;
463ee0b2 6557 int flags;
79072805
LW
6558
6559 if (!sv)
6560 return;
b23a5f78
GB
6561 if (SvGMAGICAL(sv))
6562 mg_get(sv);
ed6116ce 6563 if (SvTHINKFIRST(sv)) {
3510b4a1
NC
6564 if (SvREADONLY(sv) && SvFAKE(sv))
6565 sv_force_normal(sv);
0f15f207 6566 if (SvREADONLY(sv)) {
ef7b71f0 6567 if (IN_PERL_RUNTIME)
cea2e8a9 6568 Perl_croak(aTHX_ PL_no_modify);
0f15f207 6569 }
a0d0e21e 6570 if (SvROK(sv)) {
b5be31e9 6571 IV i;
9e7bc3e8
JD
6572 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
6573 return;
56431972 6574 i = PTR2IV(SvRV(sv));
b5be31e9
SM
6575 sv_unref(sv);
6576 sv_setiv(sv, i);
a0d0e21e 6577 }
ed6116ce 6578 }
8990e307 6579 flags = SvFLAGS(sv);
28e5dec8
JH
6580 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
6581 /* It's (privately or publicly) a float, but not tested as an
6582 integer, so test it to see. */
d460ef45 6583 (void) SvIV(sv);
28e5dec8
JH
6584 flags = SvFLAGS(sv);
6585 }
6586 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
6587 /* It's publicly an integer, or privately an integer-not-float */
59d8ce62 6588#ifdef PERL_PRESERVE_IVUV
28e5dec8 6589 oops_its_int:
59d8ce62 6590#endif
25da4f38
IZ
6591 if (SvIsUV(sv)) {
6592 if (SvUVX(sv) == UV_MAX)
a1e868e7 6593 sv_setnv(sv, UV_MAX_P1);
25da4f38
IZ
6594 else
6595 (void)SvIOK_only_UV(sv);
0da6cfda 6596 SvUV_set(sv, SvUVX(sv) + 1);
25da4f38
IZ
6597 } else {
6598 if (SvIVX(sv) == IV_MAX)
28e5dec8 6599 sv_setuv(sv, (UV)IV_MAX + 1);
25da4f38
IZ
6600 else {
6601 (void)SvIOK_only(sv);
0da6cfda 6602 SvIV_set(sv, SvIVX(sv) + 1);
1c846c1f 6603 }
55497cff 6604 }
79072805
LW
6605 return;
6606 }
28e5dec8
JH
6607 if (flags & SVp_NOK) {
6608 (void)SvNOK_only(sv);
0da6cfda 6609 SvNV_set(sv, SvNVX(sv) + 1.0);
28e5dec8
JH
6610 return;
6611 }
6612
fdac8c4b 6613 if (!(flags & SVp_POK) || !*SvPVX_const(sv)) {
28e5dec8
JH
6614 if ((flags & SVTYPEMASK) < SVt_PVIV)
6615 sv_upgrade(sv, SVt_IV);
6616 (void)SvIOK_only(sv);
0da6cfda 6617 SvIV_set(sv, 1);
79072805
LW
6618 return;
6619 }
463ee0b2 6620 d = SvPVX(sv);
79072805
LW
6621 while (isALPHA(*d)) d++;
6622 while (isDIGIT(*d)) d++;
6623 if (*d) {
28e5dec8 6624#ifdef PERL_PRESERVE_IVUV
d1be9408 6625 /* Got to punt this as an integer if needs be, but we don't issue
28e5dec8
JH
6626 warnings. Probably ought to make the sv_iv_please() that does
6627 the conversion if possible, and silently. */
547d29e4 6628 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
28e5dec8
JH
6629 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
6630 /* Need to try really hard to see if it's an integer.
6631 9.22337203685478e+18 is an integer.
6632 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
6633 so $a="9.22337203685478e+18"; $a+0; $a++
6634 needs to be the same as $a="9.22337203685478e+18"; $a++
6635 or we go insane. */
d460ef45 6636
28e5dec8
JH
6637 (void) sv_2iv(sv);
6638 if (SvIOK(sv))
6639 goto oops_its_int;
6640
6641 /* sv_2iv *should* have made this an NV */
6642 if (flags & SVp_NOK) {
6643 (void)SvNOK_only(sv);
0da6cfda 6644 SvNV_set(sv, SvNVX(sv) + 1.0);
28e5dec8
JH
6645 return;
6646 }
6647 /* I don't think we can get here. Maybe I should assert this
6648 And if we do get here I suspect that sv_setnv will croak. NWC
6649 Fall through. */
6650#if defined(USE_LONG_DOUBLE)
6651 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",
fdac8c4b 6652 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8 6653#else
1779d84d 6654 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
fdac8c4b 6655 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8
JH
6656#endif
6657 }
6658#endif /* PERL_PRESERVE_IVUV */
fdac8c4b 6659 sv_setnv(sv,Atof(SvPVX_const(sv)) + 1.0);
79072805
LW
6660 return;
6661 }
6662 d--;
fdac8c4b 6663 while (d >= SvPVX_const(sv)) {
79072805
LW
6664 if (isDIGIT(*d)) {
6665 if (++*d <= '9')
6666 return;
6667 *(d--) = '0';
6668 }
6669 else {
9d116dd7
JH
6670#ifdef EBCDIC
6671 /* MKS: The original code here died if letters weren't consecutive.
6672 * at least it didn't have to worry about non-C locales. The
6673 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
1c846c1f 6674 * arranged in order (although not consecutively) and that only
9d116dd7
JH
6675 * [A-Za-z] are accepted by isALPHA in the C locale.
6676 */
6677 if (*d != 'z' && *d != 'Z') {
6678 do { ++*d; } while (!isALPHA(*d));
6679 return;
6680 }
6681 *(d--) -= 'z' - 'a';
6682#else
79072805
LW
6683 ++*d;
6684 if (isALPHA(*d))
6685 return;
6686 *(d--) -= 'z' - 'a' + 1;
9d116dd7 6687#endif
79072805
LW
6688 }
6689 }
6690 /* oh,oh, the number grew */
6691 SvGROW(sv, SvCUR(sv) + 2);
a8dc4fe8 6692 SvCUR_set(sv, SvCUR(sv) + 1);
fdac8c4b 6693 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX_const(sv); d--)
79072805
LW
6694 *d = d[-1];
6695 if (isDIGIT(d[1]))
6696 *d = '1';
6697 else
6698 *d = d[1];
6699}
6700
954c1994
GS
6701/*
6702=for apidoc sv_dec
6703
645c22ef
DM
6704Auto-decrement of the value in the SV, doing string to numeric conversion
6705if necessary. Handles 'get' magic.
954c1994
GS
6706
6707=cut
6708*/
6709
79072805 6710void
864dbfa3 6711Perl_sv_dec(pTHX_ register SV *sv)
79072805 6712{
463ee0b2
LW
6713 int flags;
6714
79072805
LW
6715 if (!sv)
6716 return;
b23a5f78
GB
6717 if (SvGMAGICAL(sv))
6718 mg_get(sv);
ed6116ce 6719 if (SvTHINKFIRST(sv)) {
3510b4a1
NC
6720 if (SvREADONLY(sv) && SvFAKE(sv))
6721 sv_force_normal(sv);
0f15f207 6722 if (SvREADONLY(sv)) {
ef7b71f0 6723 if (IN_PERL_RUNTIME)
cea2e8a9 6724 Perl_croak(aTHX_ PL_no_modify);
0f15f207 6725 }
a0d0e21e 6726 if (SvROK(sv)) {
b5be31e9 6727 IV i;
9e7bc3e8
JD
6728 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
6729 return;
56431972 6730 i = PTR2IV(SvRV(sv));
b5be31e9
SM
6731 sv_unref(sv);
6732 sv_setiv(sv, i);
a0d0e21e 6733 }
ed6116ce 6734 }
28e5dec8
JH
6735 /* Unlike sv_inc we don't have to worry about string-never-numbers
6736 and keeping them magic. But we mustn't warn on punting */
8990e307 6737 flags = SvFLAGS(sv);
28e5dec8
JH
6738 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
6739 /* It's publicly an integer, or privately an integer-not-float */
59d8ce62 6740#ifdef PERL_PRESERVE_IVUV
28e5dec8 6741 oops_its_int:
59d8ce62 6742#endif
25da4f38
IZ
6743 if (SvIsUV(sv)) {
6744 if (SvUVX(sv) == 0) {
6745 (void)SvIOK_only(sv);
0da6cfda 6746 SvIV_set(sv, -1);
25da4f38
IZ
6747 }
6748 else {
6749 (void)SvIOK_only_UV(sv);
b43557d6 6750 SvUV_set(sv, SvUVX(sv) - 1);
1c846c1f 6751 }
25da4f38
IZ
6752 } else {
6753 if (SvIVX(sv) == IV_MIN)
65202027 6754 sv_setnv(sv, (NV)IV_MIN - 1.0);
25da4f38
IZ
6755 else {
6756 (void)SvIOK_only(sv);
0da6cfda 6757 SvIV_set(sv, SvIVX(sv) - 1);
1c846c1f 6758 }
55497cff 6759 }
6760 return;
6761 }
28e5dec8 6762 if (flags & SVp_NOK) {
0da6cfda 6763 SvNV_set(sv, SvNVX(sv) - 1.0);
28e5dec8
JH
6764 (void)SvNOK_only(sv);
6765 return;
6766 }
8990e307 6767 if (!(flags & SVp_POK)) {
b43557d6
NC
6768 if ((flags & SVTYPEMASK) < SVt_PVIV)
6769 sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV) ? SVt_PVIV : SVt_IV);
6770 SvIV_set(sv, -1);
6771 (void)SvIOK_only(sv);
79072805
LW
6772 return;
6773 }
28e5dec8
JH
6774#ifdef PERL_PRESERVE_IVUV
6775 {
547d29e4 6776 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
28e5dec8
JH
6777 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
6778 /* Need to try really hard to see if it's an integer.
6779 9.22337203685478e+18 is an integer.
6780 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
6781 so $a="9.22337203685478e+18"; $a+0; $a--
6782 needs to be the same as $a="9.22337203685478e+18"; $a--
6783 or we go insane. */
d460ef45 6784
28e5dec8
JH
6785 (void) sv_2iv(sv);
6786 if (SvIOK(sv))
6787 goto oops_its_int;
6788
6789 /* sv_2iv *should* have made this an NV */
6790 if (flags & SVp_NOK) {
6791 (void)SvNOK_only(sv);
0da6cfda 6792 SvNV_set(sv, SvNVX(sv) - 1.0);
28e5dec8
JH
6793 return;
6794 }
6795 /* I don't think we can get here. Maybe I should assert this
6796 And if we do get here I suspect that sv_setnv will croak. NWC
6797 Fall through. */
6798#if defined(USE_LONG_DOUBLE)
6799 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",
fdac8c4b 6800 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8 6801#else
1779d84d 6802 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
fdac8c4b 6803 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
28e5dec8
JH
6804#endif
6805 }
6806 }
6807#endif /* PERL_PRESERVE_IVUV */
fdac8c4b 6808 sv_setnv(sv,Atof(SvPVX_const(sv)) - 1.0); /* punt */
79072805
LW
6809}
6810
954c1994
GS
6811/*
6812=for apidoc sv_mortalcopy
6813
645c22ef 6814Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
d4236ebc
DM
6815The new SV is marked as mortal. It will be destroyed "soon", either by an
6816explicit call to FREETMPS, or by an implicit call at places such as
6817statement boundaries. See also C<sv_newmortal> and C<sv_2mortal>.
954c1994
GS
6818
6819=cut
6820*/
6821
79072805
LW
6822/* Make a string that will exist for the duration of the expression
6823 * evaluation. Actually, it may have to last longer than that, but
6824 * hopefully we won't free it until it has been assigned to a
6825 * permanent location. */
6826
6827SV *
864dbfa3 6828Perl_sv_mortalcopy(pTHX_ SV *oldstr)
79072805 6829{
463ee0b2 6830 register SV *sv;
b881518d 6831
4561caa4 6832 new_SV(sv);
79072805 6833 sv_setsv(sv,oldstr);
677b06e3
GS
6834 EXTEND_MORTAL(1);
6835 PL_tmps_stack[++PL_tmps_ix] = sv;
8990e307
LW
6836 SvTEMP_on(sv);
6837 return sv;
6838}
6839
954c1994
GS
6840/*
6841=for apidoc sv_newmortal
6842
645c22ef 6843Creates a new null SV which is mortal. The reference count of the SV is
d4236ebc
DM
6844set to 1. It will be destroyed "soon", either by an explicit call to
6845FREETMPS, or by an implicit call at places such as statement boundaries.
6846See also C<sv_mortalcopy> and C<sv_2mortal>.
954c1994
GS
6847
6848=cut
6849*/
6850
8990e307 6851SV *
864dbfa3 6852Perl_sv_newmortal(pTHX)
8990e307
LW
6853{
6854 register SV *sv;
6855
4561caa4 6856 new_SV(sv);
8990e307 6857 SvFLAGS(sv) = SVs_TEMP;
677b06e3
GS
6858 EXTEND_MORTAL(1);
6859 PL_tmps_stack[++PL_tmps_ix] = sv;
79072805
LW
6860 return sv;
6861}
6862
954c1994
GS
6863/*
6864=for apidoc sv_2mortal
6865
d4236ebc
DM
6866Marks an existing SV as mortal. The SV will be destroyed "soon", either
6867by an explicit call to FREETMPS, or by an implicit call at places such as
7aa747bc
A
6868statement boundaries. SvTEMP() is turned on which means that the SV's
6869string buffer can be "stolen" if this SV is copied. See also C<sv_newmortal>
6870and C<sv_mortalcopy>.
954c1994
GS
6871
6872=cut
6873*/
6874
79072805 6875SV *
864dbfa3 6876Perl_sv_2mortal(pTHX_ register SV *sv)
79072805
LW
6877{
6878 if (!sv)
6879 return sv;
d689ffdd 6880 if (SvREADONLY(sv) && SvIMMORTAL(sv))
11162842 6881 return sv;
677b06e3
GS
6882 EXTEND_MORTAL(1);
6883 PL_tmps_stack[++PL_tmps_ix] = sv;
8990e307 6884 SvTEMP_on(sv);
79072805
LW
6885 return sv;
6886}
6887
954c1994
GS
6888/*
6889=for apidoc newSVpv
6890
6891Creates a new SV and copies a string into it. The reference count for the
6892SV is set to 1. If C<len> is zero, Perl will compute the length using
6893strlen(). For efficiency, consider using C<newSVpvn> instead.
6894
6895=cut
6896*/
6897
79072805 6898SV *
864dbfa3 6899Perl_newSVpv(pTHX_ const char *s, STRLEN len)
79072805 6900{
463ee0b2 6901 register SV *sv;
79072805 6902
4561caa4 6903 new_SV(sv);
2a8de9e2 6904 sv_setpvn(sv,s,len ? len : strlen(s));
79072805
LW
6905 return sv;
6906}
6907
954c1994
GS
6908/*
6909=for apidoc newSVpvn
6910
6911Creates a new SV and copies a string into it. The reference count for the
1c846c1f 6912SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
954c1994 6913string. You are responsible for ensuring that the source string is at least
611e9550 6914C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
954c1994
GS
6915
6916=cut
6917*/
6918
9da1e3b5 6919SV *
864dbfa3 6920Perl_newSVpvn(pTHX_ const char *s, STRLEN len)
9da1e3b5
MUN
6921{
6922 register SV *sv;
6923
6924 new_SV(sv);
9da1e3b5
MUN
6925 sv_setpvn(sv,s,len);
6926 return sv;
6927}
6928
1c846c1f
NIS
6929/*
6930=for apidoc newSVpvn_share
6931
fdac8c4b 6932Creates a new SV with its SvPVX_const pointing to a shared string in the string
645c22ef
DM
6933table. If the string does not already exist in the table, it is created
6934first. Turns on READONLY and FAKE. The string's hash is stored in the UV
6935slot of the SV; if the C<hash> parameter is non-zero, that value is used;
6936otherwise the hash is computed. The idea here is that as the string table
fdac8c4b 6937is used for shared hash keys these strings will have SvPVX_const == HeKEY and
645c22ef 6938hash lookup will avoid string compare.
1c846c1f
NIS
6939
6940=cut
6941*/
6942
6943SV *
c3654f1a 6944Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
1c846c1f
NIS
6945{
6946 register SV *sv;
c3654f1a
IH
6947 bool is_utf8 = FALSE;
6948 if (len < 0) {
77caf834 6949 STRLEN tmplen = -len;
c3654f1a 6950 is_utf8 = TRUE;
75a54232 6951 /* See the note in hv.c:hv_fetch() --jhi */
c05e0e2f 6952 src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
75a54232
JH
6953 len = tmplen;
6954 }
1c846c1f 6955 if (!hash)
5afd6d42 6956 PERL_HASH(hash, src, len);
1c846c1f
NIS
6957 new_SV(sv);
6958 sv_upgrade(sv, SVt_PVIV);
0da6cfda 6959 SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
a8dc4fe8 6960 SvCUR_set(sv, len);
0da6cfda 6961 SvUV_set(sv, hash);
a8dc4fe8 6962 SvLEN_set(sv, 0);
1c846c1f
NIS
6963 SvREADONLY_on(sv);
6964 SvFAKE_on(sv);
6965 SvPOK_on(sv);
c3654f1a
IH
6966 if (is_utf8)
6967 SvUTF8_on(sv);
1c846c1f
NIS
6968 return sv;
6969}
6970
645c22ef 6971
cea2e8a9 6972#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
6973
6974/* pTHX_ magic can't cope with varargs, so this is a no-context
6975 * version of the main function, (which may itself be aliased to us).
6976 * Don't access this version directly.
6977 */
6978
46fc3d4c 6979SV *
cea2e8a9 6980Perl_newSVpvf_nocontext(const char* pat, ...)
46fc3d4c 6981{
cea2e8a9 6982 dTHX;
46fc3d4c 6983 register SV *sv;
6984 va_list args;
46fc3d4c 6985 va_start(args, pat);
c5be433b 6986 sv = vnewSVpvf(pat, &args);
46fc3d4c 6987 va_end(args);
6988 return sv;
6989}
cea2e8a9 6990#endif
46fc3d4c 6991
954c1994
GS
6992/*
6993=for apidoc newSVpvf
6994
645c22ef 6995Creates a new SV and initializes it with the string formatted like
954c1994
GS
6996C<sprintf>.
6997
6998=cut
6999*/
7000
cea2e8a9
GS
7001SV *
7002Perl_newSVpvf(pTHX_ const char* pat, ...)
7003{
7004 register SV *sv;
7005 va_list args;
cea2e8a9 7006 va_start(args, pat);
c5be433b 7007 sv = vnewSVpvf(pat, &args);
cea2e8a9
GS
7008 va_end(args);
7009 return sv;
7010}
46fc3d4c 7011
645c22ef
DM
7012/* backend for newSVpvf() and newSVpvf_nocontext() */
7013
79072805 7014SV *
c5be433b
GS
7015Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args)
7016{
7017 register SV *sv;
7018 new_SV(sv);
7019 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
7020 return sv;
7021}
7022
954c1994
GS
7023/*
7024=for apidoc newSVnv
7025
7026Creates a new SV and copies a floating point value into it.
7027The reference count for the SV is set to 1.
7028
7029=cut
7030*/
7031
c5be433b 7032SV *
65202027 7033Perl_newSVnv(pTHX_ NV n)
79072805 7034{
463ee0b2 7035 register SV *sv;
79072805 7036
4561caa4 7037 new_SV(sv);
79072805
LW
7038 sv_setnv(sv,n);
7039 return sv;
7040}
7041
954c1994
GS
7042/*
7043=for apidoc newSViv
7044
7045Creates a new SV and copies an integer into it. The reference count for the
7046SV is set to 1.
7047
7048=cut
7049*/
7050
79072805 7051SV *
864dbfa3 7052Perl_newSViv(pTHX_ IV i)
79072805 7053{
463ee0b2 7054 register SV *sv;
79072805 7055
4561caa4 7056 new_SV(sv);
79072805
LW
7057 sv_setiv(sv,i);
7058 return sv;
7059}
7060
954c1994 7061/*
1a3327fb
JH
7062=for apidoc newSVuv
7063
7064Creates a new SV and copies an unsigned integer into it.
7065The reference count for the SV is set to 1.
7066
7067=cut
7068*/
7069
7070SV *
7071Perl_newSVuv(pTHX_ UV u)
7072{
7073 register SV *sv;
7074
7075 new_SV(sv);
7076 sv_setuv(sv,u);
7077 return sv;
7078}
7079
7080/*
954c1994
GS
7081=for apidoc newRV_noinc
7082
7083Creates an RV wrapper for an SV. The reference count for the original
7084SV is B<not> incremented.
7085
7086=cut
7087*/
7088
2304df62 7089SV *
864dbfa3 7090Perl_newRV_noinc(pTHX_ SV *tmpRef)
2304df62
AD
7091{
7092 register SV *sv;
7093
4561caa4 7094 new_SV(sv);
2304df62 7095 sv_upgrade(sv, SVt_RV);
76e3520e 7096 SvTEMP_off(tmpRef);
a8dc4fe8 7097 SvRV_set(sv, tmpRef);
2304df62 7098 SvROK_on(sv);
2304df62
AD
7099 return sv;
7100}
7101
ff276b08 7102/* newRV_inc is the official function name to use now.
645c22ef
DM
7103 * newRV_inc is in fact #defined to newRV in sv.h
7104 */
7105
5f05dabc 7106SV *
864dbfa3 7107Perl_newRV(pTHX_ SV *tmpRef)
5f05dabc 7108{
5f6447b6 7109 return newRV_noinc(SvREFCNT_inc(tmpRef));
5f05dabc 7110}
5f05dabc 7111
954c1994
GS
7112/*
7113=for apidoc newSVsv
7114
7115Creates a new SV which is an exact duplicate of the original SV.
645c22ef 7116(Uses C<sv_setsv>).
954c1994
GS
7117
7118=cut
7119*/
7120
79072805 7121SV *
864dbfa3 7122Perl_newSVsv(pTHX_ register SV *old)
79072805 7123{
463ee0b2 7124 register SV *sv;
79072805
LW
7125
7126 if (!old)
7127 return Nullsv;
8990e307 7128 if (SvTYPE(old) == SVTYPEMASK) {
0453d815 7129 if (ckWARN_d(WARN_INTERNAL))
9014280d 7130 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
79072805
LW
7131 return Nullsv;
7132 }
4561caa4 7133 new_SV(sv);
3965bf69
NC
7134 /* SV_GMAGIC is the default for sv_setv()
7135 SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games
7136 with SvTEMP_off and SvTEMP_on round a call to sv_setsv. */
7137 sv_setsv_flags(sv, old, SV_GMAGIC | SV_NOSTEAL);
463ee0b2 7138 return sv;
79072805
LW
7139}
7140
645c22ef
DM
7141/*
7142=for apidoc sv_reset
7143
7144Underlying implementation for the C<reset> Perl function.
7145Note that the perl-level function is vaguely deprecated.
7146
7147=cut
7148*/
7149
79072805 7150void
864dbfa3 7151Perl_sv_reset(pTHX_ register char *s, HV *stash)
79072805
LW
7152{
7153 register HE *entry;
7154 register GV *gv;
7155 register SV *sv;
7156 register I32 i;
7157 register PMOP *pm;
7158 register I32 max;
4802d5d7 7159 char todo[PERL_UCHAR_MAX+1];
79072805 7160
49d8d3a1
MB
7161 if (!stash)
7162 return;
7163
79072805
LW
7164 if (!*s) { /* reset ?? searches */
7165 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
48c036b1 7166 pm->op_pmdynflags &= ~PMdf_USED;
79072805
LW
7167 }
7168 return;
7169 }
7170
7171 /* reset variables */
7172
7173 if (!HvARRAY(stash))
7174 return;
463ee0b2
LW
7175
7176 Zero(todo, 256, char);
79072805 7177 while (*s) {
4802d5d7 7178 i = (unsigned char)*s;
79072805
LW
7179 if (s[1] == '-') {
7180 s += 2;
7181 }
4802d5d7 7182 max = (unsigned char)*s++;
79072805 7183 for ( ; i <= max; i++) {
463ee0b2
LW
7184 todo[i] = 1;
7185 }
a0d0e21e 7186 for (i = 0; i <= (I32) HvMAX(stash); i++) {
79072805 7187 for (entry = HvARRAY(stash)[i];
9e35f4b3
GS
7188 entry;
7189 entry = HeNEXT(entry))
7190 {
1edc1566 7191 if (!todo[(U8)*HeKEY(entry)])
463ee0b2 7192 continue;
1edc1566 7193 gv = (GV*)HeVAL(entry);
79072805 7194 sv = GvSV(gv);
9e35f4b3
GS
7195 if (SvTHINKFIRST(sv)) {
7196 if (!SvREADONLY(sv) && SvROK(sv))
7197 sv_unref(sv);
7198 continue;
7199 }
7460c263 7200 SvOK_off(sv);
79072805
LW
7201 if (SvTYPE(sv) >= SVt_PV) {
7202 SvCUR_set(sv, 0);
fdac8c4b 7203 if (SvPVX_const(sv) != Nullch)
463ee0b2 7204 *SvPVX(sv) = '\0';
44a8e56a 7205 SvTAINT(sv);
79072805
LW
7206 }
7207 if (GvAV(gv)) {
7208 av_clear(GvAV(gv));
7209 }
26ab6a78 7210 if (GvHV(gv) && !HvNAME_get(GvHV(gv))) {
463ee0b2 7211 hv_clear(GvHV(gv));
75a5c1c6 7212#ifndef PERL_MICRO
fa6a1c44 7213#ifdef USE_ENVIRON_ARRAY
4efc5df6
GS
7214 if (gv == PL_envgv
7215# ifdef USE_ITHREADS
7216 && PL_curinterp == aTHX
7217# endif
7218 )
7219 {
79072805 7220 environ[0] = Nullch;
4efc5df6 7221 }
a0d0e21e 7222#endif
75a5c1c6 7223#endif /* !PERL_MICRO */
79072805
LW
7224 }
7225 }
7226 }
7227 }
7228}
7229
645c22ef
DM
7230/*
7231=for apidoc sv_2io
7232
7233Using various gambits, try to get an IO from an SV: the IO slot if its a
7234GV; or the recursive result if we're an RV; or the IO slot of the symbol
7235named after the PV if we're a string.
7236
7237=cut
7238*/
7239
46fc3d4c 7240IO*
864dbfa3 7241Perl_sv_2io(pTHX_ SV *sv)
46fc3d4c 7242{
7243 IO* io;
7244 GV* gv;
2d8e6c8d 7245 STRLEN n_a;
46fc3d4c 7246
7247 switch (SvTYPE(sv)) {
7248 case SVt_PVIO:
7249 io = (IO*)sv;
7250 break;
7251 case SVt_PVGV:
7252 gv = (GV*)sv;
7253 io = GvIO(gv);
7254 if (!io)
cea2e8a9 7255 Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
46fc3d4c 7256 break;
7257 default:
7258 if (!SvOK(sv))
cea2e8a9 7259 Perl_croak(aTHX_ PL_no_usym, "filehandle");
46fc3d4c 7260 if (SvROK(sv))
7261 return sv_2io(SvRV(sv));
2d8e6c8d 7262 gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO);
46fc3d4c 7263 if (gv)
7264 io = GvIO(gv);
7265 else
7266 io = 0;
7267 if (!io)
c293eb2b 7268 Perl_croak(aTHX_ "Bad filehandle: %"SVf, sv);
46fc3d4c 7269 break;
7270 }
7271 return io;
7272}
7273
645c22ef
DM
7274/*
7275=for apidoc sv_2cv
7276
7277Using various gambits, try to get a CV from an SV; in addition, try if
7278possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
7279
7280=cut
7281*/
7282
79072805 7283CV *
864dbfa3 7284Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref)
79072805 7285{
c04a4dfe
JH
7286 GV *gv = Nullgv;
7287 CV *cv = Nullcv;
2d8e6c8d 7288 STRLEN n_a;
79072805
LW
7289
7290 if (!sv)
93a17b20 7291 return *gvp = Nullgv, Nullcv;
79072805 7292 switch (SvTYPE(sv)) {
79072805
LW
7293 case SVt_PVCV:
7294 *st = CvSTASH(sv);
7295 *gvp = Nullgv;
7296 return (CV*)sv;
7297 case SVt_PVHV:
7298 case SVt_PVAV:
7299 *gvp = Nullgv;
7300 return Nullcv;
8990e307
LW
7301 case SVt_PVGV:
7302 gv = (GV*)sv;
a0d0e21e 7303 *gvp = gv;
8990e307
LW
7304 *st = GvESTASH(gv);
7305 goto fix_gv;
7306
79072805 7307 default:
a0d0e21e
LW
7308 if (SvGMAGICAL(sv))
7309 mg_get(sv);
7310 if (SvROK(sv)) {
f5284f61
IZ
7311 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
7312 tryAMAGICunDEREF(to_cv);
7313
62f274bf
GS
7314 sv = SvRV(sv);
7315 if (SvTYPE(sv) == SVt_PVCV) {
7316 cv = (CV*)sv;
7317 *gvp = Nullgv;
7318 *st = CvSTASH(cv);
7319 return cv;
7320 }
7321 else if(isGV(sv))
7322 gv = (GV*)sv;
7323 else
cea2e8a9 7324 Perl_croak(aTHX_ "Not a subroutine reference");
a0d0e21e 7325 }
62f274bf 7326 else if (isGV(sv))
79072805
LW
7327 gv = (GV*)sv;
7328 else
2d8e6c8d 7329 gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV);
79072805
LW
7330 *gvp = gv;
7331 if (!gv)
7332 return Nullcv;
7333 *st = GvESTASH(gv);
8990e307 7334 fix_gv:
8ebc5c01 7335 if (lref && !GvCVu(gv)) {
4633a7c4 7336 SV *tmpsv;
748a9306 7337 ENTER;
4633a7c4 7338 tmpsv = NEWSV(704,0);
16660edb 7339 gv_efullname3(tmpsv, gv, Nullch);
f6ec51f7
GS
7340 /* XXX this is probably not what they think they're getting.
7341 * It has the same effect as "sub name;", i.e. just a forward
7342 * declaration! */
774d564b 7343 newSUB(start_subparse(FALSE, 0),
4633a7c4
LW
7344 newSVOP(OP_CONST, 0, tmpsv),
7345 Nullop,
8990e307 7346 Nullop);
748a9306 7347 LEAVE;
8ebc5c01 7348 if (!GvCVu(gv))
c293eb2b
NC
7349 Perl_croak(aTHX_ "Unable to create sub named \"%"SVf"\"",
7350 sv);
8990e307 7351 }
8ebc5c01 7352 return GvCVu(gv);
79072805
LW
7353 }
7354}
7355
c461cf8f
JH
7356/*
7357=for apidoc sv_true
7358
7359Returns true if the SV has a true value by Perl's rules.
645c22ef
DM
7360Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
7361instead use an in-line version.
c461cf8f
JH
7362
7363=cut
7364*/
7365
79072805 7366I32
864dbfa3 7367Perl_sv_true(pTHX_ register SV *sv)
79072805 7368{
8990e307
LW
7369 if (!sv)
7370 return 0;
79072805 7371 if (SvPOK(sv)) {
c05e0e2f 7372 const register XPV* tXpv;
4e35701f 7373 if ((tXpv = (XPV*)SvANY(sv)) &&
c2f1de04 7374 (tXpv->xpv_cur > 1 ||
4e35701f 7375 (tXpv->xpv_cur && *tXpv->xpv_pv != '0')))
79072805
LW
7376 return 1;
7377 else
7378 return 0;
7379 }
7380 else {
7381 if (SvIOK(sv))
463ee0b2 7382 return SvIVX(sv) != 0;
79072805
LW
7383 else {
7384 if (SvNOK(sv))
463ee0b2 7385 return SvNVX(sv) != 0.0;
79072805 7386 else
463ee0b2 7387 return sv_2bool(sv);
79072805
LW
7388 }
7389 }
7390}
79072805 7391
645c22ef
DM
7392/*
7393=for apidoc sv_iv
7394
7395A private implementation of the C<SvIVx> macro for compilers which can't
7396cope with complex macro expressions. Always use the macro instead.
7397
7398=cut
7399*/
7400
ff68c719 7401IV
864dbfa3 7402Perl_sv_iv(pTHX_ register SV *sv)
85e6fe83 7403{
25da4f38
IZ
7404 if (SvIOK(sv)) {
7405 if (SvIsUV(sv))
7406 return (IV)SvUVX(sv);
ff68c719 7407 return SvIVX(sv);
25da4f38 7408 }
ff68c719 7409 return sv_2iv(sv);
85e6fe83 7410}
85e6fe83 7411
645c22ef
DM
7412/*
7413=for apidoc sv_uv
7414
7415A private implementation of the C<SvUVx> macro for compilers which can't
7416cope with complex macro expressions. Always use the macro instead.
7417
7418=cut
7419*/
7420
ff68c719 7421UV
864dbfa3 7422Perl_sv_uv(pTHX_ register SV *sv)
ff68c719 7423{
25da4f38
IZ
7424 if (SvIOK(sv)) {
7425 if (SvIsUV(sv))
7426 return SvUVX(sv);
7427 return (UV)SvIVX(sv);
7428 }
ff68c719 7429 return sv_2uv(sv);
7430}
85e6fe83 7431
645c22ef
DM
7432/*
7433=for apidoc sv_nv
7434
7435A private implementation of the C<SvNVx> macro for compilers which can't
7436cope with complex macro expressions. Always use the macro instead.
7437
7438=cut
7439*/
7440
65202027 7441NV
864dbfa3 7442Perl_sv_nv(pTHX_ register SV *sv)
79072805 7443{
ff68c719 7444 if (SvNOK(sv))
7445 return SvNVX(sv);
7446 return sv_2nv(sv);
79072805 7447}
79072805 7448
d34f9d2e
JH
7449/* sv_pv() is now a macro using SvPV_nolen();
7450 * this function provided for binary compatibility only
7451 */
7452
7453char *
7454Perl_sv_pv(pTHX_ SV *sv)
7455{
7456 STRLEN n_a;
7457
7458 if (SvPOK(sv))
7459 return SvPVX(sv);
7460
7461 return sv_2pv(sv, &n_a);
7462}
7463
645c22ef
DM
7464/*
7465=for apidoc sv_pv
7466
baca2b92 7467Use the C<SvPV_nolen> macro instead
645c22ef 7468
645c22ef
DM
7469=for apidoc sv_pvn
7470
7471A private implementation of the C<SvPV> macro for compilers which can't
7472cope with complex macro expressions. Always use the macro instead.
7473
7474=cut
7475*/
7476
1fa8b10d 7477char *
864dbfa3 7478Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp)
79072805 7479{
85e6fe83
LW
7480 if (SvPOK(sv)) {
7481 *lp = SvCUR(sv);
a0d0e21e 7482 return SvPVX(sv);
85e6fe83 7483 }
463ee0b2 7484 return sv_2pv(sv, lp);
79072805 7485}
79072805 7486
6e9d1081
NC
7487
7488char *
7489Perl_sv_pvn_nomg(pTHX_ register SV *sv, STRLEN *lp)
7490{
7491 if (SvPOK(sv)) {
7492 *lp = SvCUR(sv);
7493 return SvPVX(sv);
7494 }
7495 return sv_2pv_flags(sv, lp, 0);
7496}
7497
d34f9d2e
JH
7498/* sv_pvn_force() is now a macro using Perl_sv_pvn_force_flags();
7499 * this function provided for binary compatibility only
7500 */
7501
7502char *
7503Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp)
7504{
7505 return sv_pvn_force_flags(sv, lp, SV_GMAGIC);
7506}
7507
c461cf8f
JH
7508/*
7509=for apidoc sv_pvn_force
7510
7511Get a sensible string out of the SV somehow.
645c22ef
DM
7512A private implementation of the C<SvPV_force> macro for compilers which
7513can't cope with complex macro expressions. Always use the macro instead.
c461cf8f 7514
8d6d96c1
HS
7515=for apidoc sv_pvn_force_flags
7516
7517Get a sensible string out of the SV somehow.
7518If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if
7519appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
7520implemented in terms of this function.
645c22ef
DM
7521You normally want to use the various wrapper macros instead: see
7522C<SvPV_force> and C<SvPV_force_nomg>
8d6d96c1
HS
7523
7524=cut
7525*/
7526
7527char *
7528Perl_sv_pvn_force_flags(pTHX_ SV *sv, STRLEN *lp, I32 flags)
7529{
a0d0e21e 7530
6fc92669
GS
7531 if (SvTHINKFIRST(sv) && !SvROK(sv))
7532 sv_force_normal(sv);
1c846c1f 7533
a0d0e21e
LW
7534 if (SvPOK(sv)) {
7535 *lp = SvCUR(sv);
7536 }
7537 else {
8c18bf38 7538 char *s;
748a9306 7539 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
cea2e8a9 7540 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
53e06cf0 7541 OP_NAME(PL_op));
a0d0e21e 7542 }
4633a7c4 7543 else
8d6d96c1 7544 s = sv_2pv_flags(sv, lp, flags);
fdac8c4b 7545 if (s != SvPVX_const(sv)) { /* Almost, but not quite, sv_setpvn() */
8c18bf38 7546 const STRLEN len = *lp;
1c846c1f 7547
a0d0e21e
LW
7548 if (SvROK(sv))
7549 sv_unref(sv);
7550 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
7551 SvGROW(sv, len + 1);
fdac8c4b 7552 Move(s,SvPVX_const(sv),len,char);
a0d0e21e
LW
7553 SvCUR_set(sv, len);
7554 *SvEND(sv) = '\0';
7555 }
7556 if (!SvPOK(sv)) {
7557 SvPOK_on(sv); /* validate pointer */
7558 SvTAINT(sv);
1d7c1841 7559 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
fdac8c4b 7560 PTR2UV(sv),SvPVX_const(sv)));
a0d0e21e
LW
7561 }
7562 }
7563 return SvPVX(sv);
7564}
7565
d34f9d2e
JH
7566/* sv_pvbyte () is now a macro using Perl_sv_2pv_flags();
7567 * this function provided for binary compatibility only
7568 */
7569
7570char *
7571Perl_sv_pvbyte(pTHX_ SV *sv)
7572{
7573 sv_utf8_downgrade(sv,0);
7574 return sv_pv(sv);
7575}
7576
645c22ef
DM
7577/*
7578=for apidoc sv_pvbyte
7579
baca2b92 7580Use C<SvPVbyte_nolen> instead.
645c22ef 7581
645c22ef
DM
7582=for apidoc sv_pvbyten
7583
7584A private implementation of the C<SvPVbyte> macro for compilers
7585which can't cope with complex macro expressions. Always use the macro
7586instead.
7587
7588=cut
7589*/
7590
7340a771
GS
7591char *
7592Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp)
7593{
ffebcc3e 7594 sv_utf8_downgrade(sv,0);
7340a771
GS
7595 return sv_pvn(sv,lp);
7596}
7597
645c22ef
DM
7598/*
7599=for apidoc sv_pvbyten_force
7600
7601A private implementation of the C<SvPVbytex_force> macro for compilers
7602which can't cope with complex macro expressions. Always use the macro
7603instead.
7604
7605=cut
7606*/
7607
7340a771
GS
7608char *
7609Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp)
7610{
b035a42e 7611 sv_pvn_force(sv,lp);
ffebcc3e 7612 sv_utf8_downgrade(sv,0);
b035a42e
NC
7613 *lp = SvCUR(sv);
7614 return SvPVX(sv);
7340a771
GS
7615}
7616
d34f9d2e
JH
7617/* sv_pvutf8 () is now a macro using Perl_sv_2pv_flags();
7618 * this function provided for binary compatibility only
7619 */
7620
7621char *
7622Perl_sv_pvutf8(pTHX_ SV *sv)
7623{
7624 sv_utf8_upgrade(sv);
7625 return sv_pv(sv);
7626}
7627
645c22ef
DM
7628/*
7629=for apidoc sv_pvutf8
7630
baca2b92 7631Use the C<SvPVutf8_nolen> macro instead
645c22ef 7632
645c22ef
DM
7633=for apidoc sv_pvutf8n
7634
7635A private implementation of the C<SvPVutf8> macro for compilers
7636which can't cope with complex macro expressions. Always use the macro
7637instead.
7638
7639=cut
7640*/
7641
7340a771
GS
7642char *
7643Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp)
7644{
560a288e 7645 sv_utf8_upgrade(sv);
7340a771
GS
7646 return sv_pvn(sv,lp);
7647}
7648
c461cf8f
JH
7649/*
7650=for apidoc sv_pvutf8n_force
7651
645c22ef
DM
7652A private implementation of the C<SvPVutf8_force> macro for compilers
7653which can't cope with complex macro expressions. Always use the macro
7654instead.
c461cf8f
JH
7655
7656=cut
7657*/
7658
7340a771
GS
7659char *
7660Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp)
7661{
b035a42e 7662 sv_pvn_force(sv,lp);
560a288e 7663 sv_utf8_upgrade(sv);
b035a42e
NC
7664 *lp = SvCUR(sv);
7665 return SvPVX(sv);
7340a771
GS
7666}
7667
c461cf8f
JH
7668/*
7669=for apidoc sv_reftype
7670
7671Returns a string describing what the SV is a reference to.
7672
7673=cut
7674*/
7675
7340a771 7676char *
864dbfa3 7677Perl_sv_reftype(pTHX_ SV *sv, int ob)
a0d0e21e 7678{
206b424e
NC
7679 /* The fact that I don't need to downcast to char * everywhere, only in ?:
7680 inside return suggests a const propagation bug in g++. */
c86bf373 7681 if (ob && SvOBJECT(sv)) {
26ab6a78 7682 char *name = HvNAME_get(SvSTASH(sv));
206b424e 7683 return name ? name : (char *) "__ANON__";
c86bf373 7684 }
a0d0e21e
LW
7685 else {
7686 switch (SvTYPE(sv)) {
7687 case SVt_NULL:
7688 case SVt_IV:
7689 case SVt_NV:
7690 case SVt_RV:
7691 case SVt_PV:
7692 case SVt_PVIV:
7693 case SVt_PVNV:
7694 case SVt_PVMG:
7695 case SVt_PVBM:
7696 if (SvROK(sv))
7697 return "REF";
7698 else
7699 return "SCALAR";
ec6f298e 7700
206b424e 7701 case SVt_PVLV: return (char *) (SvROK(sv) ? "REF"
ac388100
JH
7702 /* tied lvalues should appear to be
7703 * scalars for backwards compatitbility */
7704 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
206b424e 7705 ? "SCALAR" : "LVALUE");
a0d0e21e
LW
7706 case SVt_PVAV: return "ARRAY";
7707 case SVt_PVHV: return "HASH";
7708 case SVt_PVCV: return "CODE";
7709 case SVt_PVGV: return "GLOB";
1d2dff63 7710 case SVt_PVFM: return "FORMAT";
27f9d8f3 7711 case SVt_PVIO: return "IO";
a0d0e21e
LW
7712 default: return "UNKNOWN";
7713 }
7714 }
7715}
7716
954c1994
GS
7717/*
7718=for apidoc sv_isobject
7719
7720Returns a boolean indicating whether the SV is an RV pointing to a blessed
7721object. If the SV is not an RV, or if the object is not blessed, then this
7722will return false.
7723
7724=cut
7725*/
7726
463ee0b2 7727int
864dbfa3 7728Perl_sv_isobject(pTHX_ SV *sv)
85e6fe83 7729{
68dc0745 7730 if (!sv)
7731 return 0;
7732 if (SvGMAGICAL(sv))
7733 mg_get(sv);
85e6fe83
LW
7734 if (!SvROK(sv))
7735 return 0;
7736 sv = (SV*)SvRV(sv);
7737 if (!SvOBJECT(sv))
7738 return 0;
7739 return 1;
7740}
7741
954c1994
GS
7742/*
7743=for apidoc sv_isa
7744
7745Returns a boolean indicating whether the SV is blessed into the specified
7746class. This does not check for subtypes; use C<sv_derived_from> to verify
7747an inheritance relationship.
7748
7749=cut
7750*/
7751
85e6fe83 7752int
864dbfa3 7753Perl_sv_isa(pTHX_ SV *sv, const char *name)
463ee0b2 7754{
26ab6a78 7755 const char *hvname;
68dc0745 7756 if (!sv)
7757 return 0;
7758 if (SvGMAGICAL(sv))
7759 mg_get(sv);
ed6116ce 7760 if (!SvROK(sv))
463ee0b2 7761 return 0;
ed6116ce
LW
7762 sv = (SV*)SvRV(sv);
7763 if (!SvOBJECT(sv))
463ee0b2 7764 return 0;
26ab6a78
NC
7765 hvname = HvNAME_get(SvSTASH(sv));
7766 if (!hvname)
3198e4e5 7767 return 0;
463ee0b2 7768
26ab6a78 7769 return strEQ(hvname, name);
463ee0b2
LW
7770}
7771
954c1994
GS
7772/*
7773=for apidoc newSVrv
7774
7775Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then
7776it will be upgraded to one. If C<classname> is non-null then the new SV will
7777be blessed in the specified package. The new SV is returned and its
7778reference count is 1.
7779
7780=cut
7781*/
7782
463ee0b2 7783SV*
864dbfa3 7784Perl_newSVrv(pTHX_ SV *rv, const char *classname)
463ee0b2 7785{
463ee0b2
LW
7786 SV *sv;
7787
4561caa4 7788 new_SV(sv);
51cf62d8 7789
2213622d 7790 SV_CHECK_THINKFIRST(rv);
51cf62d8 7791 SvAMAGIC_off(rv);
51cf62d8 7792
0199fce9 7793 if (SvTYPE(rv) >= SVt_PVMG) {
8c18bf38 7794 const U32 refcnt = SvREFCNT(rv);
0199fce9
JD
7795 SvREFCNT(rv) = 0;
7796 sv_clear(rv);
7797 SvFLAGS(rv) = 0;
7798 SvREFCNT(rv) = refcnt;
7799 }
7800
51cf62d8 7801 if (SvTYPE(rv) < SVt_RV)
0199fce9
JD
7802 sv_upgrade(rv, SVt_RV);
7803 else if (SvTYPE(rv) > SVt_RV) {
676a626c 7804 SvPV_free(rv);
0199fce9
JD
7805 SvCUR_set(rv, 0);
7806 SvLEN_set(rv, 0);
7807 }
51cf62d8 7808
7460c263 7809 SvOK_off(rv);
a8dc4fe8 7810 SvRV_set(rv, sv);
ed6116ce 7811 SvROK_on(rv);
463ee0b2 7812
a0d0e21e
LW
7813 if (classname) {
7814 HV* stash = gv_stashpv(classname, TRUE);
7815 (void)sv_bless(rv, stash);
7816 }
7817 return sv;
7818}
7819
954c1994
GS
7820/*
7821=for apidoc sv_setref_pv
7822
7823Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
7824argument will be upgraded to an RV. That RV will be modified to point to
7825the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed
7826into the SV. The C<classname> argument indicates the package for the
7827blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
89e79dea 7828will have a reference count of 1, and the RV will be returned.
954c1994
GS
7829
7830Do not use with other Perl types such as HV, AV, SV, CV, because those
7831objects will become corrupted by the pointer copy process.
7832
7833Note that C<sv_setref_pvn> copies the string while this copies the pointer.
7834
7835=cut
7836*/
7837
a0d0e21e 7838SV*
864dbfa3 7839Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv)
a0d0e21e 7840{
189b2af5 7841 if (!pv) {
3280af22 7842 sv_setsv(rv, &PL_sv_undef);
189b2af5
GS
7843 SvSETMAGIC(rv);
7844 }
a0d0e21e 7845 else
56431972 7846 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
a0d0e21e
LW
7847 return rv;
7848}
7849
954c1994
GS
7850/*
7851=for apidoc sv_setref_iv
7852
7853Copies an integer into a new SV, optionally blessing the SV. The C<rv>
7854argument will be upgraded to an RV. That RV will be modified to point to
7855the new SV. The C<classname> argument indicates the package for the
7856blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
89e79dea 7857will have a reference count of 1, and the RV will be returned.
954c1994
GS
7858
7859=cut
7860*/
7861
a0d0e21e 7862SV*
864dbfa3 7863Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv)
a0d0e21e
LW
7864{
7865 sv_setiv(newSVrv(rv,classname), iv);
7866 return rv;
7867}
7868
954c1994 7869/*
e1c57cef
JH
7870=for apidoc sv_setref_uv
7871
7872Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
7873argument will be upgraded to an RV. That RV will be modified to point to
7874the new SV. The C<classname> argument indicates the package for the
7875blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
89e79dea 7876will have a reference count of 1, and the RV will be returned.
e1c57cef
JH
7877
7878=cut
7879*/
7880
7881SV*
7882Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv)
7883{
7884 sv_setuv(newSVrv(rv,classname), uv);
7885 return rv;
7886}
7887
7888/*
954c1994
GS
7889=for apidoc sv_setref_nv
7890
7891Copies a double into a new SV, optionally blessing the SV. The C<rv>
7892argument will be upgraded to an RV. That RV will be modified to point to
7893the new SV. The C<classname> argument indicates the package for the
7894blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
89e79dea 7895will have a reference count of 1, and the RV will be returned.
954c1994
GS
7896
7897=cut
7898*/
7899
a0d0e21e 7900SV*
65202027 7901Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv)
a0d0e21e
LW
7902{
7903 sv_setnv(newSVrv(rv,classname), nv);
7904 return rv;
7905}
463ee0b2 7906
954c1994
GS
7907/*
7908=for apidoc sv_setref_pvn
7909
7910Copies a string into a new SV, optionally blessing the SV. The length of the
7911string must be specified with C<n>. The C<rv> argument will be upgraded to
7912an RV. That RV will be modified to point to the new SV. The C<classname>
7913argument indicates the package for the blessing. Set C<classname> to
89e79dea
JH
7914C<Nullch> to avoid the blessing. The new SV will have a reference count
7915of 1, and the RV will be returned.
954c1994
GS
7916
7917Note that C<sv_setref_pv> copies the pointer while this copies the string.
7918
7919=cut
7920*/
7921
a0d0e21e 7922SV*
864dbfa3 7923Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n)
a0d0e21e
LW
7924{
7925 sv_setpvn(newSVrv(rv,classname), pv, n);
463ee0b2
LW
7926 return rv;
7927}
7928
954c1994
GS
7929/*
7930=for apidoc sv_bless
7931
7932Blesses an SV into a specified package. The SV must be an RV. The package
7933must be designated by its stash (see C<gv_stashpv()>). The reference count
7934of the SV is unaffected.
7935
7936=cut
7937*/
7938
a0d0e21e 7939SV*
864dbfa3 7940Perl_sv_bless(pTHX_ SV *sv, HV *stash)
a0d0e21e 7941{
76e3520e 7942 SV *tmpRef;
a0d0e21e 7943 if (!SvROK(sv))
cea2e8a9 7944 Perl_croak(aTHX_ "Can't bless non-reference value");
76e3520e
GS
7945 tmpRef = SvRV(sv);
7946 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
7947 if (SvREADONLY(tmpRef))
cea2e8a9 7948 Perl_croak(aTHX_ PL_no_modify);
76e3520e
GS
7949 if (SvOBJECT(tmpRef)) {
7950 if (SvTYPE(tmpRef) != SVt_PVIO)
3280af22 7951 --PL_sv_objcount;
76e3520e 7952 SvREFCNT_dec(SvSTASH(tmpRef));
2e3febc6 7953 }
a0d0e21e 7954 }
76e3520e
GS
7955 SvOBJECT_on(tmpRef);
7956 if (SvTYPE(tmpRef) != SVt_PVIO)
3280af22 7957 ++PL_sv_objcount;
76e3520e 7958 (void)SvUPGRADE(tmpRef, SVt_PVMG);
a8dc4fe8 7959 SvSTASH_set(tmpRef, (HV*)SvREFCNT_inc(stash));
a0d0e21e 7960
2e3febc6
CS
7961 if (Gv_AMG(stash))
7962 SvAMAGIC_on(sv);
7963 else
7964 SvAMAGIC_off(sv);
a0d0e21e 7965
1edbfb88
AB
7966 if(SvSMAGICAL(tmpRef))
7967 if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
7968 mg_set(tmpRef);
7969
7970
ecdeb87c 7971
a0d0e21e
LW
7972 return sv;
7973}
7974
645c22ef 7975/* Downgrades a PVGV to a PVMG.
645c22ef
DM
7976 */
7977
76e3520e 7978STATIC void
cea2e8a9 7979S_sv_unglob(pTHX_ SV *sv)
a0d0e21e 7980{
850fabdf
GS
7981 void *xpvmg;
7982
a0d0e21e
LW
7983 assert(SvTYPE(sv) == SVt_PVGV);
7984 SvFAKE_off(sv);
7985 if (GvGP(sv))
1edc1566 7986 gp_free((GV*)sv);
e826b3c7
GS
7987 if (GvSTASH(sv)) {
7988 SvREFCNT_dec(GvSTASH(sv));
7989 GvSTASH(sv) = Nullhv;
7990 }
14befaf4 7991 sv_unmagic(sv, PERL_MAGIC_glob);
a0d0e21e 7992 Safefree(GvNAME(sv));
a5f75d66 7993 GvMULTI_off(sv);
850fabdf
GS
7994
7995 /* need to keep SvANY(sv) in the right arena */
7996 xpvmg = new_XPVMG();
7997 StructCopy(SvANY(sv), xpvmg, XPVMG);
7998 del_XPVGV(SvANY(sv));
7999 SvANY(sv) = xpvmg;
8000
a0d0e21e
LW
8001 SvFLAGS(sv) &= ~SVTYPEMASK;
8002 SvFLAGS(sv) |= SVt_PVMG;
8003}
8004
954c1994 8005/*
840a7b70 8006=for apidoc sv_unref_flags
954c1994
GS
8007
8008Unsets the RV status of the SV, and decrements the reference count of
8009whatever was being referenced by the RV. This can almost be thought of
840a7b70
IZ
8010as a reversal of C<newSVrv>. The C<cflags> argument can contain
8011C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
8012(otherwise the decrementing is conditional on the reference count being
8013different from one or the reference being a readonly SV).
7889fe52 8014See C<SvROK_off>.
954c1994
GS
8015
8016=cut
8017*/
8018
ed6116ce 8019void
840a7b70 8020Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags)
ed6116ce 8021{
a0d0e21e 8022 SV* rv = SvRV(sv);
810b8aa5
GS
8023
8024 if (SvWEAKREF(sv)) {
8025 sv_del_backref(sv);
8026 SvWEAKREF_off(sv);
a8dc4fe8 8027 SvRV_set(sv, NULL);
810b8aa5
GS
8028 return;
8029 }
a8dc4fe8 8030 SvRV_set(sv, NULL);
ed6116ce 8031 SvROK_off(sv);
5e8f8cda
JH
8032 /* You can't have a || SvREADONLY(rv) here, as $a = $$a, where $a was
8033 assigned to as BEGIN {$a = \"Foo"} will fail. */
8034 if (SvREFCNT(rv) != 1 || (flags & SV_IMMEDIATE_UNREF))
4633a7c4 8035 SvREFCNT_dec(rv);
840a7b70 8036 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
4633a7c4 8037 sv_2mortal(rv); /* Schedule for freeing later */
ed6116ce 8038}
8990e307 8039
840a7b70
IZ
8040/*
8041=for apidoc sv_unref
8042
8043Unsets the RV status of the SV, and decrements the reference count of
8044whatever was being referenced by the RV. This can almost be thought of
8045as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag>
7889fe52 8046being zero. See C<SvROK_off>.
840a7b70
IZ
8047
8048=cut
8049*/
8050
8051void
8052Perl_sv_unref(pTHX_ SV *sv)
8053{
8054 sv_unref_flags(sv, 0);
8055}
8056
645c22ef
DM
8057/*
8058=for apidoc sv_taint
8059
8060Taint an SV. Use C<SvTAINTED_on> instead.
8061=cut
8062*/
8063
bbce6d69 8064void
864dbfa3 8065Perl_sv_taint(pTHX_ SV *sv)
bbce6d69 8066{
14befaf4 8067 sv_magic((sv), Nullsv, PERL_MAGIC_taint, Nullch, 0);
bbce6d69 8068}
8069
645c22ef
DM
8070/*
8071=for apidoc sv_untaint
8072
8073Untaint an SV. Use C<SvTAINTED_off> instead.
8074=cut
8075*/
8076
bbce6d69 8077void
864dbfa3 8078Perl_sv_untaint(pTHX_ SV *sv)
bbce6d69 8079{
13f57bf8 8080 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
14befaf4 8081 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
36477c24 8082 if (mg)
565764a8 8083 mg->mg_len &= ~1;
36477c24 8084 }
bbce6d69 8085}
8086
645c22ef
DM
8087/*
8088=for apidoc sv_tainted
8089
8090Test an SV for taintedness. Use C<SvTAINTED> instead.
8091=cut
8092*/
8093
bbce6d69 8094bool
864dbfa3 8095Perl_sv_tainted(pTHX_ SV *sv)
bbce6d69 8096{
13f57bf8 8097 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
14befaf4 8098 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
155aba94 8099 if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv)))
36477c24 8100 return TRUE;
8101 }
8102 return FALSE;
bbce6d69 8103}
8104
d34f9d2e
JH
8105/*
8106=for apidoc sv_setpviv
8107
8108Copies an integer into the given SV, also updating its string value.
8109Does not handle 'set' magic. See C<sv_setpviv_mg>.
8110
8111=cut
8112*/
8113
8114void
8115Perl_sv_setpviv(pTHX_ SV *sv, IV iv)
8116{
8117 char buf[TYPE_CHARS(UV)];
8118 char *ebuf;
8119 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8120
8121 sv_setpvn(sv, ptr, ebuf - ptr);
8122}
8123
8124/*
8125=for apidoc sv_setpviv_mg
8126
8127Like C<sv_setpviv>, but also handles 'set' magic.
8128
8129=cut
8130*/
8131
8132void
8133Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv)
8134{
8135 char buf[TYPE_CHARS(UV)];
8136 char *ebuf;
8137 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8138
8139 sv_setpvn(sv, ptr, ebuf - ptr);
8140 SvSETMAGIC(sv);
8141}
8142
cea2e8a9 8143#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
8144
8145/* pTHX_ magic can't cope with varargs, so this is a no-context
8146 * version of the main function, (which may itself be aliased to us).
8147 * Don't access this version directly.
8148 */
8149
cea2e8a9
GS
8150void
8151Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...)
8152{
8153 dTHX;
8154 va_list args;
8155 va_start(args, pat);
c5be433b 8156 sv_vsetpvf(sv, pat, &args);
cea2e8a9
GS
8157 va_end(args);
8158}
8159
645c22ef
DM
8160/* pTHX_ magic can't cope with varargs, so this is a no-context
8161 * version of the main function, (which may itself be aliased to us).
8162 * Don't access this version directly.
8163 */
cea2e8a9
GS
8164
8165void
8166Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...)
8167{
8168 dTHX;
8169 va_list args;
8170 va_start(args, pat);
c5be433b 8171 sv_vsetpvf_mg(sv, pat, &args);
cea2e8a9 8172 va_end(args);
cea2e8a9
GS
8173}
8174#endif
8175
954c1994
GS
8176/*
8177=for apidoc sv_setpvf
8178
c4a661a8
NC
8179Works like C<sv_catpvf> but copies the text into the SV instead of
8180appending it. Does not handle 'set' magic. See C<sv_setpvf_mg>.
954c1994
GS
8181
8182=cut
8183*/
8184
46fc3d4c 8185void
864dbfa3 8186Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...)
46fc3d4c 8187{
8188 va_list args;
46fc3d4c 8189 va_start(args, pat);
c5be433b 8190 sv_vsetpvf(sv, pat, &args);
46fc3d4c 8191 va_end(args);
8192}
8193
c4a661a8
NC
8194/*
8195=for apidoc sv_vsetpvf
8196
8197Works like C<sv_vcatpvf> but copies the text into the SV instead of
8198appending it. Does not handle 'set' magic. See C<sv_vsetpvf_mg>.
8199
8200Usually used via its frontend C<sv_setpvf>.
8201
8202=cut
8203*/
645c22ef 8204
c5be433b
GS
8205void
8206Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args)
8207{
8208 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8209}
ef50df4b 8210
954c1994
GS
8211/*
8212=for apidoc sv_setpvf_mg
8213
8214Like C<sv_setpvf>, but also handles 'set' magic.
8215
8216=cut
8217*/
8218
ef50df4b 8219void
864dbfa3 8220Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...)
ef50df4b
GS
8221{
8222 va_list args;
ef50df4b 8223 va_start(args, pat);
c5be433b 8224 sv_vsetpvf_mg(sv, pat, &args);
ef50df4b 8225 va_end(args);
c5be433b
GS
8226}
8227
c4a661a8
NC
8228/*
8229=for apidoc sv_vsetpvf_mg
8230
8231Like C<sv_vsetpvf>, but also handles 'set' magic.
8232
8233Usually used via its frontend C<sv_setpvf_mg>.
8234
8235=cut
8236*/
645c22ef 8237
c5be433b
GS
8238void
8239Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
8240{
8241 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
ef50df4b
GS
8242 SvSETMAGIC(sv);
8243}
8244
cea2e8a9 8245#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
8246
8247/* pTHX_ magic can't cope with varargs, so this is a no-context
8248 * version of the main function, (which may itself be aliased to us).
8249 * Don't access this version directly.
8250 */
8251
cea2e8a9
GS
8252void
8253Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...)
8254{
8255 dTHX;
8256 va_list args;
8257 va_start(args, pat);
c5be433b 8258 sv_vcatpvf(sv, pat, &args);
cea2e8a9
GS
8259 va_end(args);
8260}
8261
645c22ef
DM
8262/* pTHX_ magic can't cope with varargs, so this is a no-context
8263 * version of the main function, (which may itself be aliased to us).
8264 * Don't access this version directly.
8265 */
8266
cea2e8a9
GS
8267void
8268Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...)
8269{
8270 dTHX;
8271 va_list args;
8272 va_start(args, pat);
c5be433b 8273 sv_vcatpvf_mg(sv, pat, &args);
cea2e8a9 8274 va_end(args);
cea2e8a9
GS
8275}
8276#endif
8277
954c1994
GS
8278/*
8279=for apidoc sv_catpvf
8280
d5ce4a7c
GA
8281Processes its arguments like C<sprintf> and appends the formatted
8282output to an SV. If the appended data contains "wide" characters
8283(including, but not limited to, SVs with a UTF-8 PV formatted with %s,
8284and characters >255 formatted with %c), the original SV might get
c4a661a8 8285upgraded to UTF-8. Handles 'get' magic, but not 'set' magic. See
a8e989f8
RB
8286C<sv_catpvf_mg>. If the original SV was UTF-8, the pattern should be
8287valid UTF-8; if the original SV was bytes, the pattern should be too.
954c1994 8288
d5ce4a7c 8289=cut */
954c1994 8290
46fc3d4c 8291void
864dbfa3 8292Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...)
46fc3d4c 8293{
8294 va_list args;
46fc3d4c 8295 va_start(args, pat);
c5be433b 8296 sv_vcatpvf(sv, pat, &args);
46fc3d4c 8297 va_end(args);
8298}
8299
c4a661a8
NC
8300/*
8301=for apidoc sv_vcatpvf
8302
8303Processes its arguments like C<vsprintf> and appends the formatted output
8304to an SV. Does not handle 'set' magic. See C<sv_vcatpvf_mg>.
8305
8306Usually used via its frontend C<sv_catpvf>.
8307
8308=cut
8309*/
645c22ef 8310
ef50df4b 8311void
c5be433b
GS
8312Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args)
8313{
8314 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8315}
8316
954c1994
GS
8317/*
8318=for apidoc sv_catpvf_mg
8319
8320Like C<sv_catpvf>, but also handles 'set' magic.
8321
8322=cut
8323*/
8324
c5be433b 8325void
864dbfa3 8326Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...)
ef50df4b
GS
8327{
8328 va_list args;
ef50df4b 8329 va_start(args, pat);
c5be433b 8330 sv_vcatpvf_mg(sv, pat, &args);
ef50df4b 8331 va_end(args);
c5be433b
GS
8332}
8333
c4a661a8
NC
8334/*
8335=for apidoc sv_vcatpvf_mg
8336
8337Like C<sv_vcatpvf>, but also handles 'set' magic.
8338
8339Usually used via its frontend C<sv_catpvf_mg>.
8340
8341=cut
8342*/
645c22ef 8343
c5be433b
GS
8344void
8345Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
8346{
8347 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
ef50df4b
GS
8348 SvSETMAGIC(sv);
8349}
8350
954c1994
GS
8351/*
8352=for apidoc sv_vsetpvfn
8353
c4a661a8 8354Works like C<sv_vcatpvfn> but copies the text into the SV instead of
954c1994
GS
8355appending it.
8356
c4a661a8 8357Usually used via one of its frontends C<sv_vsetpvf> and C<sv_vsetpvf_mg>.
645c22ef 8358
954c1994
GS
8359=cut
8360*/
8361
46fc3d4c 8362void
7d5ea4e7 8363Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
46fc3d4c 8364{
8365 sv_setpvn(sv, "", 0);
7d5ea4e7 8366 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted);
46fc3d4c 8367}
8368
645c22ef
DM
8369/* private function for use in sv_vcatpvfn via the EXPECT_NUMBER macro */
8370
2d00ba3b 8371STATIC I32
9dd79c3f 8372S_expect_number(pTHX_ char** pattern)
211dfcf1
HS
8373{
8374 I32 var = 0;
8375 switch (**pattern) {
8376 case '1': case '2': case '3':
8377 case '4': case '5': case '6':
8378 case '7': case '8': case '9':
8379 while (isDIGIT(**pattern))
8380 var = var * 10 + (*(*pattern)++ - '0');
8381 }
8382 return var;
8383}
9dd79c3f 8384#define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern))
211dfcf1 8385
48703b5e
NC
8386static char *
8387F0convert(NV nv, char *endbuf, STRLEN *len)
8388{
8c18bf38 8389 const int neg = nv < 0;
48703b5e
NC
8390 UV uv;
8391 char *p = endbuf;
8392
8393 if (neg)
8394 nv = -nv;
8395 if (nv < UV_MAX) {
8396 nv += 0.5;
1ed1e641 8397 uv = (UV)nv;
48703b5e
NC
8398 if (uv & 1 && uv == nv)
8399 uv--; /* Round to even */
8400 do {
8c18bf38 8401 const unsigned dig = uv % 10;
48703b5e
NC
8402 *--p = '0' + dig;
8403 } while (uv /= 10);
8404 if (neg)
8405 *--p = '-';
8406 *len = endbuf - p;
8407 return p;
8408 }
8409 return Nullch;
8410}
8411
8412
954c1994
GS
8413/*
8414=for apidoc sv_vcatpvfn
8415
8416Processes its arguments like C<vsprintf> and appends the formatted output
8417to an SV. Uses an array of SVs if the C style variable argument list is
8418missing (NULL). When running with taint checks enabled, indicates via
8419C<maybe_tainted> if results are untrustworthy (often due to the use of
8420locales).
8421
e2b56717
AL
8422XXX Except that it maybe_tainted is never assigned to.
8423
c4a661a8 8424Usually used via one of its frontends C<sv_vcatpvf> and C<sv_vcatpvf_mg>.
645c22ef 8425
954c1994
GS
8426=cut
8427*/
8428
0698efa8
NC
8429/* XXX maybe_tainted is never assigned to, so the doc above is lying. */
8430
46fc3d4c 8431void
7d5ea4e7 8432Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
46fc3d4c 8433{
8434 char *p;
8435 char *q;
8c18bf38 8436 const char *patend;
fc36a67e 8437 STRLEN origlen;
46fc3d4c 8438 I32 svix = 0;
c635e13b 8439 static char nullstr[] = "(null)";
9c5ffd7c 8440 SV *argsv = Nullsv;
5835a535
JH
8441 bool has_utf8; /* has the result utf8? */
8442 bool pat_utf8; /* the pattern is in utf8? */
8443 SV *nsv = Nullsv;
48703b5e
NC
8444 /* Times 4: a decimal digit takes more than 3 binary digits.
8445 * NV_DIG: mantissa takes than many decimal digits.
8446 * Plus 32: Playing safe. */
8447 char ebuf[IV_DIG * 4 + NV_DIG + 32];
8448 /* large enough for "%#.#f" --chip */
8449 /* what about long double NVs? --jhi */
5835a535
JH
8450
8451 has_utf8 = pat_utf8 = DO_UTF8(sv);
46fc3d4c 8452
8453 /* no matter what, this is a string now */
fc36a67e 8454 (void)SvPV_force(sv, origlen);
46fc3d4c 8455
fc36a67e 8456 /* special-case "", "%s", and "%_" */
46fc3d4c 8457 if (patlen == 0)
8458 return;
fc36a67e 8459 if (patlen == 2 && pat[0] == '%') {
8460 switch (pat[1]) {
8461 case 's':
c635e13b 8462 if (args) {
e2b56717 8463 const char *s = va_arg(*args, char*);
c635e13b 8464 sv_catpv(sv, s ? s : nullstr);
8465 }
7e2040f0 8466 else if (svix < svmax) {
fc36a67e 8467 sv_catsv(sv, *svargs);
7e2040f0
GS
8468 if (DO_UTF8(*svargs))
8469 SvUTF8_on(sv);
8470 }
fc36a67e 8471 return;
8472 case '_':
8473 if (args) {
7e2040f0
GS
8474 argsv = va_arg(*args, SV*);
8475 sv_catsv(sv, argsv);
8476 if (DO_UTF8(argsv))
8477 SvUTF8_on(sv);
fc36a67e 8478 return;
8479 }
8480 /* See comment on '_' below */
8481 break;
8482 }
46fc3d4c 8483 }
8484
1621ce87 8485#ifndef USE_LONG_DOUBLE
48703b5e
NC
8486 /* special-case "%.<number>[gf]" */
8487 if ( patlen <= 5 && pat[0] == '%' && pat[1] == '.'
8488 && (pat[patlen-1] == 'g' || pat[patlen-1] == 'f') ) {
8489 unsigned digits = 0;
8490 const char *pp;
8491
8492 pp = pat + 2;
8493 while (*pp >= '0' && *pp <= '9')
8494 digits = 10 * digits + (*pp++ - '0');
1ed1e641 8495 if (pp - pat == (int)patlen - 1) {
48703b5e
NC
8496 NV nv;
8497
8498 if (args)
8499 nv = (NV)va_arg(*args, double);
8500 else if (svix < svmax)
8501 nv = SvNV(*svargs);
8502 else
8503 return;
8504 if (*pp == 'g') {
5f658b8d
NC
8505 /* Add check for digits != 0 because it seems that some
8506 gconverts are buggy in this case, and we don't yet have
8507 a Configure test for this. */
8508 if (digits && digits < sizeof(ebuf) - NV_DIG - 10) {
8509 /* 0, point, slack */
1621ce87 8510 Gconvert(nv, (int)digits, 0, ebuf);
48703b5e
NC
8511 sv_catpv(sv, ebuf);
8512 if (*ebuf) /* May return an empty string for digits==0 */
8513 return;
8514 }
8515 } else if (!digits) {
8516 STRLEN l;
8517
8518 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
8519 sv_catpvn(sv, p, l);
8520 return;
8521 }
8522 }
8523 }
8524 }
1621ce87 8525#endif /* !USE_LONG_DOUBLE */
48703b5e 8526
2cf2cfc6 8527 if (!args && svix < svmax && DO_UTF8(*svargs))
5b7ea690 8528 has_utf8 = TRUE;
2cf2cfc6 8529
46fc3d4c 8530 patend = (char*)pat + patlen;
8531 for (p = (char*)pat; p < patend; p = q) {
8532 bool alt = FALSE;
8533 bool left = FALSE;
b22c7a20 8534 bool vectorize = FALSE;
211dfcf1 8535 bool vectorarg = FALSE;
2cf2cfc6 8536 bool vec_utf8 = FALSE;
46fc3d4c 8537 char fill = ' ';
8538 char plus = 0;
8539 char intsize = 0;
8540 STRLEN width = 0;
fc36a67e 8541 STRLEN zeros = 0;
46fc3d4c 8542 bool has_precis = FALSE;
8543 STRLEN precis = 0;
d34f9d2e 8544 I32 osvix = svix;
2cf2cfc6 8545 bool is_utf8 = FALSE; /* is this item utf8? */
5b7ea690
JH
8546#ifdef HAS_LDBL_SPRINTF_BUG
8547 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
8548 with sfio - Allen <allens@cpan.org> */
8549 bool fix_ldbl_sprintf_bug = FALSE;
8550#endif
8551
46fc3d4c 8552 char esignbuf[4];
a2a469f9 8553 U8 utf8buf[UTF8_MAXBYTES+1];
46fc3d4c 8554 STRLEN esignlen = 0;
8555
8556 char *eptr = Nullch;
fc36a67e 8557 STRLEN elen = 0;
81f715da 8558 SV *vecsv = Nullsv;
a05b299f 8559 U8 *vecstr = Null(U8*);
b22c7a20 8560 STRLEN veclen = 0;
934abaf1 8561 char c = 0;
46fc3d4c 8562 int i;
9c5ffd7c 8563 unsigned base = 0;
8c8eb53c
RB
8564 IV iv = 0;
8565 UV uv = 0;
9e5b023a
JH
8566 /* we need a long double target in case HAS_LONG_DOUBLE but
8567 not USE_LONG_DOUBLE
8568 */
35fff930 8569#if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE
9e5b023a
JH
8570 long double nv;
8571#else
65202027 8572 NV nv;
9e5b023a 8573#endif
46fc3d4c 8574 STRLEN have;
8575 STRLEN need;
8576 STRLEN gap;
c05e0e2f 8577 const char *dotstr = ".";
b22c7a20 8578 STRLEN dotstrlen = 1;
211dfcf1 8579 I32 efix = 0; /* explicit format parameter index */
eb3fce90 8580 I32 ewix = 0; /* explicit width index */
211dfcf1
HS
8581 I32 epix = 0; /* explicit precision index */
8582 I32 evix = 0; /* explicit vector index */
eb3fce90 8583 bool asterisk = FALSE;
46fc3d4c 8584
211dfcf1 8585 /* echo everything up to the next format specification */
46fc3d4c 8586 for (q = p; q < patend && *q != '%'; ++q) ;
8587 if (q > p) {
5835a535
JH
8588 if (has_utf8 && !pat_utf8)
8589 sv_catpvn_utf8_upgrade(sv, p, q - p, nsv);
8590 else
8591 sv_catpvn(sv, p, q - p);
46fc3d4c 8592 p = q;
8593 }
8594 if (q++ >= patend)
8595 break;
8596
211dfcf1
HS
8597/*
8598 We allow format specification elements in this order:
8599 \d+\$ explicit format parameter index
8600 [-+ 0#]+ flags
a472f209 8601 v|\*(\d+\$)?v vector with optional (optionally specified) arg
f8cf5370 8602 0 flag (as above): repeated to allow "v02"
211dfcf1
HS
8603 \d+|\*(\d+\$)? width using optional (optionally specified) arg
8604 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
8605 [hlqLV] size
8606 [%bcdefginopsux_DFOUX] format (mandatory)
8607*/
8608 if (EXPECT_NUMBER(q, width)) {
8609 if (*q == '$') {
8610 ++q;
8611 efix = width;
8612 } else {
8613 goto gotwidth;
8614 }
8615 }
8616
fc36a67e 8617 /* FLAGS */
8618
46fc3d4c 8619 while (*q) {
8620 switch (*q) {
8621 case ' ':
8622 case '+':
8623 plus = *q++;
8624 continue;
8625
8626 case '-':
8627 left = TRUE;
8628 q++;
8629 continue;
8630
8631 case '0':
8632 fill = *q++;
8633 continue;
8634
8635 case '#':
8636 alt = TRUE;
8637 q++;
8638 continue;
8639
fc36a67e 8640 default:
8641 break;
8642 }
8643 break;
8644 }
46fc3d4c 8645
211dfcf1 8646 tryasterisk:
eb3fce90 8647 if (*q == '*') {
211dfcf1
HS
8648 q++;
8649 if (EXPECT_NUMBER(q, ewix))
8650 if (*q++ != '$')
8651 goto unknown;
eb3fce90 8652 asterisk = TRUE;
211dfcf1
HS
8653 }
8654 if (*q == 'v') {
eb3fce90 8655 q++;
211dfcf1
HS
8656 if (vectorize)
8657 goto unknown;
9cbac4c7 8658 if ((vectorarg = asterisk)) {
211dfcf1
HS
8659 evix = ewix;
8660 ewix = 0;
8661 asterisk = FALSE;
8662 }
8663 vectorize = TRUE;
8664 goto tryasterisk;
eb3fce90
JH
8665 }
8666
211dfcf1 8667 if (!asterisk)
f8cf5370
JH
8668 if( *q == '0' )
8669 fill = *q++;
211dfcf1
HS
8670 EXPECT_NUMBER(q, width);
8671
9a2a392e
RB
8672#ifdef CHECK_FORMAT
8673 if ((*q == 'p') && left) {
8674 vectorize = (width == 1);
8675 }
8676#endif
211dfcf1
HS
8677 if (vectorize) {
8678 if (vectorarg) {
8679 if (args)
8680 vecsv = va_arg(*args, SV*);
8681 else
8682 vecsv = (evix ? evix <= svmax : svix < svmax) ?
219ef941 8683 svargs[evix ? evix-1 : svix++] : &PL_sv_undef;
4459522c 8684 dotstr = SvPVx(vecsv, dotstrlen);
211dfcf1 8685 if (DO_UTF8(vecsv))
2cf2cfc6 8686 is_utf8 = TRUE;
211dfcf1
HS
8687 }
8688 if (args) {
8689 vecsv = va_arg(*args, SV*);
8690 vecstr = (U8*)SvPVx(vecsv,veclen);
2cf2cfc6 8691 vec_utf8 = DO_UTF8(vecsv);
eb3fce90 8692 }
211dfcf1
HS
8693 else if (efix ? efix <= svmax : svix < svmax) {
8694 vecsv = svargs[efix ? efix-1 : svix++];
8695 vecstr = (U8*)SvPVx(vecsv,veclen);
2cf2cfc6 8696 vec_utf8 = DO_UTF8(vecsv);
211dfcf1
HS
8697 }
8698 else {
8699 vecstr = (U8*)"";
8700 veclen = 0;
8701 }
eb3fce90 8702 }
fc36a67e 8703
eb3fce90 8704 if (asterisk) {
fc36a67e 8705 if (args)
8706 i = va_arg(*args, int);
8707 else
eb3fce90
JH
8708 i = (ewix ? ewix <= svmax : svix < svmax) ?
8709 SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
fc36a67e 8710 left |= (i < 0);
8711 width = (i < 0) ? -i : i;
fc36a67e 8712 }
211dfcf1 8713 gotwidth:
fc36a67e 8714
8715 /* PRECISION */
46fc3d4c 8716
fc36a67e 8717 if (*q == '.') {
8718 q++;
8719 if (*q == '*') {
211dfcf1 8720 q++;
7b8dd722
HS
8721 if (EXPECT_NUMBER(q, epix) && *q++ != '$')
8722 goto unknown;
8723 /* XXX: todo, support specified precision parameter */
8724 if (epix)
211dfcf1 8725 goto unknown;
46fc3d4c 8726 if (args)
8727 i = va_arg(*args, int);
8728 else
eb3fce90
JH
8729 i = (ewix ? ewix <= svmax : svix < svmax)
8730 ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
fc36a67e 8731 precis = (i < 0) ? 0 : i;
fc36a67e 8732 }
8733 else {
8734 precis = 0;
8735 while (isDIGIT(*q))
8736 precis = precis * 10 + (*q++ - '0');
8737 }
8738 has_precis = TRUE;
8739 }
46fc3d4c 8740
fc36a67e 8741 /* SIZE */
46fc3d4c 8742
fc36a67e 8743 switch (*q) {
c623ac67
GS
8744#ifdef WIN32
8745 case 'I': /* Ix, I32x, and I64x */
8746# ifdef WIN64
8747 if (q[1] == '6' && q[2] == '4') {
8748 q += 3;
8749 intsize = 'q';
8750 break;
8751 }
8752# endif
8753 if (q[1] == '3' && q[2] == '2') {
8754 q += 3;
8755 break;
8756 }
8757# ifdef WIN64
8758 intsize = 'q';
8759# endif
8760 q++;
8761 break;
8762#endif
9e5b023a 8763#if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
6f9bb7fd 8764 case 'L': /* Ld */
e5c81feb 8765 /* FALL THROUGH */
e5c81feb 8766#ifdef HAS_QUAD
6f9bb7fd 8767 case 'q': /* qd */
9e5b023a 8768#endif
6f9bb7fd
GS
8769 intsize = 'q';
8770 q++;
8771 break;
8772#endif
fc36a67e 8773 case 'l':
9e5b023a 8774#if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
5b7ea690 8775 if (*(q + 1) == 'l') { /* lld, llf */
fc36a67e 8776 intsize = 'q';
8777 q += 2;
46fc3d4c 8778 break;
cf2093f6 8779 }
fc36a67e 8780#endif
6f9bb7fd 8781 /* FALL THROUGH */
fc36a67e 8782 case 'h':
cf2093f6 8783 /* FALL THROUGH */
fc36a67e 8784 case 'V':
8785 intsize = *q++;
46fc3d4c 8786 break;
8787 }
8788
fc36a67e 8789 /* CONVERSION */
8790
211dfcf1
HS
8791 if (*q == '%') {
8792 eptr = q++;
8793 elen = 1;
8794 goto string;
8795 }
8796
be75b157
HS
8797 if (vectorize)
8798 argsv = vecsv;
8799 else if (!args)
211dfcf1
HS
8800 argsv = (efix ? efix <= svmax : svix < svmax) ?
8801 svargs[efix ? efix-1 : svix++] : &PL_sv_undef;
8802
46fc3d4c 8803 switch (c = *q++) {
8804
8805 /* STRINGS */
8806
46fc3d4c 8807 case 'c':
be75b157 8808 uv = (args && !vectorize) ? va_arg(*args, int) : SvIVx(argsv);
1bd104fb
JH
8809 if ((uv > 255 ||
8810 (!UNI_IS_INVARIANT(uv) && SvUTF8(sv)))
0064a8a9 8811 && !IN_BYTES) {
dfe13c55 8812 eptr = (char*)utf8buf;
9041c2e3 8813 elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf;
2cf2cfc6 8814 is_utf8 = TRUE;
7e2040f0
GS
8815 }
8816 else {
8817 c = (char)uv;
8818 eptr = &c;
8819 elen = 1;
a0ed51b3 8820 }
46fc3d4c 8821 goto string;
8822
46fc3d4c 8823 case 's':
be75b157 8824 if (args && !vectorize) {
fc36a67e 8825 eptr = va_arg(*args, char*);
c635e13b 8826 if (eptr)
1d7c1841
GS
8827#ifdef MACOS_TRADITIONAL
8828 /* On MacOS, %#s format is used for Pascal strings */
8829 if (alt)
8830 elen = *eptr++;
8831 else
8832#endif
c635e13b 8833 elen = strlen(eptr);
8834 else {
8835 eptr = nullstr;
8836 elen = sizeof nullstr - 1;
8837 }
46fc3d4c 8838 }
211dfcf1 8839 else {
7e2040f0
GS
8840 eptr = SvPVx(argsv, elen);
8841 if (DO_UTF8(argsv)) {
a0ed51b3
LW
8842 if (has_precis && precis < elen) {
8843 I32 p = precis;
7e2040f0 8844 sv_pos_u2b(argsv, &p, 0); /* sticks at end */
a0ed51b3
LW
8845 precis = p;
8846 }
8847 if (width) { /* fudge width (can't fudge elen) */
7e2040f0 8848 width += elen - sv_len_utf8(argsv);
a0ed51b3 8849 }
2cf2cfc6 8850 is_utf8 = TRUE;
a0ed51b3
LW
8851 }
8852 }
46fc3d4c 8853 goto string;
8854
fc36a67e 8855 case '_':
9982c483
RB
8856#ifdef CHECK_FORMAT
8857 format_sv:
8858#endif
fc36a67e 8859 /*
8860 * The "%_" hack might have to be changed someday,
8861 * if ISO or ANSI decide to use '_' for something.
8862 * So we keep it hidden from users' code.
8863 */
be75b157 8864 if (!args || vectorize)
fc36a67e 8865 goto unknown;
211dfcf1 8866 argsv = va_arg(*args, SV*);
7e2040f0
GS
8867 eptr = SvPVx(argsv, elen);
8868 if (DO_UTF8(argsv))
2cf2cfc6 8869 is_utf8 = TRUE;
fc36a67e 8870
46fc3d4c 8871 string:
b22c7a20 8872 vectorize = FALSE;
46fc3d4c 8873 if (has_precis && elen > precis)
8874 elen = precis;
8875 break;
8876
8877 /* INTEGERS */
8878
fc36a67e 8879 case 'p':
9982c483
RB
8880#ifdef CHECK_FORMAT
8881 if (left) {
8882 left = FALSE;
8883 if (!width)
8884 goto format_sv; /* %-p -> %_ */
9a2a392e
RB
8885 if (vectorize) {
8886 width = 0;
8887 goto format_vd; /* %-1p -> %vd */
8888 }
9982c483
RB
8889 precis = width;
8890 has_precis = TRUE;
8891 width = 0;
8892 goto format_sv; /* %-Np -> %.N_ */
8893 }
8894#endif
be75b157 8895 if (alt || vectorize)
c2e66d9e 8896 goto unknown;
211dfcf1 8897 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
fc36a67e 8898 base = 16;
8899 goto integer;
8900
46fc3d4c 8901 case 'D':
29fe7a80 8902#ifdef IV_IS_QUAD
22f3ae8c 8903 intsize = 'q';
29fe7a80 8904#else
46fc3d4c 8905 intsize = 'l';
29fe7a80 8906#endif
46fc3d4c 8907 /* FALL THROUGH */
8908 case 'd':
8909 case 'i':
9a2a392e
RB
8910#ifdef CHECK_FORMAT
8911 format_vd:
8912#endif
b22c7a20 8913 if (vectorize) {
ba210ebe 8914 STRLEN ulen;
211dfcf1
HS
8915 if (!veclen)
8916 continue;
2cf2cfc6
A
8917 if (vec_utf8)
8918 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
8919 UTF8_ALLOW_ANYUV);
b22c7a20 8920 else {
e83d50c9 8921 uv = *vecstr;
b22c7a20
GS
8922 ulen = 1;
8923 }
8924 vecstr += ulen;
8925 veclen -= ulen;
e83d50c9
JP
8926 if (plus)
8927 esignbuf[esignlen++] = plus;
b22c7a20
GS
8928 }
8929 else if (args) {
46fc3d4c 8930 switch (intsize) {
8931 case 'h': iv = (short)va_arg(*args, int); break;
46fc3d4c 8932 case 'l': iv = va_arg(*args, long); break;
fc36a67e 8933 case 'V': iv = va_arg(*args, IV); break;
1ed1e641 8934 default: iv = va_arg(*args, int); break;
cf2093f6
JH
8935#ifdef HAS_QUAD
8936 case 'q': iv = va_arg(*args, Quad_t); break;
8937#endif
46fc3d4c 8938 }
8939 }
8940 else {
1ed1e641 8941 IV tiv = SvIVx(argsv); /* work around GCC bug #13488 */
46fc3d4c 8942 switch (intsize) {
1ed1e641
MHM
8943 case 'h': iv = (short)tiv; break;
8944 case 'l': iv = (long)tiv; break;
8945 case 'V':
8946 default: iv = tiv; break;
cf2093f6 8947#ifdef HAS_QUAD
1ed1e641 8948 case 'q': iv = (Quad_t)tiv; break;
cf2093f6 8949#endif
46fc3d4c 8950 }
8951 }
e83d50c9
JP
8952 if ( !vectorize ) /* we already set uv above */
8953 {
8954 if (iv >= 0) {
8955 uv = iv;
8956 if (plus)
8957 esignbuf[esignlen++] = plus;
8958 }
8959 else {
8960 uv = -iv;
8961 esignbuf[esignlen++] = '-';
8962 }
46fc3d4c 8963 }
8964 base = 10;
8965 goto integer;
8966
fc36a67e 8967 case 'U':
29fe7a80 8968#ifdef IV_IS_QUAD
22f3ae8c 8969 intsize = 'q';
29fe7a80 8970#else
fc36a67e 8971 intsize = 'l';
29fe7a80 8972#endif
fc36a67e 8973 /* FALL THROUGH */
8974 case 'u':
8975 base = 10;
8976 goto uns_integer;
8977
4f19785b
WSI
8978 case 'b':
8979 base = 2;
8980 goto uns_integer;
8981
46fc3d4c 8982 case 'O':
29fe7a80 8983#ifdef IV_IS_QUAD
22f3ae8c 8984 intsize = 'q';
29fe7a80 8985#else
46fc3d4c 8986 intsize = 'l';
29fe7a80 8987#endif
46fc3d4c 8988 /* FALL THROUGH */
8989 case 'o':
8990 base = 8;
8991 goto uns_integer;
8992
8993 case 'X':
46fc3d4c 8994 case 'x':
8995 base = 16;
46fc3d4c 8996
8997 uns_integer:
b22c7a20 8998 if (vectorize) {
ba210ebe 8999 STRLEN ulen;
b22c7a20 9000 vector:
211dfcf1
HS
9001 if (!veclen)
9002 continue;
2cf2cfc6
A
9003 if (vec_utf8)
9004 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
9005 UTF8_ALLOW_ANYUV);
b22c7a20 9006 else {
a05b299f 9007 uv = *vecstr;
b22c7a20
GS
9008 ulen = 1;
9009 }
9010 vecstr += ulen;
9011 veclen -= ulen;
9012 }
9013 else if (args) {
46fc3d4c 9014 switch (intsize) {
9015 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
46fc3d4c 9016 case 'l': uv = va_arg(*args, unsigned long); break;
fc36a67e 9017 case 'V': uv = va_arg(*args, UV); break;
1ed1e641 9018 default: uv = va_arg(*args, unsigned); break;
cf2093f6 9019#ifdef HAS_QUAD
1ed1e641 9020 case 'q': uv = va_arg(*args, Uquad_t); break;
cf2093f6 9021#endif
46fc3d4c 9022 }
9023 }
9024 else {
1ed1e641 9025 UV tuv = SvUVx(argsv); /* work around GCC bug #13488 */
46fc3d4c 9026 switch (intsize) {
1ed1e641
MHM
9027 case 'h': uv = (unsigned short)tuv; break;
9028 case 'l': uv = (unsigned long)tuv; break;
9029 case 'V':
9030 default: uv = tuv; break;
cf2093f6 9031#ifdef HAS_QUAD
1ed1e641 9032 case 'q': uv = (Uquad_t)tuv; break;
cf2093f6 9033#endif
46fc3d4c 9034 }
9035 }
9036
9037 integer:
46fc3d4c 9038 eptr = ebuf + sizeof ebuf;
fc36a67e 9039 switch (base) {
9040 unsigned dig;
9041 case 16:
c10ed8b9
HS
9042 if (!uv)
9043 alt = FALSE;
1d7c1841
GS
9044 p = (char*)((c == 'X')
9045 ? "0123456789ABCDEF" : "0123456789abcdef");
fc36a67e 9046 do {
9047 dig = uv & 15;
9048 *--eptr = p[dig];
9049 } while (uv >>= 4);
9050 if (alt) {
46fc3d4c 9051 esignbuf[esignlen++] = '0';
fc36a67e 9052 esignbuf[esignlen++] = c; /* 'x' or 'X' */
46fc3d4c 9053 }
fc36a67e 9054 break;
9055 case 8:
9056 do {
9057 dig = uv & 7;
9058 *--eptr = '0' + dig;
9059 } while (uv >>= 3);
9060 if (alt && *eptr != '0')
9061 *--eptr = '0';
9062 break;
4f19785b
WSI
9063 case 2:
9064 do {
9065 dig = uv & 1;
9066 *--eptr = '0' + dig;
9067 } while (uv >>= 1);
eda88b6d
JH
9068 if (alt) {
9069 esignbuf[esignlen++] = '0';
7481bb52 9070 esignbuf[esignlen++] = 'b';
eda88b6d 9071 }
4f19785b 9072 break;
fc36a67e 9073 default: /* it had better be ten or less */
6bc102ca 9074#if defined(PERL_Y2KWARN)
e476b1b5 9075 if (ckWARN(WARN_Y2K)) {
6bc102ca
GS
9076 STRLEN n;
9077 char *s = SvPV(sv,n);
9078 if (n >= 2 && s[n-2] == '1' && s[n-1] == '9'
9079 && (n == 2 || !isDIGIT(s[n-3])))
9080 {
9014280d 9081 Perl_warner(aTHX_ packWARN(WARN_Y2K),
6bc102ca
GS
9082 "Possible Y2K bug: %%%c %s",
9083 c, "format string following '19'");
9084 }
9085 }
9086#endif
fc36a67e 9087 do {
9088 dig = uv % base;
9089 *--eptr = '0' + dig;
9090 } while (uv /= base);
9091 break;
46fc3d4c 9092 }
9093 elen = (ebuf + sizeof ebuf) - eptr;
c10ed8b9
HS
9094 if (has_precis) {
9095 if (precis > elen)
9096 zeros = precis - elen;
9097 else if (precis == 0 && elen == 1 && *eptr == '0')
9098 elen = 0;
9099 }
46fc3d4c 9100 break;
9101
9102 /* FLOATING POINT */
9103
fc36a67e 9104 case 'F':
9105 c = 'f'; /* maybe %F isn't supported here */
9106 /* FALL THROUGH */
46fc3d4c 9107 case 'e': case 'E':
fc36a67e 9108 case 'f':
46fc3d4c 9109 case 'g': case 'G':
9110
9111 /* This is evil, but floating point is even more evil */
9112
9e5b023a
JH
9113 /* for SV-style calling, we can only get NV
9114 for C-style calling, we assume %f is double;
9115 for simplicity we allow any of %Lf, %llf, %qf for long double
9116 */
9117 switch (intsize) {
9118 case 'V':
9119#if defined(USE_LONG_DOUBLE)
9120 intsize = 'q';
9121#endif
9122 break;
f8cf5370
JH
9123/* [perl #20339] - we should accept and ignore %lf rather than die */
9124 case 'l':
9125 /* FALL THROUGH */
9e5b023a
JH
9126 default:
9127#if defined(USE_LONG_DOUBLE)
9128 intsize = args ? 0 : 'q';
9129#endif
9130 break;
9131 case 'q':
9132#if defined(HAS_LONG_DOUBLE)
9133 break;
9134#else
9135 /* FALL THROUGH */
9136#endif
9137 case 'h':
9e5b023a
JH
9138 goto unknown;
9139 }
9140
9141 /* now we need (long double) if intsize == 'q', else (double) */
be75b157 9142 nv = (args && !vectorize) ?
35fff930
JH
9143#if LONG_DOUBLESIZE > DOUBLESIZE
9144 intsize == 'q' ?
5b7ea690
JH
9145 va_arg(*args, long double) :
9146 va_arg(*args, double)
35fff930 9147#else
5b7ea690 9148 va_arg(*args, double)
35fff930 9149#endif
9e5b023a 9150 : SvNVx(argsv);
fc36a67e 9151
9152 need = 0;
be75b157 9153 vectorize = FALSE;
fc36a67e 9154 if (c != 'e' && c != 'E') {
9155 i = PERL_INT_MIN;
9e5b023a
JH
9156 /* FIXME: if HAS_LONG_DOUBLE but not USE_LONG_DOUBLE this
9157 will cast our (long double) to (double) */
73b309ea 9158 (void)Perl_frexp(nv, &i);
fc36a67e 9159 if (i == PERL_INT_MIN)
cea2e8a9 9160 Perl_die(aTHX_ "panic: frexp");
c635e13b 9161 if (i > 0)
fc36a67e 9162 need = BIT_DIGITS(i);
9163 }
9164 need += has_precis ? precis : 6; /* known default */
5b7ea690 9165
fc36a67e 9166 if (need < width)
9167 need = width;
9168
5b7ea690
JH
9169#ifdef HAS_LDBL_SPRINTF_BUG
9170 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
9171 with sfio - Allen <allens@cpan.org> */
9172
9173# ifdef DBL_MAX
9174# define MY_DBL_MAX DBL_MAX
9175# else /* XXX guessing! HUGE_VAL may be defined as infinity, so not using */
9176# if DOUBLESIZE >= 8
9177# define MY_DBL_MAX 1.7976931348623157E+308L
9178# else
9179# define MY_DBL_MAX 3.40282347E+38L
9180# endif
9181# endif
9182
9183# ifdef HAS_LDBL_SPRINTF_BUG_LESS1 /* only between -1L & 1L - Allen */
9184# define MY_DBL_MAX_BUG 1L
9185# else
9186# define MY_DBL_MAX_BUG MY_DBL_MAX
9187# endif
9188
9189# ifdef DBL_MIN
9190# define MY_DBL_MIN DBL_MIN
9191# else /* XXX guessing! -Allen */
9192# if DOUBLESIZE >= 8
9193# define MY_DBL_MIN 2.2250738585072014E-308L
9194# else
9195# define MY_DBL_MIN 1.17549435E-38L
9196# endif
9197# endif
9198
9199 if ((intsize == 'q') && (c == 'f') &&
9200 ((nv < MY_DBL_MAX_BUG) && (nv > -MY_DBL_MAX_BUG)) &&
9201 (need < DBL_DIG)) {
9202 /* it's going to be short enough that
9203 * long double precision is not needed */
9204
9205 if ((nv <= 0L) && (nv >= -0L))
9206 fix_ldbl_sprintf_bug = TRUE; /* 0 is 0 - easiest */
9207 else {
9208 /* would use Perl_fp_class as a double-check but not
9209 * functional on IRIX - see perl.h comments */
9210
9211 if ((nv >= MY_DBL_MIN) || (nv <= -MY_DBL_MIN)) {
9212 /* It's within the range that a double can represent */
9213#if defined(DBL_MAX) && !defined(DBL_MIN)
9214 if ((nv >= ((long double)1/DBL_MAX)) ||
9215 (nv <= (-(long double)1/DBL_MAX)))
9216#endif
9217 fix_ldbl_sprintf_bug = TRUE;
9218 }
9219 }
9220 if (fix_ldbl_sprintf_bug == TRUE) {
9221 double temp;
9222
9223 intsize = 0;
9224 temp = (double)nv;
9225 nv = (NV)temp;
9226 }
9227 }
9228
9229# undef MY_DBL_MAX
9230# undef MY_DBL_MAX_BUG
9231# undef MY_DBL_MIN
9232
9233#endif /* HAS_LDBL_SPRINTF_BUG */
9234
46fc3d4c 9235 need += 20; /* fudge factor */
80252599
GS
9236 if (PL_efloatsize < need) {
9237 Safefree(PL_efloatbuf);
9238 PL_efloatsize = need + 20; /* more fudge */
9239 New(906, PL_efloatbuf, PL_efloatsize, char);
7d5ea4e7 9240 PL_efloatbuf[0] = '\0';
46fc3d4c 9241 }
9242
48703b5e
NC
9243 if ( !(width || left || plus || alt) && fill != '0'
9244 && has_precis && intsize != 'q' ) { /* Shortcuts */
5f658b8d
NC
9245 /* See earlier comment about buggy Gconvert when digits,
9246 aka precis is 0 */
9247 if ( c == 'g' && precis) {
1621ce87 9248 Gconvert((NV)nv, (int)precis, 0, PL_efloatbuf);
48703b5e
NC
9249 if (*PL_efloatbuf) /* May return an empty string for digits==0 */
9250 goto float_converted;
9251 } else if ( c == 'f' && !precis) {
9252 if ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
9253 break;
9254 }
9255 }
46fc3d4c 9256 eptr = ebuf + sizeof ebuf;
9257 *--eptr = '\0';
9258 *--eptr = c;
9e5b023a
JH
9259 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
9260#if defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
9261 if (intsize == 'q') {
e5c81feb
JH
9262 /* Copy the one or more characters in a long double
9263 * format before the 'base' ([efgEFG]) character to
9264 * the format string. */
9265 static char const prifldbl[] = PERL_PRIfldbl;
9266 char const *p = prifldbl + sizeof(prifldbl) - 3;
9267 while (p >= prifldbl) { *--eptr = *p--; }
cf2093f6 9268 }
65202027 9269#endif
46fc3d4c 9270 if (has_precis) {
9271 base = precis;
9272 do { *--eptr = '0' + (base % 10); } while (base /= 10);
9273 *--eptr = '.';
9274 }
9275 if (width) {
9276 base = width;
9277 do { *--eptr = '0' + (base % 10); } while (base /= 10);
9278 }
9279 if (fill == '0')
9280 *--eptr = fill;
84902520
TB
9281 if (left)
9282 *--eptr = '-';
46fc3d4c 9283 if (plus)
9284 *--eptr = plus;
9285 if (alt)
9286 *--eptr = '#';
9287 *--eptr = '%';
9288
ff9121f8
JH
9289 /* No taint. Otherwise we are in the strange situation
9290 * where printf() taints but print($float) doesn't.
bda0f7a5 9291 * --jhi */
9e5b023a
JH
9292#if defined(HAS_LONG_DOUBLE)
9293 if (intsize == 'q')
9294 (void)sprintf(PL_efloatbuf, eptr, nv);
9295 else
9296 (void)sprintf(PL_efloatbuf, eptr, (double)nv);
9297#else
dd8482fc 9298 (void)sprintf(PL_efloatbuf, eptr, nv);
9e5b023a 9299#endif
48703b5e 9300 float_converted:
80252599
GS
9301 eptr = PL_efloatbuf;
9302 elen = strlen(PL_efloatbuf);
46fc3d4c 9303 break;
9304
fc36a67e 9305 /* SPECIAL */
9306
9307 case 'n':
9308 i = SvCUR(sv) - origlen;
be75b157 9309 if (args && !vectorize) {
c635e13b 9310 switch (intsize) {
9311 case 'h': *(va_arg(*args, short*)) = i; break;
9312 default: *(va_arg(*args, int*)) = i; break;
9313 case 'l': *(va_arg(*args, long*)) = i; break;
9314 case 'V': *(va_arg(*args, IV*)) = i; break;
cf2093f6
JH
9315#ifdef HAS_QUAD
9316 case 'q': *(va_arg(*args, Quad_t*)) = i; break;
9317#endif
c635e13b 9318 }
fc36a67e 9319 }
9dd79c3f 9320 else
211dfcf1 9321 sv_setuv_mg(argsv, (UV)i);
be75b157 9322 vectorize = FALSE;
fc36a67e 9323 continue; /* not "break" */
9324
9325 /* UNKNOWN */
9326
46fc3d4c 9327 default:
fc36a67e 9328 unknown:
599cee73 9329 if (!args && ckWARN(WARN_PRINTF) &&
533c011a 9330 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
c635e13b 9331 SV *msg = sv_newmortal();
c293eb2b
NC
9332 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
9333 (PL_op->op_type == OP_PRTF) ? "" : "s");
0f4b6630 9334 if (c) {
0f4b6630 9335 if (isPRINT(c))
1c846c1f 9336 Perl_sv_catpvf(aTHX_ msg,
0f4b6630
JH
9337 "\"%%%c\"", c & 0xFF);
9338 else
9339 Perl_sv_catpvf(aTHX_ msg,
57def98f 9340 "\"%%\\%03"UVof"\"",
0f4b6630 9341 (UV)c & 0xFF);
0f4b6630 9342 } else
c635e13b 9343 sv_catpv(msg, "end of string");
9014280d 9344 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%"SVf, msg); /* yes, this is reentrant */
c635e13b 9345 }
fb73857a 9346
9347 /* output mangled stuff ... */
9348 if (c == '\0')
9349 --q;
46fc3d4c 9350 eptr = p;
9351 elen = q - p;
fb73857a 9352
9353 /* ... right here, because formatting flags should not apply */
9354 SvGROW(sv, SvCUR(sv) + elen + 1);
9355 p = SvEND(sv);
4459522c 9356 Copy(eptr, p, elen, char);
fb73857a 9357 p += elen;
9358 *p = '\0';
fdac8c4b 9359 SvCUR_set(sv, p - SvPVX_const(sv));
d34f9d2e 9360 svix = osvix;
fb73857a 9361 continue; /* not "break" */
46fc3d4c 9362 }
9363
29359924
HS
9364 /* calculate width before utf8_upgrade changes it */
9365 have = esignlen + zeros + elen;
9366
d2876be5
JH
9367 if (is_utf8 != has_utf8) {
9368 if (is_utf8) {
9369 if (SvCUR(sv))
9370 sv_utf8_upgrade(sv);
9371 }
9372 else {
9373 SV *nsv = sv_2mortal(newSVpvn(eptr, elen));
9374 sv_utf8_upgrade(nsv);
9375 eptr = SvPVX(nsv);
9376 elen = SvCUR(nsv);
9377 }
9378 SvGROW(sv, SvCUR(sv) + elen + 1);
9379 p = SvEND(sv);
9380 *p = '\0';
9381 }
18729d3e
JH
9382 /* Use memchr() instead of strchr(), as eptr is not guaranteed */
9383 /* to point to a null-terminated string. */
9384 if (left && ckWARN(WARN_PRINTF) && memchr(eptr, '\n', elen) &&
67bae6b3
JH
9385 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF))
9386 Perl_warner(aTHX_ packWARN(WARN_PRINTF),
9387 "Newline in left-justified string for %sprintf",
9388 (PL_op->op_type == OP_PRTF) ? "" : "s");
d2876be5 9389
46fc3d4c 9390 need = (have > width ? have : width);
9391 gap = need - have;
9392
b22c7a20 9393 SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1);
46fc3d4c 9394 p = SvEND(sv);
9395 if (esignlen && fill == '0') {
eb160463 9396 for (i = 0; i < (int)esignlen; i++)
46fc3d4c 9397 *p++ = esignbuf[i];
9398 }
9399 if (gap && !left) {
9400 memset(p, fill, gap);
9401 p += gap;
9402 }
9403 if (esignlen && fill != '0') {
eb160463 9404 for (i = 0; i < (int)esignlen; i++)
46fc3d4c 9405 *p++ = esignbuf[i];
9406 }
fc36a67e 9407 if (zeros) {
9408 for (i = zeros; i; i--)
9409 *p++ = '0';
9410 }
46fc3d4c 9411 if (elen) {
4459522c 9412 Copy(eptr, p, elen, char);
46fc3d4c 9413 p += elen;
9414 }
9415 if (gap && left) {
9416 memset(p, ' ', gap);
9417 p += gap;
9418 }
b22c7a20
GS
9419 if (vectorize) {
9420 if (veclen) {
4459522c 9421 Copy(dotstr, p, dotstrlen, char);
b22c7a20
GS
9422 p += dotstrlen;
9423 }
9424 else
9425 vectorize = FALSE; /* done iterating over vecstr */
9426 }
2cf2cfc6
A
9427 if (is_utf8)
9428 has_utf8 = TRUE;
9429 if (has_utf8)
7e2040f0 9430 SvUTF8_on(sv);
46fc3d4c 9431 *p = '\0';
fdac8c4b 9432 SvCUR_set(sv, p - SvPVX_const(sv));
b22c7a20
GS
9433 if (vectorize) {
9434 esignlen = 0;
9435 goto vector;
9436 }
46fc3d4c 9437 }
9438}
51371543 9439
645c22ef
DM
9440/* =========================================================================
9441
9442=head1 Cloning an interpreter
9443
9444All the macros and functions in this section are for the private use of
9445the main function, perl_clone().
9446
9447The foo_dup() functions make an exact copy of an existing foo thinngy.
9448During the course of a cloning, a hash table is used to map old addresses
9449to new addresses. The table is created and manipulated with the
9450ptr_table_* functions.
9451
9452=cut
9453
9454============================================================================*/
9455
9456
1d7c1841
GS
9457#if defined(USE_ITHREADS)
9458
4d1ff10f
AB
9459#if defined(USE_5005THREADS)
9460# include "error: USE_5005THREADS and USE_ITHREADS are incompatible"
1d7c1841
GS
9461#endif
9462
1d7c1841
GS
9463#ifndef GpREFCNT_inc
9464# define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
9465#endif
9466
9467
d2d73c3e
AB
9468#define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t))
9469#define av_dup(s,t) (AV*)sv_dup((SV*)s,t)
9470#define av_dup_inc(s,t) (AV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9471#define hv_dup(s,t) (HV*)sv_dup((SV*)s,t)
9472#define hv_dup_inc(s,t) (HV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9473#define cv_dup(s,t) (CV*)sv_dup((SV*)s,t)
9474#define cv_dup_inc(s,t) (CV*)SvREFCNT_inc(sv_dup((SV*)s,t))
9475#define io_dup(s,t) (IO*)sv_dup((SV*)s,t)
9476#define io_dup_inc(s,t) (IO*)SvREFCNT_inc(sv_dup((SV*)s,t))
9477#define gv_dup(s,t) (GV*)sv_dup((SV*)s,t)
9478#define gv_dup_inc(s,t) (GV*)SvREFCNT_inc(sv_dup((SV*)s,t))
1d7c1841
GS
9479#define SAVEPV(p) (p ? savepv(p) : Nullch)
9480#define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch)
8cf8f3d1 9481
d2d73c3e 9482
d2f185dc
AMS
9483/* Duplicate a regexp. Required reading: pregcomp() and pregfree() in
9484 regcomp.c. AMS 20010712 */
645c22ef 9485
1d7c1841 9486REGEXP *
a8fc9800 9487Perl_re_dup(pTHX_ REGEXP *r, CLONE_PARAMS *param)
1d7c1841 9488{
d2f185dc
AMS
9489 REGEXP *ret;
9490 int i, len, npar;
9491 struct reg_substr_datum *s;
9492
9493 if (!r)
9494 return (REGEXP *)NULL;
9495
9496 if ((ret = (REGEXP *)ptr_table_fetch(PL_ptr_table, r)))
9497 return ret;
9498
9499 len = r->offsets[0];
9500 npar = r->nparens+1;
9501
9502 Newc(0, ret, sizeof(regexp) + (len+1)*sizeof(regnode), char, regexp);
9503 Copy(r->program, ret->program, len+1, regnode);
9504
9505 New(0, ret->startp, npar, I32);
9506 Copy(r->startp, ret->startp, npar, I32);
9507 New(0, ret->endp, npar, I32);
9508 Copy(r->startp, ret->startp, npar, I32);
9509
d2f185dc
AMS
9510 New(0, ret->substrs, 1, struct reg_substr_data);
9511 for (s = ret->substrs->data, i = 0; i < 3; i++, s++) {
9512 s->min_offset = r->substrs->data[i].min_offset;
9513 s->max_offset = r->substrs->data[i].max_offset;
9514 s->substr = sv_dup_inc(r->substrs->data[i].substr, param);
33b8afdf 9515 s->utf8_substr = sv_dup_inc(r->substrs->data[i].utf8_substr, param);
d2f185dc
AMS
9516 }
9517
70612e96 9518 ret->regstclass = NULL;
d2f185dc
AMS
9519 if (r->data) {
9520 struct reg_data *d;
c05e0e2f 9521 const int count = r->data->count;
d2f185dc
AMS
9522
9523 Newc(0, d, sizeof(struct reg_data) + count*sizeof(void *),
9524 char, struct reg_data);
9525 New(0, d->what, count, U8);
9526
9527 d->count = count;
9528 for (i = 0; i < count; i++) {
9529 d->what[i] = r->data->what[i];
9530 switch (d->what[i]) {
9531 case 's':
9532 d->data[i] = sv_dup_inc((SV *)r->data->data[i], param);
9533 break;
9534 case 'p':
9535 d->data[i] = av_dup_inc((AV *)r->data->data[i], param);
9536 break;
9537 case 'f':
9538 /* This is cheating. */
9539 New(0, d->data[i], 1, struct regnode_charclass_class);
9540 StructCopy(r->data->data[i], d->data[i],
9541 struct regnode_charclass_class);
70612e96 9542 ret->regstclass = (regnode*)d->data[i];
d2f185dc
AMS
9543 break;
9544 case 'o':
33773810
AMS
9545 /* Compiled op trees are readonly, and can thus be
9546 shared without duplication. */
46330ab1 9547 OP_REFCNT_LOCK;
9b978d73 9548 d->data[i] = (void*)OpREFCNT_inc((OP*)r->data->data[i]);
46330ab1 9549 OP_REFCNT_UNLOCK;
9b978d73 9550 break;
d2f185dc
AMS
9551 case 'n':
9552 d->data[i] = r->data->data[i];
9553 break;
9554 }
9555 }
9556
9557 ret->data = d;
9558 }
9559 else
9560 ret->data = NULL;
9561
9562 New(0, ret->offsets, 2*len+1, U32);
9563 Copy(r->offsets, ret->offsets, 2*len+1, U32);
9564
65b7047c 9565 ret->precomp = SAVEPVN(r->precomp, r->prelen);
d2f185dc
AMS
9566 ret->refcnt = r->refcnt;
9567 ret->minlen = r->minlen;
9568 ret->prelen = r->prelen;
9569 ret->nparens = r->nparens;
9570 ret->lastparen = r->lastparen;
9571 ret->lastcloseparen = r->lastcloseparen;
9572 ret->reganch = r->reganch;
9573
70612e96
RG
9574 ret->sublen = r->sublen;
9575
9576 if (RX_MATCH_COPIED(ret))
65b7047c 9577 ret->subbeg = SAVEPVN(r->subbeg, r->sublen);
70612e96
RG
9578 else
9579 ret->subbeg = Nullch;
9580
d2f185dc
AMS
9581 ptr_table_store(PL_ptr_table, r, ret);
9582 return ret;
1d7c1841
GS
9583}
9584
d2d73c3e 9585/* duplicate a file handle */
645c22ef 9586
1d7c1841 9587PerlIO *
a8fc9800 9588Perl_fp_dup(pTHX_ PerlIO *fp, char type, CLONE_PARAMS *param)
1d7c1841
GS
9589{
9590 PerlIO *ret;
e2b56717
AL
9591 (void)type;
9592
1d7c1841
GS
9593 if (!fp)
9594 return (PerlIO*)NULL;
9595
9596 /* look for it in the table first */
9597 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
9598 if (ret)
9599 return ret;
9600
9601 /* create anew and remember what it is */
ecdeb87c 9602 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
1d7c1841
GS
9603 ptr_table_store(PL_ptr_table, fp, ret);
9604 return ret;
9605}
9606
645c22ef
DM
9607/* duplicate a directory handle */
9608
1d7c1841
GS
9609DIR *
9610Perl_dirp_dup(pTHX_ DIR *dp)
9611{
9612 if (!dp)
9613 return (DIR*)NULL;
9614 /* XXX TODO */
9615 return dp;
9616}
9617
ff276b08 9618/* duplicate a typeglob */
645c22ef 9619
1d7c1841 9620GP *
a8fc9800 9621Perl_gp_dup(pTHX_ GP *gp, CLONE_PARAMS* param)
1d7c1841
GS
9622{
9623 GP *ret;
9624 if (!gp)
9625 return (GP*)NULL;
9626 /* look for it in the table first */
9627 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
9628 if (ret)
9629 return ret;
9630
9631 /* create anew and remember what it is */
9632 Newz(0, ret, 1, GP);
9633 ptr_table_store(PL_ptr_table, gp, ret);
9634
9635 /* clone */
9636 ret->gp_refcnt = 0; /* must be before any other dups! */
d2d73c3e
AB
9637 ret->gp_sv = sv_dup_inc(gp->gp_sv, param);
9638 ret->gp_io = io_dup_inc(gp->gp_io, param);
9639 ret->gp_form = cv_dup_inc(gp->gp_form, param);
9640 ret->gp_av = av_dup_inc(gp->gp_av, param);
9641 ret->gp_hv = hv_dup_inc(gp->gp_hv, param);
9642 ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
9643 ret->gp_cv = cv_dup_inc(gp->gp_cv, param);
1d7c1841
GS
9644 ret->gp_cvgen = gp->gp_cvgen;
9645 ret->gp_flags = gp->gp_flags;
9646 ret->gp_line = gp->gp_line;
9647 ret->gp_file = gp->gp_file; /* points to COP.cop_file */
9648 return ret;
9649}
9650
645c22ef
DM
9651/* duplicate a chain of magic */
9652
1d7c1841 9653MAGIC *
a8fc9800 9654Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS* param)
1d7c1841 9655{
cb359b41
JH
9656 MAGIC *mgprev = (MAGIC*)NULL;
9657 MAGIC *mgret;
1d7c1841
GS
9658 if (!mg)
9659 return (MAGIC*)NULL;
9660 /* look for it in the table first */
9661 mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg);
9662 if (mgret)
9663 return mgret;
9664
9665 for (; mg; mg = mg->mg_moremagic) {
9666 MAGIC *nmg;
9667 Newz(0, nmg, 1, MAGIC);
cb359b41 9668 if (mgprev)
1d7c1841 9669 mgprev->mg_moremagic = nmg;
cb359b41
JH
9670 else
9671 mgret = nmg;
1d7c1841
GS
9672 nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */
9673 nmg->mg_private = mg->mg_private;
9674 nmg->mg_type = mg->mg_type;
9675 nmg->mg_flags = mg->mg_flags;
14befaf4 9676 if (mg->mg_type == PERL_MAGIC_qr) {
d2f185dc 9677 nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj, param);
1d7c1841 9678 }
05bd4103 9679 else if(mg->mg_type == PERL_MAGIC_backref) {
1f49be52 9680 const AV * const av = (AV*) mg->mg_obj;
fdc9a813
AE
9681 SV **svp;
9682 I32 i;
1f49be52 9683 (void)SvREFCNT_inc(nmg->mg_obj = (SV*)newAV());
fdc9a813
AE
9684 svp = AvARRAY(av);
9685 for (i = AvFILLp(av); i >= 0; i--) {
3a81978b 9686 if (!svp[i]) continue;
fdc9a813
AE
9687 av_push((AV*)nmg->mg_obj,sv_dup(svp[i],param));
9688 }
05bd4103 9689 }
1d7c1841
GS
9690 else {
9691 nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED)
d2d73c3e
AB
9692 ? sv_dup_inc(mg->mg_obj, param)
9693 : sv_dup(mg->mg_obj, param);
1d7c1841
GS
9694 }
9695 nmg->mg_len = mg->mg_len;
9696 nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */
14befaf4 9697 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
68795e93 9698 if (mg->mg_len > 0) {
1d7c1841 9699 nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len);
14befaf4
DM
9700 if (mg->mg_type == PERL_MAGIC_overload_table &&
9701 AMT_AMAGIC((AMT*)mg->mg_ptr))
9702 {
1d7c1841
GS
9703 AMT *amtp = (AMT*)mg->mg_ptr;
9704 AMT *namtp = (AMT*)nmg->mg_ptr;
9705 I32 i;
9706 for (i = 1; i < NofAMmeth; i++) {
d2d73c3e 9707 namtp->table[i] = cv_dup_inc(amtp->table[i], param);
1d7c1841
GS
9708 }
9709 }
9710 }
9711 else if (mg->mg_len == HEf_SVKEY)
d2d73c3e 9712 nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr, param);
1d7c1841 9713 }
68795e93
NIS
9714 if ((mg->mg_flags & MGf_DUP) && mg->mg_virtual && mg->mg_virtual->svt_dup) {
9715 CALL_FPTR(nmg->mg_virtual->svt_dup)(aTHX_ nmg, param);
9716 }
1d7c1841
GS
9717 mgprev = nmg;
9718 }
9719 return mgret;
9720}
9721
645c22ef
DM
9722/* create a new pointer-mapping table */
9723
1d7c1841
GS
9724PTR_TBL_t *
9725Perl_ptr_table_new(pTHX)
9726{
9727 PTR_TBL_t *tbl;
9728 Newz(0, tbl, 1, PTR_TBL_t);
9729 tbl->tbl_max = 511;
9730 tbl->tbl_items = 0;
9731 Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
9732 return tbl;
9733}
9734
df34f778
NC
9735#if (PTRSIZE == 8)
9736# define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 3)
9737#else
9738# define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 2)
9739#endif
9740
72c9d3b6
NC
9741
9742
9743STATIC void
9744S_more_pte(pTHX)
9745{
69ddb3b9
NC
9746 struct ptr_tbl_ent* pte;
9747 struct ptr_tbl_ent* pteend;
72c9d3b6
NC
9748 XPV *ptr;
9749 New(54, ptr, PERL_ARENA_SIZE/sizeof(XPV), XPV);
9750 ptr->xpv_pv = (char*)PL_pte_arenaroot;
9751 PL_pte_arenaroot = ptr;
9752
9753 pte = (struct ptr_tbl_ent*)ptr;
9754 pteend = &pte[PERL_ARENA_SIZE / sizeof(struct ptr_tbl_ent) - 1];
9755 PL_pte_root = ++pte;
9756 while (pte < pteend) {
9757 pte->next = pte + 1;
9758 pte++;
9759 }
9760 pte->next = 0;
9761}
9762
9763STATIC struct ptr_tbl_ent*
9764S_new_pte(pTHX)
9765{
9766 struct ptr_tbl_ent* pte;
9767 if (!PL_pte_root)
9768 S_more_pte(aTHX);
9769 pte = PL_pte_root;
9770 PL_pte_root = pte->next;
9771 return pte;
9772}
9773
9774STATIC void
9775S_del_pte(pTHX_ struct ptr_tbl_ent*p)
9776{
9777 p->next = PL_pte_root;
9778 PL_pte_root = p;
9779}
9780
645c22ef
DM
9781/* map an existing pointer using a table */
9782
1d7c1841
GS
9783void *
9784Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv)
9785{
9786 PTR_TBL_ENT_t *tblent;
065cbbe5 9787 const UV hash = PTR_TABLE_HASH(sv);
1d7c1841
GS
9788 assert(tbl);
9789 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
9790 for (; tblent; tblent = tblent->next) {
9791 if (tblent->oldval == sv)
9792 return tblent->newval;
9793 }
9794 return (void*)NULL;
9795}
9796
645c22ef
DM
9797/* add a new entry to a pointer-mapping table */
9798
1d7c1841
GS
9799void
9800Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv)
9801{
9802 PTR_TBL_ENT_t *tblent, **otblent;
9803 /* XXX this may be pessimal on platforms where pointers aren't good
9804 * hash values e.g. if they grow faster in the most significant
9805 * bits */
065cbbe5 9806 const UV hash = PTR_TABLE_HASH(oldv);
df34f778 9807 bool empty = 1;
1d7c1841
GS
9808
9809 assert(tbl);
9810 otblent = &tbl->tbl_ary[hash & tbl->tbl_max];
df34f778 9811 for (tblent = *otblent; tblent; empty=0, tblent = tblent->next) {
1d7c1841
GS
9812 if (tblent->oldval == oldv) {
9813 tblent->newval = newv;
1d7c1841
GS
9814 return;
9815 }
9816 }
72c9d3b6 9817 tblent = S_new_pte(aTHX);
1d7c1841
GS
9818 tblent->oldval = oldv;
9819 tblent->newval = newv;
9820 tblent->next = *otblent;
9821 *otblent = tblent;
9822 tbl->tbl_items++;
df34f778 9823 if (!empty && tbl->tbl_items > tbl->tbl_max)
1d7c1841
GS
9824 ptr_table_split(tbl);
9825}
9826
645c22ef
DM
9827/* double the hash bucket size of an existing ptr table */
9828
1d7c1841
GS
9829void
9830Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl)
9831{
9832 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
065cbbe5 9833 const UV oldsize = tbl->tbl_max + 1;
1d7c1841
GS
9834 UV newsize = oldsize * 2;
9835 UV i;
9836
9837 Renew(ary, newsize, PTR_TBL_ENT_t*);
9838 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
9839 tbl->tbl_max = --newsize;
9840 tbl->tbl_ary = ary;
9841 for (i=0; i < oldsize; i++, ary++) {
9842 PTR_TBL_ENT_t **curentp, **entp, *ent;
9843 if (!*ary)
9844 continue;
9845 curentp = ary + oldsize;
9846 for (entp = ary, ent = *ary; ent; ent = *entp) {
df34f778 9847 if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
1d7c1841
GS
9848 *entp = ent->next;
9849 ent->next = *curentp;
9850 *curentp = ent;
9851 continue;
9852 }
9853 else
9854 entp = &ent->next;
9855 }
9856 }
9857}
9858
645c22ef
DM
9859/* remove all the entries from a ptr table */
9860
a0739874
DM
9861void
9862Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl)
9863{
9864 register PTR_TBL_ENT_t **array;
9865 register PTR_TBL_ENT_t *entry;
a0739874
DM
9866 UV riter = 0;
9867 UV max;
9868
9869 if (!tbl || !tbl->tbl_items) {
9870 return;
9871 }
9872
9873 array = tbl->tbl_ary;
9874 entry = array[0];
9875 max = tbl->tbl_max;
9876
9877 for (;;) {
9878 if (entry) {
065cbbe5 9879 PTR_TBL_ENT_t *oentry = entry;
a0739874 9880 entry = entry->next;
72c9d3b6 9881 S_del_pte(aTHX_ oentry);
a0739874
DM
9882 }
9883 if (!entry) {
9884 if (++riter > max) {
9885 break;
9886 }
9887 entry = array[riter];
9888 }
9889 }
9890
9891 tbl->tbl_items = 0;
9892}
9893
645c22ef
DM
9894/* clear and free a ptr table */
9895
a0739874
DM
9896void
9897Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl)
9898{
9899 if (!tbl) {
9900 return;
9901 }
9902 ptr_table_clear(tbl);
9903 Safefree(tbl->tbl_ary);
9904 Safefree(tbl);
9905}
9906
1d7c1841
GS
9907#ifdef DEBUGGING
9908char *PL_watch_pvx;
9909#endif
9910
645c22ef
DM
9911/* attempt to make everything in the typeglob readonly */
9912
5bd07a3d 9913STATIC SV *
59b40662 9914S_gv_share(pTHX_ SV *sstr, CLONE_PARAMS *param)
5bd07a3d
DM
9915{
9916 GV *gv = (GV*)sstr;
59b40662 9917 SV *sv = &param->proto_perl->Isv_no; /* just need SvREADONLY-ness */
5bd07a3d
DM
9918
9919 if (GvIO(gv) || GvFORM(gv)) {
7fb37951 9920 GvUNIQUE_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */
5bd07a3d
DM
9921 }
9922 else if (!GvCV(gv)) {
9923 GvCV(gv) = (CV*)sv;
9924 }
9925 else {
9926 /* CvPADLISTs cannot be shared */
37e20706 9927 if (!SvREADONLY(GvCV(gv)) && !CvXSUB(GvCV(gv))) {
7fb37951 9928 GvUNIQUE_off(gv);
5bd07a3d
DM
9929 }
9930 }
9931
7fb37951 9932 if (!GvUNIQUE(gv)) {
5bd07a3d
DM
9933#if 0
9934 PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n",
26ab6a78 9935 HvNAME_get(GvSTASH(gv)), GvNAME(gv));
5bd07a3d
DM
9936#endif
9937 return Nullsv;
9938 }
9939
4411f3b6 9940 /*
5bd07a3d
DM
9941 * write attempts will die with
9942 * "Modification of a read-only value attempted"
9943 */
9944 if (!GvSV(gv)) {
9945 GvSV(gv) = sv;
9946 }
9947 else {
9948 SvREADONLY_on(GvSV(gv));
9949 }
9950
9951 if (!GvAV(gv)) {
9952 GvAV(gv) = (AV*)sv;
9953 }
9954 else {
9955 SvREADONLY_on(GvAV(gv));
9956 }
9957
9958 if (!GvHV(gv)) {
9959 GvHV(gv) = (HV*)sv;
9960 }
9961 else {
fea227c9 9962 SvREADONLY_on(GvHV(gv));
5bd07a3d
DM
9963 }
9964
9965 return sstr; /* he_dup() will SvREFCNT_inc() */
9966}
9967
645c22ef
DM
9968/* duplicate an SV of any type (including AV, HV etc) */
9969
83841fad
NIS
9970void
9971Perl_rvpv_dup(pTHX_ SV *dstr, SV *sstr, CLONE_PARAMS* param)
9972{
9973 if (SvROK(sstr)) {
a8dc4fe8
SP
9974 SvRV_set(dstr, SvWEAKREF(sstr)
9975 ? sv_dup(SvRV(sstr), param)
9976 : sv_dup_inc(SvRV(sstr), param));
0da6cfda 9977
83841fad 9978 }
fdac8c4b 9979 else if (SvPVX_const(sstr)) {
83841fad
NIS
9980 /* Has something there */
9981 if (SvLEN(sstr)) {
68795e93 9982 /* Normal PV - clone whole allocated space */
fdac8c4b 9983 SvPV_set(dstr, SAVEPVN(SvPVX_const(sstr), SvLEN(sstr)-1));
68795e93 9984 }
83841fad
NIS
9985 else {
9986 /* Special case - not normally malloced for some reason */
9987 if (SvREADONLY(sstr) && SvFAKE(sstr)) {
9988 /* A "shared" PV - clone it as unshared string */
3e5ba712 9989 if(SvPADTMP(sstr)) {
efb84706
JH
9990 /* However, some of them live in the pad
9991 and they should not have these flags
9992 turned off */
3e5ba712 9993
fdac8c4b 9994 SvPV_set(dstr, sharepvn(SvPVX_const(sstr), SvCUR(sstr),
0da6cfda
SP
9995 SvUVX(sstr)));
9996 SvUV_set(dstr, SvUVX(sstr));
3e5ba712
JH
9997 } else {
9998
fdac8c4b 9999 SvPV_set(dstr, SAVEPVN(SvPVX_const(sstr), SvCUR(sstr)));
3e5ba712
JH
10000 SvFAKE_off(dstr);
10001 SvREADONLY_off(dstr);
efb84706 10002 }
83841fad
NIS
10003 }
10004 else {
10005 /* Some other special case - random pointer */
0da6cfda 10006 SvPV_set(dstr, SvPVX(sstr));
83841fad
NIS
10007 }
10008 }
10009 }
10010 else {
10011 /* Copy the Null */
0da6cfda 10012 if (SvTYPE(dstr) == SVt_RV)
a8dc4fe8 10013 SvRV_set(dstr, NULL);
0da6cfda
SP
10014 else
10015 SvPV_set(dstr, 0);
83841fad
NIS
10016 }
10017}
10018
1d7c1841 10019SV *
a8fc9800 10020Perl_sv_dup(pTHX_ SV *sstr, CLONE_PARAMS* param)
1d7c1841 10021{
1d7c1841
GS
10022 SV *dstr;
10023
10024 if (!sstr || SvTYPE(sstr) == SVTYPEMASK)
10025 return Nullsv;
10026 /* look for it in the table first */
10027 dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr);
10028 if (dstr)
10029 return dstr;
10030
457d3ae8
JH
10031 if(param->flags & CLONEf_JOIN_IN) {
10032 /** We are joining here so we don't want do clone
10033 something that is bad **/
26ab6a78 10034 const char *hvname;
457d3ae8
JH
10035
10036 if(SvTYPE(sstr) == SVt_PVHV &&
26ab6a78 10037 (hvname = HvNAME_get(sstr))) {
457d3ae8 10038 /** don't clone stashes if they already exist **/
26ab6a78 10039 HV* old_stash = gv_stashpv(hvname,0);
457d3ae8
JH
10040 return (SV*) old_stash;
10041 }
10042 }
10043
1d7c1841
GS
10044 /* create anew and remember what it is */
10045 new_SV(dstr);
10046 ptr_table_store(PL_ptr_table, sstr, dstr);
10047
10048 /* clone */
10049 SvFLAGS(dstr) = SvFLAGS(sstr);
10050 SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */
10051 SvREFCNT(dstr) = 0; /* must be before any other dups! */
10052
10053#ifdef DEBUGGING
fdac8c4b 10054 if (SvANY(sstr) && PL_watch_pvx && SvPVX_const(sstr) == PL_watch_pvx)
1d7c1841 10055 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
fdac8c4b 10056 PL_watch_pvx, SvPVX_const(sstr));
1d7c1841
GS
10057#endif
10058
c7df1ae8
NC
10059 /* don't clone objects whose class has asked us not to */
10060 if (SvOBJECT(sstr) && ! (SvFLAGS(SvSTASH(sstr)) & SVphv_CLONEABLE)) {
10061 SvFLAGS(dstr) &= ~SVTYPEMASK;
10062 SvOBJECT_off(dstr);
10063 return dstr;
10064 }
10065
1d7c1841
GS
10066 switch (SvTYPE(sstr)) {
10067 case SVt_NULL:
10068 SvANY(dstr) = NULL;
10069 break;
10070 case SVt_IV:
10071 SvANY(dstr) = new_XIV();
0da6cfda 10072 SvIV_set(dstr, SvIVX(sstr));
1d7c1841
GS
10073 break;
10074 case SVt_NV:
10075 SvANY(dstr) = new_XNV();
0da6cfda 10076 SvNV_set(dstr, SvNVX(sstr));
1d7c1841
GS
10077 break;
10078 case SVt_RV:
10079 SvANY(dstr) = new_XRV();
83841fad 10080 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10081 break;
10082 case SVt_PV:
10083 SvANY(dstr) = new_XPV();
a8dc4fe8
SP
10084 SvCUR_set(dstr, SvCUR(sstr));
10085 SvLEN_set(dstr, SvLEN(sstr));
83841fad 10086 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10087 break;
10088 case SVt_PVIV:
10089 SvANY(dstr) = new_XPVIV();
a8dc4fe8
SP
10090 SvCUR_set(dstr, SvCUR(sstr));
10091 SvLEN_set(dstr, SvLEN(sstr));
0da6cfda 10092 SvIV_set(dstr, SvIVX(sstr));
83841fad 10093 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10094 break;
10095 case SVt_PVNV:
10096 SvANY(dstr) = new_XPVNV();
a8dc4fe8
SP
10097 SvCUR_set(dstr, SvCUR(sstr));
10098 SvLEN_set(dstr, SvLEN(sstr));
0da6cfda
SP
10099 SvIV_set(dstr, SvIVX(sstr));
10100 SvNV_set(dstr, SvNVX(sstr));
83841fad 10101 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10102 break;
10103 case SVt_PVMG:
10104 SvANY(dstr) = new_XPVMG();
a8dc4fe8
SP
10105 SvCUR_set(dstr, SvCUR(sstr));
10106 SvLEN_set(dstr, SvLEN(sstr));
0da6cfda
SP
10107 SvIV_set(dstr, SvIVX(sstr));
10108 SvNV_set(dstr, SvNVX(sstr));
a8dc4fe8
SP
10109 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10110 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10111 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10112 break;
10113 case SVt_PVBM:
10114 SvANY(dstr) = new_XPVBM();
a8dc4fe8
SP
10115 SvCUR_set(dstr, SvCUR(sstr));
10116 SvLEN_set(dstr, SvLEN(sstr));
0da6cfda
SP
10117 SvIV_set(dstr, SvIVX(sstr));
10118 SvNV_set(dstr, SvNVX(sstr));
a8dc4fe8
SP
10119 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10120 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10121 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10122 BmRARE(dstr) = BmRARE(sstr);
10123 BmUSEFUL(dstr) = BmUSEFUL(sstr);
10124 BmPREVIOUS(dstr)= BmPREVIOUS(sstr);
10125 break;
10126 case SVt_PVLV:
10127 SvANY(dstr) = new_XPVLV();
a8dc4fe8
SP
10128 SvCUR_set(dstr, SvCUR(sstr));
10129 SvLEN_set(dstr, SvLEN(sstr));
0da6cfda
SP
10130 SvIV_set(dstr, SvIVX(sstr));
10131 SvNV_set(dstr, SvNVX(sstr));
a8dc4fe8
SP
10132 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10133 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10134 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10135 LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */
10136 LvTARGLEN(dstr) = LvTARGLEN(sstr);
73c86719
JH
10137 if (LvTYPE(sstr) == 't') /* for tie: unrefcnted fake (SV**) */
10138 LvTARG(dstr) = dstr;
10139 else if (LvTYPE(sstr) == 'T') /* for tie: fake HE */
10140 LvTARG(dstr) = (SV*)he_dup((HE*)LvTARG(sstr), 0, param);
10141 else
10142 LvTARG(dstr) = sv_dup_inc(LvTARG(sstr), param);
1d7c1841
GS
10143 LvTYPE(dstr) = LvTYPE(sstr);
10144 break;
10145 case SVt_PVGV:
7fb37951 10146 if (GvUNIQUE((GV*)sstr)) {
5bd07a3d 10147 SV *share;
59b40662 10148 if ((share = gv_share(sstr, param))) {
5bd07a3d
DM
10149 del_SV(dstr);
10150 dstr = share;
37e20706 10151 ptr_table_store(PL_ptr_table, sstr, dstr);
5bd07a3d
DM
10152#if 0
10153 PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n",
26ab6a78 10154 HvNAME_get(GvSTASH(share)), GvNAME(share));
5bd07a3d
DM
10155#endif
10156 break;
10157 }
10158 }
1d7c1841 10159 SvANY(dstr) = new_XPVGV();
a8dc4fe8
SP
10160 SvCUR_set(dstr, SvCUR(sstr));
10161 SvLEN_set(dstr, SvLEN(sstr));
0da6cfda
SP
10162 SvIV_set(dstr, SvIVX(sstr));
10163 SvNV_set(dstr, SvNVX(sstr));
a8dc4fe8
SP
10164 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10165 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10166 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10167 GvNAMELEN(dstr) = GvNAMELEN(sstr);
10168 GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr));
d2d73c3e 10169 GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr), param);
1d7c1841 10170 GvFLAGS(dstr) = GvFLAGS(sstr);
d2d73c3e 10171 GvGP(dstr) = gp_dup(GvGP(sstr), param);
1d7c1841
GS
10172 (void)GpREFCNT_inc(GvGP(dstr));
10173 break;
10174 case SVt_PVIO:
10175 SvANY(dstr) = new_XPVIO();
a8dc4fe8
SP
10176 SvCUR_set(dstr, SvCUR(sstr));
10177 SvLEN_set(dstr, SvLEN(sstr));
0da6cfda
SP
10178 SvIV_set(dstr, SvIVX(sstr));
10179 SvNV_set(dstr, SvNVX(sstr));
a8dc4fe8
SP
10180 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10181 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10182 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
a8fc9800 10183 IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr), param);
1d7c1841
GS
10184 if (IoOFP(sstr) == IoIFP(sstr))
10185 IoOFP(dstr) = IoIFP(dstr);
10186 else
a8fc9800 10187 IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr), param);
1d7c1841
GS
10188 /* PL_rsfp_filters entries have fake IoDIRP() */
10189 if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP))
10190 IoDIRP(dstr) = dirp_dup(IoDIRP(sstr));
10191 else
10192 IoDIRP(dstr) = IoDIRP(sstr);
10193 IoLINES(dstr) = IoLINES(sstr);
10194 IoPAGE(dstr) = IoPAGE(sstr);
10195 IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr);
10196 IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr);
4376702e
JH
10197 if(IoFLAGS(sstr) & IOf_FAKE_DIRP) {
10198 /* I have no idea why fake dirp (rsfps)
10199 should be treaded differently but otherwise
10200 we end up with leaks -- sky*/
10201 IoTOP_GV(dstr) = gv_dup_inc(IoTOP_GV(sstr), param);
10202 IoFMT_GV(dstr) = gv_dup_inc(IoFMT_GV(sstr), param);
10203 IoBOTTOM_GV(dstr) = gv_dup_inc(IoBOTTOM_GV(sstr), param);
10204 } else {
10205 IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr), param);
10206 IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr), param);
10207 IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr), param);
10208 }
1d7c1841 10209 IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr));
1d7c1841 10210 IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr));
1d7c1841 10211 IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr));
1d7c1841
GS
10212 IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr);
10213 IoTYPE(dstr) = IoTYPE(sstr);
10214 IoFLAGS(dstr) = IoFLAGS(sstr);
10215 break;
10216 case SVt_PVAV:
10217 SvANY(dstr) = new_XPVAV();
a8dc4fe8
SP
10218 SvCUR_set(dstr, SvCUR(sstr));
10219 SvLEN_set(dstr, SvLEN(sstr));
0da6cfda
SP
10220 SvIV_set(dstr, SvIVX(sstr));
10221 SvNV_set(dstr, SvNVX(sstr));
a8dc4fe8
SP
10222 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10223 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
d2d73c3e 10224 AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr), param);
1d7c1841
GS
10225 AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr);
10226 if (AvARRAY((AV*)sstr)) {
10227 SV **dst_ary, **src_ary;
10228 SSize_t items = AvFILLp((AV*)sstr) + 1;
10229
10230 src_ary = AvARRAY((AV*)sstr);
10231 Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*);
10232 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
0da6cfda 10233 SvPV_set(dstr, (char*)dst_ary);
1d7c1841
GS
10234 AvALLOC((AV*)dstr) = dst_ary;
10235 if (AvREAL((AV*)sstr)) {
10236 while (items-- > 0)
d2d73c3e 10237 *dst_ary++ = sv_dup_inc(*src_ary++, param);
1d7c1841
GS
10238 }
10239 else {
10240 while (items-- > 0)
d2d73c3e 10241 *dst_ary++ = sv_dup(*src_ary++, param);
1d7c1841
GS
10242 }
10243 items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr);
10244 while (items-- > 0) {
10245 *dst_ary++ = &PL_sv_undef;
10246 }
10247 }
10248 else {
0da6cfda 10249 SvPV_set(dstr, Nullch);
1d7c1841
GS
10250 AvALLOC((AV*)dstr) = (SV**)NULL;
10251 }
10252 break;
10253 case SVt_PVHV:
10254 SvANY(dstr) = new_XPVHV();
a8dc4fe8
SP
10255 SvCUR_set(dstr, SvCUR(sstr));
10256 SvLEN_set(dstr, SvLEN(sstr));
1a21fcb5 10257 HvTOTALKEYS(dstr) = HvTOTALKEYS(sstr);
0da6cfda 10258 SvNV_set(dstr, SvNVX(sstr));
a8dc4fe8
SP
10259 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10260 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
26ab6a78 10261 HvRITER_set((HV*)dstr, HvRITER_get((HV*)sstr));
1d7c1841 10262 if (HvARRAY((HV*)sstr)) {
4d4670a8 10263 bool sharekeys = !!HvSHAREKEYS(sstr);
1d7c1841
GS
10264 STRLEN i = 0;
10265 XPVHV *dxhv = (XPVHV*)SvANY(dstr);
10266 XPVHV *sxhv = (XPVHV*)SvANY(sstr);
4d4670a8 10267 New(0, dxhv->xhv_array,
1d7c1841
GS
10268 PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char);
10269 while (i <= sxhv->xhv_max) {
4d4670a8
AL
10270 HE *source = HvARRAY(sstr)[i];
10271 HvARRAY(dstr)[i]
10272 = source ? he_dup(source, sharekeys, param) : 0;
1d7c1841
GS
10273 ++i;
10274 }
eb160463
GS
10275 dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter,
10276 (bool)!!HvSHAREKEYS(sstr), param);
1d7c1841
GS
10277 }
10278 else {
0da6cfda 10279 SvPV_set(dstr, Nullch);
26ab6a78 10280 HvEITER_set((HV*)dstr, (HE*)NULL);
1d7c1841
GS
10281 }
10282 HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */
10283 HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr));
26ab6a78 10284 /* Record stashes for possible cloning in Perl_clone(). */
6676db26 10285 if(HvNAME((HV*)dstr))
d2d73c3e 10286 av_push(param->stashes, dstr);
1d7c1841
GS
10287 break;
10288 case SVt_PVFM:
10289 SvANY(dstr) = new_XPVFM();
10290 FmLINES(dstr) = FmLINES(sstr);
10291 goto dup_pvcv;
10292 /* NOTREACHED */
10293 case SVt_PVCV:
10294 SvANY(dstr) = new_XPVCV();
d2d73c3e 10295 dup_pvcv:
a8dc4fe8
SP
10296 SvCUR_set(dstr, SvCUR(sstr));
10297 SvLEN_set(dstr, SvLEN(sstr));
0da6cfda
SP
10298 SvIV_set(dstr, SvIVX(sstr));
10299 SvNV_set(dstr, SvNVX(sstr));
a8dc4fe8
SP
10300 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10301 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10302 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
d2d73c3e 10303 CvSTASH(dstr) = hv_dup(CvSTASH(sstr), param); /* NOTE: not refcounted */
1d7c1841 10304 CvSTART(dstr) = CvSTART(sstr);
46330ab1 10305 OP_REFCNT_LOCK;
1d7c1841 10306 CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr));
46330ab1 10307 OP_REFCNT_UNLOCK;
1d7c1841
GS
10308 CvXSUB(dstr) = CvXSUB(sstr);
10309 CvXSUBANY(dstr) = CvXSUBANY(sstr);
01485f8b
DM
10310 if (CvCONST(sstr)) {
10311 CvXSUBANY(dstr).any_ptr = GvUNIQUE(CvGV(sstr)) ?
10312 SvREFCNT_inc(CvXSUBANY(sstr).any_ptr) :
10313 sv_dup_inc(CvXSUBANY(sstr).any_ptr, param);
10314 }
a23cf089
JD
10315 /* don't dup if copying back - CvGV isn't refcounted, so the
10316 * duped GV may never be freed. A bit of a hack! DAPM */
10317 CvGV(dstr) = (param->flags & CLONEf_JOIN_IN) ?
10318 Nullgv : gv_dup(CvGV(sstr), param) ;
d2d73c3e
AB
10319 if (param->flags & CLONEf_COPY_STACKS) {
10320 CvDEPTH(dstr) = CvDEPTH(sstr);
10321 } else {
10322 CvDEPTH(dstr) = 0;
10323 }
9755d405 10324 PAD_DUP(CvPADLIST(dstr), CvPADLIST(sstr), param);
d7afa7f5
JH
10325 CvOUTSIDE_SEQ(dstr) = CvOUTSIDE_SEQ(sstr);
10326 CvOUTSIDE(dstr) =
10327 CvWEAKOUTSIDE(sstr)
10328 ? cv_dup( CvOUTSIDE(sstr), param)
10329 : cv_dup_inc(CvOUTSIDE(sstr), param);
1d7c1841 10330 CvFLAGS(dstr) = CvFLAGS(sstr);
54356c7d 10331 CvFILE(dstr) = CvXSUB(sstr) ? CvFILE(sstr) : SAVEPV(CvFILE(sstr));
1d7c1841
GS
10332 break;
10333 default:
c803eecc 10334 Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(sstr));
1d7c1841
GS
10335 break;
10336 }
10337
10338 if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO)
10339 ++PL_sv_objcount;
10340
10341 return dstr;
d2d73c3e 10342 }
1d7c1841 10343
645c22ef
DM
10344/* duplicate a context */
10345
1d7c1841 10346PERL_CONTEXT *
a8fc9800 10347Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
1d7c1841
GS
10348{
10349 PERL_CONTEXT *ncxs;
10350
10351 if (!cxs)
10352 return (PERL_CONTEXT*)NULL;
10353
10354 /* look for it in the table first */
10355 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
10356 if (ncxs)
10357 return ncxs;
10358
10359 /* create anew and remember what it is */
10360 Newz(56, ncxs, max + 1, PERL_CONTEXT);
10361 ptr_table_store(PL_ptr_table, cxs, ncxs);
10362
10363 while (ix >= 0) {
10364 PERL_CONTEXT *cx = &cxs[ix];
10365 PERL_CONTEXT *ncx = &ncxs[ix];
10366 ncx->cx_type = cx->cx_type;
10367 if (CxTYPE(cx) == CXt_SUBST) {
10368 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
10369 }
10370 else {
10371 ncx->blk_oldsp = cx->blk_oldsp;
10372 ncx->blk_oldcop = cx->blk_oldcop;
10373 ncx->blk_oldretsp = cx->blk_oldretsp;
10374 ncx->blk_oldmarksp = cx->blk_oldmarksp;
10375 ncx->blk_oldscopesp = cx->blk_oldscopesp;
10376 ncx->blk_oldpm = cx->blk_oldpm;
10377 ncx->blk_gimme = cx->blk_gimme;
10378 switch (CxTYPE(cx)) {
10379 case CXt_SUB:
10380 ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0
d2d73c3e
AB
10381 ? cv_dup_inc(cx->blk_sub.cv, param)
10382 : cv_dup(cx->blk_sub.cv,param));
1d7c1841 10383 ncx->blk_sub.argarray = (cx->blk_sub.hasargs
d2d73c3e 10384 ? av_dup_inc(cx->blk_sub.argarray, param)
1d7c1841 10385 : Nullav);
d2d73c3e 10386 ncx->blk_sub.savearray = av_dup_inc(cx->blk_sub.savearray, param);
1d7c1841
GS
10387 ncx->blk_sub.olddepth = cx->blk_sub.olddepth;
10388 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
10389 ncx->blk_sub.lval = cx->blk_sub.lval;
10390 break;
10391 case CXt_EVAL:
10392 ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval;
10393 ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type;
b47cad08 10394 ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv, param);
1d7c1841 10395 ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root;
d2d73c3e 10396 ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text, param);
1d7c1841
GS
10397 break;
10398 case CXt_LOOP:
10399 ncx->blk_loop.label = cx->blk_loop.label;
10400 ncx->blk_loop.resetsp = cx->blk_loop.resetsp;
10401 ncx->blk_loop.redo_op = cx->blk_loop.redo_op;
10402 ncx->blk_loop.next_op = cx->blk_loop.next_op;
10403 ncx->blk_loop.last_op = cx->blk_loop.last_op;
10404 ncx->blk_loop.iterdata = (CxPADLOOP(cx)
10405 ? cx->blk_loop.iterdata
d2d73c3e 10406 : gv_dup((GV*)cx->blk_loop.iterdata, param));
d7afa7f5
JH
10407 ncx->blk_loop.oldcomppad
10408 = (PAD*)ptr_table_fetch(PL_ptr_table,
10409 cx->blk_loop.oldcomppad);
d2d73c3e
AB
10410 ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave, param);
10411 ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval, param);
10412 ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary, param);
1d7c1841
GS
10413 ncx->blk_loop.iterix = cx->blk_loop.iterix;
10414 ncx->blk_loop.itermax = cx->blk_loop.itermax;
10415 break;
10416 case CXt_FORMAT:
d2d73c3e
AB
10417 ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv, param);
10418 ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv, param);
10419 ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv, param);
1d7c1841
GS
10420 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
10421 break;
10422 case CXt_BLOCK:
10423 case CXt_NULL:
10424 break;
10425 }
10426 }
10427 --ix;
10428 }
10429 return ncxs;
10430}
10431
645c22ef
DM
10432/* duplicate a stack info structure */
10433
1d7c1841 10434PERL_SI *
a8fc9800 10435Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
1d7c1841
GS
10436{
10437 PERL_SI *nsi;
10438
10439 if (!si)
10440 return (PERL_SI*)NULL;
10441
10442 /* look for it in the table first */
10443 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
10444 if (nsi)
10445 return nsi;
10446
10447 /* create anew and remember what it is */
10448 Newz(56, nsi, 1, PERL_SI);
10449 ptr_table_store(PL_ptr_table, si, nsi);
10450
d2d73c3e 10451 nsi->si_stack = av_dup_inc(si->si_stack, param);
1d7c1841
GS
10452 nsi->si_cxix = si->si_cxix;
10453 nsi->si_cxmax = si->si_cxmax;
d2d73c3e 10454 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
1d7c1841 10455 nsi->si_type = si->si_type;
d2d73c3e
AB
10456 nsi->si_prev = si_dup(si->si_prev, param);
10457 nsi->si_next = si_dup(si->si_next, param);
1d7c1841
GS
10458 nsi->si_markoff = si->si_markoff;
10459
10460 return nsi;
10461}
10462
10463#define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
10464#define TOPINT(ss,ix) ((ss)[ix].any_i32)
10465#define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
10466#define TOPLONG(ss,ix) ((ss)[ix].any_long)
10467#define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
10468#define TOPIV(ss,ix) ((ss)[ix].any_iv)
21637969
YST
10469#define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool)
10470#define TOPBOOL(ss,ix) ((ss)[ix].any_bool)
1d7c1841
GS
10471#define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
10472#define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
10473#define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
10474#define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
10475#define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
10476#define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
10477
10478/* XXXXX todo */
10479#define pv_dup_inc(p) SAVEPV(p)
10480#define pv_dup(p) SAVEPV(p)
10481#define svp_dup_inc(p,pp) any_dup(p,pp)
10482
645c22ef
DM
10483/* map any object to the new equivent - either something in the
10484 * ptr table, or something in the interpreter structure
10485 */
10486
1d7c1841
GS
10487void *
10488Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl)
10489{
10490 void *ret;
10491
10492 if (!v)
10493 return (void*)NULL;
10494
10495 /* look for it in the table first */
10496 ret = ptr_table_fetch(PL_ptr_table, v);
10497 if (ret)
10498 return ret;
10499
10500 /* see if it is part of the interpreter structure */
10501 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
acfe0abc 10502 ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
05ec9bb3 10503 else {
1d7c1841 10504 ret = v;
05ec9bb3 10505 }
1d7c1841
GS
10506
10507 return ret;
10508}
10509
645c22ef
DM
10510/* duplicate the save stack */
10511
1d7c1841 10512ANY *
a8fc9800 10513Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
1d7c1841
GS
10514{
10515 ANY *ss = proto_perl->Tsavestack;
10516 I32 ix = proto_perl->Tsavestack_ix;
10517 I32 max = proto_perl->Tsavestack_max;
10518 ANY *nss;
10519 SV *sv;
10520 GV *gv;
10521 AV *av;
10522 HV *hv;
10523 void* ptr;
10524 int intval;
10525 long longval;
10526 GP *gp;
10527 IV iv;
10528 I32 i;
c4e33207 10529 char *c = NULL;
1d7c1841 10530 void (*dptr) (void*);
acfe0abc 10531 void (*dxptr) (pTHX_ void*);
e977893f 10532 OP *o;
1d7c1841
GS
10533
10534 Newz(54, nss, max, ANY);
10535
10536 while (ix > 0) {
10537 i = POPINT(ss,ix);
10538 TOPINT(nss,ix) = i;
10539 switch (i) {
10540 case SAVEt_ITEM: /* normal string */
10541 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10542 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 10543 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10544 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
10545 break;
10546 case SAVEt_SV: /* scalar reference */
10547 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10548 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 10549 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 10550 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
1d7c1841 10551 break;
f4dd75d9
GS
10552 case SAVEt_GENERIC_PVREF: /* generic char* */
10553 c = (char*)POPPTR(ss,ix);
10554 TOPPTR(nss,ix) = pv_dup(c);
10555 ptr = POPPTR(ss,ix);
10556 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10557 break;
05ec9bb3
NIS
10558 case SAVEt_SHARED_PVREF: /* char* in shared space */
10559 c = (char*)POPPTR(ss,ix);
10560 TOPPTR(nss,ix) = savesharedpv(c);
10561 ptr = POPPTR(ss,ix);
10562 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10563 break;
1d7c1841
GS
10564 case SAVEt_GENERIC_SVREF: /* generic sv */
10565 case SAVEt_SVREF: /* scalar reference */
10566 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10567 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
10568 ptr = POPPTR(ss,ix);
10569 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
10570 break;
10571 case SAVEt_AV: /* array reference */
10572 av = (AV*)POPPTR(ss,ix);
d2d73c3e 10573 TOPPTR(nss,ix) = av_dup_inc(av, param);
1d7c1841 10574 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 10575 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
10576 break;
10577 case SAVEt_HV: /* hash reference */
10578 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 10579 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841 10580 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 10581 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
10582 break;
10583 case SAVEt_INT: /* int reference */
10584 ptr = POPPTR(ss,ix);
10585 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10586 intval = (int)POPINT(ss,ix);
10587 TOPINT(nss,ix) = intval;
10588 break;
10589 case SAVEt_LONG: /* long reference */
10590 ptr = POPPTR(ss,ix);
10591 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10592 longval = (long)POPLONG(ss,ix);
10593 TOPLONG(nss,ix) = longval;
10594 break;
10595 case SAVEt_I32: /* I32 reference */
10596 case SAVEt_I16: /* I16 reference */
10597 case SAVEt_I8: /* I8 reference */
10598 ptr = POPPTR(ss,ix);
10599 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10600 i = POPINT(ss,ix);
10601 TOPINT(nss,ix) = i;
10602 break;
10603 case SAVEt_IV: /* IV reference */
10604 ptr = POPPTR(ss,ix);
10605 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10606 iv = POPIV(ss,ix);
10607 TOPIV(nss,ix) = iv;
10608 break;
10609 case SAVEt_SPTR: /* SV* reference */
10610 ptr = POPPTR(ss,ix);
10611 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10612 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10613 TOPPTR(nss,ix) = sv_dup(sv, param);
1d7c1841
GS
10614 break;
10615 case SAVEt_VPTR: /* random* reference */
10616 ptr = POPPTR(ss,ix);
10617 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10618 ptr = POPPTR(ss,ix);
10619 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10620 break;
10621 case SAVEt_PPTR: /* char* reference */
10622 ptr = POPPTR(ss,ix);
10623 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10624 c = (char*)POPPTR(ss,ix);
10625 TOPPTR(nss,ix) = pv_dup(c);
10626 break;
10627 case SAVEt_HPTR: /* HV* reference */
10628 ptr = POPPTR(ss,ix);
10629 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10630 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 10631 TOPPTR(nss,ix) = hv_dup(hv, param);
1d7c1841
GS
10632 break;
10633 case SAVEt_APTR: /* AV* reference */
10634 ptr = POPPTR(ss,ix);
10635 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10636 av = (AV*)POPPTR(ss,ix);
d2d73c3e 10637 TOPPTR(nss,ix) = av_dup(av, param);
1d7c1841
GS
10638 break;
10639 case SAVEt_NSTAB:
10640 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 10641 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
10642 break;
10643 case SAVEt_GP: /* scalar reference */
10644 gp = (GP*)POPPTR(ss,ix);
d2d73c3e 10645 TOPPTR(nss,ix) = gp = gp_dup(gp, param);
1d7c1841
GS
10646 (void)GpREFCNT_inc(gp);
10647 gv = (GV*)POPPTR(ss,ix);
2ed3c8fc 10648 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
1d7c1841
GS
10649 c = (char*)POPPTR(ss,ix);
10650 TOPPTR(nss,ix) = pv_dup(c);
10651 iv = POPIV(ss,ix);
10652 TOPIV(nss,ix) = iv;
10653 iv = POPIV(ss,ix);
10654 TOPIV(nss,ix) = iv;
10655 break;
10656 case SAVEt_FREESV:
26d9b02f 10657 case SAVEt_MORTALIZESV:
1d7c1841 10658 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10659 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
10660 break;
10661 case SAVEt_FREEOP:
10662 ptr = POPPTR(ss,ix);
10663 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
10664 /* these are assumed to be refcounted properly */
10665 switch (((OP*)ptr)->op_type) {
10666 case OP_LEAVESUB:
10667 case OP_LEAVESUBLV:
10668 case OP_LEAVEEVAL:
10669 case OP_LEAVE:
10670 case OP_SCOPE:
10671 case OP_LEAVEWRITE:
e977893f
GS
10672 TOPPTR(nss,ix) = ptr;
10673 o = (OP*)ptr;
10674 OpREFCNT_inc(o);
1d7c1841
GS
10675 break;
10676 default:
10677 TOPPTR(nss,ix) = Nullop;
10678 break;
10679 }
10680 }
10681 else
10682 TOPPTR(nss,ix) = Nullop;
10683 break;
10684 case SAVEt_FREEPV:
10685 c = (char*)POPPTR(ss,ix);
10686 TOPPTR(nss,ix) = pv_dup_inc(c);
10687 break;
10688 case SAVEt_CLEARSV:
10689 longval = POPLONG(ss,ix);
10690 TOPLONG(nss,ix) = longval;
10691 break;
10692 case SAVEt_DELETE:
10693 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 10694 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841
GS
10695 c = (char*)POPPTR(ss,ix);
10696 TOPPTR(nss,ix) = pv_dup_inc(c);
10697 i = POPINT(ss,ix);
10698 TOPINT(nss,ix) = i;
10699 break;
10700 case SAVEt_DESTRUCTOR:
10701 ptr = POPPTR(ss,ix);
10702 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
10703 dptr = POPDPTR(ss,ix);
1c7ab622
NC
10704 TOPDPTR(nss,ix) = DPTR2FPTR(void (*)(void*),
10705 any_dup(FPTR2DPTR(void *, dptr),
10706 proto_perl));
1d7c1841
GS
10707 break;
10708 case SAVEt_DESTRUCTOR_X:
10709 ptr = POPPTR(ss,ix);
10710 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
10711 dxptr = POPDXPTR(ss,ix);
1c7ab622
NC
10712 TOPDXPTR(nss,ix) = DPTR2FPTR(void (*)(pTHX_ void*),
10713 any_dup(FPTR2DPTR(void *, dxptr),
10714 proto_perl));
1d7c1841
GS
10715 break;
10716 case SAVEt_REGCONTEXT:
10717 case SAVEt_ALLOC:
10718 i = POPINT(ss,ix);
10719 TOPINT(nss,ix) = i;
10720 ix -= i;
10721 break;
10722 case SAVEt_STACK_POS: /* Position on Perl stack */
10723 i = POPINT(ss,ix);
10724 TOPINT(nss,ix) = i;
10725 break;
10726 case SAVEt_AELEM: /* array element */
10727 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10728 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
10729 i = POPINT(ss,ix);
10730 TOPINT(nss,ix) = i;
10731 av = (AV*)POPPTR(ss,ix);
d2d73c3e 10732 TOPPTR(nss,ix) = av_dup_inc(av, param);
1d7c1841
GS
10733 break;
10734 case SAVEt_HELEM: /* hash element */
10735 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10736 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 10737 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10738 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 10739 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 10740 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841
GS
10741 break;
10742 case SAVEt_OP:
10743 ptr = POPPTR(ss,ix);
10744 TOPPTR(nss,ix) = ptr;
10745 break;
10746 case SAVEt_HINTS:
10747 i = POPINT(ss,ix);
10748 TOPINT(nss,ix) = i;
10749 break;
c4410b1b
GS
10750 case SAVEt_COMPPAD:
10751 av = (AV*)POPPTR(ss,ix);
c2ef0052 10752 TOPPTR(nss,ix) = av_dup(av, param);
c4410b1b 10753 break;
c3564e5c
GS
10754 case SAVEt_PADSV:
10755 longval = (long)POPLONG(ss,ix);
10756 TOPLONG(nss,ix) = longval;
10757 ptr = POPPTR(ss,ix);
10758 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
10759 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 10760 TOPPTR(nss,ix) = sv_dup(sv, param);
c3564e5c 10761 break;
4f4e7967 10762 case SAVEt_BOOL:
21637969 10763 ptr = POPPTR(ss,ix);
4f4e7967 10764 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
21637969 10765 longval = (long)POPBOOL(ss,ix);
4f4e7967
JH
10766 TOPBOOL(nss,ix) = (bool)longval;
10767 break;
1d7c1841
GS
10768 default:
10769 Perl_croak(aTHX_ "panic: ss_dup inconsistency");
10770 }
10771 }
10772
10773 return nss;
10774}
10775
c7df1ae8
NC
10776
10777/* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
10778 * flag to the result. This is done for each stash before cloning starts,
10779 * so we know which stashes want their objects cloned */
10780
10781static void
10782do_mark_cloneable_stash(pTHX_ SV *sv)
10783{
26ab6a78
NC
10784 const char *hvname = HvNAME_get((HV*)sv);
10785 if (hvname) {
c7df1ae8
NC
10786 GV* cloner = gv_fetchmethod_autoload((HV*)sv, "CLONE_SKIP", 0);
10787 SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
10788 if (cloner && GvCV(cloner)) {
10789 dSP;
10790 UV status;
10791
10792 ENTER;
10793 SAVETMPS;
10794 PUSHMARK(SP);
26ab6a78 10795 XPUSHs(sv_2mortal(newSVpv(hvname, 0)));
c7df1ae8
NC
10796 PUTBACK;
10797 call_sv((SV*)GvCV(cloner), G_SCALAR);
10798 SPAGAIN;
10799 status = POPu;
10800 PUTBACK;
10801 FREETMPS;
10802 LEAVE;
10803 if (status)
10804 SvFLAGS(sv) &= ~SVphv_CLONEABLE;
10805 }
10806 }
10807}
10808
10809
10810
645c22ef
DM
10811/*
10812=for apidoc perl_clone
10813
10814Create and return a new interpreter by cloning the current one.
10815
ac388100 10816perl_clone takes these flags as parameters:
4f4e7967
JH
10817
10818CLONEf_COPY_STACKS - is used to, well, copy the stacks also,
10819without it we only clone the data and zero the stacks,
10820with it we copy the stacks and the new perl interpreter is
10821ready to run at the exact same point as the previous one.
10822The pseudo-fork code uses COPY_STACKS while the
10823threads->new doesn't.
10824
10825CLONEf_KEEP_PTR_TABLE
10826perl_clone keeps a ptr_table with the pointer of the old
10827variable as a key and the new variable as a value,
10828this allows it to check if something has been cloned and not
10829clone it again but rather just use the value and increase the
10830refcount. If KEEP_PTR_TABLE is not set then perl_clone will kill
10831the ptr_table using the function
10832C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>,
10833reason to keep it around is if you want to dup some of your own
10834variable who are outside the graph perl scans, example of this
10835code is in threads.xs create
10836
10837CLONEf_CLONE_HOST
10838This is a win32 thing, it is ignored on unix, it tells perls
10839win32host code (which is c++) to clone itself, this is needed on
10840win32 if you want to run two threads at the same time,
10841if you just want to do some stuff in a separate perl interpreter
10842and then throw it away and return to the original one,
10843you don't need to do anything.
10844
645c22ef
DM
10845=cut
10846*/
10847
10848/* XXX the above needs expanding by someone who actually understands it ! */
3fc56081
NK
10849EXTERN_C PerlInterpreter *
10850perl_clone_host(PerlInterpreter* proto_perl, UV flags);
645c22ef 10851
1d7c1841
GS
10852PerlInterpreter *
10853perl_clone(PerlInterpreter *proto_perl, UV flags)
10854{
1d7c1841 10855#ifdef PERL_IMPLICIT_SYS
c43294b8
AB
10856
10857 /* perlhost.h so we need to call into it
10858 to clone the host, CPerlHost should have a c interface, sky */
10859
10860 if (flags & CLONEf_CLONE_HOST) {
10861 return perl_clone_host(proto_perl,flags);
10862 }
10863 return perl_clone_using(proto_perl, flags,
1d7c1841
GS
10864 proto_perl->IMem,
10865 proto_perl->IMemShared,
10866 proto_perl->IMemParse,
10867 proto_perl->IEnv,
10868 proto_perl->IStdIO,
10869 proto_perl->ILIO,
10870 proto_perl->IDir,
10871 proto_perl->ISock,
10872 proto_perl->IProc);
10873}
10874
10875PerlInterpreter *
10876perl_clone_using(PerlInterpreter *proto_perl, UV flags,
10877 struct IPerlMem* ipM, struct IPerlMem* ipMS,
10878 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
10879 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
10880 struct IPerlDir* ipD, struct IPerlSock* ipS,
10881 struct IPerlProc* ipP)
10882{
10883 /* XXX many of the string copies here can be optimized if they're
10884 * constants; they need to be allocated as common memory and just
10885 * their pointers copied. */
10886
10887 IV i;
64aa0685
GS
10888 CLONE_PARAMS clone_params;
10889 CLONE_PARAMS* param = &clone_params;
d2d73c3e 10890
1d7c1841 10891 PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
c7df1ae8
NC
10892 /* for each stash, determine whether its objects should be cloned */
10893 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
ba869deb 10894 PERL_SET_THX(my_perl);
1d7c1841 10895
acfe0abc 10896# ifdef DEBUGGING
a4530404 10897 Poison(my_perl, 1, PerlInterpreter);
2802784b
NC
10898 PL_op = Nullop;
10899 PL_curcop = (COP *)Nullop;
1d7c1841
GS
10900 PL_markstack = 0;
10901 PL_scopestack = 0;
10902 PL_savestack = 0;
26776375
JH
10903 PL_savestack_ix = 0;
10904 PL_savestack_max = -1;
1d7c1841 10905 PL_retstack = 0;
66fe0623 10906 PL_sig_pending = 0;
25596c82 10907 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
acfe0abc 10908# else /* !DEBUGGING */
1d7c1841 10909 Zero(my_perl, 1, PerlInterpreter);
acfe0abc 10910# endif /* DEBUGGING */
1d7c1841
GS
10911
10912 /* host pointers */
10913 PL_Mem = ipM;
10914 PL_MemShared = ipMS;
10915 PL_MemParse = ipMP;
10916 PL_Env = ipE;
10917 PL_StdIO = ipStd;
10918 PL_LIO = ipLIO;
10919 PL_Dir = ipD;
10920 PL_Sock = ipS;
10921 PL_Proc = ipP;
1d7c1841
GS
10922#else /* !PERL_IMPLICIT_SYS */
10923 IV i;
64aa0685
GS
10924 CLONE_PARAMS clone_params;
10925 CLONE_PARAMS* param = &clone_params;
1d7c1841 10926 PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
c7df1ae8
NC
10927 /* for each stash, determine whether its objects should be cloned */
10928 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
ba869deb 10929 PERL_SET_THX(my_perl);
1d7c1841
GS
10930
10931# ifdef DEBUGGING
a4530404 10932 Poison(my_perl, 1, PerlInterpreter);
2802784b
NC
10933 PL_op = Nullop;
10934 PL_curcop = (COP *)Nullop;
1d7c1841
GS
10935 PL_markstack = 0;
10936 PL_scopestack = 0;
10937 PL_savestack = 0;
26776375
JH
10938 PL_savestack_ix = 0;
10939 PL_savestack_max = -1;
1d7c1841 10940 PL_retstack = 0;
66fe0623 10941 PL_sig_pending = 0;
25596c82 10942 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
1d7c1841
GS
10943# else /* !DEBUGGING */
10944 Zero(my_perl, 1, PerlInterpreter);
10945# endif /* DEBUGGING */
10946#endif /* PERL_IMPLICIT_SYS */
83236556 10947 param->flags = flags;
59b40662 10948 param->proto_perl = proto_perl;
1d7c1841
GS
10949
10950 /* arena roots */
10951 PL_xiv_arenaroot = NULL;
10952 PL_xiv_root = NULL;
612f20c3 10953 PL_xnv_arenaroot = NULL;
1d7c1841 10954 PL_xnv_root = NULL;
612f20c3 10955 PL_xrv_arenaroot = NULL;
1d7c1841 10956 PL_xrv_root = NULL;
612f20c3 10957 PL_xpv_arenaroot = NULL;
1d7c1841 10958 PL_xpv_root = NULL;
612f20c3 10959 PL_xpviv_arenaroot = NULL;
1d7c1841 10960 PL_xpviv_root = NULL;
612f20c3 10961 PL_xpvnv_arenaroot = NULL;
1d7c1841 10962 PL_xpvnv_root = NULL;
612f20c3 10963 PL_xpvcv_arenaroot = NULL;
1d7c1841 10964 PL_xpvcv_root = NULL;
612f20c3 10965 PL_xpvav_arenaroot = NULL;
1d7c1841 10966 PL_xpvav_root = NULL;
612f20c3 10967 PL_xpvhv_arenaroot = NULL;
1d7c1841 10968 PL_xpvhv_root = NULL;
612f20c3 10969 PL_xpvmg_arenaroot = NULL;
1d7c1841 10970 PL_xpvmg_root = NULL;
69ddb3b9
NC
10971 PL_xpvgv_arenaroot = NULL;
10972 PL_xpvgv_root = NULL;
612f20c3 10973 PL_xpvlv_arenaroot = NULL;
1d7c1841 10974 PL_xpvlv_root = NULL;
612f20c3 10975 PL_xpvbm_arenaroot = NULL;
1d7c1841 10976 PL_xpvbm_root = NULL;
612f20c3 10977 PL_he_arenaroot = NULL;
1d7c1841 10978 PL_he_root = NULL;
72c9d3b6
NC
10979#if defined(USE_ITHREADS)
10980 PL_pte_arenaroot = NULL;
10981 PL_pte_root = NULL;
10982#endif
1d7c1841
GS
10983 PL_nice_chunk = NULL;
10984 PL_nice_chunk_size = 0;
10985 PL_sv_count = 0;
10986 PL_sv_objcount = 0;
10987 PL_sv_root = Nullsv;
10988 PL_sv_arenaroot = Nullsv;
10989
10990 PL_debug = proto_perl->Idebug;
10991
1a21fcb5
NC
10992 PL_hash_seed = proto_perl->Ihash_seed;
10993 PL_rehash_seed = proto_perl->Irehash_seed;
10994
e5dd39fc 10995#ifdef USE_REENTRANT_API
1c693a8c
NC
10996 /* XXX: things like -Dm will segfault here in perlio, but doing
10997 * PERL_SET_CONTEXT(proto_perl);
10998 * breaks too many other things
10999 */
59bd0823 11000 Perl_reentrant_init(aTHX);
e5dd39fc
AB
11001#endif
11002
1d7c1841
GS
11003 /* create SV map for pointer relocation */
11004 PL_ptr_table = ptr_table_new();
11005
11006 /* initialize these special pointers as early as possible */
11007 SvANY(&PL_sv_undef) = NULL;
11008 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
11009 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL;
11010 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
11011
1d7c1841 11012 SvANY(&PL_sv_no) = new_XPVNV();
1d7c1841 11013 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
0766c23b
NC
11014 SvFLAGS(&PL_sv_no) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
11015 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
0da6cfda 11016 SvPV_set(&PL_sv_no, SAVEPVN(PL_No, 0));
a8dc4fe8
SP
11017 SvCUR_set(&PL_sv_no, 0);
11018 SvLEN_set(&PL_sv_no, 1);
0da6cfda
SP
11019 SvIV_set(&PL_sv_no, 0);
11020 SvNV_set(&PL_sv_no, 0);
1d7c1841
GS
11021 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
11022
1d7c1841 11023 SvANY(&PL_sv_yes) = new_XPVNV();
1d7c1841 11024 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
0766c23b
NC
11025 SvFLAGS(&PL_sv_yes) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
11026 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
0da6cfda 11027 SvPV_set(&PL_sv_yes, SAVEPVN(PL_Yes, 1));
a8dc4fe8
SP
11028 SvCUR_set(&PL_sv_yes, 1);
11029 SvLEN_set(&PL_sv_yes, 2);
0da6cfda
SP
11030 SvIV_set(&PL_sv_yes, 1);
11031 SvNV_set(&PL_sv_yes, 1);
1d7c1841
GS
11032 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
11033
05ec9bb3 11034 /* create (a non-shared!) shared string table */
1d7c1841
GS
11035 PL_strtab = newHV();
11036 HvSHAREKEYS_off(PL_strtab);
1a21fcb5 11037 hv_ksplit(PL_strtab, HvTOTALKEYS(proto_perl->Istrtab));
1d7c1841
GS
11038 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
11039
05ec9bb3
NIS
11040 PL_compiling = proto_perl->Icompiling;
11041
11042 /* These two PVs will be free'd special way so must set them same way op.c does */
11043 PL_compiling.cop_stashpv = savesharedpv(PL_compiling.cop_stashpv);
11044 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_stashpv, PL_compiling.cop_stashpv);
11045
11046 PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file);
11047 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file);
11048
1d7c1841
GS
11049 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
11050 if (!specialWARN(PL_compiling.cop_warnings))
d2d73c3e 11051 PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings, param);
ac27b0f5 11052 if (!specialCopIO(PL_compiling.cop_io))
d2d73c3e 11053 PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io, param);
1d7c1841
GS
11054 PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl);
11055
11056 /* pseudo environmental stuff */
11057 PL_origargc = proto_perl->Iorigargc;
7223e9d8 11058 PL_origargv = proto_perl->Iorigargv;
d2d73c3e 11059
d2d73c3e
AB
11060 param->stashes = newAV(); /* Setup array of objects to call clone on */
11061
a1ea730d 11062#ifdef PERLIO_LAYERS
3a1ee7e8
NIS
11063 /* Clone PerlIO tables as soon as we can handle general xx_dup() */
11064 PerlIO_clone(aTHX_ proto_perl, param);
a1ea730d 11065#endif
d2d73c3e
AB
11066
11067 PL_envgv = gv_dup(proto_perl->Ienvgv, param);
11068 PL_incgv = gv_dup(proto_perl->Iincgv, param);
11069 PL_hintgv = gv_dup(proto_perl->Ihintgv, param);
1d7c1841 11070 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
d2d73c3e
AB
11071 PL_diehook = sv_dup_inc(proto_perl->Idiehook, param);
11072 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param);
1d7c1841
GS
11073
11074 /* switches */
11075 PL_minus_c = proto_perl->Iminus_c;
d2d73c3e 11076 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param);
1d7c1841
GS
11077 PL_localpatches = proto_perl->Ilocalpatches;
11078 PL_splitstr = proto_perl->Isplitstr;
11079 PL_preprocess = proto_perl->Ipreprocess;
11080 PL_minus_n = proto_perl->Iminus_n;
11081 PL_minus_p = proto_perl->Iminus_p;
11082 PL_minus_l = proto_perl->Iminus_l;
11083 PL_minus_a = proto_perl->Iminus_a;
11084 PL_minus_F = proto_perl->Iminus_F;
11085 PL_doswitches = proto_perl->Idoswitches;
11086 PL_dowarn = proto_perl->Idowarn;
11087 PL_doextract = proto_perl->Idoextract;
11088 PL_sawampersand = proto_perl->Isawampersand;
11089 PL_unsafe = proto_perl->Iunsafe;
11090 PL_inplace = SAVEPV(proto_perl->Iinplace);
d2d73c3e 11091 PL_e_script = sv_dup_inc(proto_perl->Ie_script, param);
1d7c1841
GS
11092 PL_perldb = proto_perl->Iperldb;
11093 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
1cbb0781 11094 PL_exit_flags = proto_perl->Iexit_flags;
1d7c1841
GS
11095
11096 /* magical thingies */
11097 /* XXX time(&PL_basetime) when asked for? */
11098 PL_basetime = proto_perl->Ibasetime;
d2d73c3e 11099 PL_formfeed = sv_dup(proto_perl->Iformfeed, param);
1d7c1841
GS
11100
11101 PL_maxsysfd = proto_perl->Imaxsysfd;
11102 PL_multiline = proto_perl->Imultiline;
11103 PL_statusvalue = proto_perl->Istatusvalue;
11104#ifdef VMS
11105 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
11106#endif
0a378802 11107 PL_encoding = sv_dup(proto_perl->Iencoding, param);
1d7c1841 11108
4a4c6fe3 11109 sv_setpvn(PERL_DEBUG_PAD(0), "", 0); /* For regex debugging. */
1f483ca1
JH
11110 sv_setpvn(PERL_DEBUG_PAD(1), "", 0); /* ext/re needs these */
11111 sv_setpvn(PERL_DEBUG_PAD(2), "", 0); /* even without DEBUGGING. */
4a4c6fe3 11112
d2f185dc
AMS
11113 /* Clone the regex array */
11114 PL_regex_padav = newAV();
11115 {
8c18bf38 11116 const I32 len = av_len((AV*)proto_perl->Iregex_padav);
d2f185dc 11117 SV** regexen = AvARRAY((AV*)proto_perl->Iregex_padav);
0f95fc41
AB
11118 av_push(PL_regex_padav,
11119 sv_dup_inc(regexen[0],param));
11120 for(i = 1; i <= len; i++) {
11121 if(SvREPADTMP(regexen[i])) {
11122 av_push(PL_regex_padav, sv_dup_inc(regexen[i], param));
8cf8f3d1 11123 } else {
0f95fc41
AB
11124 av_push(PL_regex_padav,
11125 SvREFCNT_inc(
8cf8f3d1 11126 newSViv(PTR2IV(re_dup(INT2PTR(REGEXP *,
cbfa9890 11127 SvIVX(regexen[i])), param)))
0f95fc41
AB
11128 ));
11129 }
d2f185dc
AMS
11130 }
11131 }
11132 PL_regex_pad = AvARRAY(PL_regex_padav);
1fcf4c12 11133
1d7c1841 11134 /* shortcuts to various I/O objects */
d2d73c3e
AB
11135 PL_stdingv = gv_dup(proto_perl->Istdingv, param);
11136 PL_stderrgv = gv_dup(proto_perl->Istderrgv, param);
11137 PL_defgv = gv_dup(proto_perl->Idefgv, param);
11138 PL_argvgv = gv_dup(proto_perl->Iargvgv, param);
11139 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param);
11140 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param);
1d7c1841
GS
11141
11142 /* shortcuts to regexp stuff */
d2d73c3e 11143 PL_replgv = gv_dup(proto_perl->Ireplgv, param);
1d7c1841
GS
11144
11145 /* shortcuts to misc objects */
d2d73c3e 11146 PL_errgv = gv_dup(proto_perl->Ierrgv, param);
1d7c1841
GS
11147
11148 /* shortcuts to debugging objects */
d2d73c3e
AB
11149 PL_DBgv = gv_dup(proto_perl->IDBgv, param);
11150 PL_DBline = gv_dup(proto_perl->IDBline, param);
11151 PL_DBsub = gv_dup(proto_perl->IDBsub, param);
11152 PL_DBsingle = sv_dup(proto_perl->IDBsingle, param);
11153 PL_DBtrace = sv_dup(proto_perl->IDBtrace, param);
11154 PL_DBsignal = sv_dup(proto_perl->IDBsignal, param);
11155 PL_lineary = av_dup(proto_perl->Ilineary, param);
11156 PL_dbargs = av_dup(proto_perl->Idbargs, param);
1d7c1841
GS
11157
11158 /* symbol tables */
d2d73c3e
AB
11159 PL_defstash = hv_dup_inc(proto_perl->Tdefstash, param);
11160 PL_curstash = hv_dup(proto_perl->Tcurstash, param);
11161 PL_nullstash = hv_dup(proto_perl->Inullstash, param);
11162 PL_debstash = hv_dup(proto_perl->Idebstash, param);
11163 PL_globalstash = hv_dup(proto_perl->Iglobalstash, param);
11164 PL_curstname = sv_dup_inc(proto_perl->Icurstname, param);
11165
11166 PL_beginav = av_dup_inc(proto_perl->Ibeginav, param);
ee1c5a4e 11167 PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param);
5b7ea690 11168 PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param);
d2d73c3e
AB
11169 PL_endav = av_dup_inc(proto_perl->Iendav, param);
11170 PL_checkav = av_dup_inc(proto_perl->Icheckav, param);
11171 PL_initav = av_dup_inc(proto_perl->Iinitav, param);
1d7c1841
GS
11172
11173 PL_sub_generation = proto_perl->Isub_generation;
11174
11175 /* funky return mechanisms */
11176 PL_forkprocess = proto_perl->Iforkprocess;
11177
11178 /* subprocess state */
d2d73c3e 11179 PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param);
1d7c1841
GS
11180
11181 /* internal state */
11182 PL_tainting = proto_perl->Itainting;
654c77f7 11183 PL_taint_warn = proto_perl->Itaint_warn;
1d7c1841
GS
11184 PL_maxo = proto_perl->Imaxo;
11185 if (proto_perl->Iop_mask)
11186 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
11187 else
11188 PL_op_mask = Nullch;
11189
11190 /* current interpreter roots */
d2d73c3e 11191 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param);
1d7c1841
GS
11192 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
11193 PL_main_start = proto_perl->Imain_start;
e977893f 11194 PL_eval_root = proto_perl->Ieval_root;
1d7c1841
GS
11195 PL_eval_start = proto_perl->Ieval_start;
11196
11197 /* runtime control stuff */
11198 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
11199 PL_copline = proto_perl->Icopline;
11200
11201 PL_filemode = proto_perl->Ifilemode;
11202 PL_lastfd = proto_perl->Ilastfd;
11203 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
11204 PL_Argv = NULL;
11205 PL_Cmd = Nullch;
11206 PL_gensym = proto_perl->Igensym;
11207 PL_preambled = proto_perl->Ipreambled;
d2d73c3e 11208 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param);
1d7c1841
GS
11209 PL_laststatval = proto_perl->Ilaststatval;
11210 PL_laststype = proto_perl->Ilaststype;
11211 PL_mess_sv = Nullsv;
11212
d2d73c3e 11213 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param);
1d7c1841
GS
11214 PL_ofmt = SAVEPV(proto_perl->Iofmt);
11215
11216 /* interpreter atexit processing */
11217 PL_exitlistlen = proto_perl->Iexitlistlen;
11218 if (PL_exitlistlen) {
11219 New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
11220 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
11221 }
11222 else
11223 PL_exitlist = (PerlExitListEntry*)NULL;
d2d73c3e 11224 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param);
19e8ce8e
AB
11225 PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param);
11226 PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param);
1d7c1841
GS
11227
11228 PL_profiledata = NULL;
a8fc9800 11229 PL_rsfp = fp_dup(proto_perl->Irsfp, '<', param);
1d7c1841 11230 /* PL_rsfp_filters entries have fake IoDIRP() */
d2d73c3e 11231 PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters, param);
1d7c1841 11232
d2d73c3e 11233 PL_compcv = cv_dup(proto_perl->Icompcv, param);
9755d405
JH
11234
11235 PAD_CLONE_VARS(proto_perl, param);
1d7c1841
GS
11236
11237#ifdef HAVE_INTERP_INTERN
11238 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
11239#endif
11240
11241 /* more statics moved here */
11242 PL_generation = proto_perl->Igeneration;
d2d73c3e 11243 PL_DBcv = cv_dup(proto_perl->IDBcv, param);
1d7c1841
GS
11244
11245 PL_in_clean_objs = proto_perl->Iin_clean_objs;
11246 PL_in_clean_all = proto_perl->Iin_clean_all;
11247
11248 PL_uid = proto_perl->Iuid;
11249 PL_euid = proto_perl->Ieuid;
11250 PL_gid = proto_perl->Igid;
11251 PL_egid = proto_perl->Iegid;
11252 PL_nomemok = proto_perl->Inomemok;
11253 PL_an = proto_perl->Ian;
1d7c1841
GS
11254 PL_op_seqmax = proto_perl->Iop_seqmax;
11255 PL_evalseq = proto_perl->Ievalseq;
11256 PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */
11257 PL_origalen = proto_perl->Iorigalen;
11258 PL_pidstatus = newHV(); /* XXX flag for cloning? */
11259 PL_osname = SAVEPV(proto_perl->Iosname);
8257dec7 11260 PL_sh_path_compat = proto_perl->Ish_path_compat; /* XXX never deallocated */
1d7c1841
GS
11261 PL_sighandlerp = proto_perl->Isighandlerp;
11262
11263
11264 PL_runops = proto_perl->Irunops;
11265
11266 Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char);
11267
11268#ifdef CSH
11269 PL_cshlen = proto_perl->Icshlen;
74f1b2b8 11270 PL_cshname = proto_perl->Icshname; /* XXX never deallocated */
1d7c1841
GS
11271#endif
11272
11273 PL_lex_state = proto_perl->Ilex_state;
11274 PL_lex_defer = proto_perl->Ilex_defer;
11275 PL_lex_expect = proto_perl->Ilex_expect;
11276 PL_lex_formbrack = proto_perl->Ilex_formbrack;
11277 PL_lex_dojoin = proto_perl->Ilex_dojoin;
11278 PL_lex_starts = proto_perl->Ilex_starts;
d2d73c3e
AB
11279 PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff, param);
11280 PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl, param);
1d7c1841
GS
11281 PL_lex_op = proto_perl->Ilex_op;
11282 PL_lex_inpat = proto_perl->Ilex_inpat;
11283 PL_lex_inwhat = proto_perl->Ilex_inwhat;
11284 PL_lex_brackets = proto_perl->Ilex_brackets;
11285 i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets);
11286 PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i);
11287 PL_lex_casemods = proto_perl->Ilex_casemods;
11288 i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods);
11289 PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i);
11290
11291 Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE);
11292 Copy(proto_perl->Inexttype, PL_nexttype, 5, I32);
11293 PL_nexttoke = proto_perl->Inexttoke;
11294
1d773130
TB
11295 /* XXX This is probably masking the deeper issue of why
11296 * SvANY(proto_perl->Ilinestr) can be NULL at this point. For test case:
11297 * http://archive.develooper.com/perl5-porters%40perl.org/msg83298.html
11298 * (A little debugging with a watchpoint on it may help.)
11299 */
389edf32
TB
11300 if (SvANY(proto_perl->Ilinestr)) {
11301 PL_linestr = sv_dup_inc(proto_perl->Ilinestr, param);
fdac8c4b 11302 i = proto_perl->Ibufptr - SvPVX_const(proto_perl->Ilinestr);
389edf32 11303 PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
fdac8c4b 11304 i = proto_perl->Ioldbufptr - SvPVX_const(proto_perl->Ilinestr);
389edf32 11305 PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
fdac8c4b 11306 i = proto_perl->Ioldoldbufptr - SvPVX_const(proto_perl->Ilinestr);
389edf32 11307 PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
fdac8c4b 11308 i = proto_perl->Ilinestart - SvPVX_const(proto_perl->Ilinestr);
389edf32
TB
11309 PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11310 }
11311 else {
11312 PL_linestr = NEWSV(65,79);
11313 sv_upgrade(PL_linestr,SVt_PVIV);
11314 sv_setpvn(PL_linestr,"",0);
11315 PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
11316 }
1d7c1841 11317 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
1d7c1841
GS
11318 PL_pending_ident = proto_perl->Ipending_ident;
11319 PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */
11320
11321 PL_expect = proto_perl->Iexpect;
11322
11323 PL_multi_start = proto_perl->Imulti_start;
11324 PL_multi_end = proto_perl->Imulti_end;
11325 PL_multi_open = proto_perl->Imulti_open;
11326 PL_multi_close = proto_perl->Imulti_close;
11327
11328 PL_error_count = proto_perl->Ierror_count;
11329 PL_subline = proto_perl->Isubline;
d2d73c3e 11330 PL_subname = sv_dup_inc(proto_perl->Isubname, param);
1d7c1841 11331
1d773130 11332 /* XXX See comment on SvANY(proto_perl->Ilinestr) above */
389edf32 11333 if (SvANY(proto_perl->Ilinestr)) {
fdac8c4b 11334 i = proto_perl->Ilast_uni - SvPVX_const(proto_perl->Ilinestr);
389edf32 11335 PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
fdac8c4b 11336 i = proto_perl->Ilast_lop - SvPVX_const(proto_perl->Ilinestr);
389edf32
TB
11337 PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11338 PL_last_lop_op = proto_perl->Ilast_lop_op;
11339 }
11340 else {
11341 PL_last_uni = SvPVX(PL_linestr);
11342 PL_last_lop = SvPVX(PL_linestr);
11343 PL_last_lop_op = 0;
11344 }
1d7c1841 11345 PL_in_my = proto_perl->Iin_my;
d2d73c3e 11346 PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash, param);
1d7c1841
GS
11347#ifdef FCRYPT
11348 PL_cryptseen = proto_perl->Icryptseen;
11349#endif
11350
11351 PL_hints = proto_perl->Ihints;
11352
11353 PL_amagic_generation = proto_perl->Iamagic_generation;
11354
11355#ifdef USE_LOCALE_COLLATE
11356 PL_collation_ix = proto_perl->Icollation_ix;
11357 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
11358 PL_collation_standard = proto_perl->Icollation_standard;
11359 PL_collxfrm_base = proto_perl->Icollxfrm_base;
11360 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
11361#endif /* USE_LOCALE_COLLATE */
11362
11363#ifdef USE_LOCALE_NUMERIC
11364 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
11365 PL_numeric_standard = proto_perl->Inumeric_standard;
11366 PL_numeric_local = proto_perl->Inumeric_local;
d2d73c3e 11367 PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param);
1d7c1841
GS
11368#endif /* !USE_LOCALE_NUMERIC */
11369
11370 /* utf8 character classes */
d2d73c3e
AB
11371 PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum, param);
11372 PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc, param);
11373 PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii, param);
11374 PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha, param);
11375 PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space, param);
11376 PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl, param);
11377 PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph, param);
11378 PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit, param);
11379 PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper, param);
11380 PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower, param);
11381 PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print, param);
11382 PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct, param);
11383 PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit, param);
11384 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param);
11385 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param);
11386 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param);
11387 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param);
b4e400f9 11388 PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param);
82686b01
JH
11389 PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param);
11390 PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param);
1d7c1841 11391
654c77f7 11392 /* Did the locale setup indicate UTF-8? */
30260be2 11393 PL_utf8locale = proto_perl->Iutf8locale;
654c77f7
JH
11394 /* Unicode features (see perlrun/-C) */
11395 PL_unicode = proto_perl->Iunicode;
11396
11397 /* Pre-5.8 signals control */
11398 PL_signals = proto_perl->Isignals;
11399
11400 /* times() ticks per second */
11401 PL_clocktick = proto_perl->Iclocktick;
11402
11403 /* Recursion stopper for PerlIO_find_layer */
11404 PL_in_load_module = proto_perl->Iin_load_module;
11405
11406 /* sort() routine */
11407 PL_sort_RealCmp = proto_perl->Isort_RealCmp;
11408
630cd32b
JH
11409 /* Not really needed/useful since the reenrant_retint is "volatile",
11410 * but do it for consistency's sake. */
11411 PL_reentrant_retint = proto_perl->Ireentrant_retint;
11412
0710cc63
JH
11413 /* Hooks to shared SVs and locks. */
11414 PL_sharehook = proto_perl->Isharehook;
11415 PL_lockhook = proto_perl->Ilockhook;
11416 PL_unlockhook = proto_perl->Iunlockhook;
11417 PL_threadhook = proto_perl->Ithreadhook;
11418
11419 PL_runops_std = proto_perl->Irunops_std;
11420 PL_runops_dbg = proto_perl->Irunops_dbg;
11421
11422#ifdef THREADS_HAVE_PIDS
11423 PL_ppid = proto_perl->Ippid;
11424#endif
11425
1d7c1841
GS
11426 /* swatch cache */
11427 PL_last_swash_hv = Nullhv; /* reinits on demand */
11428 PL_last_swash_klen = 0;
11429 PL_last_swash_key[0]= '\0';
11430 PL_last_swash_tmps = (U8*)NULL;
11431 PL_last_swash_slen = 0;
11432
11433 /* perly.c globals */
11434 PL_yydebug = proto_perl->Iyydebug;
11435 PL_yynerrs = proto_perl->Iyynerrs;
11436 PL_yyerrflag = proto_perl->Iyyerrflag;
11437 PL_yychar = proto_perl->Iyychar;
11438 PL_yyval = proto_perl->Iyyval;
11439 PL_yylval = proto_perl->Iyylval;
11440
11441 PL_glob_index = proto_perl->Iglob_index;
11442 PL_srand_called = proto_perl->Isrand_called;
11443 PL_uudmap['M'] = 0; /* reinits on demand */
11444 PL_bitcount = Nullch; /* reinits on demand */
11445
66fe0623
NIS
11446 if (proto_perl->Ipsig_pend) {
11447 Newz(0, PL_psig_pend, SIG_SIZE, int);
9dd79c3f 11448 }
66fe0623
NIS
11449 else {
11450 PL_psig_pend = (int*)NULL;
11451 }
11452
1d7c1841 11453 if (proto_perl->Ipsig_ptr) {
76d3c696
JH
11454 Newz(0, PL_psig_ptr, SIG_SIZE, SV*);
11455 Newz(0, PL_psig_name, SIG_SIZE, SV*);
76d3c696 11456 for (i = 1; i < SIG_SIZE; i++) {
d2d73c3e
AB
11457 PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i], param);
11458 PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i], param);
1d7c1841
GS
11459 }
11460 }
11461 else {
11462 PL_psig_ptr = (SV**)NULL;
11463 PL_psig_name = (SV**)NULL;
11464 }
11465
11466 /* thrdvar.h stuff */
11467
a0739874 11468 if (flags & CLONEf_COPY_STACKS) {
1d7c1841
GS
11469 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
11470 PL_tmps_ix = proto_perl->Ttmps_ix;
11471 PL_tmps_max = proto_perl->Ttmps_max;
11472 PL_tmps_floor = proto_perl->Ttmps_floor;
11473 Newz(50, PL_tmps_stack, PL_tmps_max, SV*);
11474 i = 0;
11475 while (i <= PL_tmps_ix) {
d2d73c3e 11476 PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i], param);
1d7c1841
GS
11477 ++i;
11478 }
11479
11480 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
11481 i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack;
11482 Newz(54, PL_markstack, i, I32);
11483 PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max
11484 - proto_perl->Tmarkstack);
11485 PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr
11486 - proto_perl->Tmarkstack);
11487 Copy(proto_perl->Tmarkstack, PL_markstack,
11488 PL_markstack_ptr - PL_markstack + 1, I32);
11489
11490 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
11491 * NOTE: unlike the others! */
11492 PL_scopestack_ix = proto_perl->Tscopestack_ix;
11493 PL_scopestack_max = proto_perl->Tscopestack_max;
11494 Newz(54, PL_scopestack, PL_scopestack_max, I32);
11495 Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32);
11496
11497 /* next push_return() sets PL_retstack[PL_retstack_ix]
11498 * NOTE: unlike the others! */
11499 PL_retstack_ix = proto_perl->Tretstack_ix;
11500 PL_retstack_max = proto_perl->Tretstack_max;
11501 Newz(54, PL_retstack, PL_retstack_max, OP*);
ce0a1ae0 11502 Copy(proto_perl->Tretstack, PL_retstack, PL_retstack_ix, OP*);
1d7c1841
GS
11503
11504 /* NOTE: si_dup() looks at PL_markstack */
d2d73c3e 11505 PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo, param);
1d7c1841
GS
11506
11507 /* PL_curstack = PL_curstackinfo->si_stack; */
d2d73c3e
AB
11508 PL_curstack = av_dup(proto_perl->Tcurstack, param);
11509 PL_mainstack = av_dup(proto_perl->Tmainstack, param);
1d7c1841
GS
11510
11511 /* next PUSHs() etc. set *(PL_stack_sp+1) */
11512 PL_stack_base = AvARRAY(PL_curstack);
11513 PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp
11514 - proto_perl->Tstack_base);
11515 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
11516
11517 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
11518 * NOTE: unlike the others! */
11519 PL_savestack_ix = proto_perl->Tsavestack_ix;
11520 PL_savestack_max = proto_perl->Tsavestack_max;
11521 /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/
d2d73c3e 11522 PL_savestack = ss_dup(proto_perl, param);
1d7c1841
GS
11523 }
11524 else {
11525 init_stacks();
985e7056 11526 ENTER; /* perl_destruct() wants to LEAVE; */
1d7c1841
GS
11527 }
11528
11529 PL_start_env = proto_perl->Tstart_env; /* XXXXXX */
11530 PL_top_env = &PL_start_env;
11531
11532 PL_op = proto_perl->Top;
11533
11534 PL_Sv = Nullsv;
11535 PL_Xpv = (XPV*)NULL;
11536 PL_na = proto_perl->Tna;
11537
11538 PL_statbuf = proto_perl->Tstatbuf;
11539 PL_statcache = proto_perl->Tstatcache;
d2d73c3e
AB
11540 PL_statgv = gv_dup(proto_perl->Tstatgv, param);
11541 PL_statname = sv_dup_inc(proto_perl->Tstatname, param);
1d7c1841
GS
11542#ifdef HAS_TIMES
11543 PL_timesbuf = proto_perl->Ttimesbuf;
11544#endif
11545
11546 PL_tainted = proto_perl->Ttainted;
11547 PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */
d2d73c3e
AB
11548 PL_rs = sv_dup_inc(proto_perl->Trs, param);
11549 PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv, param);
11550 PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv, param);
11551 PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv, param);
1d7c1841 11552 PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */
d2d73c3e
AB
11553 PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget, param);
11554 PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget, param);
11555 PL_formtarget = sv_dup(proto_perl->Tformtarget, param);
1d7c1841
GS
11556
11557 PL_restartop = proto_perl->Trestartop;
11558 PL_in_eval = proto_perl->Tin_eval;
11559 PL_delaymagic = proto_perl->Tdelaymagic;
11560 PL_dirty = proto_perl->Tdirty;
11561 PL_localizing = proto_perl->Tlocalizing;
11562
14dd3ad8 11563#ifdef PERL_FLEXIBLE_EXCEPTIONS
1d7c1841 11564 PL_protect = proto_perl->Tprotect;
14dd3ad8 11565#endif
d2d73c3e 11566 PL_errors = sv_dup_inc(proto_perl->Terrors, param);
73c86719 11567 PL_hv_fetch_ent_mh = Nullhe;
1d7c1841
GS
11568 PL_modcount = proto_perl->Tmodcount;
11569 PL_lastgotoprobe = Nullop;
11570 PL_dumpindent = proto_perl->Tdumpindent;
11571
11572 PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl);
d2d73c3e
AB
11573 PL_sortstash = hv_dup(proto_perl->Tsortstash, param);
11574 PL_firstgv = gv_dup(proto_perl->Tfirstgv, param);
11575 PL_secondgv = gv_dup(proto_perl->Tsecondgv, param);
1d7c1841
GS
11576 PL_sortcxix = proto_perl->Tsortcxix;
11577 PL_efloatbuf = Nullch; /* reinits on demand */
11578 PL_efloatsize = 0; /* reinits on demand */
11579
11580 /* regex stuff */
11581
11582 PL_screamfirst = NULL;
11583 PL_screamnext = NULL;
11584 PL_maxscream = -1; /* reinits on demand */
11585 PL_lastscream = Nullsv;
11586
11587 PL_watchaddr = NULL;
11588 PL_watchok = Nullch;
11589
11590 PL_regdummy = proto_perl->Tregdummy;
11591 PL_regcomp_parse = Nullch;
11592 PL_regxend = Nullch;
11593 PL_regcode = (regnode*)NULL;
11594 PL_regnaughty = 0;
11595 PL_regsawback = 0;
11596 PL_regprecomp = Nullch;
11597 PL_regnpar = 0;
11598 PL_regsize = 0;
11599 PL_regflags = 0;
11600 PL_regseen = 0;
11601 PL_seen_zerolen = 0;
11602 PL_seen_evals = 0;
11603 PL_regcomp_rx = (regexp*)NULL;
11604 PL_extralen = 0;
11605 PL_colorset = 0; /* reinits PL_colors[] */
11606 /*PL_colors[6] = {0,0,0,0,0,0};*/
11607 PL_reg_whilem_seen = 0;
11608 PL_reginput = Nullch;
11609 PL_regbol = Nullch;
11610 PL_regeol = Nullch;
11611 PL_regstartp = (I32*)NULL;
11612 PL_regendp = (I32*)NULL;
11613 PL_reglastparen = (U32*)NULL;
4e58e0cf 11614 PL_reglastcloseparen = (U32*)NULL;
1d7c1841 11615 PL_regtill = Nullch;
1d7c1841
GS
11616 PL_reg_start_tmp = (char**)NULL;
11617 PL_reg_start_tmpl = 0;
11618 PL_regdata = (struct reg_data*)NULL;
11619 PL_bostr = Nullch;
11620 PL_reg_flags = 0;
11621 PL_reg_eval_set = 0;
11622 PL_regnarrate = 0;
11623 PL_regprogram = (regnode*)NULL;
11624 PL_regindent = 0;
11625 PL_regcc = (CURCUR*)NULL;
11626 PL_reg_call_cc = (struct re_cc_state*)NULL;
11627 PL_reg_re = (regexp*)NULL;
11628 PL_reg_ganch = Nullch;
11629 PL_reg_sv = Nullsv;
53c4c00c 11630 PL_reg_match_utf8 = FALSE;
1d7c1841
GS
11631 PL_reg_magic = (MAGIC*)NULL;
11632 PL_reg_oldpos = 0;
11633 PL_reg_oldcurpm = (PMOP*)NULL;
11634 PL_reg_curpm = (PMOP*)NULL;
11635 PL_reg_oldsaved = Nullch;
11636 PL_reg_oldsavedlen = 0;
11637 PL_reg_maxiter = 0;
11638 PL_reg_leftiter = 0;
11639 PL_reg_poscache = Nullch;
11640 PL_reg_poscache_size= 0;
11641
11642 /* RE engine - function pointers */
11643 PL_regcompp = proto_perl->Tregcompp;
11644 PL_regexecp = proto_perl->Tregexecp;
11645 PL_regint_start = proto_perl->Tregint_start;
11646 PL_regint_string = proto_perl->Tregint_string;
11647 PL_regfree = proto_perl->Tregfree;
11648
11649 PL_reginterp_cnt = 0;
11650 PL_reg_starttry = 0;
11651
a2efc822
SC
11652 /* Pluggable optimizer */
11653 PL_peepp = proto_perl->Tpeepp;
11654
c0401c5d
JH
11655 PL_stashcache = newHV();
11656
a0739874
DM
11657 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
11658 ptr_table_free(PL_ptr_table);
11659 PL_ptr_table = NULL;
11660 }
8cf8f3d1 11661
f284b03f
AMS
11662 /* Call the ->CLONE method, if it exists, for each of the stashes
11663 identified by sv_dup() above.
11664 */
d2d73c3e
AB
11665 while(av_len(param->stashes) != -1) {
11666 HV* stash = (HV*) av_shift(param->stashes);
f284b03f
AMS
11667 GV* cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
11668 if (cloner && GvCV(cloner)) {
11669 dSP;
11670 ENTER;
11671 SAVETMPS;
11672 PUSHMARK(SP);
26ab6a78 11673 XPUSHs(sv_2mortal(newSVpv(HvNAME_get(stash), 0)));
f284b03f
AMS
11674 PUTBACK;
11675 call_sv((SV*)GvCV(cloner), G_DISCARD);
11676 FREETMPS;
11677 LEAVE;
11678 }
4a09accc 11679 }
a0739874 11680
dc507217 11681 SvREFCNT_dec(param->stashes);
dc507217 11682
d2e2df35
MHM
11683 /* orphaned? eg threads->new inside BEGIN or use */
11684 if (PL_compcv && ! SvREFCNT(PL_compcv)) {
8c18bf38 11685 (void)SvREFCNT_inc(PL_compcv);
d2e2df35
MHM
11686 SAVEFREESV(PL_compcv);
11687 }
11688
1d7c1841 11689 return my_perl;
1d7c1841
GS
11690}
11691
1d7c1841 11692#endif /* USE_ITHREADS */
a0ae6670 11693
9f4817db 11694/*
ccfc67b7
JH
11695=head1 Unicode Support
11696
9f4817db
JH
11697=for apidoc sv_recode_to_utf8
11698
5d170f3a
JH
11699The encoding is assumed to be an Encode object, on entry the PV
11700of the sv is assumed to be octets in that encoding, and the sv
11701will be converted into Unicode (and UTF-8).
9f4817db 11702
5d170f3a
JH
11703If the sv already is UTF-8 (or if it is not POK), or if the encoding
11704is not a reference, nothing is done to the sv. If the encoding is not
1768d7eb
JH
11705an C<Encode::XS> Encoding object, bad things will happen.
11706(See F<lib/encoding.pm> and L<Encode>).
9f4817db 11707
5d170f3a 11708The PV of the sv is returned.
9f4817db 11709
5d170f3a
JH
11710=cut */
11711
11712char *
11713Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
11714{
975adce1 11715 if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
5b7ea690
JH
11716 SV *uni;
11717 STRLEN len;
11718 char *s;
11719 dSP;
11720 ENTER;
11721 SAVETMPS;
975adce1 11722 save_re_context();
5b7ea690
JH
11723 PUSHMARK(sp);
11724 EXTEND(SP, 3);
11725 XPUSHs(encoding);
11726 XPUSHs(sv);
f9893866
NIS
11727/*
11728 NI-S 2002/07/09
11729 Passing sv_yes is wrong - it needs to be or'ed set of constants
11730 for Encode::XS, while UTf-8 decode (currently) assumes a true value means
11731 remove converted chars from source.
11732
11733 Both will default the value - let them.
11734
5b7ea690 11735 XPUSHs(&PL_sv_yes);
f9893866 11736*/
5b7ea690
JH
11737 PUTBACK;
11738 call_method("decode", G_SCALAR);
11739 SPAGAIN;
11740 uni = POPs;
11741 PUTBACK;
11742 s = SvPV(uni, len);
fdac8c4b 11743 if (s != SvPVX_const(sv)) {
5b7ea690 11744 SvGROW(sv, len + 1);
fdac8c4b 11745 Move(s, SvPVX_const(sv), len, char);
5b7ea690
JH
11746 SvCUR_set(sv, len);
11747 SvPVX(sv)[len] = 0;
11748 }
11749 FREETMPS;
11750 LEAVE;
5b7ea690 11751 SvUTF8_on(sv);
765545f3 11752 return SvPVX(sv);
f9893866 11753 }
765545f3 11754 return SvPOKp(sv) ? SvPVX(sv) : NULL;
9f4817db
JH
11755}
11756
975adce1
JH
11757/*
11758=for apidoc sv_cat_decode
11759
11760The encoding is assumed to be an Encode object, the PV of the ssv is
11761assumed to be octets in that encoding and decoding the input starts
11762from the position which (PV + *offset) pointed to. The dsv will be
11763concatenated the decoded UTF-8 string from ssv. Decoding will terminate
11764when the string tstr appears in decoding output or the input ends on
11765the PV of the ssv. The value which the offset points will be modified
11766to the last input position on the ssv.
68795e93 11767
975adce1
JH
11768Returns TRUE if the terminator was found, else returns FALSE.
11769
11770=cut */
11771
11772bool
11773Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
11774 SV *ssv, int *offset, char *tstr, int tlen)
11775{
c04fee9e 11776 bool ret = FALSE;
975adce1 11777 if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding) && offset) {
975adce1
JH
11778 SV *offsv;
11779 dSP;
11780 ENTER;
11781 SAVETMPS;
11782 save_re_context();
11783 PUSHMARK(sp);
11784 EXTEND(SP, 6);
11785 XPUSHs(encoding);
11786 XPUSHs(dsv);
11787 XPUSHs(ssv);
11788 XPUSHs(offsv = sv_2mortal(newSViv(*offset)));
11789 XPUSHs(sv_2mortal(newSVpvn(tstr, tlen)));
11790 PUTBACK;
11791 call_method("cat_decode", G_SCALAR);
11792 SPAGAIN;
11793 ret = SvTRUE(TOPs);
11794 *offset = SvIV(offsv);
11795 PUTBACK;
11796 FREETMPS;
11797 LEAVE;
975adce1 11798 }
c04fee9e
JH
11799 else
11800 Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
11801 return ret;
975adce1 11802}
f9893866 11803
583439ab
NC
11804/*
11805 * Local variables:
11806 * c-indentation-style: bsd
11807 * c-basic-offset: 4
11808 * indent-tabs-mode: t
11809 * End:
11810 *
d8294a4d
NC
11811 * ex: set ts=8 sts=4 sw=4 noet:
11812 */