This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make the arena size changeable at compile time, and up the default by
[perl5.git] / sv.c
CommitLineData
a0d0e21e 1/* sv.c
79072805 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
241d1a3b 4 * 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
79072805
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
a0d0e21e 9 * "I wonder what the Entish is for 'yes' and 'no'," he thought.
645c22ef
DM
10 *
11 *
5e045b90
AMS
12 * This file contains the code that creates, manipulates and destroys
13 * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the
14 * structure of an SV, so their creation and destruction is handled
15 * here; higher-level functions are in av.c, hv.c, and so on. Opcode
16 * level functions (eg. substr, split, join) for each of the types are
17 * in the pp*.c files.
79072805
LW
18 */
19
20#include "EXTERN.h"
864dbfa3 21#define PERL_IN_SV_C
79072805 22#include "perl.h"
d2f185dc 23#include "regcomp.h"
79072805 24
51371543 25#define FCALL *f
2c5424a7 26
2f8ed50e
OS
27#ifdef __Lynx__
28/* Missing proto on LynxOS */
29 char *gconvert(double, int, int, char *);
30#endif
31
e23c8137
JH
32#ifdef PERL_UTF8_CACHE_ASSERT
33/* The cache element 0 is the Unicode offset;
34 * the cache element 1 is the byte offset of the element 0;
35 * the cache element 2 is the Unicode length of the substring;
36 * the cache element 3 is the byte length of the substring;
37 * The checking of the substring side would be good
38 * but substr() has enough code paths to make my head spin;
39 * if adding more checks watch out for the following tests:
40 * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
41 * lib/utf8.t lib/Unicode/Collate/t/index.t
42 * --jhi
43 */
44#define ASSERT_UTF8_CACHE(cache) \
45 STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); } } STMT_END
46#else
47#define ASSERT_UTF8_CACHE(cache) NOOP
48#endif
49
765f542d
NC
50#ifdef PERL_COPY_ON_WRITE
51#define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
607fa7f2 52#define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next))
b5ccf5f2 53/* This is a pessimistic view. Scalar must be purely a read-write PV to copy-
765f542d 54 on-write. */
765f542d 55#endif
645c22ef
DM
56
57/* ============================================================================
58
59=head1 Allocation and deallocation of SVs.
60
5e045b90
AMS
61An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv,
62av, hv...) contains type and reference count information, as well as a
63pointer to the body (struct xrv, xpv, xpviv...), which contains fields
64specific to each type.
65
66Normally, this allocation is done using arenas, which are approximately
671K chunks of memory parcelled up into N heads or bodies. The first slot
68in each arena is reserved, and is used to hold a link to the next arena.
69In the case of heads, the unused first slot also contains some flags and
70a note of the number of slots. Snaked through each arena chain is a
71linked list of free items; when this becomes empty, an extra arena is
72allocated and divided up into N items which are threaded into the free
73list.
645c22ef
DM
74
75The following global variables are associated with arenas:
76
77 PL_sv_arenaroot pointer to list of SV arenas
78 PL_sv_root pointer to list of free SV structures
79
80 PL_foo_arenaroot pointer to list of foo arenas,
81 PL_foo_root pointer to list of free foo bodies
82 ... for foo in xiv, xnv, xrv, xpv etc.
83
84Note that some of the larger and more rarely used body types (eg xpvio)
85are not allocated using arenas, but are instead just malloc()/free()ed as
86required. Also, if PURIFY is defined, arenas are abandoned altogether,
87with all items individually malloc()ed. In addition, a few SV heads are
88not allocated from an arena, but are instead directly created as static
89or auto variables, eg PL_sv_undef.
90
91The SV arena serves the secondary purpose of allowing still-live SVs
92to be located and destroyed during final cleanup.
93
94At the lowest level, the macros new_SV() and del_SV() grab and free
95an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
96to return the SV to the free list with error checking.) new_SV() calls
97more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
98SVs in the free list have their SvTYPE field set to all ones.
99
100Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
101that allocate and return individual body types. Normally these are mapped
ff276b08
RG
102to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be
103instead mapped directly to malloc()/free() if PURIFY is defined. The
645c22ef
DM
104new/del functions remove from, or add to, the appropriate PL_foo_root
105list, and call more_xiv() etc to add a new arena if the list is empty.
106
ff276b08 107At the time of very final cleanup, sv_free_arenas() is called from
645c22ef
DM
108perl_destruct() to physically free all the arenas allocated since the
109start of the interpreter. Note that this also clears PL_he_arenaroot,
110which is otherwise dealt with in hv.c.
111
112Manipulation of any of the PL_*root pointers is protected by enclosing
113LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
114if threads are enabled.
115
116The function visit() scans the SV arenas list, and calls a specified
117function for each SV it finds which is still live - ie which has an SvTYPE
118other than all 1's, and a non-zero SvREFCNT. visit() is used by the
119following functions (specified as [function that calls visit()] / [function
120called by visit() for each SV]):
121
122 sv_report_used() / do_report_used()
123 dump all remaining SVs (debugging aid)
124
125 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
126 Attempt to free all objects pointed to by RVs,
127 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
128 try to do the same for all objects indirectly
129 referenced by typeglobs too. Called once from
130 perl_destruct(), prior to calling sv_clean_all()
131 below.
132
133 sv_clean_all() / do_clean_all()
134 SvREFCNT_dec(sv) each remaining SV, possibly
135 triggering an sv_free(). It also sets the
136 SVf_BREAK flag on the SV to indicate that the
137 refcnt has been artificially lowered, and thus
138 stopping sv_free() from giving spurious warnings
139 about SVs which unexpectedly have a refcnt
140 of zero. called repeatedly from perl_destruct()
141 until there are no SVs left.
142
143=head2 Summary
144
145Private API to rest of sv.c
146
147 new_SV(), del_SV(),
148
149 new_XIV(), del_XIV(),
150 new_XNV(), del_XNV(),
151 etc
152
153Public API:
154
8cf8f3d1 155 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
645c22ef
DM
156
157
158=cut
159
160============================================================================ */
161
162
51371543 163
4561caa4
CS
164/*
165 * "A time to plant, and a time to uproot what was planted..."
166 */
167
fd0854ff
DM
168#ifdef DEBUG_LEAKING_SCALARS
169# ifdef NETWARE
170# define FREE_SV_DEBUG_FILE(sv) PerlMemfree((sv)->sv_debug_file)
171# else
172# define FREE_SV_DEBUG_FILE(sv) PerlMemShared_free((sv)->sv_debug_file)
173# endif
174#else
175# define FREE_SV_DEBUG_FILE(sv)
176#endif
177
053fc874
GS
178#define plant_SV(p) \
179 STMT_START { \
fd0854ff 180 FREE_SV_DEBUG_FILE(p); \
053fc874
GS
181 SvANY(p) = (void *)PL_sv_root; \
182 SvFLAGS(p) = SVTYPEMASK; \
183 PL_sv_root = (p); \
184 --PL_sv_count; \
185 } STMT_END
a0d0e21e 186
fba3b22e 187/* sv_mutex must be held while calling uproot_SV() */
053fc874
GS
188#define uproot_SV(p) \
189 STMT_START { \
190 (p) = PL_sv_root; \
191 PL_sv_root = (SV*)SvANY(p); \
192 ++PL_sv_count; \
193 } STMT_END
194
645c22ef
DM
195
196/* new_SV(): return a new, empty SV head */
197
eba0f806
DM
198#ifdef DEBUG_LEAKING_SCALARS
199/* provide a real function for a debugger to play with */
200STATIC SV*
201S_new_SV(pTHX)
202{
203 SV* sv;
204
205 LOCK_SV_MUTEX;
206 if (PL_sv_root)
207 uproot_SV(sv);
208 else
209 sv = more_sv();
210 UNLOCK_SV_MUTEX;
211 SvANY(sv) = 0;
212 SvREFCNT(sv) = 1;
213 SvFLAGS(sv) = 0;
fd0854ff
DM
214 sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
215 sv->sv_debug_line = (U16) ((PL_copline == NOLINE) ?
216 (PL_curcop ? CopLINE(PL_curcop) : 0) : PL_copline);
217 sv->sv_debug_inpad = 0;
218 sv->sv_debug_cloned = 0;
219# ifdef NETWARE
220 sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL;
221# else
222 sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL;
223# endif
224
eba0f806
DM
225 return sv;
226}
227# define new_SV(p) (p)=S_new_SV(aTHX)
228
229#else
230# define new_SV(p) \
053fc874
GS
231 STMT_START { \
232 LOCK_SV_MUTEX; \
233 if (PL_sv_root) \
234 uproot_SV(p); \
235 else \
236 (p) = more_sv(); \
237 UNLOCK_SV_MUTEX; \
238 SvANY(p) = 0; \
239 SvREFCNT(p) = 1; \
240 SvFLAGS(p) = 0; \
241 } STMT_END
eba0f806 242#endif
463ee0b2 243
645c22ef
DM
244
245/* del_SV(): return an empty SV head to the free list */
246
a0d0e21e 247#ifdef DEBUGGING
4561caa4 248
053fc874
GS
249#define del_SV(p) \
250 STMT_START { \
251 LOCK_SV_MUTEX; \
aea4f609 252 if (DEBUG_D_TEST) \
053fc874
GS
253 del_sv(p); \
254 else \
255 plant_SV(p); \
256 UNLOCK_SV_MUTEX; \
257 } STMT_END
a0d0e21e 258
76e3520e 259STATIC void
cea2e8a9 260S_del_sv(pTHX_ SV *p)
463ee0b2 261{
aea4f609 262 if (DEBUG_D_TEST) {
4633a7c4 263 SV* sva;
a0d0e21e
LW
264 SV* sv;
265 SV* svend;
266 int ok = 0;
3280af22 267 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
4633a7c4
LW
268 sv = sva + 1;
269 svend = &sva[SvREFCNT(sva)];
a0d0e21e
LW
270 if (p >= sv && p < svend)
271 ok = 1;
272 }
273 if (!ok) {
0453d815 274 if (ckWARN_d(WARN_INTERNAL))
9014280d 275 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
472d47bc
SB
276 "Attempt to free non-arena SV: 0x%"UVxf
277 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
a0d0e21e
LW
278 return;
279 }
280 }
4561caa4 281 plant_SV(p);
463ee0b2 282}
a0d0e21e 283
4561caa4
CS
284#else /* ! DEBUGGING */
285
286#define del_SV(p) plant_SV(p)
287
288#endif /* DEBUGGING */
463ee0b2 289
645c22ef
DM
290
291/*
ccfc67b7
JH
292=head1 SV Manipulation Functions
293
645c22ef
DM
294=for apidoc sv_add_arena
295
296Given a chunk of memory, link it to the head of the list of arenas,
297and split it into a list of free SVs.
298
299=cut
300*/
301
4633a7c4 302void
864dbfa3 303Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
463ee0b2 304{
4633a7c4 305 SV* sva = (SV*)ptr;
463ee0b2
LW
306 register SV* sv;
307 register SV* svend;
4633a7c4
LW
308
309 /* The first SV in an arena isn't an SV. */
3280af22 310 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
4633a7c4
LW
311 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
312 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
313
3280af22
NIS
314 PL_sv_arenaroot = sva;
315 PL_sv_root = sva + 1;
4633a7c4
LW
316
317 svend = &sva[SvREFCNT(sva) - 1];
318 sv = sva + 1;
463ee0b2 319 while (sv < svend) {
a0d0e21e 320 SvANY(sv) = (void *)(SV*)(sv + 1);
978b032e 321 SvREFCNT(sv) = 0;
8990e307 322 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
323 sv++;
324 }
325 SvANY(sv) = 0;
4633a7c4
LW
326 SvFLAGS(sv) = SVTYPEMASK;
327}
328
645c22ef
DM
329/* make some more SVs by adding another arena */
330
fba3b22e 331/* sv_mutex must be held while calling more_sv() */
76e3520e 332STATIC SV*
cea2e8a9 333S_more_sv(pTHX)
4633a7c4 334{
4561caa4
CS
335 register SV* sv;
336
3280af22
NIS
337 if (PL_nice_chunk) {
338 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
339 PL_nice_chunk = Nullch;
30ad99e7 340 PL_nice_chunk_size = 0;
c07a80fd 341 }
1edc1566 342 else {
9c17f24a
NC
343 char *chunk; /* must use New here to match call to Safefree() */
344 New(704,chunk,PERL_ARENA_SIZE,char); /* in sv_free_arenas() */
345 sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
1edc1566 346 }
4561caa4
CS
347 uproot_SV(sv);
348 return sv;
463ee0b2
LW
349}
350
055972dc
DM
351/* visit(): call the named function for each non-free SV in the arenas
352 * whose flags field matches the flags/mask args. */
645c22ef 353
5226ed68 354STATIC I32
055972dc 355S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
8990e307 356{
4633a7c4 357 SV* sva;
8990e307
LW
358 SV* sv;
359 register SV* svend;
5226ed68 360 I32 visited = 0;
8990e307 361
3280af22 362 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
4633a7c4 363 svend = &sva[SvREFCNT(sva)];
4561caa4 364 for (sv = sva + 1; sv < svend; ++sv) {
055972dc
DM
365 if (SvTYPE(sv) != SVTYPEMASK
366 && (sv->sv_flags & mask) == flags
367 && SvREFCNT(sv))
368 {
acfe0abc 369 (FCALL)(aTHX_ sv);
5226ed68
JH
370 ++visited;
371 }
8990e307
LW
372 }
373 }
5226ed68 374 return visited;
8990e307
LW
375}
376
758a08c3
JH
377#ifdef DEBUGGING
378
645c22ef
DM
379/* called by sv_report_used() for each live SV */
380
381static void
acfe0abc 382do_report_used(pTHX_ SV *sv)
645c22ef
DM
383{
384 if (SvTYPE(sv) != SVTYPEMASK) {
385 PerlIO_printf(Perl_debug_log, "****\n");
386 sv_dump(sv);
387 }
388}
758a08c3 389#endif
645c22ef
DM
390
391/*
392=for apidoc sv_report_used
393
394Dump the contents of all SVs not yet freed. (Debugging aid).
395
396=cut
397*/
398
8990e307 399void
864dbfa3 400Perl_sv_report_used(pTHX)
4561caa4 401{
ff270d3a 402#ifdef DEBUGGING
055972dc 403 visit(do_report_used, 0, 0);
ff270d3a 404#endif
4561caa4
CS
405}
406
645c22ef
DM
407/* called by sv_clean_objs() for each live SV */
408
409static void
acfe0abc 410do_clean_objs(pTHX_ SV *sv)
645c22ef
DM
411{
412 SV* rv;
413
414 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
415 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv)));
416 if (SvWEAKREF(sv)) {
417 sv_del_backref(sv);
418 SvWEAKREF_off(sv);
b162af07 419 SvRV_set(sv, NULL);
645c22ef
DM
420 } else {
421 SvROK_off(sv);
b162af07 422 SvRV_set(sv, NULL);
645c22ef
DM
423 SvREFCNT_dec(rv);
424 }
425 }
426
427 /* XXX Might want to check arrays, etc. */
428}
429
430/* called by sv_clean_objs() for each live SV */
431
432#ifndef DISABLE_DESTRUCTOR_KLUDGE
433static void
acfe0abc 434do_clean_named_objs(pTHX_ SV *sv)
645c22ef
DM
435{
436 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
437 if ( SvOBJECT(GvSV(sv)) ||
438 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
439 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
440 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
441 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
442 {
443 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
ec5f3c78 444 SvFLAGS(sv) |= SVf_BREAK;
645c22ef
DM
445 SvREFCNT_dec(sv);
446 }
447 }
448}
449#endif
450
451/*
452=for apidoc sv_clean_objs
453
454Attempt to destroy all objects not yet freed
455
456=cut
457*/
458
4561caa4 459void
864dbfa3 460Perl_sv_clean_objs(pTHX)
4561caa4 461{
3280af22 462 PL_in_clean_objs = TRUE;
055972dc 463 visit(do_clean_objs, SVf_ROK, SVf_ROK);
4561caa4 464#ifndef DISABLE_DESTRUCTOR_KLUDGE
2d0f3c12 465 /* some barnacles may yet remain, clinging to typeglobs */
055972dc 466 visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
4561caa4 467#endif
3280af22 468 PL_in_clean_objs = FALSE;
4561caa4
CS
469}
470
645c22ef
DM
471/* called by sv_clean_all() for each live SV */
472
473static void
acfe0abc 474do_clean_all(pTHX_ SV *sv)
645c22ef
DM
475{
476 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
477 SvFLAGS(sv) |= SVf_BREAK;
0e705b3b
DM
478 if (PL_comppad == (AV*)sv) {
479 PL_comppad = Nullav;
480 PL_curpad = Null(SV**);
481 }
645c22ef
DM
482 SvREFCNT_dec(sv);
483}
484
485/*
486=for apidoc sv_clean_all
487
488Decrement the refcnt of each remaining SV, possibly triggering a
489cleanup. This function may have to be called multiple times to free
ff276b08 490SVs which are in complex self-referential hierarchies.
645c22ef
DM
491
492=cut
493*/
494
5226ed68 495I32
864dbfa3 496Perl_sv_clean_all(pTHX)
8990e307 497{
5226ed68 498 I32 cleaned;
3280af22 499 PL_in_clean_all = TRUE;
055972dc 500 cleaned = visit(do_clean_all, 0,0);
3280af22 501 PL_in_clean_all = FALSE;
5226ed68 502 return cleaned;
8990e307 503}
463ee0b2 504
645c22ef
DM
505/*
506=for apidoc sv_free_arenas
507
508Deallocate the memory used by all arenas. Note that all the individual SV
509heads and bodies within the arenas must already have been freed.
510
511=cut
512*/
513
4633a7c4 514void
864dbfa3 515Perl_sv_free_arenas(pTHX)
4633a7c4
LW
516{
517 SV* sva;
518 SV* svanext;
612f20c3 519 XPV *arena, *arenanext;
4633a7c4
LW
520
521 /* Free arenas here, but be careful about fake ones. (We assume
522 contiguity of the fake ones with the corresponding real ones.) */
523
3280af22 524 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
4633a7c4
LW
525 svanext = (SV*) SvANY(sva);
526 while (svanext && SvFAKE(svanext))
527 svanext = (SV*) SvANY(svanext);
528
529 if (!SvFAKE(sva))
1edc1566 530 Safefree((void *)sva);
4633a7c4 531 }
5f05dabc 532
612f20c3
GS
533 for (arena = PL_xiv_arenaroot; arena; arena = arenanext) {
534 arenanext = (XPV*)arena->xpv_pv;
535 Safefree(arena);
536 }
537 PL_xiv_arenaroot = 0;
bf9cdc68 538 PL_xiv_root = 0;
612f20c3
GS
539
540 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
541 arenanext = (XPV*)arena->xpv_pv;
542 Safefree(arena);
543 }
544 PL_xnv_arenaroot = 0;
bf9cdc68 545 PL_xnv_root = 0;
612f20c3
GS
546
547 for (arena = PL_xrv_arenaroot; arena; arena = arenanext) {
548 arenanext = (XPV*)arena->xpv_pv;
549 Safefree(arena);
550 }
551 PL_xrv_arenaroot = 0;
bf9cdc68 552 PL_xrv_root = 0;
612f20c3
GS
553
554 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
555 arenanext = (XPV*)arena->xpv_pv;
556 Safefree(arena);
557 }
558 PL_xpv_arenaroot = 0;
bf9cdc68 559 PL_xpv_root = 0;
612f20c3
GS
560
561 for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) {
562 arenanext = (XPV*)arena->xpv_pv;
563 Safefree(arena);
564 }
565 PL_xpviv_arenaroot = 0;
bf9cdc68 566 PL_xpviv_root = 0;
612f20c3
GS
567
568 for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) {
569 arenanext = (XPV*)arena->xpv_pv;
570 Safefree(arena);
571 }
572 PL_xpvnv_arenaroot = 0;
bf9cdc68 573 PL_xpvnv_root = 0;
612f20c3
GS
574
575 for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) {
576 arenanext = (XPV*)arena->xpv_pv;
577 Safefree(arena);
578 }
579 PL_xpvcv_arenaroot = 0;
bf9cdc68 580 PL_xpvcv_root = 0;
612f20c3
GS
581
582 for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) {
583 arenanext = (XPV*)arena->xpv_pv;
584 Safefree(arena);
585 }
586 PL_xpvav_arenaroot = 0;
bf9cdc68 587 PL_xpvav_root = 0;
612f20c3
GS
588
589 for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) {
590 arenanext = (XPV*)arena->xpv_pv;
591 Safefree(arena);
592 }
593 PL_xpvhv_arenaroot = 0;
bf9cdc68 594 PL_xpvhv_root = 0;
612f20c3
GS
595
596 for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) {
597 arenanext = (XPV*)arena->xpv_pv;
598 Safefree(arena);
599 }
600 PL_xpvmg_arenaroot = 0;
bf9cdc68 601 PL_xpvmg_root = 0;
612f20c3
GS
602
603 for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) {
604 arenanext = (XPV*)arena->xpv_pv;
605 Safefree(arena);
606 }
607 PL_xpvlv_arenaroot = 0;
bf9cdc68 608 PL_xpvlv_root = 0;
612f20c3
GS
609
610 for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) {
611 arenanext = (XPV*)arena->xpv_pv;
612 Safefree(arena);
613 }
614 PL_xpvbm_arenaroot = 0;
bf9cdc68 615 PL_xpvbm_root = 0;
612f20c3
GS
616
617 for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) {
618 arenanext = (XPV*)arena->xpv_pv;
619 Safefree(arena);
620 }
621 PL_he_arenaroot = 0;
bf9cdc68 622 PL_he_root = 0;
612f20c3 623
32e691d0
NC
624 for (arena = (XPV*)PL_pte_arenaroot; arena; arena = arenanext) {
625 arenanext = (XPV*)arena->xpv_pv;
626 Safefree(arena);
627 }
628 PL_pte_arenaroot = 0;
629 PL_pte_root = 0;
630
3280af22
NIS
631 if (PL_nice_chunk)
632 Safefree(PL_nice_chunk);
633 PL_nice_chunk = Nullch;
634 PL_nice_chunk_size = 0;
635 PL_sv_arenaroot = 0;
636 PL_sv_root = 0;
4633a7c4
LW
637}
638
29489e7c
DM
639/* ---------------------------------------------------------------------
640 *
641 * support functions for report_uninit()
642 */
643
644/* the maxiumum size of array or hash where we will scan looking
645 * for the undefined element that triggered the warning */
646
647#define FUV_MAX_SEARCH_SIZE 1000
648
649/* Look for an entry in the hash whose value has the same SV as val;
650 * If so, return a mortal copy of the key. */
651
652STATIC SV*
653S_find_hash_subscript(pTHX_ HV *hv, SV* val)
654{
27da23d5 655 dVAR;
29489e7c
DM
656 register HE **array;
657 register HE *entry;
658 I32 i;
659
660 if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
661 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
662 return Nullsv;
663
664 array = HvARRAY(hv);
665
666 for (i=HvMAX(hv); i>0; i--) {
667 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
668 if (HeVAL(entry) != val)
669 continue;
670 if ( HeVAL(entry) == &PL_sv_undef ||
671 HeVAL(entry) == &PL_sv_placeholder)
672 continue;
673 if (!HeKEY(entry))
674 return Nullsv;
675 if (HeKLEN(entry) == HEf_SVKEY)
676 return sv_mortalcopy(HeKEY_sv(entry));
677 return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry)));
678 }
679 }
680 return Nullsv;
681}
682
683/* Look for an entry in the array whose value has the same SV as val;
684 * If so, return the index, otherwise return -1. */
685
686STATIC I32
687S_find_array_subscript(pTHX_ AV *av, SV* val)
688{
689 SV** svp;
690 I32 i;
691 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
692 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
693 return -1;
694
695 svp = AvARRAY(av);
696 for (i=AvFILLp(av); i>=0; i--) {
697 if (svp[i] == val && svp[i] != &PL_sv_undef)
698 return i;
699 }
700 return -1;
701}
702
703/* S_varname(): return the name of a variable, optionally with a subscript.
704 * If gv is non-zero, use the name of that global, along with gvtype (one
705 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
706 * targ. Depending on the value of the subscript_type flag, return:
707 */
708
709#define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
710#define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
711#define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
712#define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
713
714STATIC SV*
bfed75c6 715S_varname(pTHX_ GV *gv, const char *gvtype, PADOFFSET targ,
29489e7c
DM
716 SV* keyname, I32 aindex, int subscript_type)
717{
718 AV *av;
719
720 SV *sv, *name;
721
722 name = sv_newmortal();
723 if (gv) {
724
725 /* simulate gv_fullname4(), but add literal '^' for $^FOO names
726 * XXX get rid of all this if gv_fullnameX() ever supports this
727 * directly */
728
bfed75c6 729 const char *p;
29489e7c
DM
730 HV *hv = GvSTASH(gv);
731 sv_setpv(name, gvtype);
732 if (!hv)
733 p = "???";
bfed75c6 734 else if (!(p=HvNAME(hv)))
29489e7c 735 p = "__ANON__";
29489e7c
DM
736 if (strNE(p, "main")) {
737 sv_catpv(name,p);
738 sv_catpvn(name,"::", 2);
739 }
740 if (GvNAMELEN(gv)>= 1 &&
741 ((unsigned int)*GvNAME(gv)) <= 26)
742 { /* handle $^FOO */
743 Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1);
744 sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1);
745 }
746 else
747 sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv));
748 }
749 else {
750 U32 u;
751 CV *cv = find_runcv(&u);
752 if (!cv || !CvPADLIST(cv))
753 return Nullsv;;
754 av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE));
755 sv = *av_fetch(av, targ, FALSE);
756 /* SvLEN in a pad name is not to be trusted */
757 sv_setpv(name, SvPV_nolen(sv));
758 }
759
760 if (subscript_type == FUV_SUBSCRIPT_HASH) {
761 *SvPVX(name) = '$';
762 sv = NEWSV(0,0);
763 Perl_sv_catpvf(aTHX_ name, "{%s}",
764 pv_display(sv,SvPVX(keyname), SvCUR(keyname), 0, 32));
765 SvREFCNT_dec(sv);
766 }
767 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
768 *SvPVX(name) = '$';
265a12b8 769 Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
29489e7c
DM
770 }
771 else if (subscript_type == FUV_SUBSCRIPT_WITHIN)
772 sv_insert(name, 0, 0, "within ", 7);
773
774 return name;
775}
776
777
778/*
779=for apidoc find_uninit_var
780
781Find the name of the undefined variable (if any) that caused the operator o
782to issue a "Use of uninitialized value" warning.
783If match is true, only return a name if it's value matches uninit_sv.
784So roughly speaking, if a unary operator (such as OP_COS) generates a
785warning, then following the direct child of the op may yield an
786OP_PADSV or OP_GV that gives the name of the undefined variable. On the
787other hand, with OP_ADD there are two branches to follow, so we only print
788the variable name if we get an exact match.
789
790The name is returned as a mortal SV.
791
792Assumes that PL_op is the op that originally triggered the error, and that
793PL_comppad/PL_curpad points to the currently executing pad.
794
795=cut
796*/
797
798STATIC SV *
799S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match)
800{
27da23d5 801 dVAR;
29489e7c
DM
802 SV *sv;
803 AV *av;
804 SV **svp;
805 GV *gv;
806 OP *o, *o2, *kid;
807
808 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
809 uninit_sv == &PL_sv_placeholder)))
810 return Nullsv;
811
812 switch (obase->op_type) {
813
814 case OP_RV2AV:
815 case OP_RV2HV:
816 case OP_PADAV:
817 case OP_PADHV:
818 {
819 bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
820 bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV);
112dcc46
RGS
821 I32 index = 0;
822 SV *keysv = Nullsv;
29489e7c
DM
823 int subscript_type = FUV_SUBSCRIPT_WITHIN;
824
825 if (pad) { /* @lex, %lex */
826 sv = PAD_SVl(obase->op_targ);
827 gv = Nullgv;
828 }
829 else {
830 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
831 /* @global, %global */
832 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
833 if (!gv)
834 break;
835 sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv);
836 }
837 else /* @{expr}, %{expr} */
838 return find_uninit_var(cUNOPx(obase)->op_first,
839 uninit_sv, match);
840 }
841
842 /* attempt to find a match within the aggregate */
843 if (hash) {
844 keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
845 if (keysv)
846 subscript_type = FUV_SUBSCRIPT_HASH;
847 }
848 else {
849 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
850 if (index >= 0)
851 subscript_type = FUV_SUBSCRIPT_ARRAY;
852 }
853
854 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
855 break;
856
857 return S_varname(aTHX_ gv, hash ? "%" : "@", obase->op_targ,
858 keysv, index, subscript_type);
859 }
860
861 case OP_PADSV:
862 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
863 break;
864 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
865 Nullsv, 0, FUV_SUBSCRIPT_NONE);
866
867 case OP_GVSV:
868 gv = cGVOPx_gv(obase);
869 if (!gv || (match && GvSV(gv) != uninit_sv))
870 break;
871 return S_varname(aTHX_ gv, "$", 0, Nullsv, 0, FUV_SUBSCRIPT_NONE);
872
873 case OP_AELEMFAST:
874 if (obase->op_flags & OPf_SPECIAL) { /* lexical array */
875 if (match) {
876 av = (AV*)PAD_SV(obase->op_targ);
877 if (!av || SvRMAGICAL(av))
878 break;
879 svp = av_fetch(av, (I32)obase->op_private, FALSE);
880 if (!svp || *svp != uninit_sv)
881 break;
882 }
883 return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
884 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
885 }
886 else {
887 gv = cGVOPx_gv(obase);
888 if (!gv)
889 break;
890 if (match) {
891 av = GvAV(gv);
892 if (!av || SvRMAGICAL(av))
893 break;
894 svp = av_fetch(av, (I32)obase->op_private, FALSE);
895 if (!svp || *svp != uninit_sv)
896 break;
897 }
898 return S_varname(aTHX_ gv, "$", 0,
899 Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
900 }
901 break;
902
903 case OP_EXISTS:
904 o = cUNOPx(obase)->op_first;
905 if (!o || o->op_type != OP_NULL ||
906 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
907 break;
908 return find_uninit_var(cBINOPo->op_last, uninit_sv, match);
909
910 case OP_AELEM:
911 case OP_HELEM:
912 if (PL_op == obase)
913 /* $a[uninit_expr] or $h{uninit_expr} */
914 return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match);
915
916 gv = Nullgv;
917 o = cBINOPx(obase)->op_first;
918 kid = cBINOPx(obase)->op_last;
919
920 /* get the av or hv, and optionally the gv */
921 sv = Nullsv;
922 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
923 sv = PAD_SV(o->op_targ);
924 }
925 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
926 && cUNOPo->op_first->op_type == OP_GV)
927 {
928 gv = cGVOPx_gv(cUNOPo->op_first);
929 if (!gv)
930 break;
931 sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv);
932 }
933 if (!sv)
934 break;
935
936 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
937 /* index is constant */
938 if (match) {
939 if (SvMAGICAL(sv))
940 break;
941 if (obase->op_type == OP_HELEM) {
942 HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0);
943 if (!he || HeVAL(he) != uninit_sv)
944 break;
945 }
946 else {
947 svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE);
948 if (!svp || *svp != uninit_sv)
949 break;
950 }
951 }
952 if (obase->op_type == OP_HELEM)
953 return S_varname(aTHX_ gv, "%", o->op_targ,
954 cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH);
955 else
956 return S_varname(aTHX_ gv, "@", o->op_targ, Nullsv,
957 SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY);
958 ;
959 }
960 else {
961 /* index is an expression;
962 * attempt to find a match within the aggregate */
963 if (obase->op_type == OP_HELEM) {
964 SV *keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
965 if (keysv)
966 return S_varname(aTHX_ gv, "%", o->op_targ,
967 keysv, 0, FUV_SUBSCRIPT_HASH);
968 }
969 else {
970 I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
971 if (index >= 0)
972 return S_varname(aTHX_ gv, "@", o->op_targ,
973 Nullsv, index, FUV_SUBSCRIPT_ARRAY);
974 }
975 if (match)
976 break;
977 return S_varname(aTHX_ gv,
978 (o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
979 ? "@" : "%",
980 o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN);
981 }
982
983 break;
984
985 case OP_AASSIGN:
986 /* only examine RHS */
987 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match);
988
989 case OP_OPEN:
990 o = cUNOPx(obase)->op_first;
991 if (o->op_type == OP_PUSHMARK)
992 o = o->op_sibling;
993
994 if (!o->op_sibling) {
995 /* one-arg version of open is highly magical */
996
997 if (o->op_type == OP_GV) { /* open FOO; */
998 gv = cGVOPx_gv(o);
999 if (match && GvSV(gv) != uninit_sv)
1000 break;
7a5fa8a2 1001 return S_varname(aTHX_ gv, "$", 0,
29489e7c
DM
1002 Nullsv, 0, FUV_SUBSCRIPT_NONE);
1003 }
1004 /* other possibilities not handled are:
1005 * open $x; or open my $x; should return '${*$x}'
1006 * open expr; should return '$'.expr ideally
1007 */
1008 break;
1009 }
1010 goto do_op;
1011
1012 /* ops where $_ may be an implicit arg */
1013 case OP_TRANS:
1014 case OP_SUBST:
1015 case OP_MATCH:
1016 if ( !(obase->op_flags & OPf_STACKED)) {
1017 if (uninit_sv == ((obase->op_private & OPpTARGET_MY)
1018 ? PAD_SVl(obase->op_targ)
1019 : DEFSV))
1020 {
1021 sv = sv_newmortal();
1022 sv_setpv(sv, "$_");
1023 return sv;
1024 }
1025 }
1026 goto do_op;
1027
1028 case OP_PRTF:
1029 case OP_PRINT:
1030 /* skip filehandle as it can't produce 'undef' warning */
1031 o = cUNOPx(obase)->op_first;
1032 if ((obase->op_flags & OPf_STACKED) && o->op_type == OP_PUSHMARK)
1033 o = o->op_sibling->op_sibling;
1034 goto do_op2;
1035
1036
e21bd382 1037 case OP_RV2SV:
29489e7c
DM
1038 case OP_CUSTOM:
1039 case OP_ENTERSUB:
1040 match = 1; /* XS or custom code could trigger random warnings */
1041 goto do_op;
1042
1043 case OP_SCHOMP:
1044 case OP_CHOMP:
1045 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
1046 return sv_2mortal(newSVpv("${$/}", 0));
1047 /* FALL THROUGH */
1048
1049 default:
1050 do_op:
1051 if (!(obase->op_flags & OPf_KIDS))
1052 break;
1053 o = cUNOPx(obase)->op_first;
1054
1055 do_op2:
1056 if (!o)
1057 break;
1058
1059 /* if all except one arg are constant, or have no side-effects,
1060 * or are optimized away, then it's unambiguous */
1061 o2 = Nullop;
1062 for (kid=o; kid; kid = kid->op_sibling) {
1063 if (kid &&
1064 ( (kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid)))
1065 || (kid->op_type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
1066 || (kid->op_type == OP_PUSHMARK)
1067 )
1068 )
1069 continue;
1070 if (o2) { /* more than one found */
1071 o2 = Nullop;
1072 break;
1073 }
1074 o2 = kid;
1075 }
1076 if (o2)
1077 return find_uninit_var(o2, uninit_sv, match);
1078
1079 /* scan all args */
1080 while (o) {
1081 sv = find_uninit_var(o, uninit_sv, 1);
1082 if (sv)
1083 return sv;
1084 o = o->op_sibling;
1085 }
1086 break;
1087 }
1088 return Nullsv;
1089}
1090
1091
645c22ef
DM
1092/*
1093=for apidoc report_uninit
1094
1095Print appropriate "Use of uninitialized variable" warning
1096
1097=cut
1098*/
1099
1d7c1841 1100void
29489e7c
DM
1101Perl_report_uninit(pTHX_ SV* uninit_sv)
1102{
1103 if (PL_op) {
112dcc46 1104 SV* varname = Nullsv;
29489e7c
DM
1105 if (uninit_sv) {
1106 varname = find_uninit_var(PL_op, uninit_sv,0);
1107 if (varname)
1108 sv_insert(varname, 0, 0, " ", 1);
1109 }
9014280d 1110 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
29489e7c
DM
1111 varname ? SvPV_nolen(varname) : "",
1112 " in ", OP_DESC(PL_op));
1113 }
1d7c1841 1114 else
29489e7c
DM
1115 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
1116 "", "", "");
1d7c1841
GS
1117}
1118
645c22ef
DM
1119/* grab a new IV body from the free list, allocating more if necessary */
1120
76e3520e 1121STATIC XPVIV*
cea2e8a9 1122S_new_xiv(pTHX)
463ee0b2 1123{
ea7c11a3 1124 IV* xiv;
cbe51380
GS
1125 LOCK_SV_MUTEX;
1126 if (!PL_xiv_root)
1127 more_xiv();
1128 xiv = PL_xiv_root;
1129 /*
1130 * See comment in more_xiv() -- RAM.
1131 */
1132 PL_xiv_root = *(IV**)xiv;
1133 UNLOCK_SV_MUTEX;
1134 return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv));
463ee0b2
LW
1135}
1136
645c22ef
DM
1137/* return an IV body to the free list */
1138
76e3520e 1139STATIC void
cea2e8a9 1140S_del_xiv(pTHX_ XPVIV *p)
463ee0b2 1141{
23e6a22f 1142 IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv));
cbe51380 1143 LOCK_SV_MUTEX;
3280af22
NIS
1144 *(IV**)xiv = PL_xiv_root;
1145 PL_xiv_root = xiv;
cbe51380 1146 UNLOCK_SV_MUTEX;
463ee0b2
LW
1147}
1148
645c22ef
DM
1149/* allocate another arena's worth of IV bodies */
1150
cbe51380 1151STATIC void
cea2e8a9 1152S_more_xiv(pTHX)
463ee0b2 1153{
ea7c11a3
SM
1154 register IV* xiv;
1155 register IV* xivend;
8c52afec 1156 XPV* ptr;
9c17f24a 1157 New(705, ptr, PERL_ARENA_SIZE/sizeof(XPV), XPV);
645c22ef 1158 ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */
3280af22 1159 PL_xiv_arenaroot = ptr; /* to keep Purify happy */
a0d0e21e 1160
ea7c11a3 1161 xiv = (IV*) ptr;
9c17f24a 1162 xivend = &xiv[PERL_ARENA_SIZE / sizeof(IV) - 1];
645c22ef 1163 xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */
3280af22 1164 PL_xiv_root = xiv;
463ee0b2 1165 while (xiv < xivend) {
ea7c11a3 1166 *(IV**)xiv = (IV *)(xiv + 1);
463ee0b2
LW
1167 xiv++;
1168 }
ea7c11a3 1169 *(IV**)xiv = 0;
463ee0b2
LW
1170}
1171
645c22ef
DM
1172/* grab a new NV body from the free list, allocating more if necessary */
1173
76e3520e 1174STATIC XPVNV*
cea2e8a9 1175S_new_xnv(pTHX)
463ee0b2 1176{
65202027 1177 NV* xnv;
cbe51380
GS
1178 LOCK_SV_MUTEX;
1179 if (!PL_xnv_root)
1180 more_xnv();
1181 xnv = PL_xnv_root;
65202027 1182 PL_xnv_root = *(NV**)xnv;
cbe51380
GS
1183 UNLOCK_SV_MUTEX;
1184 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
463ee0b2
LW
1185}
1186
645c22ef
DM
1187/* return an NV body to the free list */
1188
76e3520e 1189STATIC void
cea2e8a9 1190S_del_xnv(pTHX_ XPVNV *p)
463ee0b2 1191{
65202027 1192 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
cbe51380 1193 LOCK_SV_MUTEX;
65202027 1194 *(NV**)xnv = PL_xnv_root;
3280af22 1195 PL_xnv_root = xnv;
cbe51380 1196 UNLOCK_SV_MUTEX;
463ee0b2
LW
1197}
1198
645c22ef
DM
1199/* allocate another arena's worth of NV bodies */
1200
cbe51380 1201STATIC void
cea2e8a9 1202S_more_xnv(pTHX)
463ee0b2 1203{
65202027
DS
1204 register NV* xnv;
1205 register NV* xnvend;
612f20c3 1206 XPV *ptr;
9c17f24a 1207 New(711, ptr, PERL_ARENA_SIZE/sizeof(XPV), XPV);
612f20c3
GS
1208 ptr->xpv_pv = (char*)PL_xnv_arenaroot;
1209 PL_xnv_arenaroot = ptr;
1210
1211 xnv = (NV*) ptr;
9c17f24a 1212 xnvend = &xnv[PERL_ARENA_SIZE / sizeof(NV) - 1];
65202027 1213 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
3280af22 1214 PL_xnv_root = xnv;
463ee0b2 1215 while (xnv < xnvend) {
65202027 1216 *(NV**)xnv = (NV*)(xnv + 1);
463ee0b2
LW
1217 xnv++;
1218 }
65202027 1219 *(NV**)xnv = 0;
463ee0b2
LW
1220}
1221
645c22ef
DM
1222/* grab a new struct xrv from the free list, allocating more if necessary */
1223
76e3520e 1224STATIC XRV*
cea2e8a9 1225S_new_xrv(pTHX)
ed6116ce
LW
1226{
1227 XRV* xrv;
cbe51380
GS
1228 LOCK_SV_MUTEX;
1229 if (!PL_xrv_root)
1230 more_xrv();
1231 xrv = PL_xrv_root;
1232 PL_xrv_root = (XRV*)xrv->xrv_rv;
1233 UNLOCK_SV_MUTEX;
1234 return xrv;
ed6116ce
LW
1235}
1236
645c22ef
DM
1237/* return a struct xrv to the free list */
1238
76e3520e 1239STATIC void
cea2e8a9 1240S_del_xrv(pTHX_ XRV *p)
ed6116ce 1241{
cbe51380 1242 LOCK_SV_MUTEX;
3280af22
NIS
1243 p->xrv_rv = (SV*)PL_xrv_root;
1244 PL_xrv_root = p;
cbe51380 1245 UNLOCK_SV_MUTEX;
ed6116ce
LW
1246}
1247
645c22ef
DM
1248/* allocate another arena's worth of struct xrv */
1249
cbe51380 1250STATIC void
cea2e8a9 1251S_more_xrv(pTHX)
ed6116ce 1252{
ed6116ce
LW
1253 register XRV* xrv;
1254 register XRV* xrvend;
612f20c3 1255 XPV *ptr;
9c17f24a 1256 New(712, ptr, PERL_ARENA_SIZE/sizeof(XPV), XPV);
612f20c3
GS
1257 ptr->xpv_pv = (char*)PL_xrv_arenaroot;
1258 PL_xrv_arenaroot = ptr;
1259
1260 xrv = (XRV*) ptr;
9c17f24a 1261 xrvend = &xrv[PERL_ARENA_SIZE / sizeof(XRV) - 1];
612f20c3
GS
1262 xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1;
1263 PL_xrv_root = xrv;
ed6116ce
LW
1264 while (xrv < xrvend) {
1265 xrv->xrv_rv = (SV*)(xrv + 1);
1266 xrv++;
1267 }
1268 xrv->xrv_rv = 0;
ed6116ce
LW
1269}
1270
645c22ef
DM
1271/* grab a new struct xpv from the free list, allocating more if necessary */
1272
76e3520e 1273STATIC XPV*
cea2e8a9 1274S_new_xpv(pTHX)
463ee0b2
LW
1275{
1276 XPV* xpv;
cbe51380
GS
1277 LOCK_SV_MUTEX;
1278 if (!PL_xpv_root)
1279 more_xpv();
1280 xpv = PL_xpv_root;
1281 PL_xpv_root = (XPV*)xpv->xpv_pv;
1282 UNLOCK_SV_MUTEX;
1283 return xpv;
463ee0b2
LW
1284}
1285
645c22ef
DM
1286/* return a struct xpv to the free list */
1287
76e3520e 1288STATIC void
cea2e8a9 1289S_del_xpv(pTHX_ XPV *p)
463ee0b2 1290{
cbe51380 1291 LOCK_SV_MUTEX;
3280af22
NIS
1292 p->xpv_pv = (char*)PL_xpv_root;
1293 PL_xpv_root = p;
cbe51380 1294 UNLOCK_SV_MUTEX;
463ee0b2
LW
1295}
1296
645c22ef
DM
1297/* allocate another arena's worth of struct xpv */
1298
cbe51380 1299STATIC void
cea2e8a9 1300S_more_xpv(pTHX)
463ee0b2 1301{
463ee0b2
LW
1302 register XPV* xpv;
1303 register XPV* xpvend;
9c17f24a 1304 New(713, xpv, PERL_ARENA_SIZE/sizeof(XPV), XPV);
612f20c3
GS
1305 xpv->xpv_pv = (char*)PL_xpv_arenaroot;
1306 PL_xpv_arenaroot = xpv;
1307
9c17f24a 1308 xpvend = &xpv[PERL_ARENA_SIZE / sizeof(XPV) - 1];
612f20c3 1309 PL_xpv_root = ++xpv;
463ee0b2
LW
1310 while (xpv < xpvend) {
1311 xpv->xpv_pv = (char*)(xpv + 1);
1312 xpv++;
1313 }
1314 xpv->xpv_pv = 0;
463ee0b2
LW
1315}
1316
645c22ef
DM
1317/* grab a new struct xpviv from the free list, allocating more if necessary */
1318
932e9ff9
VB
1319STATIC XPVIV*
1320S_new_xpviv(pTHX)
1321{
1322 XPVIV* xpviv;
1323 LOCK_SV_MUTEX;
1324 if (!PL_xpviv_root)
1325 more_xpviv();
1326 xpviv = PL_xpviv_root;
1327 PL_xpviv_root = (XPVIV*)xpviv->xpv_pv;
1328 UNLOCK_SV_MUTEX;
1329 return xpviv;
1330}
1331
645c22ef
DM
1332/* return a struct xpviv to the free list */
1333
932e9ff9
VB
1334STATIC void
1335S_del_xpviv(pTHX_ XPVIV *p)
1336{
1337 LOCK_SV_MUTEX;
1338 p->xpv_pv = (char*)PL_xpviv_root;
1339 PL_xpviv_root = p;
1340 UNLOCK_SV_MUTEX;
1341}
1342
645c22ef
DM
1343/* allocate another arena's worth of struct xpviv */
1344
932e9ff9
VB
1345STATIC void
1346S_more_xpviv(pTHX)
1347{
1348 register XPVIV* xpviv;
1349 register XPVIV* xpvivend;
9c17f24a 1350 New(714, xpviv, PERL_ARENA_SIZE/sizeof(XPVIV), XPVIV);
612f20c3
GS
1351 xpviv->xpv_pv = (char*)PL_xpviv_arenaroot;
1352 PL_xpviv_arenaroot = xpviv;
1353
9c17f24a 1354 xpvivend = &xpviv[PERL_ARENA_SIZE / sizeof(XPVIV) - 1];
612f20c3 1355 PL_xpviv_root = ++xpviv;
932e9ff9
VB
1356 while (xpviv < xpvivend) {
1357 xpviv->xpv_pv = (char*)(xpviv + 1);
1358 xpviv++;
1359 }
1360 xpviv->xpv_pv = 0;
1361}
1362
645c22ef
DM
1363/* grab a new struct xpvnv from the free list, allocating more if necessary */
1364
932e9ff9
VB
1365STATIC XPVNV*
1366S_new_xpvnv(pTHX)
1367{
1368 XPVNV* xpvnv;
1369 LOCK_SV_MUTEX;
1370 if (!PL_xpvnv_root)
1371 more_xpvnv();
1372 xpvnv = PL_xpvnv_root;
1373 PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv;
1374 UNLOCK_SV_MUTEX;
1375 return xpvnv;
1376}
1377
645c22ef
DM
1378/* return a struct xpvnv to the free list */
1379
932e9ff9
VB
1380STATIC void
1381S_del_xpvnv(pTHX_ XPVNV *p)
1382{
1383 LOCK_SV_MUTEX;
1384 p->xpv_pv = (char*)PL_xpvnv_root;
1385 PL_xpvnv_root = p;
1386 UNLOCK_SV_MUTEX;
1387}
1388
645c22ef
DM
1389/* allocate another arena's worth of struct xpvnv */
1390
932e9ff9
VB
1391STATIC void
1392S_more_xpvnv(pTHX)
1393{
1394 register XPVNV* xpvnv;
1395 register XPVNV* xpvnvend;
9c17f24a 1396 New(715, xpvnv, PERL_ARENA_SIZE/sizeof(XPVNV), XPVNV);
612f20c3
GS
1397 xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot;
1398 PL_xpvnv_arenaroot = xpvnv;
1399
9c17f24a 1400 xpvnvend = &xpvnv[PERL_ARENA_SIZE / sizeof(XPVNV) - 1];
612f20c3 1401 PL_xpvnv_root = ++xpvnv;
932e9ff9
VB
1402 while (xpvnv < xpvnvend) {
1403 xpvnv->xpv_pv = (char*)(xpvnv + 1);
1404 xpvnv++;
1405 }
1406 xpvnv->xpv_pv = 0;
1407}
1408
645c22ef
DM
1409/* grab a new struct xpvcv from the free list, allocating more if necessary */
1410
932e9ff9
VB
1411STATIC XPVCV*
1412S_new_xpvcv(pTHX)
1413{
1414 XPVCV* xpvcv;
1415 LOCK_SV_MUTEX;
1416 if (!PL_xpvcv_root)
1417 more_xpvcv();
1418 xpvcv = PL_xpvcv_root;
1419 PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv;
1420 UNLOCK_SV_MUTEX;
1421 return xpvcv;
1422}
1423
645c22ef
DM
1424/* return a struct xpvcv to the free list */
1425
932e9ff9
VB
1426STATIC void
1427S_del_xpvcv(pTHX_ XPVCV *p)
1428{
1429 LOCK_SV_MUTEX;
1430 p->xpv_pv = (char*)PL_xpvcv_root;
1431 PL_xpvcv_root = p;
1432 UNLOCK_SV_MUTEX;
1433}
1434
645c22ef
DM
1435/* allocate another arena's worth of struct xpvcv */
1436
932e9ff9
VB
1437STATIC void
1438S_more_xpvcv(pTHX)
1439{
1440 register XPVCV* xpvcv;
1441 register XPVCV* xpvcvend;
9c17f24a 1442 New(716, xpvcv, PERL_ARENA_SIZE/sizeof(XPVCV), XPVCV);
612f20c3
GS
1443 xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot;
1444 PL_xpvcv_arenaroot = xpvcv;
1445
9c17f24a 1446 xpvcvend = &xpvcv[PERL_ARENA_SIZE / sizeof(XPVCV) - 1];
612f20c3 1447 PL_xpvcv_root = ++xpvcv;
932e9ff9
VB
1448 while (xpvcv < xpvcvend) {
1449 xpvcv->xpv_pv = (char*)(xpvcv + 1);
1450 xpvcv++;
1451 }
1452 xpvcv->xpv_pv = 0;
1453}
1454
645c22ef
DM
1455/* grab a new struct xpvav from the free list, allocating more if necessary */
1456
932e9ff9
VB
1457STATIC XPVAV*
1458S_new_xpvav(pTHX)
1459{
1460 XPVAV* xpvav;
1461 LOCK_SV_MUTEX;
1462 if (!PL_xpvav_root)
1463 more_xpvav();
1464 xpvav = PL_xpvav_root;
1465 PL_xpvav_root = (XPVAV*)xpvav->xav_array;
1466 UNLOCK_SV_MUTEX;
1467 return xpvav;
1468}
1469
645c22ef
DM
1470/* return a struct xpvav to the free list */
1471
932e9ff9
VB
1472STATIC void
1473S_del_xpvav(pTHX_ XPVAV *p)
1474{
1475 LOCK_SV_MUTEX;
1476 p->xav_array = (char*)PL_xpvav_root;
1477 PL_xpvav_root = p;
1478 UNLOCK_SV_MUTEX;
1479}
1480
645c22ef
DM
1481/* allocate another arena's worth of struct xpvav */
1482
932e9ff9
VB
1483STATIC void
1484S_more_xpvav(pTHX)
1485{
1486 register XPVAV* xpvav;
1487 register XPVAV* xpvavend;
9c17f24a 1488 New(717, xpvav, PERL_ARENA_SIZE/sizeof(XPVAV), XPVAV);
612f20c3
GS
1489 xpvav->xav_array = (char*)PL_xpvav_arenaroot;
1490 PL_xpvav_arenaroot = xpvav;
1491
9c17f24a 1492 xpvavend = &xpvav[PERL_ARENA_SIZE / sizeof(XPVAV) - 1];
612f20c3 1493 PL_xpvav_root = ++xpvav;
932e9ff9
VB
1494 while (xpvav < xpvavend) {
1495 xpvav->xav_array = (char*)(xpvav + 1);
1496 xpvav++;
1497 }
1498 xpvav->xav_array = 0;
1499}
1500
645c22ef
DM
1501/* grab a new struct xpvhv from the free list, allocating more if necessary */
1502
932e9ff9
VB
1503STATIC XPVHV*
1504S_new_xpvhv(pTHX)
1505{
1506 XPVHV* xpvhv;
1507 LOCK_SV_MUTEX;
1508 if (!PL_xpvhv_root)
1509 more_xpvhv();
1510 xpvhv = PL_xpvhv_root;
1511 PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array;
1512 UNLOCK_SV_MUTEX;
1513 return xpvhv;
1514}
1515
645c22ef
DM
1516/* return a struct xpvhv to the free list */
1517
932e9ff9
VB
1518STATIC void
1519S_del_xpvhv(pTHX_ XPVHV *p)
1520{
1521 LOCK_SV_MUTEX;
1522 p->xhv_array = (char*)PL_xpvhv_root;
1523 PL_xpvhv_root = p;
1524 UNLOCK_SV_MUTEX;
1525}
1526
645c22ef
DM
1527/* allocate another arena's worth of struct xpvhv */
1528
932e9ff9
VB
1529STATIC void
1530S_more_xpvhv(pTHX)
1531{
1532 register XPVHV* xpvhv;
1533 register XPVHV* xpvhvend;
9c17f24a 1534 New(718, xpvhv, PERL_ARENA_SIZE/sizeof(XPVHV), XPVHV);
612f20c3
GS
1535 xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot;
1536 PL_xpvhv_arenaroot = xpvhv;
1537
9c17f24a 1538 xpvhvend = &xpvhv[PERL_ARENA_SIZE / sizeof(XPVHV) - 1];
612f20c3 1539 PL_xpvhv_root = ++xpvhv;
932e9ff9
VB
1540 while (xpvhv < xpvhvend) {
1541 xpvhv->xhv_array = (char*)(xpvhv + 1);
1542 xpvhv++;
1543 }
1544 xpvhv->xhv_array = 0;
1545}
1546
645c22ef
DM
1547/* grab a new struct xpvmg from the free list, allocating more if necessary */
1548
932e9ff9
VB
1549STATIC XPVMG*
1550S_new_xpvmg(pTHX)
1551{
1552 XPVMG* xpvmg;
1553 LOCK_SV_MUTEX;
1554 if (!PL_xpvmg_root)
1555 more_xpvmg();
1556 xpvmg = PL_xpvmg_root;
1557 PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv;
1558 UNLOCK_SV_MUTEX;
1559 return xpvmg;
1560}
1561
645c22ef
DM
1562/* return a struct xpvmg to the free list */
1563
932e9ff9
VB
1564STATIC void
1565S_del_xpvmg(pTHX_ XPVMG *p)
1566{
1567 LOCK_SV_MUTEX;
1568 p->xpv_pv = (char*)PL_xpvmg_root;
1569 PL_xpvmg_root = p;
1570 UNLOCK_SV_MUTEX;
1571}
1572
645c22ef
DM
1573/* allocate another arena's worth of struct xpvmg */
1574
932e9ff9
VB
1575STATIC void
1576S_more_xpvmg(pTHX)
1577{
1578 register XPVMG* xpvmg;
1579 register XPVMG* xpvmgend;
9c17f24a 1580 New(719, xpvmg, PERL_ARENA_SIZE/sizeof(XPVMG), XPVMG);
612f20c3
GS
1581 xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot;
1582 PL_xpvmg_arenaroot = xpvmg;
1583
9c17f24a 1584 xpvmgend = &xpvmg[PERL_ARENA_SIZE / sizeof(XPVMG) - 1];
612f20c3 1585 PL_xpvmg_root = ++xpvmg;
932e9ff9
VB
1586 while (xpvmg < xpvmgend) {
1587 xpvmg->xpv_pv = (char*)(xpvmg + 1);
1588 xpvmg++;
1589 }
1590 xpvmg->xpv_pv = 0;
1591}
1592
645c22ef
DM
1593/* grab a new struct xpvlv from the free list, allocating more if necessary */
1594
932e9ff9
VB
1595STATIC XPVLV*
1596S_new_xpvlv(pTHX)
1597{
1598 XPVLV* xpvlv;
1599 LOCK_SV_MUTEX;
1600 if (!PL_xpvlv_root)
1601 more_xpvlv();
1602 xpvlv = PL_xpvlv_root;
1603 PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv;
1604 UNLOCK_SV_MUTEX;
1605 return xpvlv;
1606}
1607
645c22ef
DM
1608/* return a struct xpvlv to the free list */
1609
932e9ff9
VB
1610STATIC void
1611S_del_xpvlv(pTHX_ XPVLV *p)
1612{
1613 LOCK_SV_MUTEX;
1614 p->xpv_pv = (char*)PL_xpvlv_root;
1615 PL_xpvlv_root = p;
1616 UNLOCK_SV_MUTEX;
1617}
1618
645c22ef
DM
1619/* allocate another arena's worth of struct xpvlv */
1620
932e9ff9
VB
1621STATIC void
1622S_more_xpvlv(pTHX)
1623{
1624 register XPVLV* xpvlv;
1625 register XPVLV* xpvlvend;
9c17f24a 1626 New(720, xpvlv, PERL_ARENA_SIZE/sizeof(XPVLV), XPVLV);
612f20c3
GS
1627 xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot;
1628 PL_xpvlv_arenaroot = xpvlv;
1629
9c17f24a 1630 xpvlvend = &xpvlv[PERL_ARENA_SIZE / sizeof(XPVLV) - 1];
612f20c3 1631 PL_xpvlv_root = ++xpvlv;
932e9ff9
VB
1632 while (xpvlv < xpvlvend) {
1633 xpvlv->xpv_pv = (char*)(xpvlv + 1);
1634 xpvlv++;
1635 }
1636 xpvlv->xpv_pv = 0;
1637}
1638
645c22ef
DM
1639/* grab a new struct xpvbm from the free list, allocating more if necessary */
1640
932e9ff9
VB
1641STATIC XPVBM*
1642S_new_xpvbm(pTHX)
1643{
1644 XPVBM* xpvbm;
1645 LOCK_SV_MUTEX;
1646 if (!PL_xpvbm_root)
1647 more_xpvbm();
1648 xpvbm = PL_xpvbm_root;
1649 PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv;
1650 UNLOCK_SV_MUTEX;
1651 return xpvbm;
1652}
1653
645c22ef
DM
1654/* return a struct xpvbm to the free list */
1655
932e9ff9
VB
1656STATIC void
1657S_del_xpvbm(pTHX_ XPVBM *p)
1658{
1659 LOCK_SV_MUTEX;
1660 p->xpv_pv = (char*)PL_xpvbm_root;
1661 PL_xpvbm_root = p;
1662 UNLOCK_SV_MUTEX;
1663}
1664
645c22ef
DM
1665/* allocate another arena's worth of struct xpvbm */
1666
932e9ff9
VB
1667STATIC void
1668S_more_xpvbm(pTHX)
1669{
1670 register XPVBM* xpvbm;
1671 register XPVBM* xpvbmend;
9c17f24a 1672 New(721, xpvbm, PERL_ARENA_SIZE/sizeof(XPVBM), XPVBM);
612f20c3
GS
1673 xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot;
1674 PL_xpvbm_arenaroot = xpvbm;
1675
9c17f24a 1676 xpvbmend = &xpvbm[PERL_ARENA_SIZE / sizeof(XPVBM) - 1];
612f20c3 1677 PL_xpvbm_root = ++xpvbm;
932e9ff9
VB
1678 while (xpvbm < xpvbmend) {
1679 xpvbm->xpv_pv = (char*)(xpvbm + 1);
1680 xpvbm++;
1681 }
1682 xpvbm->xpv_pv = 0;
1683}
1684
7bab3ede
MB
1685#define my_safemalloc(s) (void*)safemalloc(s)
1686#define my_safefree(p) safefree((char*)p)
463ee0b2 1687
d33b2eba 1688#ifdef PURIFY
463ee0b2 1689
d33b2eba
GS
1690#define new_XIV() my_safemalloc(sizeof(XPVIV))
1691#define del_XIV(p) my_safefree(p)
ed6116ce 1692
d33b2eba
GS
1693#define new_XNV() my_safemalloc(sizeof(XPVNV))
1694#define del_XNV(p) my_safefree(p)
463ee0b2 1695
d33b2eba
GS
1696#define new_XRV() my_safemalloc(sizeof(XRV))
1697#define del_XRV(p) my_safefree(p)
8c52afec 1698
d33b2eba
GS
1699#define new_XPV() my_safemalloc(sizeof(XPV))
1700#define del_XPV(p) my_safefree(p)
9b94d1dd 1701
d33b2eba
GS
1702#define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1703#define del_XPVIV(p) my_safefree(p)
932e9ff9 1704
d33b2eba
GS
1705#define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1706#define del_XPVNV(p) my_safefree(p)
932e9ff9 1707
d33b2eba
GS
1708#define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1709#define del_XPVCV(p) my_safefree(p)
932e9ff9 1710
d33b2eba
GS
1711#define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1712#define del_XPVAV(p) my_safefree(p)
1713
1714#define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1715#define del_XPVHV(p) my_safefree(p)
1c846c1f 1716
d33b2eba
GS
1717#define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1718#define del_XPVMG(p) my_safefree(p)
1719
1720#define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1721#define del_XPVLV(p) my_safefree(p)
1722
1723#define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1724#define del_XPVBM(p) my_safefree(p)
1725
1726#else /* !PURIFY */
1727
1728#define new_XIV() (void*)new_xiv()
1729#define del_XIV(p) del_xiv((XPVIV*) p)
1730
1731#define new_XNV() (void*)new_xnv()
1732#define del_XNV(p) del_xnv((XPVNV*) p)
9b94d1dd 1733
d33b2eba
GS
1734#define new_XRV() (void*)new_xrv()
1735#define del_XRV(p) del_xrv((XRV*) p)
9b94d1dd 1736
d33b2eba
GS
1737#define new_XPV() (void*)new_xpv()
1738#define del_XPV(p) del_xpv((XPV *)p)
1739
1740#define new_XPVIV() (void*)new_xpviv()
1741#define del_XPVIV(p) del_xpviv((XPVIV *)p)
1742
1743#define new_XPVNV() (void*)new_xpvnv()
1744#define del_XPVNV(p) del_xpvnv((XPVNV *)p)
1745
1746#define new_XPVCV() (void*)new_xpvcv()
1747#define del_XPVCV(p) del_xpvcv((XPVCV *)p)
1748
1749#define new_XPVAV() (void*)new_xpvav()
1750#define del_XPVAV(p) del_xpvav((XPVAV *)p)
1751
1752#define new_XPVHV() (void*)new_xpvhv()
1753#define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1c846c1f 1754
d33b2eba
GS
1755#define new_XPVMG() (void*)new_xpvmg()
1756#define del_XPVMG(p) del_xpvmg((XPVMG *)p)
1757
1758#define new_XPVLV() (void*)new_xpvlv()
1759#define del_XPVLV(p) del_xpvlv((XPVLV *)p)
1760
1761#define new_XPVBM() (void*)new_xpvbm()
1762#define del_XPVBM(p) del_xpvbm((XPVBM *)p)
1763
1764#endif /* PURIFY */
9b94d1dd 1765
d33b2eba
GS
1766#define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1767#define del_XPVGV(p) my_safefree(p)
1c846c1f 1768
d33b2eba
GS
1769#define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1770#define del_XPVFM(p) my_safefree(p)
1c846c1f 1771
d33b2eba
GS
1772#define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1773#define del_XPVIO(p) my_safefree(p)
8990e307 1774
954c1994
GS
1775/*
1776=for apidoc sv_upgrade
1777
ff276b08 1778Upgrade an SV to a more complex form. Generally adds a new body type to the
645c22ef 1779SV, then copies across as much information as possible from the old body.
ff276b08 1780You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
954c1994
GS
1781
1782=cut
1783*/
1784
79072805 1785bool
864dbfa3 1786Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
79072805 1787{
e763e3dc 1788
d2e56290
NC
1789 char* pv;
1790 U32 cur;
1791 U32 len;
1792 IV iv;
1793 NV nv;
1794 MAGIC* magic;
1795 HV* stash;
79072805 1796
765f542d
NC
1797 if (mt != SVt_PV && SvIsCOW(sv)) {
1798 sv_force_normal_flags(sv, 0);
f130fd45
NIS
1799 }
1800
79072805
LW
1801 if (SvTYPE(sv) == mt)
1802 return TRUE;
1803
d2e56290
NC
1804 pv = NULL;
1805 cur = 0;
1806 len = 0;
1807 iv = 0;
1808 nv = 0.0;
1809 magic = NULL;
1810 stash = Nullhv;
1811
79072805
LW
1812 switch (SvTYPE(sv)) {
1813 case SVt_NULL:
79072805 1814 break;
79072805 1815 case SVt_IV:
463ee0b2 1816 iv = SvIVX(sv);
79072805 1817 del_XIV(SvANY(sv));
ed6116ce 1818 if (mt == SVt_NV)
463ee0b2 1819 mt = SVt_PVNV;
ed6116ce
LW
1820 else if (mt < SVt_PVIV)
1821 mt = SVt_PVIV;
79072805
LW
1822 break;
1823 case SVt_NV:
463ee0b2 1824 nv = SvNVX(sv);
79072805 1825 del_XNV(SvANY(sv));
ed6116ce 1826 if (mt < SVt_PVNV)
79072805
LW
1827 mt = SVt_PVNV;
1828 break;
ed6116ce
LW
1829 case SVt_RV:
1830 pv = (char*)SvRV(sv);
ed6116ce 1831 del_XRV(SvANY(sv));
ed6116ce 1832 break;
79072805 1833 case SVt_PV:
463ee0b2 1834 pv = SvPVX(sv);
79072805
LW
1835 cur = SvCUR(sv);
1836 len = SvLEN(sv);
79072805 1837 del_XPV(SvANY(sv));
748a9306
LW
1838 if (mt <= SVt_IV)
1839 mt = SVt_PVIV;
1840 else if (mt == SVt_NV)
1841 mt = SVt_PVNV;
79072805
LW
1842 break;
1843 case SVt_PVIV:
463ee0b2 1844 pv = SvPVX(sv);
79072805
LW
1845 cur = SvCUR(sv);
1846 len = SvLEN(sv);
463ee0b2 1847 iv = SvIVX(sv);
79072805
LW
1848 del_XPVIV(SvANY(sv));
1849 break;
1850 case SVt_PVNV:
463ee0b2 1851 pv = SvPVX(sv);
79072805
LW
1852 cur = SvCUR(sv);
1853 len = SvLEN(sv);
463ee0b2
LW
1854 iv = SvIVX(sv);
1855 nv = SvNVX(sv);
79072805
LW
1856 del_XPVNV(SvANY(sv));
1857 break;
1858 case SVt_PVMG:
0ec50a73
NC
1859 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1860 there's no way that it can be safely upgraded, because perl.c
1861 expects to Safefree(SvANY(PL_mess_sv)) */
1862 assert(sv != PL_mess_sv);
463ee0b2 1863 pv = SvPVX(sv);
79072805
LW
1864 cur = SvCUR(sv);
1865 len = SvLEN(sv);
463ee0b2
LW
1866 iv = SvIVX(sv);
1867 nv = SvNVX(sv);
79072805
LW
1868 magic = SvMAGIC(sv);
1869 stash = SvSTASH(sv);
1870 del_XPVMG(SvANY(sv));
1871 break;
1872 default:
cea2e8a9 1873 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
79072805
LW
1874 }
1875
ffb05e06
NC
1876 SvFLAGS(sv) &= ~SVTYPEMASK;
1877 SvFLAGS(sv) |= mt;
1878
79072805
LW
1879 switch (mt) {
1880 case SVt_NULL:
cea2e8a9 1881 Perl_croak(aTHX_ "Can't upgrade to undef");
79072805
LW
1882 case SVt_IV:
1883 SvANY(sv) = new_XIV();
45977657 1884 SvIV_set(sv, iv);
79072805
LW
1885 break;
1886 case SVt_NV:
1887 SvANY(sv) = new_XNV();
9d6ce603 1888 SvNV_set(sv, nv);
79072805 1889 break;
ed6116ce
LW
1890 case SVt_RV:
1891 SvANY(sv) = new_XRV();
b162af07 1892 SvRV_set(sv, (SV*)pv);
ed6116ce 1893 break;
79072805
LW
1894 case SVt_PVHV:
1895 SvANY(sv) = new_XPVHV();
bd4b1eb5
NC
1896 HvRITER(sv) = 0;
1897 HvEITER(sv) = 0;
1898 HvPMROOT(sv) = 0;
1899 HvNAME(sv) = 0;
463ee0b2
LW
1900 HvFILL(sv) = 0;
1901 HvMAX(sv) = 0;
8aacddc1
NIS
1902 HvTOTALKEYS(sv) = 0;
1903 HvPLACEHOLDERS(sv) = 0;
bd4b1eb5
NC
1904
1905 /* Fall through... */
1906 if (0) {
1907 case SVt_PVAV:
1908 SvANY(sv) = new_XPVAV();
1909 AvMAX(sv) = -1;
1910 AvFILLp(sv) = -1;
1911 AvALLOC(sv) = 0;
1912 AvARYLEN(sv)= 0;
1913 AvFLAGS(sv) = AVf_REAL;
1914 SvIV_set(sv, 0);
1915 SvNV_set(sv, 0.0);
1916 }
1917 /* to here. */
c2bfdfaf
NC
1918 /* XXX? Only SVt_NULL is ever upgraded to AV or HV? */
1919 assert(!pv);
8bd4d4c5
NC
1920 /* FIXME. Should be able to remove all this if()... if the above
1921 assertion is genuinely always true. */
1922 if(SvOOK(sv)) {
1923 pv -= iv;
1924 SvFLAGS(sv) &= ~SVf_OOK;
1925 }
1926 Safefree(pv);
bd4b1eb5 1927 SvPV_set(sv, (char*)0);
b162af07
SP
1928 SvMAGIC_set(sv, magic);
1929 SvSTASH_set(sv, stash);
79072805 1930 break;
bd4b1eb5
NC
1931
1932 case SVt_PVIO:
1933 SvANY(sv) = new_XPVIO();
1934 Zero(SvANY(sv), 1, XPVIO);
1935 IoPAGE_LEN(sv) = 60;
1936 goto set_magic_common;
1937 case SVt_PVFM:
1938 SvANY(sv) = new_XPVFM();
1939 Zero(SvANY(sv), 1, XPVFM);
1940 goto set_magic_common;
1941 case SVt_PVBM:
1942 SvANY(sv) = new_XPVBM();
1943 BmRARE(sv) = 0;
1944 BmUSEFUL(sv) = 0;
1945 BmPREVIOUS(sv) = 0;
1946 goto set_magic_common;
1947 case SVt_PVGV:
1948 SvANY(sv) = new_XPVGV();
1949 GvGP(sv) = 0;
1950 GvNAME(sv) = 0;
1951 GvNAMELEN(sv) = 0;
1952 GvSTASH(sv) = 0;
1953 GvFLAGS(sv) = 0;
1954 goto set_magic_common;
79072805
LW
1955 case SVt_PVCV:
1956 SvANY(sv) = new_XPVCV();
748a9306 1957 Zero(SvANY(sv), 1, XPVCV);
bd4b1eb5
NC
1958 goto set_magic_common;
1959 case SVt_PVLV:
1960 SvANY(sv) = new_XPVLV();
1961 LvTARGOFF(sv) = 0;
1962 LvTARGLEN(sv) = 0;
1963 LvTARG(sv) = 0;
1964 LvTYPE(sv) = 0;
93a17b20 1965 GvGP(sv) = 0;
79072805
LW
1966 GvNAME(sv) = 0;
1967 GvNAMELEN(sv) = 0;
1968 GvSTASH(sv) = 0;
a5f75d66 1969 GvFLAGS(sv) = 0;
bd4b1eb5
NC
1970 /* Fall through. */
1971 if (0) {
1972 case SVt_PVMG:
1973 SvANY(sv) = new_XPVMG();
1974 }
1975 set_magic_common:
b162af07
SP
1976 SvMAGIC_set(sv, magic);
1977 SvSTASH_set(sv, stash);
bd4b1eb5
NC
1978 /* Fall through. */
1979 if (0) {
1980 case SVt_PVNV:
1981 SvANY(sv) = new_XPVNV();
1982 }
9d6ce603 1983 SvNV_set(sv, nv);
bd4b1eb5
NC
1984 /* Fall through. */
1985 if (0) {
1986 case SVt_PVIV:
1987 SvANY(sv) = new_XPVIV();
1988 if (SvNIOK(sv))
1989 (void)SvIOK_on(sv);
1990 SvNOK_off(sv);
1991 }
1992 SvIV_set(sv, iv);
1993 /* Fall through. */
1994 if (0) {
1995 case SVt_PV:
1996 SvANY(sv) = new_XPV();
1997 }
f880fe2f 1998 SvPV_set(sv, pv);
b162af07
SP
1999 SvCUR_set(sv, cur);
2000 SvLEN_set(sv, len);
8990e307
LW
2001 break;
2002 }
79072805
LW
2003 return TRUE;
2004}
2005
645c22ef
DM
2006/*
2007=for apidoc sv_backoff
2008
2009Remove any string offset. You should normally use the C<SvOOK_off> macro
2010wrapper instead.
2011
2012=cut
2013*/
2014
79072805 2015int
864dbfa3 2016Perl_sv_backoff(pTHX_ register SV *sv)
79072805
LW
2017{
2018 assert(SvOOK(sv));
463ee0b2
LW
2019 if (SvIVX(sv)) {
2020 char *s = SvPVX(sv);
b162af07 2021 SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
f880fe2f 2022 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
79072805 2023 SvIV_set(sv, 0);
463ee0b2 2024 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
2025 }
2026 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 2027 return 0;
79072805
LW
2028}
2029
954c1994
GS
2030/*
2031=for apidoc sv_grow
2032
645c22ef
DM
2033Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
2034upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
2035Use the C<SvGROW> wrapper instead.
954c1994
GS
2036
2037=cut
2038*/
2039
79072805 2040char *
864dbfa3 2041Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
79072805
LW
2042{
2043 register char *s;
2044
55497cff 2045#ifdef HAS_64K_LIMIT
79072805 2046 if (newlen >= 0x10000) {
1d7c1841
GS
2047 PerlIO_printf(Perl_debug_log,
2048 "Allocation too large: %"UVxf"\n", (UV)newlen);
79072805
LW
2049 my_exit(1);
2050 }
55497cff 2051#endif /* HAS_64K_LIMIT */
a0d0e21e
LW
2052 if (SvROK(sv))
2053 sv_unref(sv);
79072805
LW
2054 if (SvTYPE(sv) < SVt_PV) {
2055 sv_upgrade(sv, SVt_PV);
463ee0b2 2056 s = SvPVX(sv);
79072805
LW
2057 }
2058 else if (SvOOK(sv)) { /* pv is offset? */
2059 sv_backoff(sv);
463ee0b2 2060 s = SvPVX(sv);
79072805
LW
2061 if (newlen > SvLEN(sv))
2062 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
c6f8c383
GA
2063#ifdef HAS_64K_LIMIT
2064 if (newlen >= 0x10000)
2065 newlen = 0xFFFF;
2066#endif
79072805 2067 }
bc44a8a2 2068 else
463ee0b2 2069 s = SvPVX(sv);
54f0641b 2070
79072805 2071 if (newlen > SvLEN(sv)) { /* need more room? */
8d6dde3e 2072 if (SvLEN(sv) && s) {
7bab3ede 2073#ifdef MYMALLOC
8d6dde3e
IZ
2074 STRLEN l = malloced_size((void*)SvPVX(sv));
2075 if (newlen <= l) {
2076 SvLEN_set(sv, l);
2077 return s;
2078 } else
c70c8a0a 2079#endif
79072805 2080 Renew(s,newlen,char);
8d6dde3e 2081 }
bfed75c6 2082 else {
4e83176d 2083 New(703, s, newlen, char);
40565179 2084 if (SvPVX(sv) && SvCUR(sv)) {
54f0641b 2085 Move(SvPVX(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
40565179 2086 }
4e83176d 2087 }
79072805 2088 SvPV_set(sv, s);
e1ec3a88 2089 SvLEN_set(sv, newlen);
79072805
LW
2090 }
2091 return s;
2092}
2093
954c1994
GS
2094/*
2095=for apidoc sv_setiv
2096
645c22ef
DM
2097Copies an integer into the given SV, upgrading first if necessary.
2098Does not handle 'set' magic. See also C<sv_setiv_mg>.
954c1994
GS
2099
2100=cut
2101*/
2102
79072805 2103void
864dbfa3 2104Perl_sv_setiv(pTHX_ register SV *sv, IV i)
79072805 2105{
765f542d 2106 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2
LW
2107 switch (SvTYPE(sv)) {
2108 case SVt_NULL:
79072805 2109 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
2110 break;
2111 case SVt_NV:
2112 sv_upgrade(sv, SVt_PVNV);
2113 break;
ed6116ce 2114 case SVt_RV:
463ee0b2 2115 case SVt_PV:
79072805 2116 sv_upgrade(sv, SVt_PVIV);
463ee0b2 2117 break;
a0d0e21e
LW
2118
2119 case SVt_PVGV:
a0d0e21e
LW
2120 case SVt_PVAV:
2121 case SVt_PVHV:
2122 case SVt_PVCV:
2123 case SVt_PVFM:
2124 case SVt_PVIO:
411caa50 2125 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
53e06cf0 2126 OP_DESC(PL_op));
463ee0b2 2127 }
a0d0e21e 2128 (void)SvIOK_only(sv); /* validate number */
45977657 2129 SvIV_set(sv, i);
463ee0b2 2130 SvTAINT(sv);
79072805
LW
2131}
2132
954c1994
GS
2133/*
2134=for apidoc sv_setiv_mg
2135
2136Like C<sv_setiv>, but also handles 'set' magic.
2137
2138=cut
2139*/
2140
79072805 2141void
864dbfa3 2142Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
ef50df4b
GS
2143{
2144 sv_setiv(sv,i);
2145 SvSETMAGIC(sv);
2146}
2147
954c1994
GS
2148/*
2149=for apidoc sv_setuv
2150
645c22ef
DM
2151Copies an unsigned integer into the given SV, upgrading first if necessary.
2152Does not handle 'set' magic. See also C<sv_setuv_mg>.
954c1994
GS
2153
2154=cut
2155*/
2156
ef50df4b 2157void
864dbfa3 2158Perl_sv_setuv(pTHX_ register SV *sv, UV u)
55497cff 2159{
55ada374
NC
2160 /* With these two if statements:
2161 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 2162
55ada374
NC
2163 without
2164 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 2165
55ada374
NC
2166 If you wish to remove them, please benchmark to see what the effect is
2167 */
28e5dec8
JH
2168 if (u <= (UV)IV_MAX) {
2169 sv_setiv(sv, (IV)u);
2170 return;
2171 }
25da4f38
IZ
2172 sv_setiv(sv, 0);
2173 SvIsUV_on(sv);
607fa7f2 2174 SvUV_set(sv, u);
55497cff 2175}
2176
954c1994
GS
2177/*
2178=for apidoc sv_setuv_mg
2179
2180Like C<sv_setuv>, but also handles 'set' magic.
2181
2182=cut
2183*/
2184
55497cff 2185void
864dbfa3 2186Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
ef50df4b 2187{
55ada374
NC
2188 /* With these two if statements:
2189 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 2190
55ada374
NC
2191 without
2192 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 2193
55ada374
NC
2194 If you wish to remove them, please benchmark to see what the effect is
2195 */
28e5dec8
JH
2196 if (u <= (UV)IV_MAX) {
2197 sv_setiv(sv, (IV)u);
2198 } else {
2199 sv_setiv(sv, 0);
2200 SvIsUV_on(sv);
2201 sv_setuv(sv,u);
2202 }
ef50df4b
GS
2203 SvSETMAGIC(sv);
2204}
2205
954c1994
GS
2206/*
2207=for apidoc sv_setnv
2208
645c22ef
DM
2209Copies a double into the given SV, upgrading first if necessary.
2210Does not handle 'set' magic. See also C<sv_setnv_mg>.
954c1994
GS
2211
2212=cut
2213*/
2214
ef50df4b 2215void
65202027 2216Perl_sv_setnv(pTHX_ register SV *sv, NV num)
79072805 2217{
765f542d 2218 SV_CHECK_THINKFIRST_COW_DROP(sv);
a0d0e21e
LW
2219 switch (SvTYPE(sv)) {
2220 case SVt_NULL:
2221 case SVt_IV:
79072805 2222 sv_upgrade(sv, SVt_NV);
a0d0e21e 2223 break;
a0d0e21e
LW
2224 case SVt_RV:
2225 case SVt_PV:
2226 case SVt_PVIV:
79072805 2227 sv_upgrade(sv, SVt_PVNV);
a0d0e21e 2228 break;
827b7e14 2229
a0d0e21e 2230 case SVt_PVGV:
a0d0e21e
LW
2231 case SVt_PVAV:
2232 case SVt_PVHV:
2233 case SVt_PVCV:
2234 case SVt_PVFM:
2235 case SVt_PVIO:
411caa50 2236 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
53e06cf0 2237 OP_NAME(PL_op));
79072805 2238 }
9d6ce603 2239 SvNV_set(sv, num);
a0d0e21e 2240 (void)SvNOK_only(sv); /* validate number */
463ee0b2 2241 SvTAINT(sv);
79072805
LW
2242}
2243
954c1994
GS
2244/*
2245=for apidoc sv_setnv_mg
2246
2247Like C<sv_setnv>, but also handles 'set' magic.
2248
2249=cut
2250*/
2251
ef50df4b 2252void
65202027 2253Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
ef50df4b
GS
2254{
2255 sv_setnv(sv,num);
2256 SvSETMAGIC(sv);
2257}
2258
645c22ef
DM
2259/* Print an "isn't numeric" warning, using a cleaned-up,
2260 * printable version of the offending string
2261 */
2262
76e3520e 2263STATIC void
cea2e8a9 2264S_not_a_number(pTHX_ SV *sv)
a0d0e21e 2265{
94463019
JH
2266 SV *dsv;
2267 char tmpbuf[64];
2268 char *pv;
2269
2270 if (DO_UTF8(sv)) {
2271 dsv = sv_2mortal(newSVpv("", 0));
2272 pv = sv_uni_display(dsv, sv, 10, 0);
2273 } else {
2274 char *d = tmpbuf;
2275 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
2276 /* each *s can expand to 4 chars + "...\0",
2277 i.e. need room for 8 chars */
ecdeb87c 2278
94463019
JH
2279 char *s, *end;
2280 for (s = SvPVX(sv), end = s + SvCUR(sv); s < end && d < limit; s++) {
2281 int ch = *s & 0xFF;
2282 if (ch & 128 && !isPRINT_LC(ch)) {
2283 *d++ = 'M';
2284 *d++ = '-';
2285 ch &= 127;
2286 }
2287 if (ch == '\n') {
2288 *d++ = '\\';
2289 *d++ = 'n';
2290 }
2291 else if (ch == '\r') {
2292 *d++ = '\\';
2293 *d++ = 'r';
2294 }
2295 else if (ch == '\f') {
2296 *d++ = '\\';
2297 *d++ = 'f';
2298 }
2299 else if (ch == '\\') {
2300 *d++ = '\\';
2301 *d++ = '\\';
2302 }
2303 else if (ch == '\0') {
2304 *d++ = '\\';
2305 *d++ = '0';
2306 }
2307 else if (isPRINT_LC(ch))
2308 *d++ = ch;
2309 else {
2310 *d++ = '^';
2311 *d++ = toCTRL(ch);
2312 }
2313 }
2314 if (s < end) {
2315 *d++ = '.';
2316 *d++ = '.';
2317 *d++ = '.';
2318 }
2319 *d = '\0';
2320 pv = tmpbuf;
a0d0e21e 2321 }
a0d0e21e 2322
533c011a 2323 if (PL_op)
9014280d 2324 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019
JH
2325 "Argument \"%s\" isn't numeric in %s", pv,
2326 OP_DESC(PL_op));
a0d0e21e 2327 else
9014280d 2328 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
94463019 2329 "Argument \"%s\" isn't numeric", pv);
a0d0e21e
LW
2330}
2331
c2988b20
NC
2332/*
2333=for apidoc looks_like_number
2334
645c22ef
DM
2335Test if the content of an SV looks like a number (or is a number).
2336C<Inf> and C<Infinity> are treated as numbers (so will not issue a
2337non-numeric warning), even if your atof() doesn't grok them.
c2988b20
NC
2338
2339=cut
2340*/
2341
2342I32
2343Perl_looks_like_number(pTHX_ SV *sv)
2344{
2345 register char *sbegin;
2346 STRLEN len;
2347
2348 if (SvPOK(sv)) {
2349 sbegin = SvPVX(sv);
2350 len = SvCUR(sv);
2351 }
2352 else if (SvPOKp(sv))
2353 sbegin = SvPV(sv, len);
2354 else
e0ab1c0e 2355 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
c2988b20
NC
2356 return grok_number(sbegin, len, NULL);
2357}
25da4f38
IZ
2358
2359/* Actually, ISO C leaves conversion of UV to IV undefined, but
2360 until proven guilty, assume that things are not that bad... */
2361
645c22ef
DM
2362/*
2363 NV_PRESERVES_UV:
2364
2365 As 64 bit platforms often have an NV that doesn't preserve all bits of
28e5dec8
JH
2366 an IV (an assumption perl has been based on to date) it becomes necessary
2367 to remove the assumption that the NV always carries enough precision to
2368 recreate the IV whenever needed, and that the NV is the canonical form.
2369 Instead, IV/UV and NV need to be given equal rights. So as to not lose
645c22ef 2370 precision as a side effect of conversion (which would lead to insanity
28e5dec8
JH
2371 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
2372 1) to distinguish between IV/UV/NV slots that have cached a valid
2373 conversion where precision was lost and IV/UV/NV slots that have a
2374 valid conversion which has lost no precision
645c22ef 2375 2) to ensure that if a numeric conversion to one form is requested that
28e5dec8
JH
2376 would lose precision, the precise conversion (or differently
2377 imprecise conversion) is also performed and cached, to prevent
2378 requests for different numeric formats on the same SV causing
2379 lossy conversion chains. (lossless conversion chains are perfectly
2380 acceptable (still))
2381
2382
2383 flags are used:
2384 SvIOKp is true if the IV slot contains a valid value
2385 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
2386 SvNOKp is true if the NV slot contains a valid value
2387 SvNOK is true only if the NV value is accurate
2388
2389 so
645c22ef 2390 while converting from PV to NV, check to see if converting that NV to an
28e5dec8
JH
2391 IV(or UV) would lose accuracy over a direct conversion from PV to
2392 IV(or UV). If it would, cache both conversions, return NV, but mark
2393 SV as IOK NOKp (ie not NOK).
2394
645c22ef 2395 While converting from PV to IV, check to see if converting that IV to an
28e5dec8
JH
2396 NV would lose accuracy over a direct conversion from PV to NV. If it
2397 would, cache both conversions, flag similarly.
2398
2399 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
2400 correctly because if IV & NV were set NV *always* overruled.
645c22ef
DM
2401 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
2402 changes - now IV and NV together means that the two are interchangeable:
28e5dec8 2403 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
d460ef45 2404
645c22ef
DM
2405 The benefit of this is that operations such as pp_add know that if
2406 SvIOK is true for both left and right operands, then integer addition
2407 can be used instead of floating point (for cases where the result won't
2408 overflow). Before, floating point was always used, which could lead to
28e5dec8
JH
2409 loss of precision compared with integer addition.
2410
2411 * making IV and NV equal status should make maths accurate on 64 bit
2412 platforms
2413 * may speed up maths somewhat if pp_add and friends start to use
645c22ef 2414 integers when possible instead of fp. (Hopefully the overhead in
28e5dec8
JH
2415 looking for SvIOK and checking for overflow will not outweigh the
2416 fp to integer speedup)
2417 * will slow down integer operations (callers of SvIV) on "inaccurate"
2418 values, as the change from SvIOK to SvIOKp will cause a call into
2419 sv_2iv each time rather than a macro access direct to the IV slot
2420 * should speed up number->string conversion on integers as IV is
645c22ef 2421 favoured when IV and NV are equally accurate
28e5dec8
JH
2422
2423 ####################################################################
645c22ef
DM
2424 You had better be using SvIOK_notUV if you want an IV for arithmetic:
2425 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
2426 On the other hand, SvUOK is true iff UV.
28e5dec8
JH
2427 ####################################################################
2428
645c22ef 2429 Your mileage will vary depending your CPU's relative fp to integer
28e5dec8
JH
2430 performance ratio.
2431*/
2432
2433#ifndef NV_PRESERVES_UV
645c22ef
DM
2434# define IS_NUMBER_UNDERFLOW_IV 1
2435# define IS_NUMBER_UNDERFLOW_UV 2
2436# define IS_NUMBER_IV_AND_UV 2
2437# define IS_NUMBER_OVERFLOW_IV 4
2438# define IS_NUMBER_OVERFLOW_UV 5
2439
2440/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
28e5dec8
JH
2441
2442/* For sv_2nv these three cases are "SvNOK and don't bother casting" */
2443STATIC int
645c22ef 2444S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
28e5dec8 2445{
1779d84d 2446 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%"NVgf" inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), SvNVX(sv), (UV)numtype));
28e5dec8
JH
2447 if (SvNVX(sv) < (NV)IV_MIN) {
2448 (void)SvIOKp_on(sv);
2449 (void)SvNOK_on(sv);
45977657 2450 SvIV_set(sv, IV_MIN);
28e5dec8
JH
2451 return IS_NUMBER_UNDERFLOW_IV;
2452 }
2453 if (SvNVX(sv) > (NV)UV_MAX) {
2454 (void)SvIOKp_on(sv);
2455 (void)SvNOK_on(sv);
2456 SvIsUV_on(sv);
607fa7f2 2457 SvUV_set(sv, UV_MAX);
28e5dec8
JH
2458 return IS_NUMBER_OVERFLOW_UV;
2459 }
c2988b20
NC
2460 (void)SvIOKp_on(sv);
2461 (void)SvNOK_on(sv);
2462 /* Can't use strtol etc to convert this string. (See truth table in
2463 sv_2iv */
2464 if (SvNVX(sv) <= (UV)IV_MAX) {
45977657 2465 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2466 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2467 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2468 } else {
2469 /* Integer is imprecise. NOK, IOKp */
2470 }
2471 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2472 }
2473 SvIsUV_on(sv);
607fa7f2 2474 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2475 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2476 if (SvUVX(sv) == UV_MAX) {
2477 /* As we know that NVs don't preserve UVs, UV_MAX cannot
2478 possibly be preserved by NV. Hence, it must be overflow.
2479 NOK, IOKp */
2480 return IS_NUMBER_OVERFLOW_UV;
2481 }
2482 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2483 } else {
2484 /* Integer is imprecise. NOK, IOKp */
28e5dec8 2485 }
c2988b20 2486 return IS_NUMBER_OVERFLOW_IV;
28e5dec8 2487}
645c22ef
DM
2488#endif /* !NV_PRESERVES_UV*/
2489
891f9566
YST
2490/* sv_2iv() is now a macro using Perl_sv_2iv_flags();
2491 * this function provided for binary compatibility only
2492 */
2493
2494IV
2495Perl_sv_2iv(pTHX_ register SV *sv)
2496{
2497 return sv_2iv_flags(sv, SV_GMAGIC);
2498}
2499
645c22ef 2500/*
891f9566 2501=for apidoc sv_2iv_flags
645c22ef 2502
891f9566
YST
2503Return the integer value of an SV, doing any necessary string
2504conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2505Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
645c22ef
DM
2506
2507=cut
2508*/
28e5dec8 2509
a0d0e21e 2510IV
891f9566 2511Perl_sv_2iv_flags(pTHX_ register SV *sv, I32 flags)
79072805
LW
2512{
2513 if (!sv)
2514 return 0;
8990e307 2515 if (SvGMAGICAL(sv)) {
891f9566
YST
2516 if (flags & SV_GMAGIC)
2517 mg_get(sv);
463ee0b2
LW
2518 if (SvIOKp(sv))
2519 return SvIVX(sv);
748a9306 2520 if (SvNOKp(sv)) {
25da4f38 2521 return I_V(SvNVX(sv));
748a9306 2522 }
36477c24 2523 if (SvPOKp(sv) && SvLEN(sv))
2524 return asIV(sv);
3fe9a6f1 2525 if (!SvROK(sv)) {
d008e5eb 2526 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2527 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2528 report_uninit(sv);
c6ee37c5 2529 }
36477c24 2530 return 0;
3fe9a6f1 2531 }
463ee0b2 2532 }
ed6116ce 2533 if (SvTHINKFIRST(sv)) {
a0d0e21e 2534 if (SvROK(sv)) {
a0d0e21e 2535 SV* tmpstr;
1554e226 2536 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2537 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2538 return SvIV(tmpstr);
56431972 2539 return PTR2IV(SvRV(sv));
a0d0e21e 2540 }
765f542d
NC
2541 if (SvIsCOW(sv)) {
2542 sv_force_normal_flags(sv, 0);
47deb5e7 2543 }
0336b60e 2544 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2545 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2546 report_uninit(sv);
ed6116ce
LW
2547 return 0;
2548 }
79072805 2549 }
25da4f38
IZ
2550 if (SvIOKp(sv)) {
2551 if (SvIsUV(sv)) {
2552 return (IV)(SvUVX(sv));
2553 }
2554 else {
2555 return SvIVX(sv);
2556 }
463ee0b2 2557 }
748a9306 2558 if (SvNOKp(sv)) {
28e5dec8
JH
2559 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2560 * without also getting a cached IV/UV from it at the same time
2561 * (ie PV->NV conversion should detect loss of accuracy and cache
2562 * IV or UV at same time to avoid this. NWC */
25da4f38
IZ
2563
2564 if (SvTYPE(sv) == SVt_NV)
2565 sv_upgrade(sv, SVt_PVNV);
2566
28e5dec8
JH
2567 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2568 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2569 certainly cast into the IV range at IV_MAX, whereas the correct
2570 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2571 cases go to UV */
2572 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2573 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2574 if (SvNVX(sv) == (NV) SvIVX(sv)
2575#ifndef NV_PRESERVES_UV
2576 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2577 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2578 /* Don't flag it as "accurately an integer" if the number
2579 came from a (by definition imprecise) NV operation, and
2580 we're outside the range of NV integer precision */
2581#endif
2582 ) {
2583 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2584 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2585 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2586 PTR2UV(sv),
2587 SvNVX(sv),
2588 SvIVX(sv)));
2589
2590 } else {
2591 /* IV not precise. No need to convert from PV, as NV
2592 conversion would already have cached IV if it detected
2593 that PV->IV would be better than PV->NV->IV
2594 flags already correct - don't set public IOK. */
2595 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2596 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2597 PTR2UV(sv),
2598 SvNVX(sv),
2599 SvIVX(sv)));
2600 }
2601 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2602 but the cast (NV)IV_MIN rounds to a the value less (more
2603 negative) than IV_MIN which happens to be equal to SvNVX ??
2604 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2605 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2606 (NV)UVX == NVX are both true, but the values differ. :-(
2607 Hopefully for 2s complement IV_MIN is something like
2608 0x8000000000000000 which will be exact. NWC */
d460ef45 2609 }
25da4f38 2610 else {
607fa7f2 2611 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2612 if (
2613 (SvNVX(sv) == (NV) SvUVX(sv))
2614#ifndef NV_PRESERVES_UV
2615 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2616 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2617 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2618 /* Don't flag it as "accurately an integer" if the number
2619 came from a (by definition imprecise) NV operation, and
2620 we're outside the range of NV integer precision */
2621#endif
2622 )
2623 SvIOK_on(sv);
25da4f38
IZ
2624 SvIsUV_on(sv);
2625 ret_iv_max:
1c846c1f 2626 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 2627 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 2628 PTR2UV(sv),
57def98f
JH
2629 SvUVX(sv),
2630 SvUVX(sv)));
25da4f38
IZ
2631 return (IV)SvUVX(sv);
2632 }
748a9306
LW
2633 }
2634 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20
NC
2635 UV value;
2636 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
25da4f38
IZ
2637 /* We want to avoid a possible problem when we cache an IV which
2638 may be later translated to an NV, and the resulting NV is not
c2988b20
NC
2639 the same as the direct translation of the initial string
2640 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2641 be careful to ensure that the value with the .456 is around if the
2642 NV value is requested in the future).
1c846c1f 2643
25da4f38
IZ
2644 This means that if we cache such an IV, we need to cache the
2645 NV as well. Moreover, we trade speed for space, and do not
28e5dec8 2646 cache the NV if we are sure it's not needed.
25da4f38 2647 */
16b7a9a4 2648
c2988b20
NC
2649 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2650 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2651 == IS_NUMBER_IN_UV) {
5e045b90 2652 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8
JH
2653 if (SvTYPE(sv) < SVt_PVIV)
2654 sv_upgrade(sv, SVt_PVIV);
f7bbb42a 2655 (void)SvIOK_on(sv);
c2988b20
NC
2656 } else if (SvTYPE(sv) < SVt_PVNV)
2657 sv_upgrade(sv, SVt_PVNV);
28e5dec8 2658
c2988b20
NC
2659 /* If NV preserves UV then we only use the UV value if we know that
2660 we aren't going to call atof() below. If NVs don't preserve UVs
2661 then the value returned may have more precision than atof() will
2662 return, even though value isn't perfectly accurate. */
2663 if ((numtype & (IS_NUMBER_IN_UV
2664#ifdef NV_PRESERVES_UV
2665 | IS_NUMBER_NOT_INT
2666#endif
2667 )) == IS_NUMBER_IN_UV) {
2668 /* This won't turn off the public IOK flag if it was set above */
2669 (void)SvIOKp_on(sv);
2670
2671 if (!(numtype & IS_NUMBER_NEG)) {
2672 /* positive */;
2673 if (value <= (UV)IV_MAX) {
45977657 2674 SvIV_set(sv, (IV)value);
c2988b20 2675 } else {
607fa7f2 2676 SvUV_set(sv, value);
c2988b20
NC
2677 SvIsUV_on(sv);
2678 }
2679 } else {
2680 /* 2s complement assumption */
2681 if (value <= (UV)IV_MIN) {
45977657 2682 SvIV_set(sv, -(IV)value);
c2988b20
NC
2683 } else {
2684 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2685 I'm assuming it will be rare. */
c2988b20
NC
2686 if (SvTYPE(sv) < SVt_PVNV)
2687 sv_upgrade(sv, SVt_PVNV);
2688 SvNOK_on(sv);
2689 SvIOK_off(sv);
2690 SvIOKp_on(sv);
9d6ce603 2691 SvNV_set(sv, -(NV)value);
45977657 2692 SvIV_set(sv, IV_MIN);
c2988b20
NC
2693 }
2694 }
2695 }
2696 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2697 will be in the previous block to set the IV slot, and the next
2698 block to set the NV slot. So no else here. */
2699
2700 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2701 != IS_NUMBER_IN_UV) {
2702 /* It wasn't an (integer that doesn't overflow the UV). */
9d6ce603 2703 SvNV_set(sv, Atof(SvPVX(sv)));
28e5dec8 2704
c2988b20
NC
2705 if (! numtype && ckWARN(WARN_NUMERIC))
2706 not_a_number(sv);
28e5dec8 2707
65202027 2708#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2709 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2710 PTR2UV(sv), SvNVX(sv)));
65202027 2711#else
1779d84d 2712 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
c2988b20 2713 PTR2UV(sv), SvNVX(sv)));
65202027 2714#endif
28e5dec8
JH
2715
2716
2717#ifdef NV_PRESERVES_UV
c2988b20
NC
2718 (void)SvIOKp_on(sv);
2719 (void)SvNOK_on(sv);
2720 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2721 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2722 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2723 SvIOK_on(sv);
28e5dec8 2724 } else {
c2988b20
NC
2725 /* Integer is imprecise. NOK, IOKp */
2726 }
2727 /* UV will not work better than IV */
2728 } else {
2729 if (SvNVX(sv) > (NV)UV_MAX) {
2730 SvIsUV_on(sv);
2731 /* Integer is inaccurate. NOK, IOKp, is UV */
607fa7f2 2732 SvUV_set(sv, UV_MAX);
c2988b20
NC
2733 SvIsUV_on(sv);
2734 } else {
607fa7f2 2735 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
2736 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2737 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2738 SvIOK_on(sv);
28e5dec8
JH
2739 SvIsUV_on(sv);
2740 } else {
c2988b20
NC
2741 /* Integer is imprecise. NOK, IOKp, is UV */
2742 SvIsUV_on(sv);
28e5dec8 2743 }
28e5dec8 2744 }
c2988b20
NC
2745 goto ret_iv_max;
2746 }
28e5dec8 2747#else /* NV_PRESERVES_UV */
c2988b20
NC
2748 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2749 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2750 /* The IV slot will have been set from value returned by
2751 grok_number above. The NV slot has just been set using
2752 Atof. */
560b0c46 2753 SvNOK_on(sv);
c2988b20
NC
2754 assert (SvIOKp(sv));
2755 } else {
2756 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2757 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2758 /* Small enough to preserve all bits. */
2759 (void)SvIOKp_on(sv);
2760 SvNOK_on(sv);
45977657 2761 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
2762 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2763 SvIOK_on(sv);
2764 /* Assumption: first non-preserved integer is < IV_MAX,
2765 this NV is in the preserved range, therefore: */
2766 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2767 < (UV)IV_MAX)) {
32fdb065 2768 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
2769 }
2770 } else {
2771 /* IN_UV NOT_INT
2772 0 0 already failed to read UV.
2773 0 1 already failed to read UV.
2774 1 0 you won't get here in this case. IV/UV
2775 slot set, public IOK, Atof() unneeded.
2776 1 1 already read UV.
2777 so there's no point in sv_2iuv_non_preserve() attempting
2778 to use atol, strtol, strtoul etc. */
2779 if (sv_2iuv_non_preserve (sv, numtype)
2780 >= IS_NUMBER_OVERFLOW_IV)
2781 goto ret_iv_max;
2782 }
2783 }
28e5dec8 2784#endif /* NV_PRESERVES_UV */
25da4f38 2785 }
28e5dec8 2786 } else {
599cee73 2787 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 2788 report_uninit(sv);
25da4f38
IZ
2789 if (SvTYPE(sv) < SVt_IV)
2790 /* Typically the caller expects that sv_any is not NULL now. */
2791 sv_upgrade(sv, SVt_IV);
a0d0e21e 2792 return 0;
79072805 2793 }
1d7c1841
GS
2794 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2795 PTR2UV(sv),SvIVX(sv)));
25da4f38 2796 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
2797}
2798
891f9566
YST
2799/* sv_2uv() is now a macro using Perl_sv_2uv_flags();
2800 * this function provided for binary compatibility only
2801 */
2802
2803UV
2804Perl_sv_2uv(pTHX_ register SV *sv)
2805{
2806 return sv_2uv_flags(sv, SV_GMAGIC);
2807}
2808
645c22ef 2809/*
891f9566 2810=for apidoc sv_2uv_flags
645c22ef
DM
2811
2812Return the unsigned integer value of an SV, doing any necessary string
891f9566
YST
2813conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2814Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
645c22ef
DM
2815
2816=cut
2817*/
2818
ff68c719 2819UV
891f9566 2820Perl_sv_2uv_flags(pTHX_ register SV *sv, I32 flags)
ff68c719 2821{
2822 if (!sv)
2823 return 0;
2824 if (SvGMAGICAL(sv)) {
891f9566
YST
2825 if (flags & SV_GMAGIC)
2826 mg_get(sv);
ff68c719 2827 if (SvIOKp(sv))
2828 return SvUVX(sv);
2829 if (SvNOKp(sv))
2830 return U_V(SvNVX(sv));
36477c24 2831 if (SvPOKp(sv) && SvLEN(sv))
2832 return asUV(sv);
3fe9a6f1 2833 if (!SvROK(sv)) {
d008e5eb 2834 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2835 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 2836 report_uninit(sv);
c6ee37c5 2837 }
36477c24 2838 return 0;
3fe9a6f1 2839 }
ff68c719 2840 }
2841 if (SvTHINKFIRST(sv)) {
2842 if (SvROK(sv)) {
ff68c719 2843 SV* tmpstr;
1554e226 2844 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 2845 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2846 return SvUV(tmpstr);
56431972 2847 return PTR2UV(SvRV(sv));
ff68c719 2848 }
765f542d
NC
2849 if (SvIsCOW(sv)) {
2850 sv_force_normal_flags(sv, 0);
8a818333 2851 }
0336b60e 2852 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2853 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 2854 report_uninit(sv);
ff68c719 2855 return 0;
2856 }
2857 }
25da4f38
IZ
2858 if (SvIOKp(sv)) {
2859 if (SvIsUV(sv)) {
2860 return SvUVX(sv);
2861 }
2862 else {
2863 return (UV)SvIVX(sv);
2864 }
ff68c719 2865 }
2866 if (SvNOKp(sv)) {
28e5dec8
JH
2867 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2868 * without also getting a cached IV/UV from it at the same time
2869 * (ie PV->NV conversion should detect loss of accuracy and cache
2870 * IV or UV at same time to avoid this. */
2871 /* IV-over-UV optimisation - choose to cache IV if possible */
2872
25da4f38
IZ
2873 if (SvTYPE(sv) == SVt_NV)
2874 sv_upgrade(sv, SVt_PVNV);
28e5dec8
JH
2875
2876 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2877 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 2878 SvIV_set(sv, I_V(SvNVX(sv)));
28e5dec8
JH
2879 if (SvNVX(sv) == (NV) SvIVX(sv)
2880#ifndef NV_PRESERVES_UV
2881 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2882 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2883 /* Don't flag it as "accurately an integer" if the number
2884 came from a (by definition imprecise) NV operation, and
2885 we're outside the range of NV integer precision */
2886#endif
2887 ) {
2888 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2889 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2890 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
28e5dec8
JH
2891 PTR2UV(sv),
2892 SvNVX(sv),
2893 SvIVX(sv)));
2894
2895 } else {
2896 /* IV not precise. No need to convert from PV, as NV
2897 conversion would already have cached IV if it detected
2898 that PV->IV would be better than PV->NV->IV
2899 flags already correct - don't set public IOK. */
2900 DEBUG_c(PerlIO_printf(Perl_debug_log,
7234c960 2901 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
28e5dec8
JH
2902 PTR2UV(sv),
2903 SvNVX(sv),
2904 SvIVX(sv)));
2905 }
2906 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2907 but the cast (NV)IV_MIN rounds to a the value less (more
2908 negative) than IV_MIN which happens to be equal to SvNVX ??
2909 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2910 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2911 (NV)UVX == NVX are both true, but the values differ. :-(
2912 Hopefully for 2s complement IV_MIN is something like
2913 0x8000000000000000 which will be exact. NWC */
d460ef45 2914 }
28e5dec8 2915 else {
607fa7f2 2916 SvUV_set(sv, U_V(SvNVX(sv)));
28e5dec8
JH
2917 if (
2918 (SvNVX(sv) == (NV) SvUVX(sv))
2919#ifndef NV_PRESERVES_UV
2920 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2921 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2922 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2923 /* Don't flag it as "accurately an integer" if the number
2924 came from a (by definition imprecise) NV operation, and
2925 we're outside the range of NV integer precision */
2926#endif
2927 )
2928 SvIOK_on(sv);
2929 SvIsUV_on(sv);
1c846c1f 2930 DEBUG_c(PerlIO_printf(Perl_debug_log,
28e5dec8 2931 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
57def98f 2932 PTR2UV(sv),
28e5dec8
JH
2933 SvUVX(sv),
2934 SvUVX(sv)));
25da4f38 2935 }
ff68c719 2936 }
2937 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20
NC
2938 UV value;
2939 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
25da4f38
IZ
2940
2941 /* We want to avoid a possible problem when we cache a UV which
2942 may be later translated to an NV, and the resulting NV is not
2943 the translation of the initial data.
1c846c1f 2944
25da4f38
IZ
2945 This means that if we cache such a UV, we need to cache the
2946 NV as well. Moreover, we trade speed for space, and do not
2947 cache the NV if not needed.
2948 */
16b7a9a4 2949
c2988b20
NC
2950 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2951 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2952 == IS_NUMBER_IN_UV) {
5e045b90 2953 /* It's definitely an integer, only upgrade to PVIV */
28e5dec8 2954 if (SvTYPE(sv) < SVt_PVIV)
f7bbb42a
JH
2955 sv_upgrade(sv, SVt_PVIV);
2956 (void)SvIOK_on(sv);
c2988b20
NC
2957 } else if (SvTYPE(sv) < SVt_PVNV)
2958 sv_upgrade(sv, SVt_PVNV);
d460ef45 2959
c2988b20
NC
2960 /* If NV preserves UV then we only use the UV value if we know that
2961 we aren't going to call atof() below. If NVs don't preserve UVs
2962 then the value returned may have more precision than atof() will
2963 return, even though it isn't accurate. */
2964 if ((numtype & (IS_NUMBER_IN_UV
2965#ifdef NV_PRESERVES_UV
2966 | IS_NUMBER_NOT_INT
2967#endif
2968 )) == IS_NUMBER_IN_UV) {
2969 /* This won't turn off the public IOK flag if it was set above */
2970 (void)SvIOKp_on(sv);
2971
2972 if (!(numtype & IS_NUMBER_NEG)) {
2973 /* positive */;
2974 if (value <= (UV)IV_MAX) {
45977657 2975 SvIV_set(sv, (IV)value);
28e5dec8
JH
2976 } else {
2977 /* it didn't overflow, and it was positive. */
607fa7f2 2978 SvUV_set(sv, value);
28e5dec8
JH
2979 SvIsUV_on(sv);
2980 }
c2988b20
NC
2981 } else {
2982 /* 2s complement assumption */
2983 if (value <= (UV)IV_MIN) {
45977657 2984 SvIV_set(sv, -(IV)value);
c2988b20
NC
2985 } else {
2986 /* Too negative for an IV. This is a double upgrade, but
d1be9408 2987 I'm assuming it will be rare. */
c2988b20
NC
2988 if (SvTYPE(sv) < SVt_PVNV)
2989 sv_upgrade(sv, SVt_PVNV);
2990 SvNOK_on(sv);
2991 SvIOK_off(sv);
2992 SvIOKp_on(sv);
9d6ce603 2993 SvNV_set(sv, -(NV)value);
45977657 2994 SvIV_set(sv, IV_MIN);
c2988b20
NC
2995 }
2996 }
2997 }
2998
2999 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3000 != IS_NUMBER_IN_UV) {
3001 /* It wasn't an integer, or it overflowed the UV. */
9d6ce603 3002 SvNV_set(sv, Atof(SvPVX(sv)));
28e5dec8 3003
c2988b20 3004 if (! numtype && ckWARN(WARN_NUMERIC))
28e5dec8
JH
3005 not_a_number(sv);
3006
3007#if defined(USE_LONG_DOUBLE)
c2988b20
NC
3008 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
3009 PTR2UV(sv), SvNVX(sv)));
28e5dec8 3010#else
1779d84d 3011 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
c2988b20 3012 PTR2UV(sv), SvNVX(sv)));
28e5dec8
JH
3013#endif
3014
3015#ifdef NV_PRESERVES_UV
c2988b20
NC
3016 (void)SvIOKp_on(sv);
3017 (void)SvNOK_on(sv);
3018 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
45977657 3019 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
3020 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
3021 SvIOK_on(sv);
3022 } else {
3023 /* Integer is imprecise. NOK, IOKp */
3024 }
3025 /* UV will not work better than IV */
3026 } else {
3027 if (SvNVX(sv) > (NV)UV_MAX) {
3028 SvIsUV_on(sv);
3029 /* Integer is inaccurate. NOK, IOKp, is UV */
607fa7f2 3030 SvUV_set(sv, UV_MAX);
c2988b20
NC
3031 SvIsUV_on(sv);
3032 } else {
607fa7f2 3033 SvUV_set(sv, U_V(SvNVX(sv)));
c2988b20
NC
3034 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
3035 NV preservse UV so can do correct comparison. */
3036 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
3037 SvIOK_on(sv);
3038 SvIsUV_on(sv);
3039 } else {
3040 /* Integer is imprecise. NOK, IOKp, is UV */
3041 SvIsUV_on(sv);
3042 }
3043 }
3044 }
28e5dec8 3045#else /* NV_PRESERVES_UV */
c2988b20
NC
3046 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3047 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
3048 /* The UV slot will have been set from value returned by
3049 grok_number above. The NV slot has just been set using
3050 Atof. */
560b0c46 3051 SvNOK_on(sv);
c2988b20
NC
3052 assert (SvIOKp(sv));
3053 } else {
3054 if (((UV)1 << NV_PRESERVES_UV_BITS) >
3055 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
3056 /* Small enough to preserve all bits. */
3057 (void)SvIOKp_on(sv);
3058 SvNOK_on(sv);
45977657 3059 SvIV_set(sv, I_V(SvNVX(sv)));
c2988b20
NC
3060 if ((NV)(SvIVX(sv)) == SvNVX(sv))
3061 SvIOK_on(sv);
3062 /* Assumption: first non-preserved integer is < IV_MAX,
3063 this NV is in the preserved range, therefore: */
3064 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
3065 < (UV)IV_MAX)) {
32fdb065 3066 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
3067 }
3068 } else
3069 sv_2iuv_non_preserve (sv, numtype);
3070 }
28e5dec8 3071#endif /* NV_PRESERVES_UV */
f7bbb42a 3072 }
ff68c719 3073 }
3074 else {
d008e5eb 3075 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 3076 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 3077 report_uninit(sv);
c6ee37c5 3078 }
25da4f38
IZ
3079 if (SvTYPE(sv) < SVt_IV)
3080 /* Typically the caller expects that sv_any is not NULL now. */
3081 sv_upgrade(sv, SVt_IV);
ff68c719 3082 return 0;
3083 }
25da4f38 3084
1d7c1841
GS
3085 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
3086 PTR2UV(sv),SvUVX(sv)));
25da4f38 3087 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719 3088}
3089
645c22ef
DM
3090/*
3091=for apidoc sv_2nv
3092
3093Return the num value of an SV, doing any necessary string or integer
3094conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
3095macros.
3096
3097=cut
3098*/
3099
65202027 3100NV
864dbfa3 3101Perl_sv_2nv(pTHX_ register SV *sv)
79072805
LW
3102{
3103 if (!sv)
3104 return 0.0;
8990e307 3105 if (SvGMAGICAL(sv)) {
463ee0b2
LW
3106 mg_get(sv);
3107 if (SvNOKp(sv))
3108 return SvNVX(sv);
a0d0e21e 3109 if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20
NC
3110 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
3111 !grok_number(SvPVX(sv), SvCUR(sv), NULL))
a0d0e21e 3112 not_a_number(sv);
097ee67d 3113 return Atof(SvPVX(sv));
a0d0e21e 3114 }
25da4f38 3115 if (SvIOKp(sv)) {
1c846c1f 3116 if (SvIsUV(sv))
65202027 3117 return (NV)SvUVX(sv);
25da4f38 3118 else
65202027 3119 return (NV)SvIVX(sv);
25da4f38 3120 }
16d20bd9 3121 if (!SvROK(sv)) {
d008e5eb 3122 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 3123 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 3124 report_uninit(sv);
c6ee37c5 3125 }
16d20bd9
AD
3126 return 0;
3127 }
463ee0b2 3128 }
ed6116ce 3129 if (SvTHINKFIRST(sv)) {
a0d0e21e 3130 if (SvROK(sv)) {
a0d0e21e 3131 SV* tmpstr;
1554e226 3132 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
b4b9a328 3133 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 3134 return SvNV(tmpstr);
56431972 3135 return PTR2NV(SvRV(sv));
a0d0e21e 3136 }
765f542d
NC
3137 if (SvIsCOW(sv)) {
3138 sv_force_normal_flags(sv, 0);
8a818333 3139 }
0336b60e 3140 if (SvREADONLY(sv) && !SvOK(sv)) {
599cee73 3141 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 3142 report_uninit(sv);
ed6116ce
LW
3143 return 0.0;
3144 }
79072805
LW
3145 }
3146 if (SvTYPE(sv) < SVt_NV) {
463ee0b2
LW
3147 if (SvTYPE(sv) == SVt_IV)
3148 sv_upgrade(sv, SVt_PVNV);
3149 else
3150 sv_upgrade(sv, SVt_NV);
906f284f 3151#ifdef USE_LONG_DOUBLE
097ee67d 3152 DEBUG_c({
f93f4e46 3153 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
3154 PerlIO_printf(Perl_debug_log,
3155 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
3156 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
3157 RESTORE_NUMERIC_LOCAL();
3158 });
65202027 3159#else
572bbb43 3160 DEBUG_c({
f93f4e46 3161 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 3162 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
1d7c1841 3163 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
3164 RESTORE_NUMERIC_LOCAL();
3165 });
572bbb43 3166#endif
79072805
LW
3167 }
3168 else if (SvTYPE(sv) < SVt_PVNV)
3169 sv_upgrade(sv, SVt_PVNV);
59d8ce62
NC
3170 if (SvNOKp(sv)) {
3171 return SvNVX(sv);
61604483 3172 }
59d8ce62 3173 if (SvIOKp(sv)) {
9d6ce603 3174 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
28e5dec8
JH
3175#ifdef NV_PRESERVES_UV
3176 SvNOK_on(sv);
3177#else
3178 /* Only set the public NV OK flag if this NV preserves the IV */
3179 /* Check it's not 0xFFFFFFFFFFFFFFFF */
3180 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
3181 : (SvIVX(sv) == I_V(SvNVX(sv))))
3182 SvNOK_on(sv);
3183 else
3184 SvNOKp_on(sv);
3185#endif
93a17b20 3186 }
748a9306 3187 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20
NC
3188 UV value;
3189 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
3190 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
a0d0e21e 3191 not_a_number(sv);
28e5dec8 3192#ifdef NV_PRESERVES_UV
c2988b20
NC
3193 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3194 == IS_NUMBER_IN_UV) {
5e045b90 3195 /* It's definitely an integer */
9d6ce603 3196 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
c2988b20 3197 } else
9d6ce603 3198 SvNV_set(sv, Atof(SvPVX(sv)));
28e5dec8
JH
3199 SvNOK_on(sv);
3200#else
9d6ce603 3201 SvNV_set(sv, Atof(SvPVX(sv)));
28e5dec8
JH
3202 /* Only set the public NV OK flag if this NV preserves the value in
3203 the PV at least as well as an IV/UV would.
3204 Not sure how to do this 100% reliably. */
3205 /* if that shift count is out of range then Configure's test is
3206 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
3207 UV_BITS */
3208 if (((UV)1 << NV_PRESERVES_UV_BITS) >
c2988b20 3209 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
28e5dec8 3210 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
c2988b20
NC
3211 } else if (!(numtype & IS_NUMBER_IN_UV)) {
3212 /* Can't use strtol etc to convert this string, so don't try.
3213 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
3214 SvNOK_on(sv);
3215 } else {
3216 /* value has been set. It may not be precise. */
3217 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
3218 /* 2s complement assumption for (UV)IV_MIN */
3219 SvNOK_on(sv); /* Integer is too negative. */
3220 } else {
3221 SvNOKp_on(sv);
3222 SvIOKp_on(sv);
6fa402ec 3223
c2988b20 3224 if (numtype & IS_NUMBER_NEG) {
45977657 3225 SvIV_set(sv, -(IV)value);
c2988b20 3226 } else if (value <= (UV)IV_MAX) {
45977657 3227 SvIV_set(sv, (IV)value);
c2988b20 3228 } else {
607fa7f2 3229 SvUV_set(sv, value);
c2988b20
NC
3230 SvIsUV_on(sv);
3231 }
3232
3233 if (numtype & IS_NUMBER_NOT_INT) {
3234 /* I believe that even if the original PV had decimals,
3235 they are lost beyond the limit of the FP precision.
3236 However, neither is canonical, so both only get p
3237 flags. NWC, 2000/11/25 */
3238 /* Both already have p flags, so do nothing */
3239 } else {
3240 NV nv = SvNVX(sv);
3241 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
3242 if (SvIVX(sv) == I_V(nv)) {
3243 SvNOK_on(sv);
3244 SvIOK_on(sv);
3245 } else {
3246 SvIOK_on(sv);
3247 /* It had no "." so it must be integer. */
3248 }
3249 } else {
3250 /* between IV_MAX and NV(UV_MAX).
3251 Could be slightly > UV_MAX */
6fa402ec 3252
c2988b20
NC
3253 if (numtype & IS_NUMBER_NOT_INT) {
3254 /* UV and NV both imprecise. */
3255 } else {
3256 UV nv_as_uv = U_V(nv);
3257
3258 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
3259 SvNOK_on(sv);
3260 SvIOK_on(sv);
3261 } else {
3262 SvIOK_on(sv);
3263 }
3264 }
3265 }
3266 }
3267 }
3268 }
28e5dec8 3269#endif /* NV_PRESERVES_UV */
93a17b20 3270 }
79072805 3271 else {
599cee73 3272 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 3273 report_uninit(sv);
25da4f38
IZ
3274 if (SvTYPE(sv) < SVt_NV)
3275 /* Typically the caller expects that sv_any is not NULL now. */
28e5dec8
JH
3276 /* XXX Ilya implies that this is a bug in callers that assume this
3277 and ideally should be fixed. */
25da4f38 3278 sv_upgrade(sv, SVt_NV);
a0d0e21e 3279 return 0.0;
79072805 3280 }
572bbb43 3281#if defined(USE_LONG_DOUBLE)
097ee67d 3282 DEBUG_c({
f93f4e46 3283 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
3284 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
3285 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
3286 RESTORE_NUMERIC_LOCAL();
3287 });
65202027 3288#else
572bbb43 3289 DEBUG_c({
f93f4e46 3290 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 3291 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
1d7c1841 3292 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
3293 RESTORE_NUMERIC_LOCAL();
3294 });
572bbb43 3295#endif
463ee0b2 3296 return SvNVX(sv);
79072805
LW
3297}
3298
645c22ef
DM
3299/* asIV(): extract an integer from the string value of an SV.
3300 * Caller must validate PVX */
3301
76e3520e 3302STATIC IV
cea2e8a9 3303S_asIV(pTHX_ SV *sv)
36477c24 3304{
c2988b20
NC
3305 UV value;
3306 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
3307
3308 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3309 == IS_NUMBER_IN_UV) {
645c22ef 3310 /* It's definitely an integer */
c2988b20
NC
3311 if (numtype & IS_NUMBER_NEG) {
3312 if (value < (UV)IV_MIN)
3313 return -(IV)value;
3314 } else {
3315 if (value < (UV)IV_MAX)
3316 return (IV)value;
3317 }
3318 }
d008e5eb 3319 if (!numtype) {
d008e5eb
GS
3320 if (ckWARN(WARN_NUMERIC))
3321 not_a_number(sv);
3322 }
c2988b20 3323 return I_V(Atof(SvPVX(sv)));
36477c24 3324}
3325
645c22ef
DM
3326/* asUV(): extract an unsigned integer from the string value of an SV
3327 * Caller must validate PVX */
3328
76e3520e 3329STATIC UV
cea2e8a9 3330S_asUV(pTHX_ SV *sv)
36477c24 3331{
c2988b20
NC
3332 UV value;
3333 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
36477c24 3334
c2988b20
NC
3335 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
3336 == IS_NUMBER_IN_UV) {
645c22ef 3337 /* It's definitely an integer */
6fa402ec 3338 if (!(numtype & IS_NUMBER_NEG))
c2988b20
NC
3339 return value;
3340 }
d008e5eb 3341 if (!numtype) {
d008e5eb
GS
3342 if (ckWARN(WARN_NUMERIC))
3343 not_a_number(sv);
3344 }
097ee67d 3345 return U_V(Atof(SvPVX(sv)));
36477c24 3346}
3347
645c22ef
DM
3348/*
3349=for apidoc sv_2pv_nolen
3350
3351Like C<sv_2pv()>, but doesn't return the length too. You should usually
3352use the macro wrapper C<SvPV_nolen(sv)> instead.
3353=cut
3354*/
3355
79072805 3356char *
864dbfa3 3357Perl_sv_2pv_nolen(pTHX_ register SV *sv)
1fa8b10d
JD
3358{
3359 STRLEN n_a;
3360 return sv_2pv(sv, &n_a);
3361}
3362
645c22ef
DM
3363/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
3364 * UV as a string towards the end of buf, and return pointers to start and
3365 * end of it.
3366 *
3367 * We assume that buf is at least TYPE_CHARS(UV) long.
3368 */
3369
864dbfa3 3370static char *
25da4f38
IZ
3371uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
3372{
25da4f38
IZ
3373 char *ptr = buf + TYPE_CHARS(UV);
3374 char *ebuf = ptr;
3375 int sign;
25da4f38
IZ
3376
3377 if (is_uv)
3378 sign = 0;
3379 else if (iv >= 0) {
3380 uv = iv;
3381 sign = 0;
3382 } else {
3383 uv = -iv;
3384 sign = 1;
3385 }
3386 do {
eb160463 3387 *--ptr = '0' + (char)(uv % 10);
25da4f38
IZ
3388 } while (uv /= 10);
3389 if (sign)
3390 *--ptr = '-';
3391 *peob = ebuf;
3392 return ptr;
3393}
3394
09540bc3
JH
3395/* sv_2pv() is now a macro using Perl_sv_2pv_flags();
3396 * this function provided for binary compatibility only
3397 */
3398
3399char *
3400Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
3401{
3402 return sv_2pv_flags(sv, lp, SV_GMAGIC);
3403}
3404
645c22ef
DM
3405/*
3406=for apidoc sv_2pv_flags
3407
ff276b08 3408Returns a pointer to the string value of an SV, and sets *lp to its length.
645c22ef
DM
3409If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
3410if necessary.
3411Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
3412usually end up here too.
3413
3414=cut
3415*/
3416
8d6d96c1
HS
3417char *
3418Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
3419{
79072805
LW
3420 register char *s;
3421 int olderrno;
cb50f42d 3422 SV *tsv, *origsv;
25da4f38
IZ
3423 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
3424 char *tmpbuf = tbuf;
79072805 3425
463ee0b2
LW
3426 if (!sv) {
3427 *lp = 0;
73d840c0 3428 return (char *)"";
463ee0b2 3429 }
8990e307 3430 if (SvGMAGICAL(sv)) {
8d6d96c1
HS
3431 if (flags & SV_GMAGIC)
3432 mg_get(sv);
463ee0b2
LW
3433 if (SvPOKp(sv)) {
3434 *lp = SvCUR(sv);
3435 return SvPVX(sv);
3436 }
cf2093f6 3437 if (SvIOKp(sv)) {
1c846c1f 3438 if (SvIsUV(sv))
57def98f 3439 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
cf2093f6 3440 else
57def98f 3441 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
46fc3d4c 3442 tsv = Nullsv;
a0d0e21e 3443 goto tokensave;
463ee0b2
LW
3444 }
3445 if (SvNOKp(sv)) {
2d4389e4 3446 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
46fc3d4c 3447 tsv = Nullsv;
a0d0e21e 3448 goto tokensave;
463ee0b2 3449 }
16d20bd9 3450 if (!SvROK(sv)) {
d008e5eb 3451 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 3452 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
29489e7c 3453 report_uninit(sv);
c6ee37c5 3454 }
16d20bd9 3455 *lp = 0;
73d840c0 3456 return (char *)"";
16d20bd9 3457 }
463ee0b2 3458 }
ed6116ce
LW
3459 if (SvTHINKFIRST(sv)) {
3460 if (SvROK(sv)) {
a0d0e21e 3461 SV* tmpstr;
e1ec3a88 3462 register const char *typestr;
1554e226 3463 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
b4b9a328 3464 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
446eaa42
YST
3465 char *pv = SvPV(tmpstr, *lp);
3466 if (SvUTF8(tmpstr))
3467 SvUTF8_on(sv);
3468 else
3469 SvUTF8_off(sv);
3470 return pv;
3471 }
cb50f42d 3472 origsv = sv;
ed6116ce
LW
3473 sv = (SV*)SvRV(sv);
3474 if (!sv)
e1ec3a88 3475 typestr = "NULLREF";
ed6116ce 3476 else {
f9277f47
IZ
3477 MAGIC *mg;
3478
ed6116ce 3479 switch (SvTYPE(sv)) {
f9277f47
IZ
3480 case SVt_PVMG:
3481 if ( ((SvFLAGS(sv) &
1c846c1f 3482 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
faf82a0b 3483 == (SVs_OBJECT|SVs_SMG))
14befaf4 3484 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
e1ec3a88 3485 const regexp *re = (regexp *)mg->mg_obj;
1bd3ad17 3486
2cd61cdb 3487 if (!mg->mg_ptr) {
e1ec3a88 3488 const char *fptr = "msix";
8782bef2
GB
3489 char reflags[6];
3490 char ch;
3491 int left = 0;
3492 int right = 4;
ff385a1b 3493 char need_newline = 0;
eb160463 3494 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
8782bef2 3495
155aba94 3496 while((ch = *fptr++)) {
8782bef2
GB
3497 if(reganch & 1) {
3498 reflags[left++] = ch;
3499 }
3500 else {
3501 reflags[right--] = ch;
3502 }
3503 reganch >>= 1;
3504 }
3505 if(left != 4) {
3506 reflags[left] = '-';
3507 left = 5;
3508 }
3509
3510 mg->mg_len = re->prelen + 4 + left;
ff385a1b
JF
3511 /*
3512 * If /x was used, we have to worry about a regex
3513 * ending with a comment later being embedded
3514 * within another regex. If so, we don't want this
3515 * regex's "commentization" to leak out to the
3516 * right part of the enclosing regex, we must cap
3517 * it with a newline.
3518 *
3519 * So, if /x was used, we scan backwards from the
3520 * end of the regex. If we find a '#' before we
3521 * find a newline, we need to add a newline
3522 * ourself. If we find a '\n' first (or if we
3523 * don't find '#' or '\n'), we don't need to add
3524 * anything. -jfriedl
3525 */
3526 if (PMf_EXTENDED & re->reganch)
3527 {
e1ec3a88 3528 const char *endptr = re->precomp + re->prelen;
ff385a1b
JF
3529 while (endptr >= re->precomp)
3530 {
e1ec3a88 3531 const char c = *(endptr--);
ff385a1b
JF
3532 if (c == '\n')
3533 break; /* don't need another */
3534 if (c == '#') {
3535 /* we end while in a comment, so we
3536 need a newline */
3537 mg->mg_len++; /* save space for it */
3538 need_newline = 1; /* note to add it */
ab01544f 3539 break;
ff385a1b
JF
3540 }
3541 }
3542 }
3543
8782bef2
GB
3544 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3545 Copy("(?", mg->mg_ptr, 2, char);
3546 Copy(reflags, mg->mg_ptr+2, left, char);
3547 Copy(":", mg->mg_ptr+left+2, 1, char);
3548 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
ff385a1b
JF
3549 if (need_newline)
3550 mg->mg_ptr[mg->mg_len - 2] = '\n';
1bd3ad17
IZ
3551 mg->mg_ptr[mg->mg_len - 1] = ')';
3552 mg->mg_ptr[mg->mg_len] = 0;
3553 }
3280af22 3554 PL_reginterp_cnt += re->program[0].next_off;
cb50f42d
YST
3555
3556 if (re->reganch & ROPT_UTF8)
3557 SvUTF8_on(origsv);
3558 else
3559 SvUTF8_off(origsv);
1bd3ad17
IZ
3560 *lp = mg->mg_len;
3561 return mg->mg_ptr;
f9277f47
IZ
3562 }
3563 /* Fall through */
ed6116ce
LW
3564 case SVt_NULL:
3565 case SVt_IV:
3566 case SVt_NV:
3567 case SVt_RV:
3568 case SVt_PV:
3569 case SVt_PVIV:
3570 case SVt_PVNV:
e1ec3a88
AL
3571 case SVt_PVBM: typestr = SvROK(sv) ? "REF" : "SCALAR"; break;
3572 case SVt_PVLV: typestr = SvROK(sv) ? "REF"
be65207d
DM
3573 /* tied lvalues should appear to be
3574 * scalars for backwards compatitbility */
3575 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
3576 ? "SCALAR" : "LVALUE"; break;
e1ec3a88
AL
3577 case SVt_PVAV: typestr = "ARRAY"; break;
3578 case SVt_PVHV: typestr = "HASH"; break;
3579 case SVt_PVCV: typestr = "CODE"; break;
3580 case SVt_PVGV: typestr = "GLOB"; break;
3581 case SVt_PVFM: typestr = "FORMAT"; break;
3582 case SVt_PVIO: typestr = "IO"; break;
3583 default: typestr = "UNKNOWN"; break;
ed6116ce 3584 }
46fc3d4c 3585 tsv = NEWSV(0,0);
a5cb6b62
NC
3586 if (SvOBJECT(sv)) {
3587 const char *name = HvNAME(SvSTASH(sv));
3588 Perl_sv_setpvf(aTHX_ tsv, "%s=%s(0x%"UVxf")",
e1ec3a88 3589 name ? name : "__ANON__" , typestr, PTR2UV(sv));
a5cb6b62 3590 }
ed6116ce 3591 else
e1ec3a88 3592 Perl_sv_setpvf(aTHX_ tsv, "%s(0x%"UVxf")", typestr, PTR2UV(sv));
a0d0e21e 3593 goto tokensaveref;
463ee0b2 3594 }
e1ec3a88 3595 *lp = strlen(typestr);
73d840c0 3596 return (char *)typestr;
79072805 3597 }
0336b60e 3598 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 3599 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 3600 report_uninit(sv);
ed6116ce 3601 *lp = 0;
73d840c0 3602 return (char *)"";
79072805 3603 }
79072805 3604 }
28e5dec8
JH
3605 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3606 /* I'm assuming that if both IV and NV are equally valid then
3607 converting the IV is going to be more efficient */
e1ec3a88
AL
3608 const U32 isIOK = SvIOK(sv);
3609 const U32 isUIOK = SvIsUV(sv);
28e5dec8
JH
3610 char buf[TYPE_CHARS(UV)];
3611 char *ebuf, *ptr;
3612
3613 if (SvTYPE(sv) < SVt_PVIV)
3614 sv_upgrade(sv, SVt_PVIV);
3615 if (isUIOK)
3616 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3617 else
3618 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
eb160463 3619 SvGROW(sv, (STRLEN)(ebuf - ptr + 1)); /* inlined from sv_setpvn */
28e5dec8
JH
3620 Move(ptr,SvPVX(sv),ebuf - ptr,char);
3621 SvCUR_set(sv, ebuf - ptr);
3622 s = SvEND(sv);
3623 *s = '\0';
3624 if (isIOK)
3625 SvIOK_on(sv);
3626 else
3627 SvIOKp_on(sv);
3628 if (isUIOK)
3629 SvIsUV_on(sv);
3630 }
3631 else if (SvNOKp(sv)) {
79072805
LW
3632 if (SvTYPE(sv) < SVt_PVNV)
3633 sv_upgrade(sv, SVt_PVNV);
1c846c1f 3634 /* The +20 is pure guesswork. Configure test needed. --jhi */
59155cc0 3635 SvGROW(sv, NV_DIG + 20);
463ee0b2 3636 s = SvPVX(sv);
79072805 3637 olderrno = errno; /* some Xenix systems wipe out errno here */
79072805 3638#ifdef apollo
463ee0b2 3639 if (SvNVX(sv) == 0.0)
79072805
LW
3640 (void)strcpy(s,"0");
3641 else
3642#endif /*apollo*/
bbce6d69 3643 {
2d4389e4 3644 Gconvert(SvNVX(sv), NV_DIG, 0, s);
bbce6d69 3645 }
79072805 3646 errno = olderrno;
a0d0e21e
LW
3647#ifdef FIXNEGATIVEZERO
3648 if (*s == '-' && s[1] == '0' && !s[2])
3649 strcpy(s,"0");
3650#endif
79072805
LW
3651 while (*s) s++;
3652#ifdef hcx
3653 if (s[-1] == '.')
46fc3d4c 3654 *--s = '\0';
79072805
LW
3655#endif
3656 }
79072805 3657 else {
0336b60e
IZ
3658 if (ckWARN(WARN_UNINITIALIZED)
3659 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
29489e7c 3660 report_uninit(sv);
a0d0e21e 3661 *lp = 0;
25da4f38
IZ
3662 if (SvTYPE(sv) < SVt_PV)
3663 /* Typically the caller expects that sv_any is not NULL now. */
3664 sv_upgrade(sv, SVt_PV);
73d840c0 3665 return (char *)"";
79072805 3666 }
463ee0b2
LW
3667 *lp = s - SvPVX(sv);
3668 SvCUR_set(sv, *lp);
79072805 3669 SvPOK_on(sv);
1d7c1841
GS
3670 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3671 PTR2UV(sv),SvPVX(sv)));
463ee0b2 3672 return SvPVX(sv);
a0d0e21e
LW
3673
3674 tokensave:
3675 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3676 /* Sneaky stuff here */
3677
3678 tokensaveref:
46fc3d4c 3679 if (!tsv)
96827780 3680 tsv = newSVpv(tmpbuf, 0);
46fc3d4c 3681 sv_2mortal(tsv);
3682 *lp = SvCUR(tsv);
3683 return SvPVX(tsv);
a0d0e21e
LW
3684 }
3685 else {
27da23d5 3686 dVAR;
a0d0e21e 3687 STRLEN len;
73d840c0 3688 const char *t;
46fc3d4c 3689
3690 if (tsv) {
3691 sv_2mortal(tsv);
3692 t = SvPVX(tsv);
3693 len = SvCUR(tsv);
3694 }
3695 else {
96827780
MB
3696 t = tmpbuf;
3697 len = strlen(tmpbuf);
46fc3d4c 3698 }
a0d0e21e 3699#ifdef FIXNEGATIVEZERO
46fc3d4c 3700 if (len == 2 && t[0] == '-' && t[1] == '0') {
3701 t = "0";
3702 len = 1;
3703 }
a0d0e21e
LW
3704#endif
3705 (void)SvUPGRADE(sv, SVt_PV);
46fc3d4c 3706 *lp = len;
a0d0e21e
LW
3707 s = SvGROW(sv, len + 1);
3708 SvCUR_set(sv, len);
6bf554b4 3709 SvPOKp_on(sv);
e90e2364 3710 return strcpy(s, t);
a0d0e21e 3711 }
463ee0b2
LW
3712}
3713
645c22ef 3714/*
6050d10e
JP
3715=for apidoc sv_copypv
3716
3717Copies a stringified representation of the source SV into the
3718destination SV. Automatically performs any necessary mg_get and
54f0641b 3719coercion of numeric values into strings. Guaranteed to preserve
6050d10e 3720UTF-8 flag even from overloaded objects. Similar in nature to
54f0641b
NIS
3721sv_2pv[_flags] but operates directly on an SV instead of just the
3722string. Mostly uses sv_2pv_flags to do its work, except when that
6050d10e
JP
3723would lose the UTF-8'ness of the PV.
3724
3725=cut
3726*/
3727
3728void
3729Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3730{
446eaa42
YST
3731 STRLEN len;
3732 char *s;
3733 s = SvPV(ssv,len);
cb50f42d 3734 sv_setpvn(dsv,s,len);
446eaa42 3735 if (SvUTF8(ssv))
cb50f42d 3736 SvUTF8_on(dsv);
446eaa42 3737 else
cb50f42d 3738 SvUTF8_off(dsv);
6050d10e
JP
3739}
3740
3741/*
645c22ef
DM
3742=for apidoc sv_2pvbyte_nolen
3743
3744Return a pointer to the byte-encoded representation of the SV.
1e54db1a 3745May cause the SV to be downgraded from UTF-8 as a side-effect.
645c22ef
DM
3746
3747Usually accessed via the C<SvPVbyte_nolen> macro.
3748
3749=cut
3750*/
3751
7340a771
GS
3752char *
3753Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3754{
560a288e
GS
3755 STRLEN n_a;
3756 return sv_2pvbyte(sv, &n_a);
7340a771
GS
3757}
3758
645c22ef
DM
3759/*
3760=for apidoc sv_2pvbyte
3761
3762Return a pointer to the byte-encoded representation of the SV, and set *lp
1e54db1a 3763to its length. May cause the SV to be downgraded from UTF-8 as a
645c22ef
DM
3764side-effect.
3765
3766Usually accessed via the C<SvPVbyte> macro.
3767
3768=cut
3769*/
3770
7340a771
GS
3771char *
3772Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3773{
0875d2fe
NIS
3774 sv_utf8_downgrade(sv,0);
3775 return SvPV(sv,*lp);
7340a771
GS
3776}
3777
645c22ef
DM
3778/*
3779=for apidoc sv_2pvutf8_nolen
3780
1e54db1a
JH
3781Return a pointer to the UTF-8-encoded representation of the SV.
3782May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3783
3784Usually accessed via the C<SvPVutf8_nolen> macro.
3785
3786=cut
3787*/
3788
7340a771
GS
3789char *
3790Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3791{
560a288e
GS
3792 STRLEN n_a;
3793 return sv_2pvutf8(sv, &n_a);
7340a771
GS
3794}
3795
645c22ef
DM
3796/*
3797=for apidoc sv_2pvutf8
3798
1e54db1a
JH
3799Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3800to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
645c22ef
DM
3801
3802Usually accessed via the C<SvPVutf8> macro.
3803
3804=cut
3805*/
3806
7340a771
GS
3807char *
3808Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3809{
560a288e 3810 sv_utf8_upgrade(sv);
7d59b7e4 3811 return SvPV(sv,*lp);
7340a771 3812}
1c846c1f 3813
645c22ef
DM
3814/*
3815=for apidoc sv_2bool
3816
3817This function is only called on magical items, and is only used by
8cf8f3d1 3818sv_true() or its macro equivalent.
645c22ef
DM
3819
3820=cut
3821*/
3822
463ee0b2 3823bool
864dbfa3 3824Perl_sv_2bool(pTHX_ register SV *sv)
463ee0b2 3825{
8990e307 3826 if (SvGMAGICAL(sv))
463ee0b2
LW
3827 mg_get(sv);
3828
a0d0e21e
LW
3829 if (!SvOK(sv))
3830 return 0;
3831 if (SvROK(sv)) {
a0d0e21e 3832 SV* tmpsv;
1554e226 3833 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
9e3013b1 3834 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
8a31060d 3835 return (bool)SvTRUE(tmpsv);
a0d0e21e
LW
3836 return SvRV(sv) != 0;
3837 }
463ee0b2 3838 if (SvPOKp(sv)) {
11343788
MB
3839 register XPV* Xpvtmp;
3840 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
3841 (*Xpvtmp->xpv_pv > '0' ||
3842 Xpvtmp->xpv_cur > 1 ||
3843 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
463ee0b2
LW
3844 return 1;
3845 else
3846 return 0;
3847 }
3848 else {
3849 if (SvIOKp(sv))
3850 return SvIVX(sv) != 0;
3851 else {
3852 if (SvNOKp(sv))
3853 return SvNVX(sv) != 0.0;
3854 else
3855 return FALSE;
3856 }
3857 }
79072805
LW
3858}
3859
09540bc3
JH
3860/* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3861 * this function provided for binary compatibility only
3862 */
3863
3864
3865STRLEN
3866Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3867{
3868 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3869}
3870
c461cf8f
JH
3871/*
3872=for apidoc sv_utf8_upgrade
3873
78ea37eb 3874Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3875Forces the SV to string form if it is not already.
4411f3b6
NIS
3876Always sets the SvUTF8 flag to avoid future validity checks even
3877if all the bytes have hibit clear.
c461cf8f 3878
13a6c0e0
JH
3879This is not as a general purpose byte encoding to Unicode interface:
3880use the Encode extension for that.
3881
8d6d96c1
HS
3882=for apidoc sv_utf8_upgrade_flags
3883
78ea37eb 3884Converts the PV of an SV to its UTF-8-encoded form.
645c22ef 3885Forces the SV to string form if it is not already.
8d6d96c1
HS
3886Always sets the SvUTF8 flag to avoid future validity checks even
3887if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3888will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3889C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3890
13a6c0e0
JH
3891This is not as a general purpose byte encoding to Unicode interface:
3892use the Encode extension for that.
3893
8d6d96c1
HS
3894=cut
3895*/
3896
3897STRLEN
3898Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3899{
808c356f
RGS
3900 if (sv == &PL_sv_undef)
3901 return 0;
e0e62c2a
NIS
3902 if (!SvPOK(sv)) {
3903 STRLEN len = 0;
d52b7888
NC
3904 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3905 (void) sv_2pv_flags(sv,&len, flags);
3906 if (SvUTF8(sv))
3907 return len;
3908 } else {
3909 (void) SvPV_force(sv,len);
3910 }
e0e62c2a 3911 }
4411f3b6 3912
f5cee72b 3913 if (SvUTF8(sv)) {
5fec3b1d 3914 return SvCUR(sv);
f5cee72b 3915 }
5fec3b1d 3916
765f542d
NC
3917 if (SvIsCOW(sv)) {
3918 sv_force_normal_flags(sv, 0);
db42d148
NIS
3919 }
3920
88632417 3921 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
799ef3cb 3922 sv_recode_to_utf8(sv, PL_encoding);
9f4817db 3923 else { /* Assume Latin-1/EBCDIC */
c4e7c712
NC
3924 /* This function could be much more efficient if we
3925 * had a FLAG in SVs to signal if there are any hibit
3926 * chars in the PV. Given that there isn't such a flag
3927 * make the loop as fast as possible. */
3928 U8 *s = (U8 *) SvPVX(sv);
3929 U8 *e = (U8 *) SvEND(sv);
3930 U8 *t = s;
3931 int hibit = 0;
3932
3933 while (t < e) {
3934 U8 ch = *t++;
3935 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
3936 break;
3937 }
3938 if (hibit) {
3939 STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */
3940 s = bytes_to_utf8((U8*)s, &len);
3941
3942 SvPV_free(sv); /* No longer using what was there before. */
3943
3944 SvPV_set(sv, (char*)s);
3945 SvCUR_set(sv, len - 1);
3946 SvLEN_set(sv, len); /* No longer know the real size. */
3947 }
3948 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3949 SvUTF8_on(sv);
560a288e 3950 }
4411f3b6 3951 return SvCUR(sv);
560a288e
GS
3952}
3953
c461cf8f
JH
3954/*
3955=for apidoc sv_utf8_downgrade
3956
78ea37eb
TS
3957Attempts to convert the PV of an SV from characters to bytes.
3958If the PV contains a character beyond byte, this conversion will fail;
3959in this case, either returns false or, if C<fail_ok> is not
c461cf8f
JH
3960true, croaks.
3961
13a6c0e0
JH
3962This is not as a general purpose Unicode to byte encoding interface:
3963use the Encode extension for that.
3964
c461cf8f
JH
3965=cut
3966*/
3967
560a288e
GS
3968bool
3969Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3970{
78ea37eb 3971 if (SvPOKp(sv) && SvUTF8(sv)) {
fa301091 3972 if (SvCUR(sv)) {
03cfe0ae 3973 U8 *s;
652088fc 3974 STRLEN len;
fa301091 3975
765f542d
NC
3976 if (SvIsCOW(sv)) {
3977 sv_force_normal_flags(sv, 0);
3978 }
03cfe0ae
NIS
3979 s = (U8 *) SvPV(sv, len);
3980 if (!utf8_to_bytes(s, &len)) {
fa301091
JH
3981 if (fail_ok)
3982 return FALSE;
3983 else {
3984 if (PL_op)
3985 Perl_croak(aTHX_ "Wide character in %s",
53e06cf0 3986 OP_DESC(PL_op));
fa301091
JH
3987 else
3988 Perl_croak(aTHX_ "Wide character");
3989 }
4b3603a4 3990 }
b162af07 3991 SvCUR_set(sv, len);
67e989fb 3992 }
560a288e 3993 }
ffebcc3e 3994 SvUTF8_off(sv);
560a288e
GS
3995 return TRUE;
3996}
3997
c461cf8f
JH
3998/*
3999=for apidoc sv_utf8_encode
4000
78ea37eb
TS
4001Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
4002flag off so that it looks like octets again.
c461cf8f
JH
4003
4004=cut
4005*/
4006
560a288e
GS
4007void
4008Perl_sv_utf8_encode(pTHX_ register SV *sv)
4009{
4411f3b6 4010 (void) sv_utf8_upgrade(sv);
4c94c214
NC
4011 if (SvIsCOW(sv)) {
4012 sv_force_normal_flags(sv, 0);
4013 }
4014 if (SvREADONLY(sv)) {
4015 Perl_croak(aTHX_ PL_no_modify);
4016 }
560a288e
GS
4017 SvUTF8_off(sv);
4018}
4019
4411f3b6
NIS
4020/*
4021=for apidoc sv_utf8_decode
4022
78ea37eb
TS
4023If the PV of the SV is an octet sequence in UTF-8
4024and contains a multiple-byte character, the C<SvUTF8> flag is turned on
4025so that it looks like a character. If the PV contains only single-byte
4026characters, the C<SvUTF8> flag stays being off.
4027Scans PV for validity and returns false if the PV is invalid UTF-8.
4411f3b6
NIS
4028
4029=cut
4030*/
4031
560a288e
GS
4032bool
4033Perl_sv_utf8_decode(pTHX_ register SV *sv)
4034{
78ea37eb 4035 if (SvPOKp(sv)) {
63cd0674
NIS
4036 U8 *c;
4037 U8 *e;
9cbac4c7 4038
645c22ef
DM
4039 /* The octets may have got themselves encoded - get them back as
4040 * bytes
4041 */
4042 if (!sv_utf8_downgrade(sv, TRUE))
560a288e
GS
4043 return FALSE;
4044
4045 /* it is actually just a matter of turning the utf8 flag on, but
4046 * we want to make sure everything inside is valid utf8 first.
4047 */
63cd0674
NIS
4048 c = (U8 *) SvPVX(sv);
4049 if (!is_utf8_string(c, SvCUR(sv)+1))
67e989fb 4050 return FALSE;
63cd0674 4051 e = (U8 *) SvEND(sv);
511c2ff0 4052 while (c < e) {
c4d5f83a
NIS
4053 U8 ch = *c++;
4054 if (!UTF8_IS_INVARIANT(ch)) {
67e989fb
JH
4055 SvUTF8_on(sv);
4056 break;
4057 }
560a288e 4058 }
560a288e
GS
4059 }
4060 return TRUE;
4061}
4062
09540bc3
JH
4063/* sv_setsv() is now a macro using Perl_sv_setsv_flags();
4064 * this function provided for binary compatibility only
4065 */
4066
4067void
4068Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
4069{
4070 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
4071}
4072
954c1994
GS
4073/*
4074=for apidoc sv_setsv
4075
645c22ef
DM
4076Copies the contents of the source SV C<ssv> into the destination SV
4077C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
4078function if the source SV needs to be reused. Does not handle 'set' magic.
4079Loosely speaking, it performs a copy-by-value, obliterating any previous
4080content of the destination.
4081
4082You probably want to use one of the assortment of wrappers, such as
4083C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
4084C<SvSetMagicSV_nosteal>.
4085
8d6d96c1
HS
4086=for apidoc sv_setsv_flags
4087
645c22ef
DM
4088Copies the contents of the source SV C<ssv> into the destination SV
4089C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
4090function if the source SV needs to be reused. Does not handle 'set' magic.
4091Loosely speaking, it performs a copy-by-value, obliterating any previous
4092content of the destination.
4093If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
5fcdf167
NC
4094C<ssv> if appropriate, else not. If the C<flags> parameter has the
4095C<NOSTEAL> bit set then the buffers of temps will not be stolen. <sv_setsv>
4096and C<sv_setsv_nomg> are implemented in terms of this function.
645c22ef
DM
4097
4098You probably want to use one of the assortment of wrappers, such as
4099C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
4100C<SvSetMagicSV_nosteal>.
4101
4102This is the primary function for copying scalars, and most other
4103copy-ish functions and macros use this underneath.
8d6d96c1
HS
4104
4105=cut
4106*/
4107
4108void
4109Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
4110{
8990e307
LW
4111 register U32 sflags;
4112 register int dtype;
4113 register int stype;
463ee0b2 4114
79072805
LW
4115 if (sstr == dstr)
4116 return;
765f542d 4117 SV_CHECK_THINKFIRST_COW_DROP(dstr);
79072805 4118 if (!sstr)
3280af22 4119 sstr = &PL_sv_undef;
8990e307
LW
4120 stype = SvTYPE(sstr);
4121 dtype = SvTYPE(dstr);
79072805 4122
a0d0e21e 4123 SvAMAGIC_off(dstr);
7a5fa8a2 4124 if ( SvVOK(dstr) )
ece467f9
JP
4125 {
4126 /* need to nuke the magic */
4127 mg_free(dstr);
4128 SvRMAGICAL_off(dstr);
4129 }
9e7bc3e8 4130
463ee0b2 4131 /* There's a lot of redundancy below but we're going for speed here */
79072805 4132
8990e307 4133 switch (stype) {
79072805 4134 case SVt_NULL:
aece5585 4135 undef_sstr:
20408e3c
GS
4136 if (dtype != SVt_PVGV) {
4137 (void)SvOK_off(dstr);
4138 return;
4139 }
4140 break;
463ee0b2 4141 case SVt_IV:
aece5585
GA
4142 if (SvIOK(sstr)) {
4143 switch (dtype) {
4144 case SVt_NULL:
8990e307 4145 sv_upgrade(dstr, SVt_IV);
aece5585
GA
4146 break;
4147 case SVt_NV:
8990e307 4148 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
4149 break;
4150 case SVt_RV:
4151 case SVt_PV:
a0d0e21e 4152 sv_upgrade(dstr, SVt_PVIV);
aece5585
GA
4153 break;
4154 }
4155 (void)SvIOK_only(dstr);
45977657 4156 SvIV_set(dstr, SvIVX(sstr));
25da4f38
IZ
4157 if (SvIsUV(sstr))
4158 SvIsUV_on(dstr);
27c9684d
AP
4159 if (SvTAINTED(sstr))
4160 SvTAINT(dstr);
aece5585 4161 return;
8990e307 4162 }
aece5585
GA
4163 goto undef_sstr;
4164
463ee0b2 4165 case SVt_NV:
aece5585
GA
4166 if (SvNOK(sstr)) {
4167 switch (dtype) {
4168 case SVt_NULL:
4169 case SVt_IV:
8990e307 4170 sv_upgrade(dstr, SVt_NV);
aece5585
GA
4171 break;
4172 case SVt_RV:
4173 case SVt_PV:
4174 case SVt_PVIV:
a0d0e21e 4175 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
4176 break;
4177 }
9d6ce603 4178 SvNV_set(dstr, SvNVX(sstr));
aece5585 4179 (void)SvNOK_only(dstr);
27c9684d
AP
4180 if (SvTAINTED(sstr))
4181 SvTAINT(dstr);
aece5585 4182 return;
8990e307 4183 }
aece5585
GA
4184 goto undef_sstr;
4185
ed6116ce 4186 case SVt_RV:
8990e307 4187 if (dtype < SVt_RV)
ed6116ce 4188 sv_upgrade(dstr, SVt_RV);
c07a80fd 4189 else if (dtype == SVt_PVGV &&
23bb1b96 4190 SvROK(sstr) && SvTYPE(SvRV(sstr)) == SVt_PVGV) {
c07a80fd 4191 sstr = SvRV(sstr);
a5f75d66 4192 if (sstr == dstr) {
1d7c1841
GS
4193 if (GvIMPORTED(dstr) != GVf_IMPORTED
4194 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4195 {
a5f75d66 4196 GvIMPORTED_on(dstr);
1d7c1841 4197 }
a5f75d66
AD
4198 GvMULTI_on(dstr);
4199 return;
4200 }
c07a80fd 4201 goto glob_assign;
4202 }
ed6116ce 4203 break;
fc36a67e 4204 case SVt_PVFM:
d89fc664
NC
4205#ifdef PERL_COPY_ON_WRITE
4206 if ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS) {
4207 if (dtype < SVt_PVIV)
4208 sv_upgrade(dstr, SVt_PVIV);
4209 break;
4210 }
4211 /* Fall through */
4212#endif
4213 case SVt_PV:
8990e307 4214 if (dtype < SVt_PV)
463ee0b2 4215 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
4216 break;
4217 case SVt_PVIV:
8990e307 4218 if (dtype < SVt_PVIV)
463ee0b2 4219 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
4220 break;
4221 case SVt_PVNV:
8990e307 4222 if (dtype < SVt_PVNV)
463ee0b2 4223 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 4224 break;
4633a7c4
LW
4225 case SVt_PVAV:
4226 case SVt_PVHV:
4227 case SVt_PVCV:
4633a7c4 4228 case SVt_PVIO:
533c011a 4229 if (PL_op)
cea2e8a9 4230 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
53e06cf0 4231 OP_NAME(PL_op));
4633a7c4 4232 else
cea2e8a9 4233 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
4633a7c4
LW
4234 break;
4235
79072805 4236 case SVt_PVGV:
8990e307 4237 if (dtype <= SVt_PVGV) {
c07a80fd 4238 glob_assign:
a5f75d66 4239 if (dtype != SVt_PVGV) {
a0d0e21e
LW
4240 char *name = GvNAME(sstr);
4241 STRLEN len = GvNAMELEN(sstr);
b76195c2
DM
4242 /* don't upgrade SVt_PVLV: it can hold a glob */
4243 if (dtype != SVt_PVLV)
4244 sv_upgrade(dstr, SVt_PVGV);
14befaf4 4245 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
85aff577 4246 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
a0d0e21e
LW
4247 GvNAME(dstr) = savepvn(name, len);
4248 GvNAMELEN(dstr) = len;
4249 SvFAKE_on(dstr); /* can coerce to non-glob */
4250 }
7bac28a0 4251 /* ahem, death to those who redefine active sort subs */
3280af22
NIS
4252 else if (PL_curstackinfo->si_type == PERLSI_SORT
4253 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
cea2e8a9 4254 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
7bac28a0 4255 GvNAME(dstr));
5bd07a3d 4256
7fb37951
AMS
4257#ifdef GV_UNIQUE_CHECK
4258 if (GvUNIQUE((GV*)dstr)) {
5bd07a3d
DM
4259 Perl_croak(aTHX_ PL_no_modify);
4260 }
4261#endif
4262
a0d0e21e 4263 (void)SvOK_off(dstr);
a5f75d66 4264 GvINTRO_off(dstr); /* one-shot flag */
1edc1566 4265 gp_free((GV*)dstr);
79072805 4266 GvGP(dstr) = gp_ref(GvGP(sstr));
27c9684d
AP
4267 if (SvTAINTED(sstr))
4268 SvTAINT(dstr);
1d7c1841
GS
4269 if (GvIMPORTED(dstr) != GVf_IMPORTED
4270 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4271 {
a5f75d66 4272 GvIMPORTED_on(dstr);
1d7c1841 4273 }
a5f75d66 4274 GvMULTI_on(dstr);
79072805
LW
4275 return;
4276 }
4277 /* FALL THROUGH */
4278
4279 default:
8d6d96c1 4280 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
973f89ab 4281 mg_get(sstr);
eb160463 4282 if ((int)SvTYPE(sstr) != stype) {
973f89ab
CS
4283 stype = SvTYPE(sstr);
4284 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
4285 goto glob_assign;
4286 }
4287 }
ded42b9f 4288 if (stype == SVt_PVLV)
6fc92669 4289 (void)SvUPGRADE(dstr, SVt_PVNV);
ded42b9f 4290 else
eb160463 4291 (void)SvUPGRADE(dstr, (U32)stype);
79072805
LW
4292 }
4293
8990e307
LW
4294 sflags = SvFLAGS(sstr);
4295
4296 if (sflags & SVf_ROK) {
4297 if (dtype >= SVt_PV) {
4298 if (dtype == SVt_PVGV) {
4299 SV *sref = SvREFCNT_inc(SvRV(sstr));
4300 SV *dref = 0;
a5f75d66 4301 int intro = GvINTRO(dstr);
a0d0e21e 4302
7fb37951
AMS
4303#ifdef GV_UNIQUE_CHECK
4304 if (GvUNIQUE((GV*)dstr)) {
5bd07a3d
DM
4305 Perl_croak(aTHX_ PL_no_modify);
4306 }
4307#endif
4308
a0d0e21e 4309 if (intro) {
a5f75d66 4310 GvINTRO_off(dstr); /* one-shot flag */
1d7c1841 4311 GvLINE(dstr) = CopLINE(PL_curcop);
1edc1566 4312 GvEGV(dstr) = (GV*)dstr;
a0d0e21e 4313 }
a5f75d66 4314 GvMULTI_on(dstr);
8990e307
LW
4315 switch (SvTYPE(sref)) {
4316 case SVt_PVAV:
a0d0e21e 4317 if (intro)
890ed176 4318 SAVEGENERICSV(GvAV(dstr));
a0d0e21e
LW
4319 else
4320 dref = (SV*)GvAV(dstr);
8990e307 4321 GvAV(dstr) = (AV*)sref;
39bac7f7 4322 if (!GvIMPORTED_AV(dstr)
1d7c1841
GS
4323 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4324 {
a5f75d66 4325 GvIMPORTED_AV_on(dstr);
1d7c1841 4326 }
8990e307
LW
4327 break;
4328 case SVt_PVHV:
a0d0e21e 4329 if (intro)
890ed176 4330 SAVEGENERICSV(GvHV(dstr));
a0d0e21e
LW
4331 else
4332 dref = (SV*)GvHV(dstr);
8990e307 4333 GvHV(dstr) = (HV*)sref;
39bac7f7 4334 if (!GvIMPORTED_HV(dstr)
1d7c1841
GS
4335 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4336 {
a5f75d66 4337 GvIMPORTED_HV_on(dstr);
1d7c1841 4338 }
8990e307
LW
4339 break;
4340 case SVt_PVCV:
8ebc5c01 4341 if (intro) {
4342 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
4343 SvREFCNT_dec(GvCV(dstr));
4344 GvCV(dstr) = Nullcv;
68dc0745 4345 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3280af22 4346 PL_sub_generation++;
8ebc5c01 4347 }
890ed176 4348 SAVEGENERICSV(GvCV(dstr));
8ebc5c01 4349 }
68dc0745 4350 else
4351 dref = (SV*)GvCV(dstr);
4352 if (GvCV(dstr) != (CV*)sref) {
748a9306 4353 CV* cv = GvCV(dstr);
4633a7c4 4354 if (cv) {
68dc0745 4355 if (!GvCVGEN((GV*)dstr) &&
4356 (CvROOT(cv) || CvXSUB(cv)))
4357 {
7bac28a0 4358 /* ahem, death to those who redefine
4359 * active sort subs */
3280af22
NIS
4360 if (PL_curstackinfo->si_type == PERLSI_SORT &&
4361 PL_sortcop == CvSTART(cv))
1c846c1f 4362 Perl_croak(aTHX_
7bac28a0 4363 "Can't redefine active sort subroutine %s",
4364 GvENAME((GV*)dstr));
beab0874
JT
4365 /* Redefining a sub - warning is mandatory if
4366 it was a const and its value changed. */
4367 if (ckWARN(WARN_REDEFINE)
4368 || (CvCONST(cv)
4369 && (!CvCONST((CV*)sref)
4370 || sv_cmp(cv_const_sv(cv),
4371 cv_const_sv((CV*)sref)))))
4372 {
9014280d 4373 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
beab0874 4374 CvCONST(cv)
910764e6
RGS
4375 ? "Constant subroutine %s::%s redefined"
4376 : "Subroutine %s::%s redefined",
4377 HvNAME(GvSTASH((GV*)dstr)),
beab0874
JT
4378 GvENAME((GV*)dstr));
4379 }
9607fc9c 4380 }
fb24441d
RGS
4381 if (!intro)
4382 cv_ckproto(cv, (GV*)dstr,
4383 SvPOK(sref) ? SvPVX(sref) : Nullch);
4633a7c4 4384 }
a5f75d66 4385 GvCV(dstr) = (CV*)sref;
7a4c00b4 4386 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
a5f75d66 4387 GvASSUMECV_on(dstr);
3280af22 4388 PL_sub_generation++;
a5f75d66 4389 }
39bac7f7 4390 if (!GvIMPORTED_CV(dstr)
1d7c1841
GS
4391 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4392 {
a5f75d66 4393 GvIMPORTED_CV_on(dstr);
1d7c1841 4394 }
8990e307 4395 break;
91bba347
LW
4396 case SVt_PVIO:
4397 if (intro)
890ed176 4398 SAVEGENERICSV(GvIOp(dstr));
91bba347
LW
4399 else
4400 dref = (SV*)GvIOp(dstr);
4401 GvIOp(dstr) = (IO*)sref;
4402 break;
f4d13ee9
JH
4403 case SVt_PVFM:
4404 if (intro)
890ed176 4405 SAVEGENERICSV(GvFORM(dstr));
f4d13ee9
JH
4406 else
4407 dref = (SV*)GvFORM(dstr);
4408 GvFORM(dstr) = (CV*)sref;
4409 break;
8990e307 4410 default:
a0d0e21e 4411 if (intro)
890ed176 4412 SAVEGENERICSV(GvSV(dstr));
a0d0e21e
LW
4413 else
4414 dref = (SV*)GvSV(dstr);
8990e307 4415 GvSV(dstr) = sref;
39bac7f7 4416 if (!GvIMPORTED_SV(dstr)
1d7c1841
GS
4417 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4418 {
a5f75d66 4419 GvIMPORTED_SV_on(dstr);
1d7c1841 4420 }
8990e307
LW
4421 break;
4422 }
4423 if (dref)
4424 SvREFCNT_dec(dref);
27c9684d
AP
4425 if (SvTAINTED(sstr))
4426 SvTAINT(dstr);
8990e307
LW
4427 return;
4428 }
a0d0e21e 4429 if (SvPVX(dstr)) {
8bd4d4c5 4430 SvPV_free(dstr);
b162af07
SP
4431 SvLEN_set(dstr, 0);
4432 SvCUR_set(dstr, 0);
a0d0e21e 4433 }
8990e307 4434 }
a0d0e21e 4435 (void)SvOK_off(dstr);
b162af07 4436 SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
ed6116ce 4437 SvROK_on(dstr);
8990e307 4438 if (sflags & SVp_NOK) {
3332b3c1
JH
4439 SvNOKp_on(dstr);
4440 /* Only set the public OK flag if the source has public OK. */
4441 if (sflags & SVf_NOK)
4442 SvFLAGS(dstr) |= SVf_NOK;
9d6ce603 4443 SvNV_set(dstr, SvNVX(sstr));
ed6116ce 4444 }
8990e307 4445 if (sflags & SVp_IOK) {
3332b3c1
JH
4446 (void)SvIOKp_on(dstr);
4447 if (sflags & SVf_IOK)
4448 SvFLAGS(dstr) |= SVf_IOK;
2b1c7e3e 4449 if (sflags & SVf_IVisUV)
25da4f38 4450 SvIsUV_on(dstr);
45977657 4451 SvIV_set(dstr, SvIVX(sstr));
ed6116ce 4452 }
a0d0e21e
LW
4453 if (SvAMAGIC(sstr)) {
4454 SvAMAGIC_on(dstr);
4455 }
ed6116ce 4456 }
8990e307 4457 else if (sflags & SVp_POK) {
765f542d 4458 bool isSwipe = 0;
79072805
LW
4459
4460 /*
4461 * Check to see if we can just swipe the string. If so, it's a
4462 * possible small lose on short strings, but a big win on long ones.
463ee0b2
LW
4463 * It might even be a win on short strings if SvPVX(dstr)
4464 * has to be allocated and SvPVX(sstr) has to be freed.
79072805
LW
4465 */
4466
120fac95
NC
4467 /* Whichever path we take through the next code, we want this true,
4468 and doing it now facilitates the COW check. */
4469 (void)SvPOK_only(dstr);
4470
765f542d
NC
4471 if (
4472#ifdef PERL_COPY_ON_WRITE
4473 (sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY)
4474 &&
4475#endif
4476 !(isSwipe =
4477 (sflags & SVs_TEMP) && /* slated for free anyway? */
4478 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
5fcdf167
NC
4479 (!(flags & SV_NOSTEAL)) &&
4480 /* and we're allowed to steal temps */
765f542d
NC
4481 SvREFCNT(sstr) == 1 && /* and no other references to it? */
4482 SvLEN(sstr) && /* and really is a string */
645c22ef 4483 /* and won't be needed again, potentially */
765f542d
NC
4484 !(PL_op && PL_op->op_type == OP_AASSIGN))
4485#ifdef PERL_COPY_ON_WRITE
4486 && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS
120fac95 4487 && (SvFLAGS(dstr) & CAN_COW_MASK) == CAN_COW_FLAGS
765f542d
NC
4488 && SvTYPE(sstr) >= SVt_PVIV)
4489#endif
4490 ) {
4491 /* Failed the swipe test, and it's not a shared hash key either.
4492 Have to copy the string. */
4493 STRLEN len = SvCUR(sstr);
4494 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
4495 Move(SvPVX(sstr),SvPVX(dstr),len,char);
4496 SvCUR_set(dstr, len);
4497 *SvEND(dstr) = '\0';
765f542d
NC
4498 } else {
4499 /* If PERL_COPY_ON_WRITE is not defined, then isSwipe will always
4500 be true in here. */
4501#ifdef PERL_COPY_ON_WRITE
4502 /* Either it's a shared hash key, or it's suitable for
4503 copy-on-write or we can swipe the string. */
46187eeb 4504 if (DEBUG_C_TEST) {
ed252734 4505 PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
e419cbc5
NC
4506 sv_dump(sstr);
4507 sv_dump(dstr);
46187eeb 4508 }
765f542d
NC
4509 if (!isSwipe) {
4510 /* I believe I should acquire a global SV mutex if
4511 it's a COW sv (not a shared hash key) to stop
4512 it going un copy-on-write.
4513 If the source SV has gone un copy on write between up there
4514 and down here, then (assert() that) it is of the correct
4515 form to make it copy on write again */
4516 if ((sflags & (SVf_FAKE | SVf_READONLY))
4517 != (SVf_FAKE | SVf_READONLY)) {
4518 SvREADONLY_on(sstr);
4519 SvFAKE_on(sstr);
4520 /* Make the source SV into a loop of 1.
4521 (about to become 2) */
a29f6d03 4522 SV_COW_NEXT_SV_SET(sstr, sstr);
765f542d
NC
4523 }
4524 }
4525#endif
4526 /* Initial code is common. */
adbc6bb1 4527 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
a5f75d66
AD
4528 if (SvOOK(dstr)) {
4529 SvFLAGS(dstr) &= ~SVf_OOK;
4530 Safefree(SvPVX(dstr) - SvIVX(dstr));
4531 }
50483b2c 4532 else if (SvLEN(dstr))
a5f75d66 4533 Safefree(SvPVX(dstr));
79072805 4534 }
765f542d
NC
4535
4536#ifdef PERL_COPY_ON_WRITE
4537 if (!isSwipe) {
4538 /* making another shared SV. */
4539 STRLEN cur = SvCUR(sstr);
4540 STRLEN len = SvLEN(sstr);
d89fc664 4541 assert (SvTYPE(dstr) >= SVt_PVIV);
765f542d
NC
4542 if (len) {
4543 /* SvIsCOW_normal */
4544 /* splice us in between source and next-after-source. */
a29f6d03
NC
4545 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4546 SV_COW_NEXT_SV_SET(sstr, dstr);
765f542d
NC
4547 SvPV_set(dstr, SvPVX(sstr));
4548 } else {
4549 /* SvIsCOW_shared_hash */
4550 UV hash = SvUVX(sstr);
46187eeb
NC
4551 DEBUG_C(PerlIO_printf(Perl_debug_log,
4552 "Copy on write: Sharing hash\n"));
765f542d
NC
4553 SvPV_set(dstr,
4554 sharepvn(SvPVX(sstr),
4555 (sflags & SVf_UTF8?-cur:cur), hash));
607fa7f2 4556 SvUV_set(dstr, hash);
765f542d 4557 }
87a1ef3d
SP
4558 SvLEN_set(dstr, len);
4559 SvCUR_set(dstr, cur);
765f542d
NC
4560 SvREADONLY_on(dstr);
4561 SvFAKE_on(dstr);
4562 /* Relesase a global SV mutex. */
4563 }
4564 else
4565#endif
4566 { /* Passes the swipe test. */
4567 SvPV_set(dstr, SvPVX(sstr));
4568 SvLEN_set(dstr, SvLEN(sstr));
4569 SvCUR_set(dstr, SvCUR(sstr));
4570
4571 SvTEMP_off(dstr);
4572 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
4573 SvPV_set(sstr, Nullch);
4574 SvLEN_set(sstr, 0);
4575 SvCUR_set(sstr, 0);
4576 SvTEMP_off(sstr);
4577 }
4578 }
9aa983d2 4579 if (sflags & SVf_UTF8)
a7cb1f99 4580 SvUTF8_on(dstr);
79072805 4581 /*SUPPRESS 560*/
8990e307 4582 if (sflags & SVp_NOK) {
3332b3c1
JH
4583 SvNOKp_on(dstr);
4584 if (sflags & SVf_NOK)
4585 SvFLAGS(dstr) |= SVf_NOK;
9d6ce603 4586 SvNV_set(dstr, SvNVX(sstr));
79072805 4587 }
8990e307 4588 if (sflags & SVp_IOK) {
3332b3c1
JH
4589 (void)SvIOKp_on(dstr);
4590 if (sflags & SVf_IOK)
4591 SvFLAGS(dstr) |= SVf_IOK;
2b1c7e3e 4592 if (sflags & SVf_IVisUV)
25da4f38 4593 SvIsUV_on(dstr);
45977657 4594 SvIV_set(dstr, SvIVX(sstr));
79072805 4595 }
92f0c265 4596 if (SvVOK(sstr)) {
7a5fa8a2 4597 MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring);
ece467f9
JP
4598 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4599 smg->mg_ptr, smg->mg_len);
439cb1c4 4600 SvRMAGICAL_on(dstr);
7a5fa8a2 4601 }
79072805 4602 }
8990e307 4603 else if (sflags & SVp_IOK) {
3332b3c1
JH
4604 if (sflags & SVf_IOK)
4605 (void)SvIOK_only(dstr);
4606 else {
9cbac4c7
DM
4607 (void)SvOK_off(dstr);
4608 (void)SvIOKp_on(dstr);
3332b3c1
JH
4609 }
4610 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
2b1c7e3e 4611 if (sflags & SVf_IVisUV)
25da4f38 4612 SvIsUV_on(dstr);
45977657 4613 SvIV_set(dstr, SvIVX(sstr));
3332b3c1
JH
4614 if (sflags & SVp_NOK) {
4615 if (sflags & SVf_NOK)
4616 (void)SvNOK_on(dstr);
4617 else
4618 (void)SvNOKp_on(dstr);
9d6ce603 4619 SvNV_set(dstr, SvNVX(sstr));
3332b3c1
JH
4620 }
4621 }
4622 else if (sflags & SVp_NOK) {
4623 if (sflags & SVf_NOK)
4624 (void)SvNOK_only(dstr);
4625 else {
9cbac4c7 4626 (void)SvOK_off(dstr);
3332b3c1
JH
4627 SvNOKp_on(dstr);
4628 }
9d6ce603 4629 SvNV_set(dstr, SvNVX(sstr));
79072805
LW
4630 }
4631 else {
20408e3c 4632 if (dtype == SVt_PVGV) {
e476b1b5 4633 if (ckWARN(WARN_MISC))
9014280d 4634 Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
20408e3c
GS
4635 }
4636 else
4637 (void)SvOK_off(dstr);
a0d0e21e 4638 }
27c9684d
AP
4639 if (SvTAINTED(sstr))
4640 SvTAINT(dstr);
79072805
LW
4641}
4642
954c1994
GS
4643/*
4644=for apidoc sv_setsv_mg
4645
4646Like C<sv_setsv>, but also handles 'set' magic.
4647
4648=cut
4649*/
4650
79072805 4651void
864dbfa3 4652Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
ef50df4b
GS
4653{
4654 sv_setsv(dstr,sstr);
4655 SvSETMAGIC(dstr);
4656}
4657
ed252734
NC
4658#ifdef PERL_COPY_ON_WRITE
4659SV *
4660Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr)
4661{
4662 STRLEN cur = SvCUR(sstr);
4663 STRLEN len = SvLEN(sstr);
4664 register char *new_pv;
4665
4666 if (DEBUG_C_TEST) {
4667 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4668 sstr, dstr);
4669 sv_dump(sstr);
4670 if (dstr)
4671 sv_dump(dstr);
4672 }
4673
4674 if (dstr) {
4675 if (SvTHINKFIRST(dstr))
4676 sv_force_normal_flags(dstr, SV_COW_DROP_PV);
4677 else if (SvPVX(dstr))
4678 Safefree(SvPVX(dstr));
4679 }
4680 else
4681 new_SV(dstr);
b988aa42 4682 (void)SvUPGRADE (dstr, SVt_PVIV);
ed252734
NC
4683
4684 assert (SvPOK(sstr));
4685 assert (SvPOKp(sstr));
4686 assert (!SvIOK(sstr));
4687 assert (!SvIOKp(sstr));
4688 assert (!SvNOK(sstr));
4689 assert (!SvNOKp(sstr));
4690
4691 if (SvIsCOW(sstr)) {
4692
4693 if (SvLEN(sstr) == 0) {
4694 /* source is a COW shared hash key. */
4695 UV hash = SvUVX(sstr);
4696 DEBUG_C(PerlIO_printf(Perl_debug_log,
4697 "Fast copy on write: Sharing hash\n"));
607fa7f2 4698 SvUV_set(dstr, hash);
ed252734
NC
4699 new_pv = sharepvn(SvPVX(sstr), (SvUTF8(sstr)?-cur:cur), hash);
4700 goto common_exit;
4701 }
4702 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4703 } else {
4704 assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS);
b988aa42 4705 (void)SvUPGRADE (sstr, SVt_PVIV);
ed252734
NC
4706 SvREADONLY_on(sstr);
4707 SvFAKE_on(sstr);
4708 DEBUG_C(PerlIO_printf(Perl_debug_log,
4709 "Fast copy on write: Converting sstr to COW\n"));
4710 SV_COW_NEXT_SV_SET(dstr, sstr);
4711 }
4712 SV_COW_NEXT_SV_SET(sstr, dstr);
4713 new_pv = SvPVX(sstr);
4714
4715 common_exit:
4716 SvPV_set(dstr, new_pv);
4717 SvFLAGS(dstr) = (SVt_PVIV|SVf_POK|SVp_POK|SVf_FAKE|SVf_READONLY);
4718 if (SvUTF8(sstr))
4719 SvUTF8_on(dstr);
87a1ef3d
SP
4720 SvLEN_set(dstr, len);
4721 SvCUR_set(dstr, cur);
ed252734
NC
4722 if (DEBUG_C_TEST) {
4723 sv_dump(dstr);
4724 }
4725 return dstr;
4726}
4727#endif
4728
954c1994
GS
4729/*
4730=for apidoc sv_setpvn
4731
4732Copies a string into an SV. The C<len> parameter indicates the number of
9e09f5f2
MHM
4733bytes to be copied. If the C<ptr> argument is NULL the SV will become
4734undefined. Does not handle 'set' magic. See C<sv_setpvn_mg>.
954c1994
GS
4735
4736=cut
4737*/
4738
ef50df4b 4739void
864dbfa3 4740Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
79072805 4741{
c6f8c383 4742 register char *dptr;
22c522df 4743
765f542d 4744 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2 4745 if (!ptr) {
a0d0e21e 4746 (void)SvOK_off(sv);
463ee0b2
LW
4747 return;
4748 }
22c522df
JH
4749 else {
4750 /* len is STRLEN which is unsigned, need to copy to signed */
4751 IV iv = len;
9c5ffd7c
JH
4752 if (iv < 0)
4753 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
22c522df 4754 }
6fc92669 4755 (void)SvUPGRADE(sv, SVt_PV);
c6f8c383 4756
79072805 4757 SvGROW(sv, len + 1);
c6f8c383
GA
4758 dptr = SvPVX(sv);
4759 Move(ptr,dptr,len,char);
4760 dptr[len] = '\0';
79072805 4761 SvCUR_set(sv, len);
1aa99e6b 4762 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4763 SvTAINT(sv);
79072805
LW
4764}
4765
954c1994
GS
4766/*
4767=for apidoc sv_setpvn_mg
4768
4769Like C<sv_setpvn>, but also handles 'set' magic.
4770
4771=cut
4772*/
4773
79072805 4774void
864dbfa3 4775Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
4776{
4777 sv_setpvn(sv,ptr,len);
4778 SvSETMAGIC(sv);
4779}
4780
954c1994
GS
4781/*
4782=for apidoc sv_setpv
4783
4784Copies a string into an SV. The string must be null-terminated. Does not
4785handle 'set' magic. See C<sv_setpv_mg>.
4786
4787=cut
4788*/
4789
ef50df4b 4790void
864dbfa3 4791Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
4792{
4793 register STRLEN len;
4794
765f542d 4795 SV_CHECK_THINKFIRST_COW_DROP(sv);
463ee0b2 4796 if (!ptr) {
a0d0e21e 4797 (void)SvOK_off(sv);
463ee0b2
LW
4798 return;
4799 }
79072805 4800 len = strlen(ptr);
6fc92669 4801 (void)SvUPGRADE(sv, SVt_PV);
c6f8c383 4802
79072805 4803 SvGROW(sv, len + 1);
463ee0b2 4804 Move(ptr,SvPVX(sv),len+1,char);
79072805 4805 SvCUR_set(sv, len);
1aa99e6b 4806 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2
LW
4807 SvTAINT(sv);
4808}
4809
954c1994
GS
4810/*
4811=for apidoc sv_setpv_mg
4812
4813Like C<sv_setpv>, but also handles 'set' magic.
4814
4815=cut
4816*/
4817
463ee0b2 4818void
864dbfa3 4819Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b
GS
4820{
4821 sv_setpv(sv,ptr);
4822 SvSETMAGIC(sv);
4823}
4824
954c1994
GS
4825/*
4826=for apidoc sv_usepvn
4827
4828Tells an SV to use C<ptr> to find its string value. Normally the string is
1c846c1f 4829stored inside the SV but sv_usepvn allows the SV to use an outside string.
954c1994
GS
4830The C<ptr> should point to memory that was allocated by C<malloc>. The
4831string length, C<len>, must be supplied. This function will realloc the
4832memory pointed to by C<ptr>, so that pointer should not be freed or used by
4833the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
4834See C<sv_usepvn_mg>.
4835
4836=cut
4837*/
4838
ef50df4b 4839void
864dbfa3 4840Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
463ee0b2 4841{
765f542d 4842 SV_CHECK_THINKFIRST_COW_DROP(sv);
c6f8c383 4843 (void)SvUPGRADE(sv, SVt_PV);
463ee0b2 4844 if (!ptr) {
a0d0e21e 4845 (void)SvOK_off(sv);
463ee0b2
LW
4846 return;
4847 }
8bd4d4c5
NC
4848 if (SvPVX(sv))
4849 SvPV_free(sv);
463ee0b2 4850 Renew(ptr, len+1, char);
f880fe2f 4851 SvPV_set(sv, ptr);
463ee0b2
LW
4852 SvCUR_set(sv, len);
4853 SvLEN_set(sv, len+1);
4854 *SvEND(sv) = '\0';
1aa99e6b 4855 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4856 SvTAINT(sv);
79072805
LW
4857}
4858
954c1994
GS
4859/*
4860=for apidoc sv_usepvn_mg
4861
4862Like C<sv_usepvn>, but also handles 'set' magic.
4863
4864=cut
4865*/
4866
ef50df4b 4867void
864dbfa3 4868Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
ef50df4b 4869{
51c1089b 4870 sv_usepvn(sv,ptr,len);
ef50df4b
GS
4871 SvSETMAGIC(sv);
4872}
4873
765f542d
NC
4874#ifdef PERL_COPY_ON_WRITE
4875/* Need to do this *after* making the SV normal, as we need the buffer
4876 pointer to remain valid until after we've copied it. If we let go too early,
4877 another thread could invalidate it by unsharing last of the same hash key
4878 (which it can do by means other than releasing copy-on-write Svs)
4879 or by changing the other copy-on-write SVs in the loop. */
4880STATIC void
4881S_sv_release_COW(pTHX_ register SV *sv, char *pvx, STRLEN cur, STRLEN len,
4882 U32 hash, SV *after)
4883{
4884 if (len) { /* this SV was SvIsCOW_normal(sv) */
4885 /* we need to find the SV pointing to us. */
4886 SV *current = SV_COW_NEXT_SV(after);
7a5fa8a2 4887
765f542d
NC
4888 if (current == sv) {
4889 /* The SV we point to points back to us (there were only two of us
4890 in the loop.)
4891 Hence other SV is no longer copy on write either. */
4892 SvFAKE_off(after);
4893 SvREADONLY_off(after);
4894 } else {
4895 /* We need to follow the pointers around the loop. */
4896 SV *next;
4897 while ((next = SV_COW_NEXT_SV(current)) != sv) {
4898 assert (next);
4899 current = next;
4900 /* don't loop forever if the structure is bust, and we have
4901 a pointer into a closed loop. */
4902 assert (current != after);
e419cbc5 4903 assert (SvPVX(current) == pvx);
765f542d
NC
4904 }
4905 /* Make the SV before us point to the SV after us. */
a29f6d03 4906 SV_COW_NEXT_SV_SET(current, after);
765f542d
NC
4907 }
4908 } else {
4909 unsharepvn(pvx, SvUTF8(sv) ? -(I32)cur : cur, hash);
4910 }
4911}
4912
4913int
4914Perl_sv_release_IVX(pTHX_ register SV *sv)
4915{
4916 if (SvIsCOW(sv))
4917 sv_force_normal_flags(sv, 0);
0c34ef67
MHM
4918 SvOOK_off(sv);
4919 return 0;
765f542d
NC
4920}
4921#endif
645c22ef
DM
4922/*
4923=for apidoc sv_force_normal_flags
4924
4925Undo various types of fakery on an SV: if the PV is a shared string, make
4926a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
765f542d
NC
4927an xpvmg; if we're a copy-on-write scalar, this is the on-write time when
4928we do the copy, and is also used locally. If C<SV_COW_DROP_PV> is set
4929then a copy-on-write scalar drops its PV buffer (if any) and becomes
4930SvPOK_off rather than making a copy. (Used where this scalar is about to be
d3050d9d 4931set to some other value.) In addition, the C<flags> parameter gets passed to
765f542d
NC
4932C<sv_unref_flags()> when unrefing. C<sv_force_normal> calls this function
4933with flags set to 0.
645c22ef
DM
4934
4935=cut
4936*/
4937
6fc92669 4938void
840a7b70 4939Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
0f15f207 4940{
765f542d
NC
4941#ifdef PERL_COPY_ON_WRITE
4942 if (SvREADONLY(sv)) {
4943 /* At this point I believe I should acquire a global SV mutex. */
4944 if (SvFAKE(sv)) {
4945 char *pvx = SvPVX(sv);
4946 STRLEN len = SvLEN(sv);
4947 STRLEN cur = SvCUR(sv);
4948 U32 hash = SvUVX(sv);
4949 SV *next = SV_COW_NEXT_SV(sv); /* next COW sv in the loop. */
46187eeb
NC
4950 if (DEBUG_C_TEST) {
4951 PerlIO_printf(Perl_debug_log,
4952 "Copy on write: Force normal %ld\n",
4953 (long) flags);
e419cbc5 4954 sv_dump(sv);
46187eeb 4955 }
765f542d
NC
4956 SvFAKE_off(sv);
4957 SvREADONLY_off(sv);
4958 /* This SV doesn't own the buffer, so need to New() a new one: */
f880fe2f 4959 SvPV_set(sv, (char*)0);
87a1ef3d 4960 SvLEN_set(sv, 0);
765f542d
NC
4961 if (flags & SV_COW_DROP_PV) {
4962 /* OK, so we don't need to copy our buffer. */
4963 SvPOK_off(sv);
4964 } else {
4965 SvGROW(sv, cur + 1);
4966 Move(pvx,SvPVX(sv),cur,char);
87a1ef3d 4967 SvCUR_set(sv, cur);
765f542d
NC
4968 *SvEND(sv) = '\0';
4969 }
e419cbc5 4970 sv_release_COW(sv, pvx, cur, len, hash, next);
46187eeb 4971 if (DEBUG_C_TEST) {
e419cbc5 4972 sv_dump(sv);
46187eeb 4973 }
765f542d 4974 }
923e4eb5 4975 else if (IN_PERL_RUNTIME)
765f542d
NC
4976 Perl_croak(aTHX_ PL_no_modify);
4977 /* At this point I believe that I can drop the global SV mutex. */
4978 }
4979#else
2213622d 4980 if (SvREADONLY(sv)) {
1c846c1f
NIS
4981 if (SvFAKE(sv)) {
4982 char *pvx = SvPVX(sv);
5c98da1c 4983 int is_utf8 = SvUTF8(sv);
1c846c1f
NIS
4984 STRLEN len = SvCUR(sv);
4985 U32 hash = SvUVX(sv);
10bcdfd6
NC
4986 SvFAKE_off(sv);
4987 SvREADONLY_off(sv);
f880fe2f 4988 SvPV_set(sv, (char*)0);
b162af07 4989 SvLEN_set(sv, 0);
1c846c1f
NIS
4990 SvGROW(sv, len + 1);
4991 Move(pvx,SvPVX(sv),len,char);
4992 *SvEND(sv) = '\0';
5c98da1c 4993 unsharepvn(pvx, is_utf8 ? -(I32)len : len, hash);
1c846c1f 4994 }
923e4eb5 4995 else if (IN_PERL_RUNTIME)
cea2e8a9 4996 Perl_croak(aTHX_ PL_no_modify);
0f15f207 4997 }
765f542d 4998#endif
2213622d 4999 if (SvROK(sv))
840a7b70 5000 sv_unref_flags(sv, flags);
6fc92669
GS
5001 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
5002 sv_unglob(sv);
0f15f207 5003}
1c846c1f 5004
645c22ef
DM
5005/*
5006=for apidoc sv_force_normal
5007
5008Undo various types of fakery on an SV: if the PV is a shared string, make
5009a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
5010an xpvmg. See also C<sv_force_normal_flags>.
5011
5012=cut
5013*/
5014
840a7b70
IZ
5015void
5016Perl_sv_force_normal(pTHX_ register SV *sv)
5017{
5018 sv_force_normal_flags(sv, 0);
5019}
5020
954c1994
GS
5021/*
5022=for apidoc sv_chop
5023
1c846c1f 5024Efficient removal of characters from the beginning of the string buffer.
954c1994
GS
5025SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
5026the string buffer. The C<ptr> becomes the first character of the adjusted
645c22ef 5027string. Uses the "OOK hack".
31869a79
AE
5028Beware: after this function returns, C<ptr> and SvPVX(sv) may no longer
5029refer to the same chunk of data.
954c1994
GS
5030
5031=cut
5032*/
5033
79072805 5034void
645c22ef 5035Perl_sv_chop(pTHX_ register SV *sv, register char *ptr)
79072805
LW
5036{
5037 register STRLEN delta;
a0d0e21e 5038 if (!ptr || !SvPOKp(sv))
79072805 5039 return;
31869a79 5040 delta = ptr - SvPVX(sv);
2213622d 5041 SV_CHECK_THINKFIRST(sv);
79072805
LW
5042 if (SvTYPE(sv) < SVt_PVIV)
5043 sv_upgrade(sv,SVt_PVIV);
5044
5045 if (!SvOOK(sv)) {
50483b2c
JD
5046 if (!SvLEN(sv)) { /* make copy of shared string */
5047 char *pvx = SvPVX(sv);
5048 STRLEN len = SvCUR(sv);
5049 SvGROW(sv, len + 1);
5050 Move(pvx,SvPVX(sv),len,char);
5051 *SvEND(sv) = '\0';
5052 }
45977657 5053 SvIV_set(sv, 0);
a4bfb290
AB
5054 /* Same SvOOK_on but SvOOK_on does a SvIOK_off
5055 and we do that anyway inside the SvNIOK_off
5056 */
7a5fa8a2 5057 SvFLAGS(sv) |= SVf_OOK;
79072805 5058 }
a4bfb290 5059 SvNIOK_off(sv);
b162af07
SP
5060 SvLEN_set(sv, SvLEN(sv) - delta);
5061 SvCUR_set(sv, SvCUR(sv) - delta);
f880fe2f 5062 SvPV_set(sv, SvPVX(sv) + delta);
45977657 5063 SvIV_set(sv, SvIVX(sv) + delta);
79072805
LW
5064}
5065
09540bc3
JH
5066/* sv_catpvn() is now a macro using Perl_sv_catpvn_flags();
5067 * this function provided for binary compatibility only
5068 */
5069
5070void
5071Perl_sv_catpvn(pTHX_ SV *dsv, const char* sstr, STRLEN slen)
5072{
5073 sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC);
5074}
5075
954c1994
GS
5076/*
5077=for apidoc sv_catpvn
5078
5079Concatenates the string onto the end of the string which is in the SV. The
1e54db1a
JH
5080C<len> indicates number of bytes to copy. If the SV has the UTF-8
5081status set, then the bytes appended should be valid UTF-8.
d5ce4a7c 5082Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>.
954c1994 5083
8d6d96c1
HS
5084=for apidoc sv_catpvn_flags
5085
5086Concatenates the string onto the end of the string which is in the SV. The
1e54db1a
JH
5087C<len> indicates number of bytes to copy. If the SV has the UTF-8
5088status set, then the bytes appended should be valid UTF-8.
8d6d96c1
HS
5089If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if
5090appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented
5091in terms of this function.
5092
5093=cut
5094*/
5095
5096void
5097Perl_sv_catpvn_flags(pTHX_ register SV *dsv, register const char *sstr, register STRLEN slen, I32 flags)
5098{
5099 STRLEN dlen;
5100 char *dstr;
5101
5102 dstr = SvPV_force_flags(dsv, dlen, flags);
5103 SvGROW(dsv, dlen + slen + 1);
5104 if (sstr == dstr)
5105 sstr = SvPVX(dsv);
5106 Move(sstr, SvPVX(dsv) + dlen, slen, char);
b162af07 5107 SvCUR_set(dsv, SvCUR(dsv) + slen);
8d6d96c1
HS
5108 *SvEND(dsv) = '\0';
5109 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
5110 SvTAINT(dsv);
79072805
LW
5111}
5112
954c1994
GS
5113/*
5114=for apidoc sv_catpvn_mg
5115
5116Like C<sv_catpvn>, but also handles 'set' magic.
5117
5118=cut
5119*/
5120
79072805 5121void
864dbfa3 5122Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
5123{
5124 sv_catpvn(sv,ptr,len);
5125 SvSETMAGIC(sv);
5126}
5127
09540bc3
JH
5128/* sv_catsv() is now a macro using Perl_sv_catsv_flags();
5129 * this function provided for binary compatibility only
5130 */
5131
5132void
5133Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
5134{
5135 sv_catsv_flags(dstr, sstr, SV_GMAGIC);
5136}
5137
954c1994
GS
5138/*
5139=for apidoc sv_catsv
5140
13e8c8e3
JH
5141Concatenates the string from SV C<ssv> onto the end of the string in
5142SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
5143not 'set' magic. See C<sv_catsv_mg>.
954c1994 5144
8d6d96c1
HS
5145=for apidoc sv_catsv_flags
5146
5147Concatenates the string from SV C<ssv> onto the end of the string in
5148SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC>
5149bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv>
5150and C<sv_catsv_nomg> are implemented in terms of this function.
5151
5152=cut */
5153
ef50df4b 5154void
8d6d96c1 5155Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags)
79072805 5156{
13e8c8e3
JH
5157 char *spv;
5158 STRLEN slen;
46199a12 5159 if (!ssv)
79072805 5160 return;
46199a12 5161 if ((spv = SvPV(ssv, slen))) {
4fd84b44
AD
5162 /* sutf8 and dutf8 were type bool, but under USE_ITHREADS,
5163 gcc version 2.95.2 20000220 (Debian GNU/Linux) for
8cf8f3d1
NIS
5164 Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously
5165 get dutf8 = 0x20000000, (i.e. SVf_UTF8) even though
4fd84b44
AD
5166 dsv->sv_flags doesn't have that bit set.
5167 Andy Dougherty 12 Oct 2001
5168 */
5169 I32 sutf8 = DO_UTF8(ssv);
5170 I32 dutf8;
13e8c8e3 5171
8d6d96c1
HS
5172 if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC))
5173 mg_get(dsv);
5174 dutf8 = DO_UTF8(dsv);
5175
5176 if (dutf8 != sutf8) {
13e8c8e3 5177 if (dutf8) {
46199a12 5178 /* Not modifying source SV, so taking a temporary copy. */
8d6d96c1 5179 SV* csv = sv_2mortal(newSVpvn(spv, slen));
13e8c8e3 5180
46199a12 5181 sv_utf8_upgrade(csv);
8d6d96c1 5182 spv = SvPV(csv, slen);
13e8c8e3 5183 }
8d6d96c1
HS
5184 else
5185 sv_utf8_upgrade_nomg(dsv);
e84ff256 5186 }
8d6d96c1 5187 sv_catpvn_nomg(dsv, spv, slen);
560a288e 5188 }
79072805
LW
5189}
5190
954c1994
GS
5191/*
5192=for apidoc sv_catsv_mg
5193
5194Like C<sv_catsv>, but also handles 'set' magic.
5195
5196=cut
5197*/
5198
79072805 5199void
46199a12 5200Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv)
ef50df4b 5201{
46199a12
JH
5202 sv_catsv(dsv,ssv);
5203 SvSETMAGIC(dsv);
ef50df4b
GS
5204}
5205
954c1994
GS
5206/*
5207=for apidoc sv_catpv
5208
5209Concatenates the string onto the end of the string which is in the SV.
1e54db1a
JH
5210If the SV has the UTF-8 status set, then the bytes appended should be
5211valid UTF-8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
954c1994 5212
d5ce4a7c 5213=cut */
954c1994 5214
ef50df4b 5215void
0c981600 5216Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
5217{
5218 register STRLEN len;
463ee0b2 5219 STRLEN tlen;
748a9306 5220 char *junk;
79072805 5221
0c981600 5222 if (!ptr)
79072805 5223 return;
748a9306 5224 junk = SvPV_force(sv, tlen);
0c981600 5225 len = strlen(ptr);
463ee0b2 5226 SvGROW(sv, tlen + len + 1);
0c981600
JH
5227 if (ptr == junk)
5228 ptr = SvPVX(sv);
5229 Move(ptr,SvPVX(sv)+tlen,len+1,char);
b162af07 5230 SvCUR_set(sv, SvCUR(sv) + len);
d41ff1b8 5231 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 5232 SvTAINT(sv);
79072805
LW
5233}
5234
954c1994
GS
5235/*
5236=for apidoc sv_catpv_mg
5237
5238Like C<sv_catpv>, but also handles 'set' magic.
5239
5240=cut
5241*/
5242
ef50df4b 5243void
0c981600 5244Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b 5245{
0c981600 5246 sv_catpv(sv,ptr);
ef50df4b
GS
5247 SvSETMAGIC(sv);
5248}
5249
645c22ef
DM
5250/*
5251=for apidoc newSV
5252
5253Create a new null SV, or if len > 0, create a new empty SVt_PV type SV
5254with an initial PV allocation of len+1. Normally accessed via the C<NEWSV>
5255macro.
5256
5257=cut
5258*/
5259
79072805 5260SV *
864dbfa3 5261Perl_newSV(pTHX_ STRLEN len)
79072805
LW
5262{
5263 register SV *sv;
1c846c1f 5264
4561caa4 5265 new_SV(sv);
79072805
LW
5266 if (len) {
5267 sv_upgrade(sv, SVt_PV);
5268 SvGROW(sv, len + 1);
5269 }
5270 return sv;
5271}
954c1994 5272/*
92110913 5273=for apidoc sv_magicext
954c1994 5274
68795e93 5275Adds magic to an SV, upgrading it if necessary. Applies the
2d8d5d5a 5276supplied vtable and returns a pointer to the magic added.
92110913 5277
2d8d5d5a
SH
5278Note that C<sv_magicext> will allow things that C<sv_magic> will not.
5279In particular, you can add magic to SvREADONLY SVs, and add more than
5280one instance of the same 'how'.
645c22ef 5281
2d8d5d5a
SH
5282If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
5283stored, if C<namlen> is zero then C<name> is stored as-is and - as another
5284special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
5285to contain an C<SV*> and is stored as-is with its REFCNT incremented.
92110913 5286
2d8d5d5a 5287(This is now used as a subroutine by C<sv_magic>.)
954c1994
GS
5288
5289=cut
5290*/
92110913 5291MAGIC *
e1ec3a88 5292Perl_sv_magicext(pTHX_ SV* sv, SV* obj, int how, const MGVTBL *vtable,
92110913 5293 const char* name, I32 namlen)
79072805
LW
5294{
5295 MAGIC* mg;
68795e93 5296
92110913
NIS
5297 if (SvTYPE(sv) < SVt_PVMG) {
5298 (void)SvUPGRADE(sv, SVt_PVMG);
463ee0b2 5299 }
79072805
LW
5300 Newz(702,mg, 1, MAGIC);
5301 mg->mg_moremagic = SvMAGIC(sv);
b162af07 5302 SvMAGIC_set(sv, mg);
75f9d97a 5303
05f95b08
SB
5304 /* Sometimes a magic contains a reference loop, where the sv and
5305 object refer to each other. To prevent a reference loop that
5306 would prevent such objects being freed, we look for such loops
5307 and if we find one we avoid incrementing the object refcount.
87f0b213
JH
5308
5309 Note we cannot do this to avoid self-tie loops as intervening RV must
b5ccf5f2 5310 have its REFCNT incremented to keep it in existence.
87f0b213
JH
5311
5312 */
14befaf4
DM
5313 if (!obj || obj == sv ||
5314 how == PERL_MAGIC_arylen ||
5315 how == PERL_MAGIC_qr ||
75f9d97a
JH
5316 (SvTYPE(obj) == SVt_PVGV &&
5317 (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv ||
5318 GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv ||
2628be26 5319 GvFORM(obj) == (CV*)sv)))
75f9d97a 5320 {
8990e307 5321 mg->mg_obj = obj;
75f9d97a 5322 }
85e6fe83 5323 else {
8990e307 5324 mg->mg_obj = SvREFCNT_inc(obj);
85e6fe83
LW
5325 mg->mg_flags |= MGf_REFCOUNTED;
5326 }
b5ccf5f2
YST
5327
5328 /* Normal self-ties simply pass a null object, and instead of
5329 using mg_obj directly, use the SvTIED_obj macro to produce a
5330 new RV as needed. For glob "self-ties", we are tieing the PVIO
5331 with an RV obj pointing to the glob containing the PVIO. In
5332 this case, to avoid a reference loop, we need to weaken the
5333 reference.
5334 */
5335
5336 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
5337 obj && SvROK(obj) && GvIO(SvRV(obj)) == (IO*)sv)
5338 {
5339 sv_rvweaken(obj);
5340 }
5341
79072805 5342 mg->mg_type = how;
565764a8 5343 mg->mg_len = namlen;
9cbac4c7 5344 if (name) {
92110913 5345 if (namlen > 0)
1edc1566 5346 mg->mg_ptr = savepvn(name, namlen);
c6ee37c5 5347 else if (namlen == HEf_SVKEY)
1edc1566 5348 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
68795e93 5349 else
92110913 5350 mg->mg_ptr = (char *) name;
9cbac4c7 5351 }
92110913 5352 mg->mg_virtual = vtable;
68795e93 5353
92110913
NIS
5354 mg_magical(sv);
5355 if (SvGMAGICAL(sv))
5356 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
5357 return mg;
5358}
5359
5360/*
5361=for apidoc sv_magic
1c846c1f 5362
92110913
NIS
5363Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary,
5364then adds a new magic item of type C<how> to the head of the magic list.
5365
2d8d5d5a
SH
5366See C<sv_magicext> (which C<sv_magic> now calls) for a description of the
5367handling of the C<name> and C<namlen> arguments.
5368
4509d3fb
SB
5369You need to use C<sv_magicext> to add magic to SvREADONLY SVs and also
5370to add more than one instance of the same 'how'.
5371
92110913
NIS
5372=cut
5373*/
5374
5375void
5376Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
68795e93 5377{
e1ec3a88 5378 const MGVTBL *vtable = 0;
92110913 5379 MAGIC* mg;
92110913 5380
765f542d
NC
5381#ifdef PERL_COPY_ON_WRITE
5382 if (SvIsCOW(sv))
5383 sv_force_normal_flags(sv, 0);
5384#endif
92110913 5385 if (SvREADONLY(sv)) {
923e4eb5 5386 if (IN_PERL_RUNTIME
92110913
NIS
5387 && how != PERL_MAGIC_regex_global
5388 && how != PERL_MAGIC_bm
5389 && how != PERL_MAGIC_fm
5390 && how != PERL_MAGIC_sv
e6469971 5391 && how != PERL_MAGIC_backref
92110913
NIS
5392 )
5393 {
5394 Perl_croak(aTHX_ PL_no_modify);
5395 }
5396 }
5397 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
5398 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
68795e93
NIS
5399 /* sv_magic() refuses to add a magic of the same 'how' as an
5400 existing one
92110913
NIS
5401 */
5402 if (how == PERL_MAGIC_taint)
5403 mg->mg_len |= 1;
5404 return;
5405 }
5406 }
68795e93 5407
79072805 5408 switch (how) {
14befaf4 5409 case PERL_MAGIC_sv:
92110913 5410 vtable = &PL_vtbl_sv;
79072805 5411 break;
14befaf4 5412 case PERL_MAGIC_overload:
92110913 5413 vtable = &PL_vtbl_amagic;
a0d0e21e 5414 break;
14befaf4 5415 case PERL_MAGIC_overload_elem:
92110913 5416 vtable = &PL_vtbl_amagicelem;
a0d0e21e 5417 break;
14befaf4 5418 case PERL_MAGIC_overload_table:
92110913 5419 vtable = &PL_vtbl_ovrld;
a0d0e21e 5420 break;
14befaf4 5421 case PERL_MAGIC_bm:
92110913 5422 vtable = &PL_vtbl_bm;
79072805 5423 break;
14befaf4 5424 case PERL_MAGIC_regdata:
92110913 5425 vtable = &PL_vtbl_regdata;
6cef1e77 5426 break;
14befaf4 5427 case PERL_MAGIC_regdatum:
92110913 5428 vtable = &PL_vtbl_regdatum;
6cef1e77 5429 break;
14befaf4 5430 case PERL_MAGIC_env:
92110913 5431 vtable = &PL_vtbl_env;
79072805 5432 break;
14befaf4 5433 case PERL_MAGIC_fm:
92110913 5434 vtable = &PL_vtbl_fm;
55497cff 5435 break;
14befaf4 5436 case PERL_MAGIC_envelem:
92110913 5437 vtable = &PL_vtbl_envelem;
79072805 5438 break;
14befaf4 5439 case PERL_MAGIC_regex_global:
92110913 5440 vtable = &PL_vtbl_mglob;
93a17b20 5441 break;
14befaf4 5442 case PERL_MAGIC_isa:
92110913 5443 vtable = &PL_vtbl_isa;
463ee0b2 5444 break;
14befaf4 5445 case PERL_MAGIC_isaelem:
92110913 5446 vtable = &PL_vtbl_isaelem;
463ee0b2 5447 break;
14befaf4 5448 case PERL_MAGIC_nkeys:
92110913 5449 vtable = &PL_vtbl_nkeys;
16660edb 5450 break;
14befaf4 5451 case PERL_MAGIC_dbfile:
92110913 5452 vtable = 0;
93a17b20 5453 break;
14befaf4 5454 case PERL_MAGIC_dbline:
92110913 5455 vtable = &PL_vtbl_dbline;
79072805 5456 break;
36477c24 5457#ifdef USE_LOCALE_COLLATE
14befaf4 5458 case PERL_MAGIC_collxfrm:
92110913 5459 vtable = &PL_vtbl_collxfrm;
bbce6d69 5460 break;
36477c24 5461#endif /* USE_LOCALE_COLLATE */
14befaf4 5462 case PERL_MAGIC_tied:
92110913 5463 vtable = &PL_vtbl_pack;
463ee0b2 5464 break;
14befaf4
DM
5465 case PERL_MAGIC_tiedelem:
5466 case PERL_MAGIC_tiedscalar:
92110913 5467 vtable = &PL_vtbl_packelem;
463ee0b2 5468 break;
14befaf4 5469 case PERL_MAGIC_qr:
92110913 5470 vtable = &PL_vtbl_regexp;
c277df42 5471 break;
14befaf4 5472 case PERL_MAGIC_sig:
92110913 5473 vtable = &PL_vtbl_sig;
79072805 5474 break;
14befaf4 5475 case PERL_MAGIC_sigelem:
92110913 5476 vtable = &PL_vtbl_sigelem;
79072805 5477 break;
14befaf4 5478 case PERL_MAGIC_taint:
92110913 5479 vtable = &PL_vtbl_taint;
463ee0b2 5480 break;
14befaf4 5481 case PERL_MAGIC_uvar:
92110913 5482 vtable = &PL_vtbl_uvar;
79072805 5483 break;
14befaf4 5484 case PERL_MAGIC_vec:
92110913 5485 vtable = &PL_vtbl_vec;
79072805 5486 break;
ece467f9
JP
5487 case PERL_MAGIC_vstring:
5488 vtable = 0;
5489 break;
7e8c5dac
HS
5490 case PERL_MAGIC_utf8:
5491 vtable = &PL_vtbl_utf8;
5492 break;
14befaf4 5493 case PERL_MAGIC_substr:
92110913 5494 vtable = &PL_vtbl_substr;
79072805 5495 break;
14befaf4 5496 case PERL_MAGIC_defelem:
92110913 5497 vtable = &PL_vtbl_defelem;
5f05dabc 5498 break;
14befaf4 5499 case PERL_MAGIC_glob:
92110913 5500 vtable = &PL_vtbl_glob;
79072805 5501 break;
14befaf4 5502 case PERL_MAGIC_arylen:
92110913 5503 vtable = &PL_vtbl_arylen;
79072805 5504 break;
14befaf4 5505 case PERL_MAGIC_pos:
92110913 5506 vtable = &PL_vtbl_pos;
a0d0e21e 5507 break;
14befaf4 5508 case PERL_MAGIC_backref:
92110913 5509 vtable = &PL_vtbl_backref;
810b8aa5 5510 break;
14befaf4
DM
5511 case PERL_MAGIC_ext:
5512 /* Reserved for use by extensions not perl internals. */
4633a7c4
LW
5513 /* Useful for attaching extension internal data to perl vars. */
5514 /* Note that multiple extensions may clash if magical scalars */
5515 /* etc holding private data from one are passed to another. */
a0d0e21e 5516 break;
79072805 5517 default:
14befaf4 5518 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
463ee0b2 5519 }
68795e93 5520
92110913 5521 /* Rest of work is done else where */
27da23d5 5522 mg = sv_magicext(sv,obj,how,(MGVTBL*)vtable,name,namlen);
68795e93 5523
92110913
NIS
5524 switch (how) {
5525 case PERL_MAGIC_taint:
5526 mg->mg_len = 1;
5527 break;
5528 case PERL_MAGIC_ext:
5529 case PERL_MAGIC_dbfile:
5530 SvRMAGICAL_on(sv);
5531 break;
5532 }
463ee0b2
LW
5533}
5534
c461cf8f
JH
5535/*
5536=for apidoc sv_unmagic
5537
645c22ef 5538Removes all magic of type C<type> from an SV.
c461cf8f
JH
5539
5540=cut
5541*/
5542
463ee0b2 5543int
864dbfa3 5544Perl_sv_unmagic(pTHX_ SV *sv, int type)
463ee0b2
LW
5545{
5546 MAGIC* mg;
5547 MAGIC** mgp;
91bba347 5548 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
463ee0b2
LW
5549 return 0;
5550 mgp = &SvMAGIC(sv);
5551 for (mg = *mgp; mg; mg = *mgp) {
5552 if (mg->mg_type == type) {
e1ec3a88 5553 const MGVTBL* const vtbl = mg->mg_virtual;
463ee0b2 5554 *mgp = mg->mg_moremagic;
1d7c1841 5555 if (vtbl && vtbl->svt_free)
fc0dc3b3 5556 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
14befaf4 5557 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
92110913 5558 if (mg->mg_len > 0)
1edc1566 5559 Safefree(mg->mg_ptr);
565764a8 5560 else if (mg->mg_len == HEf_SVKEY)
1edc1566 5561 SvREFCNT_dec((SV*)mg->mg_ptr);
7e8c5dac
HS
5562 else if (mg->mg_type == PERL_MAGIC_utf8 && mg->mg_ptr)
5563 Safefree(mg->mg_ptr);
9cbac4c7 5564 }
a0d0e21e
LW
5565 if (mg->mg_flags & MGf_REFCOUNTED)
5566 SvREFCNT_dec(mg->mg_obj);
463ee0b2
LW
5567 Safefree(mg);
5568 }
5569 else
5570 mgp = &mg->mg_moremagic;
79072805 5571 }
91bba347 5572 if (!SvMAGIC(sv)) {
463ee0b2 5573 SvMAGICAL_off(sv);
06759ea0 5574 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
463ee0b2
LW
5575 }
5576
5577 return 0;
79072805
LW
5578}
5579
c461cf8f
JH
5580/*
5581=for apidoc sv_rvweaken
5582
645c22ef
DM
5583Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
5584referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
5585push a back-reference to this RV onto the array of backreferences
5586associated with that magic.
c461cf8f
JH
5587
5588=cut
5589*/
5590
810b8aa5 5591SV *
864dbfa3 5592Perl_sv_rvweaken(pTHX_ SV *sv)
810b8aa5
GS
5593{
5594 SV *tsv;
5595 if (!SvOK(sv)) /* let undefs pass */
5596 return sv;
5597 if (!SvROK(sv))
cea2e8a9 5598 Perl_croak(aTHX_ "Can't weaken a nonreference");
810b8aa5 5599 else if (SvWEAKREF(sv)) {
810b8aa5 5600 if (ckWARN(WARN_MISC))
9014280d 5601 Perl_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
810b8aa5
GS
5602 return sv;
5603 }
5604 tsv = SvRV(sv);
5605 sv_add_backref(tsv, sv);
5606 SvWEAKREF_on(sv);
1c846c1f 5607 SvREFCNT_dec(tsv);
810b8aa5
GS
5608 return sv;
5609}
5610
645c22ef
DM
5611/* Give tsv backref magic if it hasn't already got it, then push a
5612 * back-reference to sv onto the array associated with the backref magic.
5613 */
5614
810b8aa5 5615STATIC void
cea2e8a9 5616S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
810b8aa5
GS
5617{
5618 AV *av;
5619 MAGIC *mg;
14befaf4 5620 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, PERL_MAGIC_backref)))
810b8aa5
GS
5621 av = (AV*)mg->mg_obj;
5622 else {
5623 av = newAV();
14befaf4 5624 sv_magic(tsv, (SV*)av, PERL_MAGIC_backref, NULL, 0);
d99b02a1
DM
5625 /* av now has a refcnt of 2, which avoids it getting freed
5626 * before us during global cleanup. The extra ref is removed
5627 * by magic_killbackrefs() when tsv is being freed */
810b8aa5 5628 }
d91d49e8 5629 if (AvFILLp(av) >= AvMAX(av)) {
fdc9a813 5630 I32 i;
d91d49e8 5631 SV **svp = AvARRAY(av);
fdc9a813
AE
5632 for (i = AvFILLp(av); i >= 0; i--)
5633 if (!svp[i]) {
d91d49e8
MM
5634 svp[i] = sv; /* reuse the slot */
5635 return;
5636 }
d91d49e8
MM
5637 av_extend(av, AvFILLp(av)+1);
5638 }
5639 AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
810b8aa5
GS
5640}
5641
645c22ef
DM
5642/* delete a back-reference to ourselves from the backref magic associated
5643 * with the SV we point to.
5644 */
5645
1c846c1f 5646STATIC void
cea2e8a9 5647S_sv_del_backref(pTHX_ SV *sv)
810b8aa5
GS
5648{
5649 AV *av;
5650 SV **svp;
5651 I32 i;
5652 SV *tsv = SvRV(sv);
c04a4dfe 5653 MAGIC *mg = NULL;
14befaf4 5654 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref)))
cea2e8a9 5655 Perl_croak(aTHX_ "panic: del_backref");
810b8aa5
GS
5656 av = (AV *)mg->mg_obj;
5657 svp = AvARRAY(av);
fdc9a813
AE
5658 for (i = AvFILLp(av); i >= 0; i--)
5659 if (svp[i] == sv) svp[i] = Nullsv;
810b8aa5
GS
5660}
5661
954c1994
GS
5662/*
5663=for apidoc sv_insert
5664
5665Inserts a string at the specified offset/length within the SV. Similar to
5666the Perl substr() function.
5667
5668=cut
5669*/
5670
79072805 5671void
e1ec3a88 5672Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, const char *little, STRLEN littlelen)
79072805
LW
5673{
5674 register char *big;
5675 register char *mid;
5676 register char *midend;
5677 register char *bigend;
5678 register I32 i;
6ff81951 5679 STRLEN curlen;
1c846c1f 5680
79072805 5681
8990e307 5682 if (!bigstr)
cea2e8a9 5683 Perl_croak(aTHX_ "Can't modify non-existent substring");
6ff81951 5684 SvPV_force(bigstr, curlen);
60fa28ff 5685 (void)SvPOK_only_UTF8(bigstr);
6ff81951
GS
5686 if (offset + len > curlen) {
5687 SvGROW(bigstr, offset+len+1);
5688 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
5689 SvCUR_set(bigstr, offset+len);
5690 }
79072805 5691
69b47968 5692 SvTAINT(bigstr);
79072805
LW
5693 i = littlelen - len;
5694 if (i > 0) { /* string might grow */
a0d0e21e 5695 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
79072805
LW
5696 mid = big + offset + len;
5697 midend = bigend = big + SvCUR(bigstr);
5698 bigend += i;
5699 *bigend = '\0';
5700 while (midend > mid) /* shove everything down */
5701 *--bigend = *--midend;
5702 Move(little,big+offset,littlelen,char);
b162af07 5703 SvCUR_set(bigstr, SvCUR(bigstr) + i);
79072805
LW
5704 SvSETMAGIC(bigstr);
5705 return;
5706 }
5707 else if (i == 0) {
463ee0b2 5708 Move(little,SvPVX(bigstr)+offset,len,char);
79072805
LW
5709 SvSETMAGIC(bigstr);
5710 return;
5711 }
5712
463ee0b2 5713 big = SvPVX(bigstr);
79072805
LW
5714 mid = big + offset;
5715 midend = mid + len;
5716 bigend = big + SvCUR(bigstr);
5717
5718 if (midend > bigend)
cea2e8a9 5719 Perl_croak(aTHX_ "panic: sv_insert");
79072805
LW
5720
5721 if (mid - big > bigend - midend) { /* faster to shorten from end */
5722 if (littlelen) {
5723 Move(little, mid, littlelen,char);
5724 mid += littlelen;
5725 }
5726 i = bigend - midend;
5727 if (i > 0) {
5728 Move(midend, mid, i,char);
5729 mid += i;
5730 }
5731 *mid = '\0';
5732 SvCUR_set(bigstr, mid - big);
5733 }
5734 /*SUPPRESS 560*/
155aba94 5735 else if ((i = mid - big)) { /* faster from front */
79072805
LW
5736 midend -= littlelen;
5737 mid = midend;
5738 sv_chop(bigstr,midend-i);
5739 big += i;
5740 while (i--)
5741 *--midend = *--big;
5742 if (littlelen)
5743 Move(little, mid, littlelen,char);
5744 }
5745 else if (littlelen) {
5746 midend -= littlelen;
5747 sv_chop(bigstr,midend);
5748 Move(little,midend,littlelen,char);
5749 }
5750 else {
5751 sv_chop(bigstr,midend);
5752 }
5753 SvSETMAGIC(bigstr);
5754}
5755
c461cf8f
JH
5756/*
5757=for apidoc sv_replace
5758
5759Make the first argument a copy of the second, then delete the original.
645c22ef
DM
5760The target SV physically takes over ownership of the body of the source SV
5761and inherits its flags; however, the target keeps any magic it owns,
5762and any magic in the source is discarded.
ff276b08 5763Note that this is a rather specialist SV copying operation; most of the
645c22ef 5764time you'll want to use C<sv_setsv> or one of its many macro front-ends.
c461cf8f
JH
5765
5766=cut
5767*/
79072805
LW
5768
5769void
864dbfa3 5770Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
79072805
LW
5771{
5772 U32 refcnt = SvREFCNT(sv);
765f542d 5773 SV_CHECK_THINKFIRST_COW_DROP(sv);
0453d815 5774 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
9014280d 5775 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Reference miscount in sv_replace()");
93a17b20 5776 if (SvMAGICAL(sv)) {
a0d0e21e
LW
5777 if (SvMAGICAL(nsv))
5778 mg_free(nsv);
5779 else
5780 sv_upgrade(nsv, SVt_PVMG);
b162af07 5781 SvMAGIC_set(nsv, SvMAGIC(sv));
a0d0e21e 5782 SvFLAGS(nsv) |= SvMAGICAL(sv);
93a17b20 5783 SvMAGICAL_off(sv);
b162af07 5784 SvMAGIC_set(sv, NULL);
93a17b20 5785 }
79072805
LW
5786 SvREFCNT(sv) = 0;
5787 sv_clear(sv);
477f5d66 5788 assert(!SvREFCNT(sv));
fd0854ff
DM
5789#ifdef DEBUG_LEAKING_SCALARS
5790 sv->sv_flags = nsv->sv_flags;
5791 sv->sv_any = nsv->sv_any;
5792 sv->sv_refcnt = nsv->sv_refcnt;
5793#else
79072805 5794 StructCopy(nsv,sv,SV);
fd0854ff
DM
5795#endif
5796
d3d0e6f1
NC
5797#ifdef PERL_COPY_ON_WRITE
5798 if (SvIsCOW_normal(nsv)) {
5799 /* We need to follow the pointers around the loop to make the
5800 previous SV point to sv, rather than nsv. */
5801 SV *next;
5802 SV *current = nsv;
5803 while ((next = SV_COW_NEXT_SV(current)) != nsv) {
5804 assert(next);
5805 current = next;
5806 assert(SvPVX(current) == SvPVX(nsv));
5807 }
5808 /* Make the SV before us point to the SV after us. */
5809 if (DEBUG_C_TEST) {
5810 PerlIO_printf(Perl_debug_log, "previous is\n");
5811 sv_dump(current);
a29f6d03
NC
5812 PerlIO_printf(Perl_debug_log,
5813 "move it from 0x%"UVxf" to 0x%"UVxf"\n",
d3d0e6f1
NC
5814 (UV) SV_COW_NEXT_SV(current), (UV) sv);
5815 }
a29f6d03 5816 SV_COW_NEXT_SV_SET(current, sv);
d3d0e6f1
NC
5817 }
5818#endif
79072805 5819 SvREFCNT(sv) = refcnt;
1edc1566 5820 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
39cf41c2 5821 SvREFCNT(nsv) = 0;
463ee0b2 5822 del_SV(nsv);
79072805
LW
5823}
5824
c461cf8f
JH
5825/*
5826=for apidoc sv_clear
5827
645c22ef
DM
5828Clear an SV: call any destructors, free up any memory used by the body,
5829and free the body itself. The SV's head is I<not> freed, although
5830its type is set to all 1's so that it won't inadvertently be assumed
5831to be live during global destruction etc.
5832This function should only be called when REFCNT is zero. Most of the time
5833you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
5834instead.
c461cf8f
JH
5835
5836=cut
5837*/
5838
79072805 5839void
864dbfa3 5840Perl_sv_clear(pTHX_ register SV *sv)
79072805 5841{
27da23d5 5842 dVAR;
ec12f114 5843 HV* stash;
79072805
LW
5844 assert(sv);
5845 assert(SvREFCNT(sv) == 0);
5846
ed6116ce 5847 if (SvOBJECT(sv)) {
3280af22 5848 if (PL_defstash) { /* Still have a symbol table? */
39644a26 5849 dSP;
32251b26 5850 CV* destructor;
a0d0e21e 5851
5cc433a6 5852
8ebc5c01 5853
d460ef45 5854 do {
4e8e7886 5855 stash = SvSTASH(sv);
32251b26 5856 destructor = StashHANDLER(stash,DESTROY);
4e8e7886 5857 if (destructor) {
5cc433a6
AB
5858 SV* tmpref = newRV(sv);
5859 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
4e8e7886 5860 ENTER;
e788e7d3 5861 PUSHSTACKi(PERLSI_DESTROY);
4e8e7886
GS
5862 EXTEND(SP, 2);
5863 PUSHMARK(SP);
5cc433a6 5864 PUSHs(tmpref);
4e8e7886 5865 PUTBACK;
44389ee9 5866 call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
7a5fa8a2
NIS
5867
5868
d3acc0f7 5869 POPSTACK;
3095d977 5870 SPAGAIN;
4e8e7886 5871 LEAVE;
5cc433a6
AB
5872 if(SvREFCNT(tmpref) < 2) {
5873 /* tmpref is not kept alive! */
5874 SvREFCNT(sv)--;
b162af07 5875 SvRV_set(tmpref, NULL);
5cc433a6
AB
5876 SvROK_off(tmpref);
5877 }
5878 SvREFCNT_dec(tmpref);
4e8e7886
GS
5879 }
5880 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
8ebc5c01 5881
6f44e0a4
JP
5882
5883 if (SvREFCNT(sv)) {
5884 if (PL_in_clean_objs)
cea2e8a9 5885 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
6f44e0a4
JP
5886 HvNAME(stash));
5887 /* DESTROY gave object new lease on life */
5888 return;
5889 }
a0d0e21e 5890 }
4e8e7886 5891
a0d0e21e 5892 if (SvOBJECT(sv)) {
4e8e7886 5893 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
a0d0e21e
LW
5894 SvOBJECT_off(sv); /* Curse the object. */
5895 if (SvTYPE(sv) != SVt_PVIO)
3280af22 5896 --PL_sv_objcount; /* XXX Might want something more general */
a0d0e21e 5897 }
463ee0b2 5898 }
524189f1
JH
5899 if (SvTYPE(sv) >= SVt_PVMG) {
5900 if (SvMAGIC(sv))
5901 mg_free(sv);
5902 if (SvFLAGS(sv) & SVpad_TYPED)
5903 SvREFCNT_dec(SvSTASH(sv));
5904 }
ec12f114 5905 stash = NULL;
79072805 5906 switch (SvTYPE(sv)) {
8990e307 5907 case SVt_PVIO:
df0bd2f4
GS
5908 if (IoIFP(sv) &&
5909 IoIFP(sv) != PerlIO_stdin() &&
5f05dabc 5910 IoIFP(sv) != PerlIO_stdout() &&
5911 IoIFP(sv) != PerlIO_stderr())
93578b34 5912 {
f2b5be74 5913 io_close((IO*)sv, FALSE);
93578b34 5914 }
1d7c1841 5915 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
1236053a 5916 PerlDir_close(IoDIRP(sv));
1d7c1841 5917 IoDIRP(sv) = (DIR*)NULL;
8990e307
LW
5918 Safefree(IoTOP_NAME(sv));
5919 Safefree(IoFMT_NAME(sv));
5920 Safefree(IoBOTTOM_NAME(sv));
a0d0e21e 5921 /* FALL THROUGH */
79072805 5922 case SVt_PVBM:
a0d0e21e 5923 goto freescalar;
79072805 5924 case SVt_PVCV:
748a9306 5925 case SVt_PVFM:
85e6fe83 5926 cv_undef((CV*)sv);
a0d0e21e 5927 goto freescalar;
79072805 5928 case SVt_PVHV:
85e6fe83 5929 hv_undef((HV*)sv);
a0d0e21e 5930 break;
79072805 5931 case SVt_PVAV:
85e6fe83 5932 av_undef((AV*)sv);
a0d0e21e 5933 break;
02270b4e 5934 case SVt_PVLV:
dd28f7bb
DM
5935 if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
5936 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
5937 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
5938 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
5939 }
5940 else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */
5941 SvREFCNT_dec(LvTARG(sv));
02270b4e 5942 goto freescalar;
a0d0e21e 5943 case SVt_PVGV:
1edc1566 5944 gp_free((GV*)sv);
a0d0e21e 5945 Safefree(GvNAME(sv));
ec12f114
JPC
5946 /* cannot decrease stash refcount yet, as we might recursively delete
5947 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
5948 of stash until current sv is completely gone.
5949 -- JohnPC, 27 Mar 1998 */
5950 stash = GvSTASH(sv);
a0d0e21e 5951 /* FALL THROUGH */
79072805 5952 case SVt_PVMG:
79072805
LW
5953 case SVt_PVNV:
5954 case SVt_PVIV:
a0d0e21e 5955 freescalar:
5228ca4e
NC
5956 /* Don't bother with SvOOK_off(sv); as we're only going to free it. */
5957 if (SvOOK(sv)) {
5958 SvPV_set(sv, SvPVX(sv) - SvIVX(sv));
5959 /* Don't even bother with turning off the OOK flag. */
5960 }
79072805
LW
5961 /* FALL THROUGH */
5962 case SVt_PV:
a0d0e21e 5963 case SVt_RV:
810b8aa5
GS
5964 if (SvROK(sv)) {
5965 if (SvWEAKREF(sv))
5966 sv_del_backref(sv);
5967 else
5968 SvREFCNT_dec(SvRV(sv));
5969 }
765f542d
NC
5970#ifdef PERL_COPY_ON_WRITE
5971 else if (SvPVX(sv)) {
5972 if (SvIsCOW(sv)) {
5973 /* I believe I need to grab the global SV mutex here and
5974 then recheck the COW status. */
46187eeb
NC
5975 if (DEBUG_C_TEST) {
5976 PerlIO_printf(Perl_debug_log, "Copy on write: clear\n");
e419cbc5 5977 sv_dump(sv);
46187eeb 5978 }
e419cbc5 5979 sv_release_COW(sv, SvPVX(sv), SvCUR(sv), SvLEN(sv),
765f542d
NC
5980 SvUVX(sv), SV_COW_NEXT_SV(sv));
5981 /* And drop it here. */
5982 SvFAKE_off(sv);
5983 } else if (SvLEN(sv)) {
5984 Safefree(SvPVX(sv));
5985 }
5986 }
5987#else
1edc1566 5988 else if (SvPVX(sv) && SvLEN(sv))
463ee0b2 5989 Safefree(SvPVX(sv));
1c846c1f 5990 else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) {
25716404
GS
5991 unsharepvn(SvPVX(sv),
5992 SvUTF8(sv) ? -(I32)SvCUR(sv) : SvCUR(sv),
5993 SvUVX(sv));
1c846c1f
NIS
5994 SvFAKE_off(sv);
5995 }
765f542d 5996#endif
79072805 5997 break;
a0d0e21e 5998/*
79072805 5999 case SVt_NV:
79072805 6000 case SVt_IV:
79072805
LW
6001 case SVt_NULL:
6002 break;
a0d0e21e 6003*/
79072805
LW
6004 }
6005
6006 switch (SvTYPE(sv)) {
6007 case SVt_NULL:
6008 break;
79072805
LW
6009 case SVt_IV:
6010 del_XIV(SvANY(sv));
6011 break;
6012 case SVt_NV:
6013 del_XNV(SvANY(sv));
6014 break;
ed6116ce
LW
6015 case SVt_RV:
6016 del_XRV(SvANY(sv));
6017 break;
79072805
LW
6018 case SVt_PV:
6019 del_XPV(SvANY(sv));
6020 break;
6021 case SVt_PVIV:
6022 del_XPVIV(SvANY(sv));
6023 break;
6024 case SVt_PVNV:
6025 del_XPVNV(SvANY(sv));
6026 break;
6027 case SVt_PVMG:
6028 del_XPVMG(SvANY(sv));
6029 break;
6030 case SVt_PVLV:
6031 del_XPVLV(SvANY(sv));
6032 break;
6033 case SVt_PVAV:
6034 del_XPVAV(SvANY(sv));
6035 break;
6036 case SVt_PVHV:
6037 del_XPVHV(SvANY(sv));
6038 break;
6039 case SVt_PVCV:
6040 del_XPVCV(SvANY(sv));
6041 break;
6042 case SVt_PVGV:
6043 del_XPVGV(SvANY(sv));
ec12f114
JPC
6044 /* code duplication for increased performance. */
6045 SvFLAGS(sv) &= SVf_BREAK;
6046 SvFLAGS(sv) |= SVTYPEMASK;
6047 /* decrease refcount of the stash that owns this GV, if any */
6048 if (stash)
6049 SvREFCNT_dec(stash);
6050 return; /* not break, SvFLAGS reset already happened */
79072805
LW
6051 case SVt_PVBM:
6052 del_XPVBM(SvANY(sv));
6053 break;
6054 case SVt_PVFM:
6055 del_XPVFM(SvANY(sv));
6056 break;
8990e307
LW
6057 case SVt_PVIO:
6058 del_XPVIO(SvANY(sv));
6059 break;
79072805 6060 }
a0d0e21e 6061 SvFLAGS(sv) &= SVf_BREAK;
8990e307 6062 SvFLAGS(sv) |= SVTYPEMASK;
79072805
LW
6063}
6064
645c22ef
DM
6065/*
6066=for apidoc sv_newref
6067
6068Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
6069instead.
6070
6071=cut
6072*/
6073
79072805 6074SV *
864dbfa3 6075Perl_sv_newref(pTHX_ SV *sv)
79072805 6076{
463ee0b2 6077 if (sv)
4db098f4 6078 (SvREFCNT(sv))++;
79072805
LW
6079 return sv;
6080}
6081
c461cf8f
JH
6082/*
6083=for apidoc sv_free
6084
645c22ef
DM
6085Decrement an SV's reference count, and if it drops to zero, call
6086C<sv_clear> to invoke destructors and free up any memory used by
6087the body; finally, deallocate the SV's head itself.
6088Normally called via a wrapper macro C<SvREFCNT_dec>.
c461cf8f
JH
6089
6090=cut
6091*/
6092
79072805 6093void
864dbfa3 6094Perl_sv_free(pTHX_ SV *sv)
79072805 6095{
27da23d5 6096 dVAR;
79072805
LW
6097 if (!sv)
6098 return;
a0d0e21e
LW
6099 if (SvREFCNT(sv) == 0) {
6100 if (SvFLAGS(sv) & SVf_BREAK)
645c22ef
DM
6101 /* this SV's refcnt has been artificially decremented to
6102 * trigger cleanup */
a0d0e21e 6103 return;
3280af22 6104 if (PL_in_clean_all) /* All is fair */
1edc1566 6105 return;
d689ffdd
JP
6106 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
6107 /* make sure SvREFCNT(sv)==0 happens very seldom */
6108 SvREFCNT(sv) = (~(U32)0)/2;
6109 return;
6110 }
0453d815 6111 if (ckWARN_d(WARN_INTERNAL))
d5dede04 6112 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
472d47bc
SB
6113 "Attempt to free unreferenced scalar: SV 0x%"UVxf
6114 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
79072805
LW
6115 return;
6116 }
4db098f4 6117 if (--(SvREFCNT(sv)) > 0)
8990e307 6118 return;
8c4d3c90
NC
6119 Perl_sv_free2(aTHX_ sv);
6120}
6121
6122void
6123Perl_sv_free2(pTHX_ SV *sv)
6124{
27da23d5 6125 dVAR;
463ee0b2
LW
6126#ifdef DEBUGGING
6127 if (SvTEMP(sv)) {
0453d815 6128 if (ckWARN_d(WARN_DEBUGGING))
9014280d 6129 Perl_warner(aTHX_ packWARN(WARN_DEBUGGING),
472d47bc
SB
6130 "Attempt to free temp prematurely: SV 0x%"UVxf
6131 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
79072805 6132 return;
79072805 6133 }
463ee0b2 6134#endif
d689ffdd
JP
6135 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
6136 /* make sure SvREFCNT(sv)==0 happens very seldom */
6137 SvREFCNT(sv) = (~(U32)0)/2;
6138 return;
6139 }
79072805 6140 sv_clear(sv);
477f5d66
CS
6141 if (! SvREFCNT(sv))
6142 del_SV(sv);
79072805
LW
6143}
6144
954c1994
GS
6145/*
6146=for apidoc sv_len
6147
645c22ef
DM
6148Returns the length of the string in the SV. Handles magic and type
6149coercion. See also C<SvCUR>, which gives raw access to the xpv_cur slot.
954c1994
GS
6150
6151=cut
6152*/
6153
79072805 6154STRLEN
864dbfa3 6155Perl_sv_len(pTHX_ register SV *sv)
79072805 6156{
463ee0b2 6157 STRLEN len;
79072805
LW
6158
6159 if (!sv)
6160 return 0;
6161
8990e307 6162 if (SvGMAGICAL(sv))
565764a8 6163 len = mg_length(sv);
8990e307 6164 else
497b47a8 6165 (void)SvPV(sv, len);
463ee0b2 6166 return len;
79072805
LW
6167}
6168
c461cf8f
JH
6169/*
6170=for apidoc sv_len_utf8
6171
6172Returns the number of characters in the string in an SV, counting wide
1e54db1a 6173UTF-8 bytes as a single character. Handles magic and type coercion.
c461cf8f
JH
6174
6175=cut
6176*/
6177
7e8c5dac
HS
6178/*
6179 * The length is cached in PERL_UTF8_magic, in the mg_len field. Also the
6180 * mg_ptr is used, by sv_pos_u2b(), see the comments of S_utf8_mg_pos_init().
6181 * (Note that the mg_len is not the length of the mg_ptr field.)
7a5fa8a2 6182 *
7e8c5dac
HS
6183 */
6184
a0ed51b3 6185STRLEN
864dbfa3 6186Perl_sv_len_utf8(pTHX_ register SV *sv)
a0ed51b3 6187{
a0ed51b3
LW
6188 if (!sv)
6189 return 0;
6190
a0ed51b3 6191 if (SvGMAGICAL(sv))
b76347f2 6192 return mg_length(sv);
a0ed51b3 6193 else
b76347f2 6194 {
7e8c5dac 6195 STRLEN len, ulen;
b76347f2 6196 U8 *s = (U8*)SvPV(sv, len);
7e8c5dac
HS
6197 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : 0;
6198
e23c8137 6199 if (mg && mg->mg_len != -1 && (mg->mg_len > 0 || len == 0)) {
7e8c5dac 6200 ulen = mg->mg_len;
e23c8137
JH
6201#ifdef PERL_UTF8_CACHE_ASSERT
6202 assert(ulen == Perl_utf8_length(aTHX_ s, s + len));
6203#endif
6204 }
7e8c5dac
HS
6205 else {
6206 ulen = Perl_utf8_length(aTHX_ s, s + len);
6207 if (!mg && !SvREADONLY(sv)) {
6208 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
6209 mg = mg_find(sv, PERL_MAGIC_utf8);
6210 assert(mg);
6211 }
6212 if (mg)
6213 mg->mg_len = ulen;
6214 }
6215 return ulen;
6216 }
6217}
6218
6219/* S_utf8_mg_pos_init() is used to initialize the mg_ptr field of
6220 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
6221 * between UTF-8 and byte offsets. There are two (substr offset and substr
6222 * length, the i offset, PERL_MAGIC_UTF8_CACHESIZE) times two (UTF-8 offset
6223 * and byte offset) cache positions.
6224 *
6225 * The mg_len field is used by sv_len_utf8(), see its comments.
6226 * Note that the mg_len is not the length of the mg_ptr field.
6227 *
6228 */
6229STATIC bool
6e551876 6230S_utf8_mg_pos_init(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, U8 *s, U8 *start)
7e8c5dac 6231{
7a5fa8a2 6232 bool found = FALSE;
7e8c5dac
HS
6233
6234 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
8f78557a 6235 if (!*mgp)
27da23d5 6236 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0, 0);
7e8c5dac 6237 assert(*mgp);
b76347f2 6238
7e8c5dac
HS
6239 if ((*mgp)->mg_ptr)
6240 *cachep = (STRLEN *) (*mgp)->mg_ptr;
6241 else {
6242 Newz(0, *cachep, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
6243 (*mgp)->mg_ptr = (char *) *cachep;
6244 }
6245 assert(*cachep);
6246
6247 (*cachep)[i] = *offsetp;
6248 (*cachep)[i+1] = s - start;
6249 found = TRUE;
a0ed51b3 6250 }
7e8c5dac
HS
6251
6252 return found;
a0ed51b3
LW
6253}
6254
645c22ef 6255/*
7e8c5dac
HS
6256 * S_utf8_mg_pos() is used to query and update mg_ptr field of
6257 * a PERL_UTF8_magic. The mg_ptr is used to store the mapping
6258 * between UTF-8 and byte offsets. See also the comments of
6259 * S_utf8_mg_pos_init().
6260 *
6261 */
6262STATIC bool
6e551876 6263S_utf8_mg_pos(pTHX_ SV *sv, MAGIC **mgp, STRLEN **cachep, I32 i, I32 *offsetp, I32 uoff, U8 **sp, U8 *start, U8 *send)
7e8c5dac
HS
6264{
6265 bool found = FALSE;
6266
6267 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
6268 if (!*mgp)
6269 *mgp = mg_find(sv, PERL_MAGIC_utf8);
6270 if (*mgp && (*mgp)->mg_ptr) {
6271 *cachep = (STRLEN *) (*mgp)->mg_ptr;
e23c8137 6272 ASSERT_UTF8_CACHE(*cachep);
667208dd 6273 if ((*cachep)[i] == (STRLEN)uoff) /* An exact match. */
7a5fa8a2 6274 found = TRUE;
7e8c5dac
HS
6275 else { /* We will skip to the right spot. */
6276 STRLEN forw = 0;
6277 STRLEN backw = 0;
6278 U8* p = NULL;
6279
6280 /* The assumption is that going backward is half
6281 * the speed of going forward (that's where the
6282 * 2 * backw in the below comes from). (The real
6283 * figure of course depends on the UTF-8 data.) */
6284
667208dd 6285 if ((*cachep)[i] > (STRLEN)uoff) {
7e8c5dac 6286 forw = uoff;
667208dd 6287 backw = (*cachep)[i] - (STRLEN)uoff;
7e8c5dac
HS
6288
6289 if (forw < 2 * backw)
6290 p = start;
6291 else
6292 p = start + (*cachep)[i+1];
6293 }
6294 /* Try this only for the substr offset (i == 0),
6295 * not for the substr length (i == 2). */
6296 else if (i == 0) { /* (*cachep)[i] < uoff */
6297 STRLEN ulen = sv_len_utf8(sv);
6298
667208dd
JH
6299 if ((STRLEN)uoff < ulen) {
6300 forw = (STRLEN)uoff - (*cachep)[i];
6301 backw = ulen - (STRLEN)uoff;
7e8c5dac
HS
6302
6303 if (forw < 2 * backw)
6304 p = start + (*cachep)[i+1];
6305 else
6306 p = send;
6307 }
6308
6309 /* If the string is not long enough for uoff,
6310 * we could extend it, but not at this low a level. */
6311 }
6312
6313 if (p) {
6314 if (forw < 2 * backw) {
6315 while (forw--)
6316 p += UTF8SKIP(p);
6317 }
6318 else {
6319 while (backw--) {
6320 p--;
6321 while (UTF8_IS_CONTINUATION(*p))
6322 p--;
6323 }
6324 }
6325
6326 /* Update the cache. */
667208dd 6327 (*cachep)[i] = (STRLEN)uoff;
7e8c5dac 6328 (*cachep)[i+1] = p - start;
8f78557a
AE
6329
6330 /* Drop the stale "length" cache */
6331 if (i == 0) {
6332 (*cachep)[2] = 0;
6333 (*cachep)[3] = 0;
6334 }
7a5fa8a2 6335
7e8c5dac
HS
6336 found = TRUE;
6337 }
6338 }
6339 if (found) { /* Setup the return values. */
6340 *offsetp = (*cachep)[i+1];
6341 *sp = start + *offsetp;
6342 if (*sp >= send) {
6343 *sp = send;
6344 *offsetp = send - start;
6345 }
6346 else if (*sp < start) {
6347 *sp = start;
6348 *offsetp = 0;
6349 }
6350 }
6351 }
e23c8137
JH
6352#ifdef PERL_UTF8_CACHE_ASSERT
6353 if (found) {
6354 U8 *s = start;
6355 I32 n = uoff;
6356
6357 while (n-- && s < send)
6358 s += UTF8SKIP(s);
6359
6360 if (i == 0) {
6361 assert(*offsetp == s - start);
6362 assert((*cachep)[0] == (STRLEN)uoff);
6363 assert((*cachep)[1] == *offsetp);
6364 }
6365 ASSERT_UTF8_CACHE(*cachep);
6366 }
6367#endif
7e8c5dac 6368 }
e23c8137 6369
7e8c5dac
HS
6370 return found;
6371}
7a5fa8a2 6372
7e8c5dac 6373/*
645c22ef
DM
6374=for apidoc sv_pos_u2b
6375
1e54db1a 6376Converts the value pointed to by offsetp from a count of UTF-8 chars from
645c22ef
DM
6377the start of the string, to a count of the equivalent number of bytes; if
6378lenp is non-zero, it does the same to lenp, but this time starting from
6379the offset, rather than from the start of the string. Handles magic and
6380type coercion.
6381
6382=cut
6383*/
6384
7e8c5dac
HS
6385/*
6386 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
6387 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
6388 * byte offsets. See also the comments of S_utf8_mg_pos().
6389 *
6390 */
6391
a0ed51b3 6392void
864dbfa3 6393Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
a0ed51b3 6394{
dfe13c55
GS
6395 U8 *start;
6396 U8 *s;
a0ed51b3 6397 STRLEN len;
7e8c5dac
HS
6398 STRLEN *cache = 0;
6399 STRLEN boffset = 0;
a0ed51b3
LW
6400
6401 if (!sv)
6402 return;
6403
dfe13c55 6404 start = s = (U8*)SvPV(sv, len);
7e8c5dac
HS
6405 if (len) {
6406 I32 uoffset = *offsetp;
6407 U8 *send = s + len;
6408 MAGIC *mg = 0;
6409 bool found = FALSE;
6410
bdf77a2a 6411 if (utf8_mg_pos(sv, &mg, &cache, 0, offsetp, *offsetp, &s, start, send))
7e8c5dac
HS
6412 found = TRUE;
6413 if (!found && uoffset > 0) {
6414 while (s < send && uoffset--)
6415 s += UTF8SKIP(s);
6416 if (s >= send)
6417 s = send;
bdf77a2a 6418 if (utf8_mg_pos_init(sv, &mg, &cache, 0, offsetp, s, start))
7e8c5dac
HS
6419 boffset = cache[1];
6420 *offsetp = s - start;
6421 }
6422 if (lenp) {
6423 found = FALSE;
6424 start = s;
ec062429 6425 if (utf8_mg_pos(sv, &mg, &cache, 2, lenp, *lenp, &s, start, send)) {
7e8c5dac
HS
6426 *lenp -= boffset;
6427 found = TRUE;
6428 }
6429 if (!found && *lenp > 0) {
6430 I32 ulen = *lenp;
6431 if (ulen > 0)
6432 while (s < send && ulen--)
6433 s += UTF8SKIP(s);
6434 if (s >= send)
6435 s = send;
a67d7df9 6436 utf8_mg_pos_init(sv, &mg, &cache, 2, lenp, s, start);
7e8c5dac
HS
6437 }
6438 *lenp = s - start;
6439 }
e23c8137 6440 ASSERT_UTF8_CACHE(cache);
7e8c5dac
HS
6441 }
6442 else {
6443 *offsetp = 0;
6444 if (lenp)
6445 *lenp = 0;
a0ed51b3 6446 }
e23c8137 6447
a0ed51b3
LW
6448 return;
6449}
6450
645c22ef
DM
6451/*
6452=for apidoc sv_pos_b2u
6453
6454Converts the value pointed to by offsetp from a count of bytes from the
1e54db1a 6455start of the string, to a count of the equivalent number of UTF-8 chars.
645c22ef
DM
6456Handles magic and type coercion.
6457
6458=cut
6459*/
6460
7e8c5dac
HS
6461/*
6462 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
6463 * PERL_UTF8_magic of the sv to store the mapping between UTF-8 and
6464 * byte offsets. See also the comments of S_utf8_mg_pos().
6465 *
6466 */
6467
a0ed51b3 6468void
7e8c5dac 6469Perl_sv_pos_b2u(pTHX_ register SV* sv, I32* offsetp)
a0ed51b3 6470{
7e8c5dac 6471 U8* s;
a0ed51b3
LW
6472 STRLEN len;
6473
6474 if (!sv)
6475 return;
6476
dfe13c55 6477 s = (U8*)SvPV(sv, len);
eb160463 6478 if ((I32)len < *offsetp)
a0dbb045 6479 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset");
7e8c5dac
HS
6480 else {
6481 U8* send = s + *offsetp;
6482 MAGIC* mg = NULL;
6483 STRLEN *cache = NULL;
6484
6485 len = 0;
6486
6487 if (SvMAGICAL(sv) && !SvREADONLY(sv)) {
6488 mg = mg_find(sv, PERL_MAGIC_utf8);
6489 if (mg && mg->mg_ptr) {
6490 cache = (STRLEN *) mg->mg_ptr;
c5661c80 6491 if (cache[1] == (STRLEN)*offsetp) {
7e8c5dac
HS
6492 /* An exact match. */
6493 *offsetp = cache[0];
6494
6495 return;
6496 }
c5661c80 6497 else if (cache[1] < (STRLEN)*offsetp) {
7e8c5dac
HS
6498 /* We already know part of the way. */
6499 len = cache[0];
6500 s += cache[1];
7a5fa8a2 6501 /* Let the below loop do the rest. */
7e8c5dac
HS
6502 }
6503 else { /* cache[1] > *offsetp */
6504 /* We already know all of the way, now we may
6505 * be able to walk back. The same assumption
6506 * is made as in S_utf8_mg_pos(), namely that
6507 * walking backward is twice slower than
6508 * walking forward. */
6509 STRLEN forw = *offsetp;
6510 STRLEN backw = cache[1] - *offsetp;
6511
6512 if (!(forw < 2 * backw)) {
6513 U8 *p = s + cache[1];
6514 STRLEN ubackw = 0;
7a5fa8a2 6515
a5b510f2
AE
6516 cache[1] -= backw;
6517
7e8c5dac
HS
6518 while (backw--) {
6519 p--;
0aeb64d0 6520 while (UTF8_IS_CONTINUATION(*p)) {
7e8c5dac 6521 p--;
0aeb64d0
JH
6522 backw--;
6523 }
7e8c5dac
HS
6524 ubackw++;
6525 }
6526
6527 cache[0] -= ubackw;
0aeb64d0 6528 *offsetp = cache[0];
a67d7df9
TS
6529
6530 /* Drop the stale "length" cache */
6531 cache[2] = 0;
6532 cache[3] = 0;
6533
0aeb64d0 6534 return;
7e8c5dac
HS
6535 }
6536 }
6537 }
e23c8137 6538 ASSERT_UTF8_CACHE(cache);
a0dbb045 6539 }
7e8c5dac
HS
6540
6541 while (s < send) {
6542 STRLEN n = 1;
6543
6544 /* Call utf8n_to_uvchr() to validate the sequence
6545 * (unless a simple non-UTF character) */
6546 if (!UTF8_IS_INVARIANT(*s))
6547 utf8n_to_uvchr(s, UTF8SKIP(s), &n, 0);
6548 if (n > 0) {
6549 s += n;
6550 len++;
6551 }
6552 else
6553 break;
6554 }
6555
6556 if (!SvREADONLY(sv)) {
6557 if (!mg) {
6558 sv_magic(sv, 0, PERL_MAGIC_utf8, 0, 0);
6559 mg = mg_find(sv, PERL_MAGIC_utf8);
6560 }
6561 assert(mg);
6562
6563 if (!mg->mg_ptr) {
979acdb5 6564 Newz(0, cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
7e8c5dac
HS
6565 mg->mg_ptr = (char *) cache;
6566 }
6567 assert(cache);
6568
6569 cache[0] = len;
6570 cache[1] = *offsetp;
a67d7df9
TS
6571 /* Drop the stale "length" cache */
6572 cache[2] = 0;
6573 cache[3] = 0;
7e8c5dac
HS
6574 }
6575
6576 *offsetp = len;
a0ed51b3 6577 }
a0ed51b3
LW
6578 return;
6579}
6580
954c1994
GS
6581/*
6582=for apidoc sv_eq
6583
6584Returns a boolean indicating whether the strings in the two SVs are
645c22ef
DM
6585identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will
6586coerce its args to strings if necessary.
954c1994
GS
6587
6588=cut
6589*/
6590
79072805 6591I32
e01b9e88 6592Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2)
79072805 6593{
e1ec3a88 6594 const char *pv1;
463ee0b2 6595 STRLEN cur1;
e1ec3a88 6596 const char *pv2;
463ee0b2 6597 STRLEN cur2;
e01b9e88 6598 I32 eq = 0;
553e1bcc
AT
6599 char *tpv = Nullch;
6600 SV* svrecode = Nullsv;
79072805 6601
e01b9e88 6602 if (!sv1) {
79072805
LW
6603 pv1 = "";
6604 cur1 = 0;
6605 }
463ee0b2 6606 else
e01b9e88 6607 pv1 = SvPV(sv1, cur1);
79072805 6608
e01b9e88
SC
6609 if (!sv2){
6610 pv2 = "";
6611 cur2 = 0;
92d29cee 6612 }
e01b9e88
SC
6613 else
6614 pv2 = SvPV(sv2, cur2);
79072805 6615
cf48d248 6616 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
799ef3cb
JH
6617 /* Differing utf8ness.
6618 * Do not UTF8size the comparands as a side-effect. */
6619 if (PL_encoding) {
6620 if (SvUTF8(sv1)) {
553e1bcc
AT
6621 svrecode = newSVpvn(pv2, cur2);
6622 sv_recode_to_utf8(svrecode, PL_encoding);
6623 pv2 = SvPV(svrecode, cur2);
799ef3cb
JH
6624 }
6625 else {
553e1bcc
AT
6626 svrecode = newSVpvn(pv1, cur1);
6627 sv_recode_to_utf8(svrecode, PL_encoding);
6628 pv1 = SvPV(svrecode, cur1);
799ef3cb
JH
6629 }
6630 /* Now both are in UTF-8. */
0a1bd7ac
DM
6631 if (cur1 != cur2) {
6632 SvREFCNT_dec(svrecode);
799ef3cb 6633 return FALSE;
0a1bd7ac 6634 }
799ef3cb
JH
6635 }
6636 else {
6637 bool is_utf8 = TRUE;
6638
6639 if (SvUTF8(sv1)) {
6640 /* sv1 is the UTF-8 one,
6641 * if is equal it must be downgrade-able */
e1ec3a88 6642 char *pv = (char*)bytes_from_utf8((const U8*)pv1,
799ef3cb
JH
6643 &cur1, &is_utf8);
6644 if (pv != pv1)
553e1bcc 6645 pv1 = tpv = pv;
799ef3cb
JH
6646 }
6647 else {
6648 /* sv2 is the UTF-8 one,
6649 * if is equal it must be downgrade-able */
e1ec3a88 6650 char *pv = (char *)bytes_from_utf8((const U8*)pv2,
799ef3cb
JH
6651 &cur2, &is_utf8);
6652 if (pv != pv2)
553e1bcc 6653 pv2 = tpv = pv;
799ef3cb
JH
6654 }
6655 if (is_utf8) {
6656 /* Downgrade not possible - cannot be eq */
bf694877 6657 assert (tpv == 0);
799ef3cb
JH
6658 return FALSE;
6659 }
6660 }
cf48d248
JH
6661 }
6662
6663 if (cur1 == cur2)
765f542d 6664 eq = (pv1 == pv2) || memEQ(pv1, pv2, cur1);
e01b9e88 6665
553e1bcc
AT
6666 if (svrecode)
6667 SvREFCNT_dec(svrecode);
799ef3cb 6668
553e1bcc
AT
6669 if (tpv)
6670 Safefree(tpv);
cf48d248 6671
e01b9e88 6672 return eq;
79072805
LW
6673}
6674
954c1994
GS
6675/*
6676=for apidoc sv_cmp
6677
6678Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
6679string in C<sv1> is less than, equal to, or greater than the string in
645c22ef
DM
6680C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will
6681coerce its args to strings if necessary. See also C<sv_cmp_locale>.
954c1994
GS
6682
6683=cut
6684*/
6685
79072805 6686I32
e01b9e88 6687Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2)
79072805 6688{
560a288e 6689 STRLEN cur1, cur2;
e1ec3a88
AL
6690 const char *pv1, *pv2;
6691 char *tpv = Nullch;
cf48d248 6692 I32 cmp;
553e1bcc 6693 SV *svrecode = Nullsv;
560a288e 6694
e01b9e88
SC
6695 if (!sv1) {
6696 pv1 = "";
560a288e
GS
6697 cur1 = 0;
6698 }
e01b9e88
SC
6699 else
6700 pv1 = SvPV(sv1, cur1);
560a288e 6701
553e1bcc 6702 if (!sv2) {
e01b9e88 6703 pv2 = "";
560a288e
GS
6704 cur2 = 0;
6705 }
e01b9e88
SC
6706 else
6707 pv2 = SvPV(sv2, cur2);
79072805 6708
cf48d248 6709 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
799ef3cb
JH
6710 /* Differing utf8ness.
6711 * Do not UTF8size the comparands as a side-effect. */
cf48d248 6712 if (SvUTF8(sv1)) {
799ef3cb 6713 if (PL_encoding) {
553e1bcc
AT
6714 svrecode = newSVpvn(pv2, cur2);
6715 sv_recode_to_utf8(svrecode, PL_encoding);
6716 pv2 = SvPV(svrecode, cur2);
799ef3cb
JH
6717 }
6718 else {
e1ec3a88 6719 pv2 = tpv = (char*)bytes_to_utf8((const U8*)pv2, &cur2);
799ef3cb 6720 }
cf48d248
JH
6721 }
6722 else {
799ef3cb 6723 if (PL_encoding) {
553e1bcc
AT
6724 svrecode = newSVpvn(pv1, cur1);
6725 sv_recode_to_utf8(svrecode, PL_encoding);
6726 pv1 = SvPV(svrecode, cur1);
799ef3cb
JH
6727 }
6728 else {
e1ec3a88 6729 pv1 = tpv = (char*)bytes_to_utf8((const U8*)pv1, &cur1);
799ef3cb 6730 }
cf48d248
JH
6731 }
6732 }
6733
e01b9e88 6734 if (!cur1) {
cf48d248 6735 cmp = cur2 ? -1 : 0;
e01b9e88 6736 } else if (!cur2) {
cf48d248
JH
6737 cmp = 1;
6738 } else {
e1ec3a88 6739 const I32 retval = memcmp((const void*)pv1, (const void*)pv2, cur1 < cur2 ? cur1 : cur2);
e01b9e88
SC
6740
6741 if (retval) {
cf48d248 6742 cmp = retval < 0 ? -1 : 1;
e01b9e88 6743 } else if (cur1 == cur2) {
cf48d248
JH
6744 cmp = 0;
6745 } else {
6746 cmp = cur1 < cur2 ? -1 : 1;
e01b9e88 6747 }
cf48d248 6748 }
16660edb 6749
553e1bcc
AT
6750 if (svrecode)
6751 SvREFCNT_dec(svrecode);
799ef3cb 6752
553e1bcc
AT
6753 if (tpv)
6754 Safefree(tpv);
cf48d248
JH
6755
6756 return cmp;
bbce6d69 6757}
16660edb 6758
c461cf8f
JH
6759/*
6760=for apidoc sv_cmp_locale
6761
645c22ef
DM
6762Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
6763'use bytes' aware, handles get magic, and will coerce its args to strings
6764if necessary. See also C<sv_cmp_locale>. See also C<sv_cmp>.
c461cf8f
JH
6765
6766=cut
6767*/
6768
bbce6d69 6769I32
864dbfa3 6770Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
bbce6d69 6771{
36477c24 6772#ifdef USE_LOCALE_COLLATE
16660edb 6773
bbce6d69 6774 char *pv1, *pv2;
6775 STRLEN len1, len2;
6776 I32 retval;
16660edb 6777
3280af22 6778 if (PL_collation_standard)
bbce6d69 6779 goto raw_compare;
16660edb 6780
bbce6d69 6781 len1 = 0;
8ac85365 6782 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
bbce6d69 6783 len2 = 0;
8ac85365 6784 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
16660edb 6785
bbce6d69 6786 if (!pv1 || !len1) {
6787 if (pv2 && len2)
6788 return -1;
6789 else
6790 goto raw_compare;
6791 }
6792 else {
6793 if (!pv2 || !len2)
6794 return 1;
6795 }
16660edb 6796
bbce6d69 6797 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
16660edb 6798
bbce6d69 6799 if (retval)
16660edb 6800 return retval < 0 ? -1 : 1;
6801
bbce6d69 6802 /*
6803 * When the result of collation is equality, that doesn't mean
6804 * that there are no differences -- some locales exclude some
6805 * characters from consideration. So to avoid false equalities,
6806 * we use the raw string as a tiebreaker.
6807 */
16660edb 6808
bbce6d69 6809 raw_compare:
6810 /* FALL THROUGH */
16660edb 6811
36477c24 6812#endif /* USE_LOCALE_COLLATE */
16660edb 6813
bbce6d69 6814 return sv_cmp(sv1, sv2);
6815}
79072805 6816
645c22ef 6817
36477c24 6818#ifdef USE_LOCALE_COLLATE
645c22ef 6819
7a4c00b4 6820/*
645c22ef
DM
6821=for apidoc sv_collxfrm
6822
6823Add Collate Transform magic to an SV if it doesn't already have it.
6824
6825Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the
6826scalar data of the variable, but transformed to such a format that a normal
6827memory comparison can be used to compare the data according to the locale
6828settings.
6829
6830=cut
6831*/
6832
bbce6d69 6833char *
864dbfa3 6834Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp)
bbce6d69 6835{
7a4c00b4 6836 MAGIC *mg;
16660edb 6837
14befaf4 6838 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
3280af22 6839 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
bbce6d69 6840 char *s, *xf;
6841 STRLEN len, xlen;
6842
7a4c00b4 6843 if (mg)
6844 Safefree(mg->mg_ptr);
bbce6d69 6845 s = SvPV(sv, len);
6846 if ((xf = mem_collxfrm(s, len, &xlen))) {
ff0cee69 6847 if (SvREADONLY(sv)) {
6848 SAVEFREEPV(xf);
6849 *nxp = xlen;
3280af22 6850 return xf + sizeof(PL_collation_ix);
ff0cee69 6851 }
7a4c00b4 6852 if (! mg) {
14befaf4
DM
6853 sv_magic(sv, 0, PERL_MAGIC_collxfrm, 0, 0);
6854 mg = mg_find(sv, PERL_MAGIC_collxfrm);
7a4c00b4 6855 assert(mg);
bbce6d69 6856 }
7a4c00b4 6857 mg->mg_ptr = xf;
565764a8 6858 mg->mg_len = xlen;
7a4c00b4 6859 }
6860 else {
ff0cee69 6861 if (mg) {
6862 mg->mg_ptr = NULL;
565764a8 6863 mg->mg_len = -1;
ff0cee69 6864 }
bbce6d69 6865 }
6866 }
7a4c00b4 6867 if (mg && mg->mg_ptr) {
565764a8 6868 *nxp = mg->mg_len;
3280af22 6869 return mg->mg_ptr + sizeof(PL_collation_ix);
bbce6d69 6870 }
6871 else {
6872 *nxp = 0;
6873 return NULL;
16660edb 6874 }
79072805
LW
6875}
6876
36477c24 6877#endif /* USE_LOCALE_COLLATE */
bbce6d69 6878
c461cf8f
JH
6879/*
6880=for apidoc sv_gets
6881
6882Get a line from the filehandle and store it into the SV, optionally
6883appending to the currently-stored string.
6884
6885=cut
6886*/
6887
79072805 6888char *
864dbfa3 6889Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append)
79072805 6890{
e1ec3a88 6891 const char *rsptr;
c07a80fd 6892 STRLEN rslen;
6893 register STDCHAR rslast;
6894 register STDCHAR *bp;
6895 register I32 cnt;
9c5ffd7c 6896 I32 i = 0;
8bfdd7d9 6897 I32 rspara = 0;
e311fd51 6898 I32 recsize;
c07a80fd 6899
bc44a8a2
NC
6900 if (SvTHINKFIRST(sv))
6901 sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
765f542d
NC
6902 /* XXX. If you make this PVIV, then copy on write can copy scalars read
6903 from <>.
6904 However, perlbench says it's slower, because the existing swipe code
6905 is faster than copy on write.
6906 Swings and roundabouts. */
6fc92669 6907 (void)SvUPGRADE(sv, SVt_PV);
99491443 6908
ff68c719 6909 SvSCREAM_off(sv);
efd8b2ba
AE
6910
6911 if (append) {
6912 if (PerlIO_isutf8(fp)) {
6913 if (!SvUTF8(sv)) {
6914 sv_utf8_upgrade_nomg(sv);
6915 sv_pos_u2b(sv,&append,0);
6916 }
6917 } else if (SvUTF8(sv)) {
6918 SV *tsv = NEWSV(0,0);
6919 sv_gets(tsv, fp, 0);
6920 sv_utf8_upgrade_nomg(tsv);
6921 SvCUR_set(sv,append);
6922 sv_catsv(sv,tsv);
6923 sv_free(tsv);
6924 goto return_string_or_null;
6925 }
6926 }
6927
6928 SvPOK_only(sv);
6929 if (PerlIO_isutf8(fp))
6930 SvUTF8_on(sv);
c07a80fd 6931
923e4eb5 6932 if (IN_PERL_COMPILETIME) {
8bfdd7d9
HS
6933 /* we always read code in line mode */
6934 rsptr = "\n";
6935 rslen = 1;
6936 }
6937 else if (RsSNARF(PL_rs)) {
7a5fa8a2
NIS
6938 /* If it is a regular disk file use size from stat() as estimate
6939 of amount we are going to read - may result in malloc-ing
6940 more memory than we realy need if layers bellow reduce
e468d35b
NIS
6941 size we read (e.g. CRLF or a gzip layer)
6942 */
e311fd51 6943 Stat_t st;
e468d35b
NIS
6944 if (!PerlLIO_fstat(PerlIO_fileno(fp), &st) && S_ISREG(st.st_mode)) {
6945 Off_t offset = PerlIO_tell(fp);
58f1856e 6946 if (offset != (Off_t) -1 && st.st_size + append > offset) {
e468d35b
NIS
6947 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
6948 }
6949 }
c07a80fd 6950 rsptr = NULL;
6951 rslen = 0;
6952 }
3280af22 6953 else if (RsRECORD(PL_rs)) {
e311fd51 6954 I32 bytesread;
5b2b9c68
HM
6955 char *buffer;
6956
6957 /* Grab the size of the record we're getting */
3280af22 6958 recsize = SvIV(SvRV(PL_rs));
e311fd51 6959 buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
5b2b9c68
HM
6960 /* Go yank in */
6961#ifdef VMS
6962 /* VMS wants read instead of fread, because fread doesn't respect */
6963 /* RMS record boundaries. This is not necessarily a good thing to be */
e468d35b
NIS
6964 /* doing, but we've got no other real choice - except avoid stdio
6965 as implementation - perhaps write a :vms layer ?
6966 */
5b2b9c68
HM
6967 bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
6968#else
6969 bytesread = PerlIO_read(fp, buffer, recsize);
6970#endif
27e6ca2d
AE
6971 if (bytesread < 0)
6972 bytesread = 0;
e311fd51 6973 SvCUR_set(sv, bytesread += append);
e670df4e 6974 buffer[bytesread] = '\0';
efd8b2ba 6975 goto return_string_or_null;
5b2b9c68 6976 }
3280af22 6977 else if (RsPARA(PL_rs)) {
c07a80fd 6978 rsptr = "\n\n";
6979 rslen = 2;
8bfdd7d9 6980 rspara = 1;
c07a80fd 6981 }
7d59b7e4
NIS
6982 else {
6983 /* Get $/ i.e. PL_rs into same encoding as stream wants */
6984 if (PerlIO_isutf8(fp)) {
6985 rsptr = SvPVutf8(PL_rs, rslen);
6986 }
6987 else {
6988 if (SvUTF8(PL_rs)) {
6989 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
6990 Perl_croak(aTHX_ "Wide character in $/");
6991 }
6992 }
6993 rsptr = SvPV(PL_rs, rslen);
6994 }
6995 }
6996
c07a80fd 6997 rslast = rslen ? rsptr[rslen - 1] : '\0';
6998
8bfdd7d9 6999 if (rspara) { /* have to do this both before and after */
79072805 7000 do { /* to make sure file boundaries work right */
760ac839 7001 if (PerlIO_eof(fp))
a0d0e21e 7002 return 0;
760ac839 7003 i = PerlIO_getc(fp);
79072805 7004 if (i != '\n') {
a0d0e21e
LW
7005 if (i == -1)
7006 return 0;
760ac839 7007 PerlIO_ungetc(fp,i);
79072805
LW
7008 break;
7009 }
7010 } while (i != EOF);
7011 }
c07a80fd 7012
760ac839
LW
7013 /* See if we know enough about I/O mechanism to cheat it ! */
7014
7015 /* This used to be #ifdef test - it is made run-time test for ease
1c846c1f 7016 of abstracting out stdio interface. One call should be cheap
760ac839
LW
7017 enough here - and may even be a macro allowing compile
7018 time optimization.
7019 */
7020
7021 if (PerlIO_fast_gets(fp)) {
7022
7023 /*
7024 * We're going to steal some values from the stdio struct
7025 * and put EVERYTHING in the innermost loop into registers.
7026 */
7027 register STDCHAR *ptr;
7028 STRLEN bpx;
7029 I32 shortbuffered;
7030
16660edb 7031#if defined(VMS) && defined(PERLIO_IS_STDIO)
7032 /* An ungetc()d char is handled separately from the regular
7033 * buffer, so we getc() it back out and stuff it in the buffer.
7034 */
7035 i = PerlIO_getc(fp);
7036 if (i == EOF) return 0;
7037 *(--((*fp)->_ptr)) = (unsigned char) i;
7038 (*fp)->_cnt++;
7039#endif
c07a80fd 7040
c2960299 7041 /* Here is some breathtakingly efficient cheating */
c07a80fd 7042
a20bf0c3 7043 cnt = PerlIO_get_cnt(fp); /* get count into register */
e468d35b 7044 /* make sure we have the room */
7a5fa8a2 7045 if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
e468d35b 7046 /* Not room for all of it
7a5fa8a2 7047 if we are looking for a separator and room for some
e468d35b
NIS
7048 */
7049 if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
7a5fa8a2 7050 /* just process what we have room for */
79072805
LW
7051 shortbuffered = cnt - SvLEN(sv) + append + 1;
7052 cnt -= shortbuffered;
7053 }
7054 else {
7055 shortbuffered = 0;
bbce6d69 7056 /* remember that cnt can be negative */
eb160463 7057 SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
79072805
LW
7058 }
7059 }
7a5fa8a2 7060 else
79072805 7061 shortbuffered = 0;
c07a80fd 7062 bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */
a20bf0c3 7063 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
16660edb 7064 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7065 "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
16660edb 7066 DEBUG_P(PerlIO_printf(Perl_debug_log,
ba7abf9d 7067 "Screamer: entering: PerlIO * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 7068 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 7069 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
79072805
LW
7070 for (;;) {
7071 screamer:
93a17b20 7072 if (cnt > 0) {
c07a80fd 7073 if (rslen) {
760ac839
LW
7074 while (cnt > 0) { /* this | eat */
7075 cnt--;
c07a80fd 7076 if ((*bp++ = *ptr++) == rslast) /* really | dust */
7077 goto thats_all_folks; /* screams | sed :-) */
7078 }
7079 }
7080 else {
1c846c1f
NIS
7081 Copy(ptr, bp, cnt, char); /* this | eat */
7082 bp += cnt; /* screams | dust */
c07a80fd 7083 ptr += cnt; /* louder | sed :-) */
a5f75d66 7084 cnt = 0;
93a17b20 7085 }
79072805
LW
7086 }
7087
748a9306 7088 if (shortbuffered) { /* oh well, must extend */
79072805
LW
7089 cnt = shortbuffered;
7090 shortbuffered = 0;
c07a80fd 7091 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
79072805
LW
7092 SvCUR_set(sv, bpx);
7093 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
c07a80fd 7094 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
79072805
LW
7095 continue;
7096 }
7097
16660edb 7098 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841
GS
7099 "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n",
7100 PTR2UV(ptr),(long)cnt));
cc00df79 7101 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
ba7abf9d 7102#if 0
16660edb 7103 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7104 "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 7105 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 7106 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
ba7abf9d 7107#endif
1c846c1f 7108 /* This used to call 'filbuf' in stdio form, but as that behaves like
774d564b 7109 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
7110 another abstraction. */
760ac839 7111 i = PerlIO_getc(fp); /* get more characters */
ba7abf9d 7112#if 0
16660edb 7113 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7114 "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 7115 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 7116 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
ba7abf9d 7117#endif
a20bf0c3
JH
7118 cnt = PerlIO_get_cnt(fp);
7119 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
16660edb 7120 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7121 "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
79072805 7122
748a9306
LW
7123 if (i == EOF) /* all done for ever? */
7124 goto thats_really_all_folks;
7125
c07a80fd 7126 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
79072805
LW
7127 SvCUR_set(sv, bpx);
7128 SvGROW(sv, bpx + cnt + 2);
c07a80fd 7129 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
7130
eb160463 7131 *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */
79072805 7132
c07a80fd 7133 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
79072805 7134 goto thats_all_folks;
79072805
LW
7135 }
7136
7137thats_all_folks:
eb160463 7138 if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX(sv)) < rslen) ||
36477c24 7139 memNE((char*)bp - rslen, rsptr, rslen))
760ac839 7140 goto screamer; /* go back to the fray */
79072805
LW
7141thats_really_all_folks:
7142 if (shortbuffered)
7143 cnt += shortbuffered;
16660edb 7144 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7145 "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
cc00df79 7146 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */
16660edb 7147 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 7148 "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 7149 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 7150 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
79072805 7151 *bp = '\0';
760ac839 7152 SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */
16660edb 7153 DEBUG_P(PerlIO_printf(Perl_debug_log,
fb73857a 7154 "Screamer: done, len=%ld, string=|%.*s|\n",
7155 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv)));
760ac839
LW
7156 }
7157 else
79072805 7158 {
6edd2cd5 7159 /*The big, slow, and stupid way. */
27da23d5 7160#ifdef USE_HEAP_INSTEAD_OF_STACK /* Even slower way. */
6edd2cd5
JH
7161 STDCHAR *buf = 0;
7162 New(0, buf, 8192, STDCHAR);
7163 assert(buf);
4d2c4e07 7164#else
6edd2cd5 7165 STDCHAR buf[8192];
4d2c4e07 7166#endif
79072805 7167
760ac839 7168screamer2:
c07a80fd 7169 if (rslen) {
6867be6d 7170 const register STDCHAR *bpe = buf + sizeof(buf);
760ac839 7171 bp = buf;
eb160463 7172 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
760ac839
LW
7173 ; /* keep reading */
7174 cnt = bp - buf;
c07a80fd 7175 }
7176 else {
760ac839 7177 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
16660edb 7178 /* Accomodate broken VAXC compiler, which applies U8 cast to
7179 * both args of ?: operator, causing EOF to change into 255
7180 */
37be0adf 7181 if (cnt > 0)
cbe9e203
JH
7182 i = (U8)buf[cnt - 1];
7183 else
37be0adf 7184 i = EOF;
c07a80fd 7185 }
79072805 7186
cbe9e203
JH
7187 if (cnt < 0)
7188 cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
7189 if (append)
7190 sv_catpvn(sv, (char *) buf, cnt);
7191 else
7192 sv_setpvn(sv, (char *) buf, cnt);
c07a80fd 7193
7194 if (i != EOF && /* joy */
7195 (!rslen ||
7196 SvCUR(sv) < rslen ||
36477c24 7197 memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
79072805
LW
7198 {
7199 append = -1;
63e4d877
CS
7200 /*
7201 * If we're reading from a TTY and we get a short read,
7202 * indicating that the user hit his EOF character, we need
7203 * to notice it now, because if we try to read from the TTY
7204 * again, the EOF condition will disappear.
7205 *
7206 * The comparison of cnt to sizeof(buf) is an optimization
7207 * that prevents unnecessary calls to feof().
7208 *
7209 * - jik 9/25/96
7210 */
7211 if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
7212 goto screamer2;
79072805 7213 }
6edd2cd5 7214
27da23d5 7215#ifdef USE_HEAP_INSTEAD_OF_STACK
6edd2cd5
JH
7216 Safefree(buf);
7217#endif
79072805
LW
7218 }
7219
8bfdd7d9 7220 if (rspara) { /* have to do this both before and after */
c07a80fd 7221 while (i != EOF) { /* to make sure file boundaries work right */
760ac839 7222 i = PerlIO_getc(fp);
79072805 7223 if (i != '\n') {
760ac839 7224 PerlIO_ungetc(fp,i);
79072805
LW
7225 break;
7226 }
7227 }
7228 }
c07a80fd 7229
efd8b2ba 7230return_string_or_null:
c07a80fd 7231 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
79072805
LW
7232}
7233
954c1994
GS
7234/*
7235=for apidoc sv_inc
7236
645c22ef
DM
7237Auto-increment of the value in the SV, doing string to numeric conversion
7238if necessary. Handles 'get' magic.
954c1994
GS
7239
7240=cut
7241*/
7242
79072805 7243void
864dbfa3 7244Perl_sv_inc(pTHX_ register SV *sv)
79072805
LW
7245{
7246 register char *d;
463ee0b2 7247 int flags;
79072805
LW
7248
7249 if (!sv)
7250 return;
b23a5f78
GB
7251 if (SvGMAGICAL(sv))
7252 mg_get(sv);
ed6116ce 7253 if (SvTHINKFIRST(sv)) {
765f542d
NC
7254 if (SvIsCOW(sv))
7255 sv_force_normal_flags(sv, 0);
0f15f207 7256 if (SvREADONLY(sv)) {
923e4eb5 7257 if (IN_PERL_RUNTIME)
cea2e8a9 7258 Perl_croak(aTHX_ PL_no_modify);
0f15f207 7259 }
a0d0e21e 7260 if (SvROK(sv)) {
b5be31e9 7261 IV i;
9e7bc3e8
JD
7262 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
7263 return;
56431972 7264 i = PTR2IV(SvRV(sv));
b5be31e9
SM
7265 sv_unref(sv);
7266 sv_setiv(sv, i);
a0d0e21e 7267 }
ed6116ce 7268 }
8990e307 7269 flags = SvFLAGS(sv);
28e5dec8
JH
7270 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
7271 /* It's (privately or publicly) a float, but not tested as an
7272 integer, so test it to see. */
d460ef45 7273 (void) SvIV(sv);
28e5dec8
JH
7274 flags = SvFLAGS(sv);
7275 }
7276 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
7277 /* It's publicly an integer, or privately an integer-not-float */
59d8ce62 7278#ifdef PERL_PRESERVE_IVUV
28e5dec8 7279 oops_its_int:
59d8ce62 7280#endif
25da4f38
IZ
7281 if (SvIsUV(sv)) {
7282 if (SvUVX(sv) == UV_MAX)
a1e868e7 7283 sv_setnv(sv, UV_MAX_P1);
25da4f38
IZ
7284 else
7285 (void)SvIOK_only_UV(sv);
607fa7f2 7286 SvUV_set(sv, SvUVX(sv) + 1);
25da4f38
IZ
7287 } else {
7288 if (SvIVX(sv) == IV_MAX)
28e5dec8 7289 sv_setuv(sv, (UV)IV_MAX + 1);
25da4f38
IZ
7290 else {
7291 (void)SvIOK_only(sv);
45977657 7292 SvIV_set(sv, SvIVX(sv) + 1);
1c846c1f 7293 }
55497cff 7294 }
79072805
LW
7295 return;
7296 }
28e5dec8
JH
7297 if (flags & SVp_NOK) {
7298 (void)SvNOK_only(sv);
9d6ce603 7299 SvNV_set(sv, SvNVX(sv) + 1.0);
28e5dec8
JH
7300 return;
7301 }
7302
8990e307 7303 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
28e5dec8
JH
7304 if ((flags & SVTYPEMASK) < SVt_PVIV)
7305 sv_upgrade(sv, SVt_IV);
7306 (void)SvIOK_only(sv);
45977657 7307 SvIV_set(sv, 1);
79072805
LW
7308 return;
7309 }
463ee0b2 7310 d = SvPVX(sv);
79072805
LW
7311 while (isALPHA(*d)) d++;
7312 while (isDIGIT(*d)) d++;
7313 if (*d) {
28e5dec8 7314#ifdef PERL_PRESERVE_IVUV
d1be9408 7315 /* Got to punt this as an integer if needs be, but we don't issue
28e5dec8
JH
7316 warnings. Probably ought to make the sv_iv_please() that does
7317 the conversion if possible, and silently. */
c2988b20 7318 int numtype = grok_number(SvPVX(sv), SvCUR(sv), NULL);
28e5dec8
JH
7319 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
7320 /* Need to try really hard to see if it's an integer.
7321 9.22337203685478e+18 is an integer.
7322 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
7323 so $a="9.22337203685478e+18"; $a+0; $a++
7324 needs to be the same as $a="9.22337203685478e+18"; $a++
7325 or we go insane. */
d460ef45 7326
28e5dec8
JH
7327 (void) sv_2iv(sv);
7328 if (SvIOK(sv))
7329 goto oops_its_int;
7330
7331 /* sv_2iv *should* have made this an NV */
7332 if (flags & SVp_NOK) {
7333 (void)SvNOK_only(sv);
9d6ce603 7334 SvNV_set(sv, SvNVX(sv) + 1.0);
28e5dec8
JH
7335 return;
7336 }
7337 /* I don't think we can get here. Maybe I should assert this
7338 And if we do get here I suspect that sv_setnv will croak. NWC
7339 Fall through. */
7340#if defined(USE_LONG_DOUBLE)
7341 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",
7342 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
7343#else
1779d84d 7344 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
28e5dec8
JH
7345 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
7346#endif
7347 }
7348#endif /* PERL_PRESERVE_IVUV */
7349 sv_setnv(sv,Atof(SvPVX(sv)) + 1.0);
79072805
LW
7350 return;
7351 }
7352 d--;
463ee0b2 7353 while (d >= SvPVX(sv)) {
79072805
LW
7354 if (isDIGIT(*d)) {
7355 if (++*d <= '9')
7356 return;
7357 *(d--) = '0';
7358 }
7359 else {
9d116dd7
JH
7360#ifdef EBCDIC
7361 /* MKS: The original code here died if letters weren't consecutive.
7362 * at least it didn't have to worry about non-C locales. The
7363 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
1c846c1f 7364 * arranged in order (although not consecutively) and that only
9d116dd7
JH
7365 * [A-Za-z] are accepted by isALPHA in the C locale.
7366 */
7367 if (*d != 'z' && *d != 'Z') {
7368 do { ++*d; } while (!isALPHA(*d));
7369 return;
7370 }
7371 *(d--) -= 'z' - 'a';
7372#else
79072805
LW
7373 ++*d;
7374 if (isALPHA(*d))
7375 return;
7376 *(d--) -= 'z' - 'a' + 1;
9d116dd7 7377#endif
79072805
LW
7378 }
7379 }
7380 /* oh,oh, the number grew */
7381 SvGROW(sv, SvCUR(sv) + 2);
b162af07 7382 SvCUR_set(sv, SvCUR(sv) + 1);
463ee0b2 7383 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
79072805
LW
7384 *d = d[-1];
7385 if (isDIGIT(d[1]))
7386 *d = '1';
7387 else
7388 *d = d[1];
7389}
7390
954c1994
GS
7391/*
7392=for apidoc sv_dec
7393
645c22ef
DM
7394Auto-decrement of the value in the SV, doing string to numeric conversion
7395if necessary. Handles 'get' magic.
954c1994
GS
7396
7397=cut
7398*/
7399
79072805 7400void
864dbfa3 7401Perl_sv_dec(pTHX_ register SV *sv)
79072805 7402{
463ee0b2
LW
7403 int flags;
7404
79072805
LW
7405 if (!sv)
7406 return;
b23a5f78
GB
7407 if (SvGMAGICAL(sv))
7408 mg_get(sv);
ed6116ce 7409 if (SvTHINKFIRST(sv)) {
765f542d
NC
7410 if (SvIsCOW(sv))
7411 sv_force_normal_flags(sv, 0);
0f15f207 7412 if (SvREADONLY(sv)) {
923e4eb5 7413 if (IN_PERL_RUNTIME)
cea2e8a9 7414 Perl_croak(aTHX_ PL_no_modify);
0f15f207 7415 }
a0d0e21e 7416 if (SvROK(sv)) {
b5be31e9 7417 IV i;
9e7bc3e8
JD
7418 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
7419 return;
56431972 7420 i = PTR2IV(SvRV(sv));
b5be31e9
SM
7421 sv_unref(sv);
7422 sv_setiv(sv, i);
a0d0e21e 7423 }
ed6116ce 7424 }
28e5dec8
JH
7425 /* Unlike sv_inc we don't have to worry about string-never-numbers
7426 and keeping them magic. But we mustn't warn on punting */
8990e307 7427 flags = SvFLAGS(sv);
28e5dec8
JH
7428 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
7429 /* It's publicly an integer, or privately an integer-not-float */
59d8ce62 7430#ifdef PERL_PRESERVE_IVUV
28e5dec8 7431 oops_its_int:
59d8ce62 7432#endif
25da4f38
IZ
7433 if (SvIsUV(sv)) {
7434 if (SvUVX(sv) == 0) {
7435 (void)SvIOK_only(sv);
45977657 7436 SvIV_set(sv, -1);
25da4f38
IZ
7437 }
7438 else {
7439 (void)SvIOK_only_UV(sv);
607fa7f2 7440 SvUV_set(sv, SvUVX(sv) + 1);
1c846c1f 7441 }
25da4f38
IZ
7442 } else {
7443 if (SvIVX(sv) == IV_MIN)
65202027 7444 sv_setnv(sv, (NV)IV_MIN - 1.0);
25da4f38
IZ
7445 else {
7446 (void)SvIOK_only(sv);
45977657 7447 SvIV_set(sv, SvIVX(sv) - 1);
1c846c1f 7448 }
55497cff 7449 }
7450 return;
7451 }
28e5dec8 7452 if (flags & SVp_NOK) {
9d6ce603 7453 SvNV_set(sv, SvNVX(sv) - 1.0);
28e5dec8
JH
7454 (void)SvNOK_only(sv);
7455 return;
7456 }
8990e307 7457 if (!(flags & SVp_POK)) {
4633a7c4
LW
7458 if ((flags & SVTYPEMASK) < SVt_PVNV)
7459 sv_upgrade(sv, SVt_NV);
f599b64b 7460 SvNV_set(sv, 1.0);
a0d0e21e 7461 (void)SvNOK_only(sv);
79072805
LW
7462 return;
7463 }
28e5dec8
JH
7464#ifdef PERL_PRESERVE_IVUV
7465 {
c2988b20 7466 int numtype = grok_number(SvPVX(sv), SvCUR(sv), NULL);
28e5dec8
JH
7467 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
7468 /* Need to try really hard to see if it's an integer.
7469 9.22337203685478e+18 is an integer.
7470 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
7471 so $a="9.22337203685478e+18"; $a+0; $a--
7472 needs to be the same as $a="9.22337203685478e+18"; $a--
7473 or we go insane. */
d460ef45 7474
28e5dec8
JH
7475 (void) sv_2iv(sv);
7476 if (SvIOK(sv))
7477 goto oops_its_int;
7478
7479 /* sv_2iv *should* have made this an NV */
7480 if (flags & SVp_NOK) {
7481 (void)SvNOK_only(sv);
9d6ce603 7482 SvNV_set(sv, SvNVX(sv) - 1.0);
28e5dec8
JH
7483 return;
7484 }
7485 /* I don't think we can get here. Maybe I should assert this
7486 And if we do get here I suspect that sv_setnv will croak. NWC
7487 Fall through. */
7488#if defined(USE_LONG_DOUBLE)
7489 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",
7490 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
7491#else
1779d84d 7492 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"NVgf"\n",
28e5dec8
JH
7493 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
7494#endif
7495 }
7496 }
7497#endif /* PERL_PRESERVE_IVUV */
097ee67d 7498 sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */
79072805
LW
7499}
7500
954c1994
GS
7501/*
7502=for apidoc sv_mortalcopy
7503
645c22ef 7504Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
d4236ebc
DM
7505The new SV is marked as mortal. It will be destroyed "soon", either by an
7506explicit call to FREETMPS, or by an implicit call at places such as
7507statement boundaries. See also C<sv_newmortal> and C<sv_2mortal>.
954c1994
GS
7508
7509=cut
7510*/
7511
79072805
LW
7512/* Make a string that will exist for the duration of the expression
7513 * evaluation. Actually, it may have to last longer than that, but
7514 * hopefully we won't free it until it has been assigned to a
7515 * permanent location. */
7516
7517SV *
864dbfa3 7518Perl_sv_mortalcopy(pTHX_ SV *oldstr)
79072805 7519{
463ee0b2 7520 register SV *sv;
b881518d 7521
4561caa4 7522 new_SV(sv);
79072805 7523 sv_setsv(sv,oldstr);
677b06e3
GS
7524 EXTEND_MORTAL(1);
7525 PL_tmps_stack[++PL_tmps_ix] = sv;
8990e307
LW
7526 SvTEMP_on(sv);
7527 return sv;
7528}
7529
954c1994
GS
7530/*
7531=for apidoc sv_newmortal
7532
645c22ef 7533Creates a new null SV which is mortal. The reference count of the SV is
d4236ebc
DM
7534set to 1. It will be destroyed "soon", either by an explicit call to
7535FREETMPS, or by an implicit call at places such as statement boundaries.
7536See also C<sv_mortalcopy> and C<sv_2mortal>.
954c1994
GS
7537
7538=cut
7539*/
7540
8990e307 7541SV *
864dbfa3 7542Perl_sv_newmortal(pTHX)
8990e307
LW
7543{
7544 register SV *sv;
7545
4561caa4 7546 new_SV(sv);
8990e307 7547 SvFLAGS(sv) = SVs_TEMP;
677b06e3
GS
7548 EXTEND_MORTAL(1);
7549 PL_tmps_stack[++PL_tmps_ix] = sv;
79072805
LW
7550 return sv;
7551}
7552
954c1994
GS
7553/*
7554=for apidoc sv_2mortal
7555
d4236ebc
DM
7556Marks an existing SV as mortal. The SV will be destroyed "soon", either
7557by an explicit call to FREETMPS, or by an implicit call at places such as
37d2ac18
NC
7558statement boundaries. SvTEMP() is turned on which means that the SV's
7559string buffer can be "stolen" if this SV is copied. See also C<sv_newmortal>
7560and C<sv_mortalcopy>.
954c1994
GS
7561
7562=cut
7563*/
7564
79072805 7565SV *
864dbfa3 7566Perl_sv_2mortal(pTHX_ register SV *sv)
79072805 7567{
27da23d5 7568 dVAR;
79072805
LW
7569 if (!sv)
7570 return sv;
d689ffdd 7571 if (SvREADONLY(sv) && SvIMMORTAL(sv))
11162842 7572 return sv;
677b06e3
GS
7573 EXTEND_MORTAL(1);
7574 PL_tmps_stack[++PL_tmps_ix] = sv;
8990e307 7575 SvTEMP_on(sv);
79072805
LW
7576 return sv;
7577}
7578
954c1994
GS
7579/*
7580=for apidoc newSVpv
7581
7582Creates a new SV and copies a string into it. The reference count for the
7583SV is set to 1. If C<len> is zero, Perl will compute the length using
7584strlen(). For efficiency, consider using C<newSVpvn> instead.
7585
7586=cut
7587*/
7588
79072805 7589SV *
864dbfa3 7590Perl_newSVpv(pTHX_ const char *s, STRLEN len)
79072805 7591{
463ee0b2 7592 register SV *sv;
79072805 7593
4561caa4 7594 new_SV(sv);
79072805
LW
7595 if (!len)
7596 len = strlen(s);
7597 sv_setpvn(sv,s,len);
7598 return sv;
7599}
7600
954c1994
GS
7601/*
7602=for apidoc newSVpvn
7603
7604Creates a new SV and copies a string into it. The reference count for the
1c846c1f 7605SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
954c1994 7606string. You are responsible for ensuring that the source string is at least
9e09f5f2 7607C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
954c1994
GS
7608
7609=cut
7610*/
7611
9da1e3b5 7612SV *
864dbfa3 7613Perl_newSVpvn(pTHX_ const char *s, STRLEN len)
9da1e3b5
MUN
7614{
7615 register SV *sv;
7616
7617 new_SV(sv);
9da1e3b5
MUN
7618 sv_setpvn(sv,s,len);
7619 return sv;
7620}
7621
1c846c1f
NIS
7622/*
7623=for apidoc newSVpvn_share
7624
645c22ef
DM
7625Creates a new SV with its SvPVX pointing to a shared string in the string
7626table. If the string does not already exist in the table, it is created
7627first. Turns on READONLY and FAKE. The string's hash is stored in the UV
7628slot of the SV; if the C<hash> parameter is non-zero, that value is used;
7629otherwise the hash is computed. The idea here is that as the string table
7630is used for shared hash keys these strings will have SvPVX == HeKEY and
7631hash lookup will avoid string compare.
1c846c1f
NIS
7632
7633=cut
7634*/
7635
7636SV *
c3654f1a 7637Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
1c846c1f
NIS
7638{
7639 register SV *sv;
c3654f1a
IH
7640 bool is_utf8 = FALSE;
7641 if (len < 0) {
77caf834 7642 STRLEN tmplen = -len;
c3654f1a 7643 is_utf8 = TRUE;
75a54232 7644 /* See the note in hv.c:hv_fetch() --jhi */
e1ec3a88 7645 src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
75a54232
JH
7646 len = tmplen;
7647 }
1c846c1f 7648 if (!hash)
5afd6d42 7649 PERL_HASH(hash, src, len);
1c846c1f
NIS
7650 new_SV(sv);
7651 sv_upgrade(sv, SVt_PVIV);
f880fe2f 7652 SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
b162af07 7653 SvCUR_set(sv, len);
607fa7f2 7654 SvUV_set(sv, hash);
b162af07 7655 SvLEN_set(sv, 0);
1c846c1f
NIS
7656 SvREADONLY_on(sv);
7657 SvFAKE_on(sv);
7658 SvPOK_on(sv);
c3654f1a
IH
7659 if (is_utf8)
7660 SvUTF8_on(sv);
1c846c1f
NIS
7661 return sv;
7662}
7663
645c22ef 7664
cea2e8a9 7665#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
7666
7667/* pTHX_ magic can't cope with varargs, so this is a no-context
7668 * version of the main function, (which may itself be aliased to us).
7669 * Don't access this version directly.
7670 */
7671
46fc3d4c 7672SV *
cea2e8a9 7673Perl_newSVpvf_nocontext(const char* pat, ...)
46fc3d4c 7674{
cea2e8a9 7675 dTHX;
46fc3d4c 7676 register SV *sv;
7677 va_list args;
46fc3d4c 7678 va_start(args, pat);
c5be433b 7679 sv = vnewSVpvf(pat, &args);
46fc3d4c 7680 va_end(args);
7681 return sv;
7682}
cea2e8a9 7683#endif
46fc3d4c 7684
954c1994
GS
7685/*
7686=for apidoc newSVpvf
7687
645c22ef 7688Creates a new SV and initializes it with the string formatted like
954c1994
GS
7689C<sprintf>.
7690
7691=cut
7692*/
7693
cea2e8a9
GS
7694SV *
7695Perl_newSVpvf(pTHX_ const char* pat, ...)
7696{
7697 register SV *sv;
7698 va_list args;
cea2e8a9 7699 va_start(args, pat);
c5be433b 7700 sv = vnewSVpvf(pat, &args);
cea2e8a9
GS
7701 va_end(args);
7702 return sv;
7703}
46fc3d4c 7704
645c22ef
DM
7705/* backend for newSVpvf() and newSVpvf_nocontext() */
7706
79072805 7707SV *
c5be433b
GS
7708Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args)
7709{
7710 register SV *sv;
7711 new_SV(sv);
7712 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
7713 return sv;
7714}
7715
954c1994
GS
7716/*
7717=for apidoc newSVnv
7718
7719Creates a new SV and copies a floating point value into it.
7720The reference count for the SV is set to 1.
7721
7722=cut
7723*/
7724
c5be433b 7725SV *
65202027 7726Perl_newSVnv(pTHX_ NV n)
79072805 7727{
463ee0b2 7728 register SV *sv;
79072805 7729
4561caa4 7730 new_SV(sv);
79072805
LW
7731 sv_setnv(sv,n);
7732 return sv;
7733}
7734
954c1994
GS
7735/*
7736=for apidoc newSViv
7737
7738Creates a new SV and copies an integer into it. The reference count for the
7739SV is set to 1.
7740
7741=cut
7742*/
7743
79072805 7744SV *
864dbfa3 7745Perl_newSViv(pTHX_ IV i)
79072805 7746{
463ee0b2 7747 register SV *sv;
79072805 7748
4561caa4 7749 new_SV(sv);
79072805
LW
7750 sv_setiv(sv,i);
7751 return sv;
7752}
7753
954c1994 7754/*
1a3327fb
JH
7755=for apidoc newSVuv
7756
7757Creates a new SV and copies an unsigned integer into it.
7758The reference count for the SV is set to 1.
7759
7760=cut
7761*/
7762
7763SV *
7764Perl_newSVuv(pTHX_ UV u)
7765{
7766 register SV *sv;
7767
7768 new_SV(sv);
7769 sv_setuv(sv,u);
7770 return sv;
7771}
7772
7773/*
954c1994
GS
7774=for apidoc newRV_noinc
7775
7776Creates an RV wrapper for an SV. The reference count for the original
7777SV is B<not> incremented.
7778
7779=cut
7780*/
7781
2304df62 7782SV *
864dbfa3 7783Perl_newRV_noinc(pTHX_ SV *tmpRef)
2304df62
AD
7784{
7785 register SV *sv;
7786
4561caa4 7787 new_SV(sv);
2304df62 7788 sv_upgrade(sv, SVt_RV);
76e3520e 7789 SvTEMP_off(tmpRef);
b162af07 7790 SvRV_set(sv, tmpRef);
2304df62 7791 SvROK_on(sv);
2304df62
AD
7792 return sv;
7793}
7794
ff276b08 7795/* newRV_inc is the official function name to use now.
645c22ef
DM
7796 * newRV_inc is in fact #defined to newRV in sv.h
7797 */
7798
5f05dabc 7799SV *
864dbfa3 7800Perl_newRV(pTHX_ SV *tmpRef)
5f05dabc 7801{
5f6447b6 7802 return newRV_noinc(SvREFCNT_inc(tmpRef));
5f05dabc 7803}
5f05dabc 7804
954c1994
GS
7805/*
7806=for apidoc newSVsv
7807
7808Creates a new SV which is an exact duplicate of the original SV.
645c22ef 7809(Uses C<sv_setsv>).
954c1994
GS
7810
7811=cut
7812*/
7813
79072805 7814SV *
864dbfa3 7815Perl_newSVsv(pTHX_ register SV *old)
79072805 7816{
463ee0b2 7817 register SV *sv;
79072805
LW
7818
7819 if (!old)
7820 return Nullsv;
8990e307 7821 if (SvTYPE(old) == SVTYPEMASK) {
0453d815 7822 if (ckWARN_d(WARN_INTERNAL))
9014280d 7823 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
79072805
LW
7824 return Nullsv;
7825 }
4561caa4 7826 new_SV(sv);
e90aabeb
NC
7827 /* SV_GMAGIC is the default for sv_setv()
7828 SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games
7829 with SvTEMP_off and SvTEMP_on round a call to sv_setsv. */
7830 sv_setsv_flags(sv, old, SV_GMAGIC | SV_NOSTEAL);
463ee0b2 7831 return sv;
79072805
LW
7832}
7833
645c22ef
DM
7834/*
7835=for apidoc sv_reset
7836
7837Underlying implementation for the C<reset> Perl function.
7838Note that the perl-level function is vaguely deprecated.
7839
7840=cut
7841*/
7842
79072805 7843void
e1ec3a88 7844Perl_sv_reset(pTHX_ register const char *s, HV *stash)
79072805 7845{
27da23d5 7846 dVAR;
79072805
LW
7847 register HE *entry;
7848 register GV *gv;
7849 register SV *sv;
7850 register I32 i;
7851 register PMOP *pm;
7852 register I32 max;
4802d5d7 7853 char todo[PERL_UCHAR_MAX+1];
79072805 7854
49d8d3a1
MB
7855 if (!stash)
7856 return;
7857
79072805
LW
7858 if (!*s) { /* reset ?? searches */
7859 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
48c036b1 7860 pm->op_pmdynflags &= ~PMdf_USED;
79072805
LW
7861 }
7862 return;
7863 }
7864
7865 /* reset variables */
7866
7867 if (!HvARRAY(stash))
7868 return;
463ee0b2
LW
7869
7870 Zero(todo, 256, char);
79072805 7871 while (*s) {
4802d5d7 7872 i = (unsigned char)*s;
79072805
LW
7873 if (s[1] == '-') {
7874 s += 2;
7875 }
4802d5d7 7876 max = (unsigned char)*s++;
79072805 7877 for ( ; i <= max; i++) {
463ee0b2
LW
7878 todo[i] = 1;
7879 }
a0d0e21e 7880 for (i = 0; i <= (I32) HvMAX(stash); i++) {
79072805 7881 for (entry = HvARRAY(stash)[i];
9e35f4b3
GS
7882 entry;
7883 entry = HeNEXT(entry))
7884 {
1edc1566 7885 if (!todo[(U8)*HeKEY(entry)])
463ee0b2 7886 continue;
1edc1566 7887 gv = (GV*)HeVAL(entry);
79072805 7888 sv = GvSV(gv);
9e35f4b3
GS
7889 if (SvTHINKFIRST(sv)) {
7890 if (!SvREADONLY(sv) && SvROK(sv))
7891 sv_unref(sv);
7892 continue;
7893 }
0c34ef67 7894 SvOK_off(sv);
79072805
LW
7895 if (SvTYPE(sv) >= SVt_PV) {
7896 SvCUR_set(sv, 0);
463ee0b2
LW
7897 if (SvPVX(sv) != Nullch)
7898 *SvPVX(sv) = '\0';
44a8e56a 7899 SvTAINT(sv);
79072805
LW
7900 }
7901 if (GvAV(gv)) {
7902 av_clear(GvAV(gv));
7903 }
44a8e56a 7904 if (GvHV(gv) && !HvNAME(GvHV(gv))) {
463ee0b2 7905 hv_clear(GvHV(gv));
2f42fcb0 7906#ifndef PERL_MICRO
fa6a1c44 7907#ifdef USE_ENVIRON_ARRAY
4efc5df6
GS
7908 if (gv == PL_envgv
7909# ifdef USE_ITHREADS
7910 && PL_curinterp == aTHX
7911# endif
7912 )
7913 {
79072805 7914 environ[0] = Nullch;
4efc5df6 7915 }
a0d0e21e 7916#endif
2f42fcb0 7917#endif /* !PERL_MICRO */
79072805
LW
7918 }
7919 }
7920 }
7921 }
7922}
7923
645c22ef
DM
7924/*
7925=for apidoc sv_2io
7926
7927Using various gambits, try to get an IO from an SV: the IO slot if its a
7928GV; or the recursive result if we're an RV; or the IO slot of the symbol
7929named after the PV if we're a string.
7930
7931=cut
7932*/
7933
46fc3d4c 7934IO*
864dbfa3 7935Perl_sv_2io(pTHX_ SV *sv)
46fc3d4c 7936{
7937 IO* io;
7938 GV* gv;
7939
7940 switch (SvTYPE(sv)) {
7941 case SVt_PVIO:
7942 io = (IO*)sv;
7943 break;
7944 case SVt_PVGV:
7945 gv = (GV*)sv;
7946 io = GvIO(gv);
7947 if (!io)
cea2e8a9 7948 Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
46fc3d4c 7949 break;
7950 default:
7951 if (!SvOK(sv))
cea2e8a9 7952 Perl_croak(aTHX_ PL_no_usym, "filehandle");
46fc3d4c 7953 if (SvROK(sv))
7954 return sv_2io(SvRV(sv));
7a5fd60d 7955 gv = gv_fetchsv(sv, FALSE, SVt_PVIO);
46fc3d4c 7956 if (gv)
7957 io = GvIO(gv);
7958 else
7959 io = 0;
7960 if (!io)
35c1215d 7961 Perl_croak(aTHX_ "Bad filehandle: %"SVf, sv);
46fc3d4c 7962 break;
7963 }
7964 return io;
7965}
7966
645c22ef
DM
7967/*
7968=for apidoc sv_2cv
7969
7970Using various gambits, try to get a CV from an SV; in addition, try if
7971possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
7972
7973=cut
7974*/
7975
79072805 7976CV *
864dbfa3 7977Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref)
79072805 7978{
27da23d5 7979 dVAR;
c04a4dfe
JH
7980 GV *gv = Nullgv;
7981 CV *cv = Nullcv;
79072805
LW
7982
7983 if (!sv)
93a17b20 7984 return *gvp = Nullgv, Nullcv;
79072805 7985 switch (SvTYPE(sv)) {
79072805
LW
7986 case SVt_PVCV:
7987 *st = CvSTASH(sv);
7988 *gvp = Nullgv;
7989 return (CV*)sv;
7990 case SVt_PVHV:
7991 case SVt_PVAV:
7992 *gvp = Nullgv;
7993 return Nullcv;
8990e307
LW
7994 case SVt_PVGV:
7995 gv = (GV*)sv;
a0d0e21e 7996 *gvp = gv;
8990e307
LW
7997 *st = GvESTASH(gv);
7998 goto fix_gv;
7999
79072805 8000 default:
a0d0e21e
LW
8001 if (SvGMAGICAL(sv))
8002 mg_get(sv);
8003 if (SvROK(sv)) {
f5284f61
IZ
8004 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
8005 tryAMAGICunDEREF(to_cv);
8006
62f274bf
GS
8007 sv = SvRV(sv);
8008 if (SvTYPE(sv) == SVt_PVCV) {
8009 cv = (CV*)sv;
8010 *gvp = Nullgv;
8011 *st = CvSTASH(cv);
8012 return cv;
8013 }
8014 else if(isGV(sv))
8015 gv = (GV*)sv;
8016 else
cea2e8a9 8017 Perl_croak(aTHX_ "Not a subroutine reference");
a0d0e21e 8018 }
62f274bf 8019 else if (isGV(sv))
79072805
LW
8020 gv = (GV*)sv;
8021 else
7a5fd60d 8022 gv = gv_fetchsv(sv, lref, SVt_PVCV);
79072805
LW
8023 *gvp = gv;
8024 if (!gv)
8025 return Nullcv;
8026 *st = GvESTASH(gv);
8990e307 8027 fix_gv:
8ebc5c01 8028 if (lref && !GvCVu(gv)) {
4633a7c4 8029 SV *tmpsv;
748a9306 8030 ENTER;
4633a7c4 8031 tmpsv = NEWSV(704,0);
16660edb 8032 gv_efullname3(tmpsv, gv, Nullch);
f6ec51f7
GS
8033 /* XXX this is probably not what they think they're getting.
8034 * It has the same effect as "sub name;", i.e. just a forward
8035 * declaration! */
774d564b 8036 newSUB(start_subparse(FALSE, 0),
4633a7c4
LW
8037 newSVOP(OP_CONST, 0, tmpsv),
8038 Nullop,
8990e307 8039 Nullop);
748a9306 8040 LEAVE;
8ebc5c01 8041 if (!GvCVu(gv))
35c1215d
NC
8042 Perl_croak(aTHX_ "Unable to create sub named \"%"SVf"\"",
8043 sv);
8990e307 8044 }
8ebc5c01 8045 return GvCVu(gv);
79072805
LW
8046 }
8047}
8048
c461cf8f
JH
8049/*
8050=for apidoc sv_true
8051
8052Returns true if the SV has a true value by Perl's rules.
645c22ef
DM
8053Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
8054instead use an in-line version.
c461cf8f
JH
8055
8056=cut
8057*/
8058
79072805 8059I32
864dbfa3 8060Perl_sv_true(pTHX_ register SV *sv)
79072805 8061{
8990e307
LW
8062 if (!sv)
8063 return 0;
79072805 8064 if (SvPOK(sv)) {
e1ec3a88 8065 const register XPV* tXpv;
4e35701f 8066 if ((tXpv = (XPV*)SvANY(sv)) &&
c2f1de04 8067 (tXpv->xpv_cur > 1 ||
4e35701f 8068 (tXpv->xpv_cur && *tXpv->xpv_pv != '0')))
79072805
LW
8069 return 1;
8070 else
8071 return 0;
8072 }
8073 else {
8074 if (SvIOK(sv))
463ee0b2 8075 return SvIVX(sv) != 0;
79072805
LW
8076 else {
8077 if (SvNOK(sv))
463ee0b2 8078 return SvNVX(sv) != 0.0;
79072805 8079 else
463ee0b2 8080 return sv_2bool(sv);
79072805
LW
8081 }
8082 }
8083}
79072805 8084
645c22ef
DM
8085/*
8086=for apidoc sv_iv
8087
8088A private implementation of the C<SvIVx> macro for compilers which can't
8089cope with complex macro expressions. Always use the macro instead.
8090
8091=cut
8092*/
8093
ff68c719 8094IV
864dbfa3 8095Perl_sv_iv(pTHX_ register SV *sv)
85e6fe83 8096{
25da4f38
IZ
8097 if (SvIOK(sv)) {
8098 if (SvIsUV(sv))
8099 return (IV)SvUVX(sv);
ff68c719 8100 return SvIVX(sv);
25da4f38 8101 }
ff68c719 8102 return sv_2iv(sv);
85e6fe83 8103}
85e6fe83 8104
645c22ef
DM
8105/*
8106=for apidoc sv_uv
8107
8108A private implementation of the C<SvUVx> macro for compilers which can't
8109cope with complex macro expressions. Always use the macro instead.
8110
8111=cut
8112*/
8113
ff68c719 8114UV
864dbfa3 8115Perl_sv_uv(pTHX_ register SV *sv)
ff68c719 8116{
25da4f38
IZ
8117 if (SvIOK(sv)) {
8118 if (SvIsUV(sv))
8119 return SvUVX(sv);
8120 return (UV)SvIVX(sv);
8121 }
ff68c719 8122 return sv_2uv(sv);
8123}
85e6fe83 8124
645c22ef
DM
8125/*
8126=for apidoc sv_nv
8127
8128A private implementation of the C<SvNVx> macro for compilers which can't
8129cope with complex macro expressions. Always use the macro instead.
8130
8131=cut
8132*/
8133
65202027 8134NV
864dbfa3 8135Perl_sv_nv(pTHX_ register SV *sv)
79072805 8136{
ff68c719 8137 if (SvNOK(sv))
8138 return SvNVX(sv);
8139 return sv_2nv(sv);
79072805 8140}
79072805 8141
09540bc3
JH
8142/* sv_pv() is now a macro using SvPV_nolen();
8143 * this function provided for binary compatibility only
8144 */
8145
8146char *
8147Perl_sv_pv(pTHX_ SV *sv)
8148{
8149 STRLEN n_a;
8150
8151 if (SvPOK(sv))
8152 return SvPVX(sv);
8153
8154 return sv_2pv(sv, &n_a);
8155}
8156
645c22ef
DM
8157/*
8158=for apidoc sv_pv
8159
baca2b92 8160Use the C<SvPV_nolen> macro instead
645c22ef 8161
645c22ef
DM
8162=for apidoc sv_pvn
8163
8164A private implementation of the C<SvPV> macro for compilers which can't
8165cope with complex macro expressions. Always use the macro instead.
8166
8167=cut
8168*/
8169
1fa8b10d 8170char *
864dbfa3 8171Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp)
79072805 8172{
85e6fe83
LW
8173 if (SvPOK(sv)) {
8174 *lp = SvCUR(sv);
a0d0e21e 8175 return SvPVX(sv);
85e6fe83 8176 }
463ee0b2 8177 return sv_2pv(sv, lp);
79072805 8178}
79072805 8179
6e9d1081
NC
8180
8181char *
8182Perl_sv_pvn_nomg(pTHX_ register SV *sv, STRLEN *lp)
8183{
8184 if (SvPOK(sv)) {
8185 *lp = SvCUR(sv);
8186 return SvPVX(sv);
8187 }
8188 return sv_2pv_flags(sv, lp, 0);
8189}
8190
09540bc3
JH
8191/* sv_pvn_force() is now a macro using Perl_sv_pvn_force_flags();
8192 * this function provided for binary compatibility only
8193 */
8194
8195char *
8196Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp)
8197{
8198 return sv_pvn_force_flags(sv, lp, SV_GMAGIC);
8199}
8200
c461cf8f
JH
8201/*
8202=for apidoc sv_pvn_force
8203
8204Get a sensible string out of the SV somehow.
645c22ef
DM
8205A private implementation of the C<SvPV_force> macro for compilers which
8206can't cope with complex macro expressions. Always use the macro instead.
c461cf8f 8207
8d6d96c1
HS
8208=for apidoc sv_pvn_force_flags
8209
8210Get a sensible string out of the SV somehow.
8211If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if
8212appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
8213implemented in terms of this function.
645c22ef
DM
8214You normally want to use the various wrapper macros instead: see
8215C<SvPV_force> and C<SvPV_force_nomg>
8d6d96c1
HS
8216
8217=cut
8218*/
8219
8220char *
8221Perl_sv_pvn_force_flags(pTHX_ SV *sv, STRLEN *lp, I32 flags)
8222{
c04a4dfe 8223 char *s = NULL;
a0d0e21e 8224
6fc92669 8225 if (SvTHINKFIRST(sv) && !SvROK(sv))
765f542d 8226 sv_force_normal_flags(sv, 0);
1c846c1f 8227
a0d0e21e
LW
8228 if (SvPOK(sv)) {
8229 *lp = SvCUR(sv);
8230 }
8231 else {
748a9306 8232 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
cea2e8a9 8233 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
53e06cf0 8234 OP_NAME(PL_op));
a0d0e21e 8235 }
4633a7c4 8236 else
8d6d96c1 8237 s = sv_2pv_flags(sv, lp, flags);
a0d0e21e
LW
8238 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
8239 STRLEN len = *lp;
1c846c1f 8240
a0d0e21e
LW
8241 if (SvROK(sv))
8242 sv_unref(sv);
8243 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
8244 SvGROW(sv, len + 1);
8245 Move(s,SvPVX(sv),len,char);
8246 SvCUR_set(sv, len);
8247 *SvEND(sv) = '\0';
8248 }
8249 if (!SvPOK(sv)) {
8250 SvPOK_on(sv); /* validate pointer */
8251 SvTAINT(sv);
1d7c1841
GS
8252 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
8253 PTR2UV(sv),SvPVX(sv)));
a0d0e21e
LW
8254 }
8255 }
8256 return SvPVX(sv);
8257}
8258
09540bc3
JH
8259/* sv_pvbyte () is now a macro using Perl_sv_2pv_flags();
8260 * this function provided for binary compatibility only
8261 */
8262
8263char *
8264Perl_sv_pvbyte(pTHX_ SV *sv)
8265{
8266 sv_utf8_downgrade(sv,0);
8267 return sv_pv(sv);
8268}
8269
645c22ef
DM
8270/*
8271=for apidoc sv_pvbyte
8272
baca2b92 8273Use C<SvPVbyte_nolen> instead.
645c22ef 8274
645c22ef
DM
8275=for apidoc sv_pvbyten
8276
8277A private implementation of the C<SvPVbyte> macro for compilers
8278which can't cope with complex macro expressions. Always use the macro
8279instead.
8280
8281=cut
8282*/
8283
7340a771
GS
8284char *
8285Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp)
8286{
ffebcc3e 8287 sv_utf8_downgrade(sv,0);
7340a771
GS
8288 return sv_pvn(sv,lp);
8289}
8290
645c22ef
DM
8291/*
8292=for apidoc sv_pvbyten_force
8293
8294A private implementation of the C<SvPVbytex_force> macro for compilers
8295which can't cope with complex macro expressions. Always use the macro
8296instead.
8297
8298=cut
8299*/
8300
7340a771
GS
8301char *
8302Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp)
8303{
46ec2f14 8304 sv_pvn_force(sv,lp);
ffebcc3e 8305 sv_utf8_downgrade(sv,0);
46ec2f14
TS
8306 *lp = SvCUR(sv);
8307 return SvPVX(sv);
7340a771
GS
8308}
8309
09540bc3
JH
8310/* sv_pvutf8 () is now a macro using Perl_sv_2pv_flags();
8311 * this function provided for binary compatibility only
8312 */
8313
8314char *
8315Perl_sv_pvutf8(pTHX_ SV *sv)
8316{
8317 sv_utf8_upgrade(sv);
8318 return sv_pv(sv);
8319}
8320
645c22ef
DM
8321/*
8322=for apidoc sv_pvutf8
8323
baca2b92 8324Use the C<SvPVutf8_nolen> macro instead
645c22ef 8325
645c22ef
DM
8326=for apidoc sv_pvutf8n
8327
8328A private implementation of the C<SvPVutf8> macro for compilers
8329which can't cope with complex macro expressions. Always use the macro
8330instead.
8331
8332=cut
8333*/
8334
7340a771
GS
8335char *
8336Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp)
8337{
560a288e 8338 sv_utf8_upgrade(sv);
7340a771
GS
8339 return sv_pvn(sv,lp);
8340}
8341
c461cf8f
JH
8342/*
8343=for apidoc sv_pvutf8n_force
8344
645c22ef
DM
8345A private implementation of the C<SvPVutf8_force> macro for compilers
8346which can't cope with complex macro expressions. Always use the macro
8347instead.
c461cf8f
JH
8348
8349=cut
8350*/
8351
7340a771
GS
8352char *
8353Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp)
8354{
46ec2f14 8355 sv_pvn_force(sv,lp);
560a288e 8356 sv_utf8_upgrade(sv);
46ec2f14
TS
8357 *lp = SvCUR(sv);
8358 return SvPVX(sv);
7340a771
GS
8359}
8360
c461cf8f
JH
8361/*
8362=for apidoc sv_reftype
8363
8364Returns a string describing what the SV is a reference to.
8365
8366=cut
8367*/
8368
1cb0ed9b 8369char *
bfed75c6 8370Perl_sv_reftype(pTHX_ const SV *sv, int ob)
a0d0e21e 8371{
07409e01
NC
8372 /* The fact that I don't need to downcast to char * everywhere, only in ?:
8373 inside return suggests a const propagation bug in g++. */
c86bf373 8374 if (ob && SvOBJECT(sv)) {
1cb0ed9b 8375 char *name = HvNAME(SvSTASH(sv));
07409e01 8376 return name ? name : (char *) "__ANON__";
c86bf373 8377 }
a0d0e21e
LW
8378 else {
8379 switch (SvTYPE(sv)) {
8380 case SVt_NULL:
8381 case SVt_IV:
8382 case SVt_NV:
8383 case SVt_RV:
8384 case SVt_PV:
8385 case SVt_PVIV:
8386 case SVt_PVNV:
8387 case SVt_PVMG:
8388 case SVt_PVBM:
1cb0ed9b 8389 if (SvVOK(sv))
439cb1c4 8390 return "VSTRING";
a0d0e21e
LW
8391 if (SvROK(sv))
8392 return "REF";
8393 else
8394 return "SCALAR";
1cb0ed9b 8395
07409e01 8396 case SVt_PVLV: return (char *) (SvROK(sv) ? "REF"
be65207d
DM
8397 /* tied lvalues should appear to be
8398 * scalars for backwards compatitbility */
8399 : (LvTYPE(sv) == 't' || LvTYPE(sv) == 'T')
07409e01 8400 ? "SCALAR" : "LVALUE");
a0d0e21e
LW
8401 case SVt_PVAV: return "ARRAY";
8402 case SVt_PVHV: return "HASH";
8403 case SVt_PVCV: return "CODE";
8404 case SVt_PVGV: return "GLOB";
1d2dff63 8405 case SVt_PVFM: return "FORMAT";
27f9d8f3 8406 case SVt_PVIO: return "IO";
a0d0e21e
LW
8407 default: return "UNKNOWN";
8408 }
8409 }
8410}
8411
954c1994
GS
8412/*
8413=for apidoc sv_isobject
8414
8415Returns a boolean indicating whether the SV is an RV pointing to a blessed
8416object. If the SV is not an RV, or if the object is not blessed, then this
8417will return false.
8418
8419=cut
8420*/
8421
463ee0b2 8422int
864dbfa3 8423Perl_sv_isobject(pTHX_ SV *sv)
85e6fe83 8424{
68dc0745 8425 if (!sv)
8426 return 0;
8427 if (SvGMAGICAL(sv))
8428 mg_get(sv);
85e6fe83
LW
8429 if (!SvROK(sv))
8430 return 0;
8431 sv = (SV*)SvRV(sv);
8432 if (!SvOBJECT(sv))
8433 return 0;
8434 return 1;
8435}
8436
954c1994
GS
8437/*
8438=for apidoc sv_isa
8439
8440Returns a boolean indicating whether the SV is blessed into the specified
8441class. This does not check for subtypes; use C<sv_derived_from> to verify
8442an inheritance relationship.
8443
8444=cut
8445*/
8446
85e6fe83 8447int
864dbfa3 8448Perl_sv_isa(pTHX_ SV *sv, const char *name)
463ee0b2 8449{
68dc0745 8450 if (!sv)
8451 return 0;
8452 if (SvGMAGICAL(sv))
8453 mg_get(sv);
ed6116ce 8454 if (!SvROK(sv))
463ee0b2 8455 return 0;
ed6116ce
LW
8456 sv = (SV*)SvRV(sv);
8457 if (!SvOBJECT(sv))
463ee0b2 8458 return 0;
e27ad1f2
AV
8459 if (!HvNAME(SvSTASH(sv)))
8460 return 0;
463ee0b2
LW
8461
8462 return strEQ(HvNAME(SvSTASH(sv)), name);
8463}
8464
954c1994
GS
8465/*
8466=for apidoc newSVrv
8467
8468Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then
8469it will be upgraded to one. If C<classname> is non-null then the new SV will
8470be blessed in the specified package. The new SV is returned and its
8471reference count is 1.
8472
8473=cut
8474*/
8475
463ee0b2 8476SV*
864dbfa3 8477Perl_newSVrv(pTHX_ SV *rv, const char *classname)
463ee0b2 8478{
463ee0b2
LW
8479 SV *sv;
8480
4561caa4 8481 new_SV(sv);
51cf62d8 8482
765f542d 8483 SV_CHECK_THINKFIRST_COW_DROP(rv);
51cf62d8 8484 SvAMAGIC_off(rv);
51cf62d8 8485
0199fce9
JD
8486 if (SvTYPE(rv) >= SVt_PVMG) {
8487 U32 refcnt = SvREFCNT(rv);
8488 SvREFCNT(rv) = 0;
8489 sv_clear(rv);
8490 SvFLAGS(rv) = 0;
8491 SvREFCNT(rv) = refcnt;
8492 }
8493
51cf62d8 8494 if (SvTYPE(rv) < SVt_RV)
0199fce9
JD
8495 sv_upgrade(rv, SVt_RV);
8496 else if (SvTYPE(rv) > SVt_RV) {
8bd4d4c5 8497 SvPV_free(rv);
0199fce9
JD
8498 SvCUR_set(rv, 0);
8499 SvLEN_set(rv, 0);
8500 }
51cf62d8 8501
0c34ef67 8502 SvOK_off(rv);
b162af07 8503 SvRV_set(rv, sv);
ed6116ce 8504 SvROK_on(rv);
463ee0b2 8505
a0d0e21e
LW
8506 if (classname) {
8507 HV* stash = gv_stashpv(classname, TRUE);
8508 (void)sv_bless(rv, stash);
8509 }
8510 return sv;
8511}
8512
954c1994
GS
8513/*
8514=for apidoc sv_setref_pv
8515
8516Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
8517argument will be upgraded to an RV. That RV will be modified to point to
8518the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed
8519into the SV. The C<classname> argument indicates the package for the
8520blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
d34c2299 8521will have a reference count of 1, and the RV will be returned.
954c1994
GS
8522
8523Do not use with other Perl types such as HV, AV, SV, CV, because those
8524objects will become corrupted by the pointer copy process.
8525
8526Note that C<sv_setref_pvn> copies the string while this copies the pointer.
8527
8528=cut
8529*/
8530
a0d0e21e 8531SV*
864dbfa3 8532Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv)
a0d0e21e 8533{
189b2af5 8534 if (!pv) {
3280af22 8535 sv_setsv(rv, &PL_sv_undef);
189b2af5
GS
8536 SvSETMAGIC(rv);
8537 }
a0d0e21e 8538 else
56431972 8539 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
a0d0e21e
LW
8540 return rv;
8541}
8542
954c1994
GS
8543/*
8544=for apidoc sv_setref_iv
8545
8546Copies an integer into a new SV, optionally blessing the SV. The C<rv>
8547argument will be upgraded to an RV. That RV will be modified to point to
8548the new SV. The C<classname> argument indicates the package for the
8549blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
d34c2299 8550will have a reference count of 1, and the RV will be returned.
954c1994
GS
8551
8552=cut
8553*/
8554
a0d0e21e 8555SV*
864dbfa3 8556Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv)
a0d0e21e
LW
8557{
8558 sv_setiv(newSVrv(rv,classname), iv);
8559 return rv;
8560}
8561
954c1994 8562/*
e1c57cef
JH
8563=for apidoc sv_setref_uv
8564
8565Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
8566argument will be upgraded to an RV. That RV will be modified to point to
8567the new SV. The C<classname> argument indicates the package for the
8568blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
d34c2299 8569will have a reference count of 1, and the RV will be returned.
e1c57cef
JH
8570
8571=cut
8572*/
8573
8574SV*
8575Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv)
8576{
8577 sv_setuv(newSVrv(rv,classname), uv);
8578 return rv;
8579}
8580
8581/*
954c1994
GS
8582=for apidoc sv_setref_nv
8583
8584Copies a double into a new SV, optionally blessing the SV. The C<rv>
8585argument will be upgraded to an RV. That RV will be modified to point to
8586the new SV. The C<classname> argument indicates the package for the
8587blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
d34c2299 8588will have a reference count of 1, and the RV will be returned.
954c1994
GS
8589
8590=cut
8591*/
8592
a0d0e21e 8593SV*
65202027 8594Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv)
a0d0e21e
LW
8595{
8596 sv_setnv(newSVrv(rv,classname), nv);
8597 return rv;
8598}
463ee0b2 8599
954c1994
GS
8600/*
8601=for apidoc sv_setref_pvn
8602
8603Copies a string into a new SV, optionally blessing the SV. The length of the
8604string must be specified with C<n>. The C<rv> argument will be upgraded to
8605an RV. That RV will be modified to point to the new SV. The C<classname>
8606argument indicates the package for the blessing. Set C<classname> to
7a5fa8a2 8607C<Nullch> to avoid the blessing. The new SV will have a reference count
d34c2299 8608of 1, and the RV will be returned.
954c1994
GS
8609
8610Note that C<sv_setref_pv> copies the pointer while this copies the string.
8611
8612=cut
8613*/
8614
a0d0e21e 8615SV*
864dbfa3 8616Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n)
a0d0e21e
LW
8617{
8618 sv_setpvn(newSVrv(rv,classname), pv, n);
463ee0b2
LW
8619 return rv;
8620}
8621
954c1994
GS
8622/*
8623=for apidoc sv_bless
8624
8625Blesses an SV into a specified package. The SV must be an RV. The package
8626must be designated by its stash (see C<gv_stashpv()>). The reference count
8627of the SV is unaffected.
8628
8629=cut
8630*/
8631
a0d0e21e 8632SV*
864dbfa3 8633Perl_sv_bless(pTHX_ SV *sv, HV *stash)
a0d0e21e 8634{
76e3520e 8635 SV *tmpRef;
a0d0e21e 8636 if (!SvROK(sv))
cea2e8a9 8637 Perl_croak(aTHX_ "Can't bless non-reference value");
76e3520e
GS
8638 tmpRef = SvRV(sv);
8639 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
8640 if (SvREADONLY(tmpRef))
cea2e8a9 8641 Perl_croak(aTHX_ PL_no_modify);
76e3520e
GS
8642 if (SvOBJECT(tmpRef)) {
8643 if (SvTYPE(tmpRef) != SVt_PVIO)
3280af22 8644 --PL_sv_objcount;
76e3520e 8645 SvREFCNT_dec(SvSTASH(tmpRef));
2e3febc6 8646 }
a0d0e21e 8647 }
76e3520e
GS
8648 SvOBJECT_on(tmpRef);
8649 if (SvTYPE(tmpRef) != SVt_PVIO)
3280af22 8650 ++PL_sv_objcount;
76e3520e 8651 (void)SvUPGRADE(tmpRef, SVt_PVMG);
b162af07 8652 SvSTASH_set(tmpRef, (HV*)SvREFCNT_inc(stash));
a0d0e21e 8653
2e3febc6
CS
8654 if (Gv_AMG(stash))
8655 SvAMAGIC_on(sv);
8656 else
8657 SvAMAGIC_off(sv);
a0d0e21e 8658
1edbfb88
AB
8659 if(SvSMAGICAL(tmpRef))
8660 if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
8661 mg_set(tmpRef);
8662
8663
ecdeb87c 8664
a0d0e21e
LW
8665 return sv;
8666}
8667
645c22ef 8668/* Downgrades a PVGV to a PVMG.
645c22ef
DM
8669 */
8670
76e3520e 8671STATIC void
cea2e8a9 8672S_sv_unglob(pTHX_ SV *sv)
a0d0e21e 8673{
850fabdf
GS
8674 void *xpvmg;
8675
a0d0e21e
LW
8676 assert(SvTYPE(sv) == SVt_PVGV);
8677 SvFAKE_off(sv);
8678 if (GvGP(sv))
1edc1566 8679 gp_free((GV*)sv);
e826b3c7
GS
8680 if (GvSTASH(sv)) {
8681 SvREFCNT_dec(GvSTASH(sv));
8682 GvSTASH(sv) = Nullhv;
8683 }
14befaf4 8684 sv_unmagic(sv, PERL_MAGIC_glob);
a0d0e21e 8685 Safefree(GvNAME(sv));
a5f75d66 8686 GvMULTI_off(sv);
850fabdf
GS
8687
8688 /* need to keep SvANY(sv) in the right arena */
8689 xpvmg = new_XPVMG();
8690 StructCopy(SvANY(sv), xpvmg, XPVMG);
8691 del_XPVGV(SvANY(sv));
8692 SvANY(sv) = xpvmg;
8693
a0d0e21e
LW
8694 SvFLAGS(sv) &= ~SVTYPEMASK;
8695 SvFLAGS(sv) |= SVt_PVMG;
8696}
8697
954c1994 8698/*
840a7b70 8699=for apidoc sv_unref_flags
954c1994
GS
8700
8701Unsets the RV status of the SV, and decrements the reference count of
8702whatever was being referenced by the RV. This can almost be thought of
840a7b70
IZ
8703as a reversal of C<newSVrv>. The C<cflags> argument can contain
8704C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
8705(otherwise the decrementing is conditional on the reference count being
8706different from one or the reference being a readonly SV).
7889fe52 8707See C<SvROK_off>.
954c1994
GS
8708
8709=cut
8710*/
8711
ed6116ce 8712void
840a7b70 8713Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags)
ed6116ce 8714{
a0d0e21e 8715 SV* rv = SvRV(sv);
810b8aa5
GS
8716
8717 if (SvWEAKREF(sv)) {
8718 sv_del_backref(sv);
8719 SvWEAKREF_off(sv);
b162af07 8720 SvRV_set(sv, NULL);
810b8aa5
GS
8721 return;
8722 }
b162af07 8723 SvRV_set(sv, NULL);
ed6116ce 8724 SvROK_off(sv);
04ca4930
NC
8725 /* You can't have a || SvREADONLY(rv) here, as $a = $$a, where $a was
8726 assigned to as BEGIN {$a = \"Foo"} will fail. */
8727 if (SvREFCNT(rv) != 1 || (flags & SV_IMMEDIATE_UNREF))
4633a7c4 8728 SvREFCNT_dec(rv);
840a7b70 8729 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
4633a7c4 8730 sv_2mortal(rv); /* Schedule for freeing later */
ed6116ce 8731}
8990e307 8732
840a7b70
IZ
8733/*
8734=for apidoc sv_unref
8735
8736Unsets the RV status of the SV, and decrements the reference count of
8737whatever was being referenced by the RV. This can almost be thought of
8738as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag>
7889fe52 8739being zero. See C<SvROK_off>.
840a7b70
IZ
8740
8741=cut
8742*/
8743
8744void
8745Perl_sv_unref(pTHX_ SV *sv)
8746{
8747 sv_unref_flags(sv, 0);
8748}
8749
645c22ef
DM
8750/*
8751=for apidoc sv_taint
8752
8753Taint an SV. Use C<SvTAINTED_on> instead.
8754=cut
8755*/
8756
bbce6d69 8757void
864dbfa3 8758Perl_sv_taint(pTHX_ SV *sv)
bbce6d69 8759{
14befaf4 8760 sv_magic((sv), Nullsv, PERL_MAGIC_taint, Nullch, 0);
bbce6d69 8761}
8762
645c22ef
DM
8763/*
8764=for apidoc sv_untaint
8765
8766Untaint an SV. Use C<SvTAINTED_off> instead.
8767=cut
8768*/
8769
bbce6d69 8770void
864dbfa3 8771Perl_sv_untaint(pTHX_ SV *sv)
bbce6d69 8772{
13f57bf8 8773 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
14befaf4 8774 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
36477c24 8775 if (mg)
565764a8 8776 mg->mg_len &= ~1;
36477c24 8777 }
bbce6d69 8778}
8779
645c22ef
DM
8780/*
8781=for apidoc sv_tainted
8782
8783Test an SV for taintedness. Use C<SvTAINTED> instead.
8784=cut
8785*/
8786
bbce6d69 8787bool
864dbfa3 8788Perl_sv_tainted(pTHX_ SV *sv)
bbce6d69 8789{
13f57bf8 8790 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
14befaf4 8791 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
155aba94 8792 if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv)))
36477c24 8793 return TRUE;
8794 }
8795 return FALSE;
bbce6d69 8796}
8797
09540bc3
JH
8798/*
8799=for apidoc sv_setpviv
8800
8801Copies an integer into the given SV, also updating its string value.
8802Does not handle 'set' magic. See C<sv_setpviv_mg>.
8803
8804=cut
8805*/
8806
8807void
8808Perl_sv_setpviv(pTHX_ SV *sv, IV iv)
8809{
8810 char buf[TYPE_CHARS(UV)];
8811 char *ebuf;
8812 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8813
8814 sv_setpvn(sv, ptr, ebuf - ptr);
8815}
8816
8817/*
8818=for apidoc sv_setpviv_mg
8819
8820Like C<sv_setpviv>, but also handles 'set' magic.
8821
8822=cut
8823*/
8824
8825void
8826Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv)
8827{
8828 char buf[TYPE_CHARS(UV)];
8829 char *ebuf;
8830 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
8831
8832 sv_setpvn(sv, ptr, ebuf - ptr);
8833 SvSETMAGIC(sv);
8834}
8835
cea2e8a9 8836#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
8837
8838/* pTHX_ magic can't cope with varargs, so this is a no-context
8839 * version of the main function, (which may itself be aliased to us).
8840 * Don't access this version directly.
8841 */
8842
cea2e8a9
GS
8843void
8844Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...)
8845{
8846 dTHX;
8847 va_list args;
8848 va_start(args, pat);
c5be433b 8849 sv_vsetpvf(sv, pat, &args);
cea2e8a9
GS
8850 va_end(args);
8851}
8852
645c22ef
DM
8853/* pTHX_ magic can't cope with varargs, so this is a no-context
8854 * version of the main function, (which may itself be aliased to us).
8855 * Don't access this version directly.
8856 */
cea2e8a9
GS
8857
8858void
8859Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...)
8860{
8861 dTHX;
8862 va_list args;
8863 va_start(args, pat);
c5be433b 8864 sv_vsetpvf_mg(sv, pat, &args);
cea2e8a9 8865 va_end(args);
cea2e8a9
GS
8866}
8867#endif
8868
954c1994
GS
8869/*
8870=for apidoc sv_setpvf
8871
bffc3d17
SH
8872Works like C<sv_catpvf> but copies the text into the SV instead of
8873appending it. Does not handle 'set' magic. See C<sv_setpvf_mg>.
954c1994
GS
8874
8875=cut
8876*/
8877
46fc3d4c 8878void
864dbfa3 8879Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...)
46fc3d4c 8880{
8881 va_list args;
46fc3d4c 8882 va_start(args, pat);
c5be433b 8883 sv_vsetpvf(sv, pat, &args);
46fc3d4c 8884 va_end(args);
8885}
8886
bffc3d17
SH
8887/*
8888=for apidoc sv_vsetpvf
8889
8890Works like C<sv_vcatpvf> but copies the text into the SV instead of
8891appending it. Does not handle 'set' magic. See C<sv_vsetpvf_mg>.
8892
8893Usually used via its frontend C<sv_setpvf>.
8894
8895=cut
8896*/
645c22ef 8897
c5be433b
GS
8898void
8899Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args)
8900{
8901 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
8902}
ef50df4b 8903
954c1994
GS
8904/*
8905=for apidoc sv_setpvf_mg
8906
8907Like C<sv_setpvf>, but also handles 'set' magic.
8908
8909=cut
8910*/
8911
ef50df4b 8912void
864dbfa3 8913Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...)
ef50df4b
GS
8914{
8915 va_list args;
ef50df4b 8916 va_start(args, pat);
c5be433b 8917 sv_vsetpvf_mg(sv, pat, &args);
ef50df4b 8918 va_end(args);
c5be433b
GS
8919}
8920
bffc3d17
SH
8921/*
8922=for apidoc sv_vsetpvf_mg
8923
8924Like C<sv_vsetpvf>, but also handles 'set' magic.
8925
8926Usually used via its frontend C<sv_setpvf_mg>.
8927
8928=cut
8929*/
645c22ef 8930
c5be433b
GS
8931void
8932Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
8933{
8934 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
ef50df4b
GS
8935 SvSETMAGIC(sv);
8936}
8937
cea2e8a9 8938#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
8939
8940/* pTHX_ magic can't cope with varargs, so this is a no-context
8941 * version of the main function, (which may itself be aliased to us).
8942 * Don't access this version directly.
8943 */
8944
cea2e8a9
GS
8945void
8946Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...)
8947{
8948 dTHX;
8949 va_list args;
8950 va_start(args, pat);
c5be433b 8951 sv_vcatpvf(sv, pat, &args);
cea2e8a9
GS
8952 va_end(args);
8953}
8954
645c22ef
DM
8955/* pTHX_ magic can't cope with varargs, so this is a no-context
8956 * version of the main function, (which may itself be aliased to us).
8957 * Don't access this version directly.
8958 */
8959
cea2e8a9
GS
8960void
8961Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...)
8962{
8963 dTHX;
8964 va_list args;
8965 va_start(args, pat);
c5be433b 8966 sv_vcatpvf_mg(sv, pat, &args);
cea2e8a9 8967 va_end(args);
cea2e8a9
GS
8968}
8969#endif
8970
954c1994
GS
8971/*
8972=for apidoc sv_catpvf
8973
d5ce4a7c
GA
8974Processes its arguments like C<sprintf> and appends the formatted
8975output to an SV. If the appended data contains "wide" characters
8976(including, but not limited to, SVs with a UTF-8 PV formatted with %s,
8977and characters >255 formatted with %c), the original SV might get
bffc3d17 8978upgraded to UTF-8. Handles 'get' magic, but not 'set' magic. See
cdd94ca7
NC
8979C<sv_catpvf_mg>. If the original SV was UTF-8, the pattern should be
8980valid UTF-8; if the original SV was bytes, the pattern should be too.
954c1994 8981
d5ce4a7c 8982=cut */
954c1994 8983
46fc3d4c 8984void
864dbfa3 8985Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...)
46fc3d4c 8986{
8987 va_list args;
46fc3d4c 8988 va_start(args, pat);
c5be433b 8989 sv_vcatpvf(sv, pat, &args);
46fc3d4c 8990 va_end(args);
8991}
8992
bffc3d17
SH
8993/*
8994=for apidoc sv_vcatpvf
8995
8996Processes its arguments like C<vsprintf> and appends the formatted output
8997to an SV. Does not handle 'set' magic. See C<sv_vcatpvf_mg>.
8998
8999Usually used via its frontend C<sv_catpvf>.
9000
9001=cut
9002*/
645c22ef 9003
ef50df4b 9004void
c5be433b
GS
9005Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args)
9006{
9007 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
9008}
9009
954c1994
GS
9010/*
9011=for apidoc sv_catpvf_mg
9012
9013Like C<sv_catpvf>, but also handles 'set' magic.
9014
9015=cut
9016*/
9017
c5be433b 9018void
864dbfa3 9019Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...)
ef50df4b
GS
9020{
9021 va_list args;
ef50df4b 9022 va_start(args, pat);
c5be433b 9023 sv_vcatpvf_mg(sv, pat, &args);
ef50df4b 9024 va_end(args);
c5be433b
GS
9025}
9026
bffc3d17
SH
9027/*
9028=for apidoc sv_vcatpvf_mg
9029
9030Like C<sv_vcatpvf>, but also handles 'set' magic.
9031
9032Usually used via its frontend C<sv_catpvf_mg>.
9033
9034=cut
9035*/
645c22ef 9036
c5be433b
GS
9037void
9038Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
9039{
9040 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
ef50df4b
GS
9041 SvSETMAGIC(sv);
9042}
9043
954c1994
GS
9044/*
9045=for apidoc sv_vsetpvfn
9046
bffc3d17 9047Works like C<sv_vcatpvfn> but copies the text into the SV instead of
954c1994
GS
9048appending it.
9049
bffc3d17 9050Usually used via one of its frontends C<sv_vsetpvf> and C<sv_vsetpvf_mg>.
645c22ef 9051
954c1994
GS
9052=cut
9053*/
9054
46fc3d4c 9055void
7d5ea4e7 9056Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
46fc3d4c 9057{
9058 sv_setpvn(sv, "", 0);
7d5ea4e7 9059 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted);
46fc3d4c 9060}
9061
645c22ef
DM
9062/* private function for use in sv_vcatpvfn via the EXPECT_NUMBER macro */
9063
2d00ba3b 9064STATIC I32
9dd79c3f 9065S_expect_number(pTHX_ char** pattern)
211dfcf1
HS
9066{
9067 I32 var = 0;
9068 switch (**pattern) {
9069 case '1': case '2': case '3':
9070 case '4': case '5': case '6':
9071 case '7': case '8': case '9':
9072 while (isDIGIT(**pattern))
9073 var = var * 10 + (*(*pattern)++ - '0');
9074 }
9075 return var;
9076}
9dd79c3f 9077#define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern))
211dfcf1 9078
4151a5fe
IZ
9079static char *
9080F0convert(NV nv, char *endbuf, STRLEN *len)
9081{
9082 int neg = nv < 0;
9083 UV uv;
9084 char *p = endbuf;
9085
9086 if (neg)
9087 nv = -nv;
9088 if (nv < UV_MAX) {
9089 nv += 0.5;
028f8eaa 9090 uv = (UV)nv;
4151a5fe
IZ
9091 if (uv & 1 && uv == nv)
9092 uv--; /* Round to even */
9093 do {
9094 unsigned dig = uv % 10;
9095 *--p = '0' + dig;
9096 } while (uv /= 10);
9097 if (neg)
9098 *--p = '-';
9099 *len = endbuf - p;
9100 return p;
9101 }
9102 return Nullch;
9103}
9104
9105
954c1994
GS
9106/*
9107=for apidoc sv_vcatpvfn
9108
9109Processes its arguments like C<vsprintf> and appends the formatted output
9110to an SV. Uses an array of SVs if the C style variable argument list is
9111missing (NULL). When running with taint checks enabled, indicates via
9112C<maybe_tainted> if results are untrustworthy (often due to the use of
9113locales).
9114
bffc3d17 9115Usually used via one of its frontends C<sv_vcatpvf> and C<sv_vcatpvf_mg>.
645c22ef 9116
954c1994
GS
9117=cut
9118*/
9119
1ef29b0e
RGS
9120/* XXX maybe_tainted is never assigned to, so the doc above is lying. */
9121
46fc3d4c 9122void
7d5ea4e7 9123Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
46fc3d4c 9124{
9125 char *p;
9126 char *q;
9127 char *patend;
fc36a67e 9128 STRLEN origlen;
46fc3d4c 9129 I32 svix = 0;
27da23d5 9130 static const char nullstr[] = "(null)";
9c5ffd7c 9131 SV *argsv = Nullsv;
db79b45b
JH
9132 bool has_utf8; /* has the result utf8? */
9133 bool pat_utf8; /* the pattern is in utf8? */
9134 SV *nsv = Nullsv;
4151a5fe
IZ
9135 /* Times 4: a decimal digit takes more than 3 binary digits.
9136 * NV_DIG: mantissa takes than many decimal digits.
9137 * Plus 32: Playing safe. */
9138 char ebuf[IV_DIG * 4 + NV_DIG + 32];
9139 /* large enough for "%#.#f" --chip */
9140 /* what about long double NVs? --jhi */
db79b45b
JH
9141
9142 has_utf8 = pat_utf8 = DO_UTF8(sv);
46fc3d4c 9143
9144 /* no matter what, this is a string now */
fc36a67e 9145 (void)SvPV_force(sv, origlen);
46fc3d4c 9146
fc36a67e 9147 /* special-case "", "%s", and "%_" */
46fc3d4c 9148 if (patlen == 0)
9149 return;
fc36a67e 9150 if (patlen == 2 && pat[0] == '%') {
9151 switch (pat[1]) {
9152 case 's':
c635e13b 9153 if (args) {
73d840c0 9154 const char *s = va_arg(*args, char*);
c635e13b 9155 sv_catpv(sv, s ? s : nullstr);
9156 }
7e2040f0 9157 else if (svix < svmax) {
fc36a67e 9158 sv_catsv(sv, *svargs);
7e2040f0
GS
9159 if (DO_UTF8(*svargs))
9160 SvUTF8_on(sv);
9161 }
fc36a67e 9162 return;
9163 case '_':
9164 if (args) {
7e2040f0
GS
9165 argsv = va_arg(*args, SV*);
9166 sv_catsv(sv, argsv);
9167 if (DO_UTF8(argsv))
9168 SvUTF8_on(sv);
fc36a67e 9169 return;
9170 }
9171 /* See comment on '_' below */
9172 break;
9173 }
46fc3d4c 9174 }
9175
1d917b39 9176#ifndef USE_LONG_DOUBLE
4151a5fe
IZ
9177 /* special-case "%.<number>[gf]" */
9178 if ( patlen <= 5 && pat[0] == '%' && pat[1] == '.'
9179 && (pat[patlen-1] == 'g' || pat[patlen-1] == 'f') ) {
9180 unsigned digits = 0;
9181 const char *pp;
9182
9183 pp = pat + 2;
9184 while (*pp >= '0' && *pp <= '9')
9185 digits = 10 * digits + (*pp++ - '0');
028f8eaa 9186 if (pp - pat == (int)patlen - 1) {
4151a5fe
IZ
9187 NV nv;
9188
9189 if (args)
9190 nv = (NV)va_arg(*args, double);
9191 else if (svix < svmax)
9192 nv = SvNV(*svargs);
9193 else
9194 return;
9195 if (*pp == 'g') {
2873255c
NC
9196 /* Add check for digits != 0 because it seems that some
9197 gconverts are buggy in this case, and we don't yet have
9198 a Configure test for this. */
9199 if (digits && digits < sizeof(ebuf) - NV_DIG - 10) {
9200 /* 0, point, slack */
2e59c212 9201 Gconvert(nv, (int)digits, 0, ebuf);
4151a5fe
IZ
9202 sv_catpv(sv, ebuf);
9203 if (*ebuf) /* May return an empty string for digits==0 */
9204 return;
9205 }
9206 } else if (!digits) {
9207 STRLEN l;
9208
9209 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
9210 sv_catpvn(sv, p, l);
9211 return;
9212 }
9213 }
9214 }
9215 }
1d917b39 9216#endif /* !USE_LONG_DOUBLE */
4151a5fe 9217
2cf2cfc6 9218 if (!args && svix < svmax && DO_UTF8(*svargs))
205f51d8 9219 has_utf8 = TRUE;
2cf2cfc6 9220
46fc3d4c 9221 patend = (char*)pat + patlen;
9222 for (p = (char*)pat; p < patend; p = q) {
9223 bool alt = FALSE;
9224 bool left = FALSE;
b22c7a20 9225 bool vectorize = FALSE;
211dfcf1 9226 bool vectorarg = FALSE;
2cf2cfc6 9227 bool vec_utf8 = FALSE;
46fc3d4c 9228 char fill = ' ';
9229 char plus = 0;
9230 char intsize = 0;
9231 STRLEN width = 0;
fc36a67e 9232 STRLEN zeros = 0;
46fc3d4c 9233 bool has_precis = FALSE;
9234 STRLEN precis = 0;
58e33a90 9235 I32 osvix = svix;
2cf2cfc6 9236 bool is_utf8 = FALSE; /* is this item utf8? */
20f6aaab
AS
9237#ifdef HAS_LDBL_SPRINTF_BUG
9238 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
205f51d8 9239 with sfio - Allen <allens@cpan.org> */
20f6aaab
AS
9240 bool fix_ldbl_sprintf_bug = FALSE;
9241#endif
205f51d8 9242
46fc3d4c 9243 char esignbuf[4];
89ebb4a3 9244 U8 utf8buf[UTF8_MAXBYTES+1];
46fc3d4c 9245 STRLEN esignlen = 0;
9246
9247 char *eptr = Nullch;
fc36a67e 9248 STRLEN elen = 0;
81f715da 9249 SV *vecsv = Nullsv;
a05b299f 9250 U8 *vecstr = Null(U8*);
b22c7a20 9251 STRLEN veclen = 0;
934abaf1 9252 char c = 0;
46fc3d4c 9253 int i;
9c5ffd7c 9254 unsigned base = 0;
8c8eb53c
RB
9255 IV iv = 0;
9256 UV uv = 0;
9e5b023a
JH
9257 /* we need a long double target in case HAS_LONG_DOUBLE but
9258 not USE_LONG_DOUBLE
9259 */
35fff930 9260#if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE
9e5b023a
JH
9261 long double nv;
9262#else
65202027 9263 NV nv;
9e5b023a 9264#endif
46fc3d4c 9265 STRLEN have;
9266 STRLEN need;
9267 STRLEN gap;
e1ec3a88 9268 const char *dotstr = ".";
b22c7a20 9269 STRLEN dotstrlen = 1;
211dfcf1 9270 I32 efix = 0; /* explicit format parameter index */
eb3fce90 9271 I32 ewix = 0; /* explicit width index */
211dfcf1
HS
9272 I32 epix = 0; /* explicit precision index */
9273 I32 evix = 0; /* explicit vector index */
eb3fce90 9274 bool asterisk = FALSE;
46fc3d4c 9275
211dfcf1 9276 /* echo everything up to the next format specification */
46fc3d4c 9277 for (q = p; q < patend && *q != '%'; ++q) ;
9278 if (q > p) {
db79b45b
JH
9279 if (has_utf8 && !pat_utf8)
9280 sv_catpvn_utf8_upgrade(sv, p, q - p, nsv);
9281 else
9282 sv_catpvn(sv, p, q - p);
46fc3d4c 9283 p = q;
9284 }
9285 if (q++ >= patend)
9286 break;
9287
211dfcf1
HS
9288/*
9289 We allow format specification elements in this order:
9290 \d+\$ explicit format parameter index
9291 [-+ 0#]+ flags
a472f209 9292 v|\*(\d+\$)?v vector with optional (optionally specified) arg
f3583277 9293 0 flag (as above): repeated to allow "v02"
211dfcf1
HS
9294 \d+|\*(\d+\$)? width using optional (optionally specified) arg
9295 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
9296 [hlqLV] size
9297 [%bcdefginopsux_DFOUX] format (mandatory)
9298*/
9299 if (EXPECT_NUMBER(q, width)) {
9300 if (*q == '$') {
9301 ++q;
9302 efix = width;
9303 } else {
9304 goto gotwidth;
9305 }
9306 }
9307
fc36a67e 9308 /* FLAGS */
9309
46fc3d4c 9310 while (*q) {
9311 switch (*q) {
9312 case ' ':
9313 case '+':
9314 plus = *q++;
9315 continue;
9316
9317 case '-':
9318 left = TRUE;
9319 q++;
9320 continue;
9321
9322 case '0':
9323 fill = *q++;
9324 continue;
9325
9326 case '#':
9327 alt = TRUE;
9328 q++;
9329 continue;
9330
fc36a67e 9331 default:
9332 break;
9333 }
9334 break;
9335 }
46fc3d4c 9336
211dfcf1 9337 tryasterisk:
eb3fce90 9338 if (*q == '*') {
211dfcf1
HS
9339 q++;
9340 if (EXPECT_NUMBER(q, ewix))
9341 if (*q++ != '$')
9342 goto unknown;
eb3fce90 9343 asterisk = TRUE;
211dfcf1
HS
9344 }
9345 if (*q == 'v') {
eb3fce90 9346 q++;
211dfcf1
HS
9347 if (vectorize)
9348 goto unknown;
9cbac4c7 9349 if ((vectorarg = asterisk)) {
211dfcf1
HS
9350 evix = ewix;
9351 ewix = 0;
9352 asterisk = FALSE;
9353 }
9354 vectorize = TRUE;
9355 goto tryasterisk;
eb3fce90
JH
9356 }
9357
211dfcf1 9358 if (!asterisk)
7a5fa8a2 9359 if( *q == '0' )
f3583277 9360 fill = *q++;
211dfcf1
HS
9361 EXPECT_NUMBER(q, width);
9362
9363 if (vectorize) {
9364 if (vectorarg) {
9365 if (args)
9366 vecsv = va_arg(*args, SV*);
9367 else
9368 vecsv = (evix ? evix <= svmax : svix < svmax) ?
3a7a539e 9369 svargs[evix ? evix-1 : svix++] : &PL_sv_undef;
4459522c 9370 dotstr = SvPVx(vecsv, dotstrlen);
211dfcf1 9371 if (DO_UTF8(vecsv))
2cf2cfc6 9372 is_utf8 = TRUE;
211dfcf1
HS
9373 }
9374 if (args) {
9375 vecsv = va_arg(*args, SV*);
9376 vecstr = (U8*)SvPVx(vecsv,veclen);
2cf2cfc6 9377 vec_utf8 = DO_UTF8(vecsv);
eb3fce90 9378 }
211dfcf1
HS
9379 else if (efix ? efix <= svmax : svix < svmax) {
9380 vecsv = svargs[efix ? efix-1 : svix++];
9381 vecstr = (U8*)SvPVx(vecsv,veclen);
2cf2cfc6 9382 vec_utf8 = DO_UTF8(vecsv);
d7aa5382
JP
9383 /* if this is a version object, we need to return the
9384 * stringified representation (which the SvPVX has
9385 * already done for us), but not vectorize the args
9386 */
9387 if ( *q == 'd' && sv_derived_from(vecsv,"version") )
9388 {
9389 q++; /* skip past the rest of the %vd format */
da6068d9 9390 eptr = (char *) vecstr;
d7aa5382
JP
9391 elen = strlen(eptr);
9392 vectorize=FALSE;
9393 goto string;
9394 }
211dfcf1
HS
9395 }
9396 else {
9397 vecstr = (U8*)"";
9398 veclen = 0;
9399 }
eb3fce90 9400 }
fc36a67e 9401
eb3fce90 9402 if (asterisk) {
fc36a67e 9403 if (args)
9404 i = va_arg(*args, int);
9405 else
eb3fce90
JH
9406 i = (ewix ? ewix <= svmax : svix < svmax) ?
9407 SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
fc36a67e 9408 left |= (i < 0);
9409 width = (i < 0) ? -i : i;
fc36a67e 9410 }
211dfcf1 9411 gotwidth:
fc36a67e 9412
9413 /* PRECISION */
46fc3d4c 9414
fc36a67e 9415 if (*q == '.') {
9416 q++;
9417 if (*q == '*') {
211dfcf1 9418 q++;
7b8dd722
HS
9419 if (EXPECT_NUMBER(q, epix) && *q++ != '$')
9420 goto unknown;
9421 /* XXX: todo, support specified precision parameter */
9422 if (epix)
211dfcf1 9423 goto unknown;
46fc3d4c 9424 if (args)
9425 i = va_arg(*args, int);
9426 else
eb3fce90
JH
9427 i = (ewix ? ewix <= svmax : svix < svmax)
9428 ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
fc36a67e 9429 precis = (i < 0) ? 0 : i;
fc36a67e 9430 }
9431 else {
9432 precis = 0;
9433 while (isDIGIT(*q))
9434 precis = precis * 10 + (*q++ - '0');
9435 }
9436 has_precis = TRUE;
9437 }
46fc3d4c 9438
fc36a67e 9439 /* SIZE */
46fc3d4c 9440
fc36a67e 9441 switch (*q) {
c623ac67
GS
9442#ifdef WIN32
9443 case 'I': /* Ix, I32x, and I64x */
9444# ifdef WIN64
9445 if (q[1] == '6' && q[2] == '4') {
9446 q += 3;
9447 intsize = 'q';
9448 break;
9449 }
9450# endif
9451 if (q[1] == '3' && q[2] == '2') {
9452 q += 3;
9453 break;
9454 }
9455# ifdef WIN64
9456 intsize = 'q';
9457# endif
9458 q++;
9459 break;
9460#endif
9e5b023a 9461#if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
6f9bb7fd 9462 case 'L': /* Ld */
e5c81feb 9463 /* FALL THROUGH */
e5c81feb 9464#ifdef HAS_QUAD
6f9bb7fd 9465 case 'q': /* qd */
9e5b023a 9466#endif
6f9bb7fd
GS
9467 intsize = 'q';
9468 q++;
9469 break;
9470#endif
fc36a67e 9471 case 'l':
9e5b023a 9472#if defined(HAS_QUAD) || defined(HAS_LONG_DOUBLE)
205f51d8 9473 if (*(q + 1) == 'l') { /* lld, llf */
fc36a67e 9474 intsize = 'q';
9475 q += 2;
46fc3d4c 9476 break;
cf2093f6 9477 }
fc36a67e 9478#endif
6f9bb7fd 9479 /* FALL THROUGH */
fc36a67e 9480 case 'h':
cf2093f6 9481 /* FALL THROUGH */
fc36a67e 9482 case 'V':
9483 intsize = *q++;
46fc3d4c 9484 break;
9485 }
9486
fc36a67e 9487 /* CONVERSION */
9488
211dfcf1
HS
9489 if (*q == '%') {
9490 eptr = q++;
9491 elen = 1;
9492 goto string;
9493 }
9494
be75b157
HS
9495 if (vectorize)
9496 argsv = vecsv;
9497 else if (!args)
211dfcf1
HS
9498 argsv = (efix ? efix <= svmax : svix < svmax) ?
9499 svargs[efix ? efix-1 : svix++] : &PL_sv_undef;
9500
46fc3d4c 9501 switch (c = *q++) {
9502
9503 /* STRINGS */
9504
46fc3d4c 9505 case 'c':
be75b157 9506 uv = (args && !vectorize) ? va_arg(*args, int) : SvIVx(argsv);
1bd104fb
JH
9507 if ((uv > 255 ||
9508 (!UNI_IS_INVARIANT(uv) && SvUTF8(sv)))
0064a8a9 9509 && !IN_BYTES) {
dfe13c55 9510 eptr = (char*)utf8buf;
9041c2e3 9511 elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf;
2cf2cfc6 9512 is_utf8 = TRUE;
7e2040f0
GS
9513 }
9514 else {
9515 c = (char)uv;
9516 eptr = &c;
9517 elen = 1;
a0ed51b3 9518 }
46fc3d4c 9519 goto string;
9520
46fc3d4c 9521 case 's':
be75b157 9522 if (args && !vectorize) {
fc36a67e 9523 eptr = va_arg(*args, char*);
c635e13b 9524 if (eptr)
1d7c1841
GS
9525#ifdef MACOS_TRADITIONAL
9526 /* On MacOS, %#s format is used for Pascal strings */
9527 if (alt)
9528 elen = *eptr++;
9529 else
9530#endif
c635e13b 9531 elen = strlen(eptr);
9532 else {
27da23d5 9533 eptr = (char *)nullstr;
c635e13b 9534 elen = sizeof nullstr - 1;
9535 }
46fc3d4c 9536 }
211dfcf1 9537 else {
7e2040f0
GS
9538 eptr = SvPVx(argsv, elen);
9539 if (DO_UTF8(argsv)) {
a0ed51b3
LW
9540 if (has_precis && precis < elen) {
9541 I32 p = precis;
7e2040f0 9542 sv_pos_u2b(argsv, &p, 0); /* sticks at end */
a0ed51b3
LW
9543 precis = p;
9544 }
9545 if (width) { /* fudge width (can't fudge elen) */
7e2040f0 9546 width += elen - sv_len_utf8(argsv);
a0ed51b3 9547 }
2cf2cfc6 9548 is_utf8 = TRUE;
a0ed51b3
LW
9549 }
9550 }
46fc3d4c 9551 goto string;
9552
fc36a67e 9553 case '_':
5df617be
RB
9554#ifdef CHECK_FORMAT
9555 format_sv:
9556#endif
fc36a67e 9557 /*
9558 * The "%_" hack might have to be changed someday,
9559 * if ISO or ANSI decide to use '_' for something.
9560 * So we keep it hidden from users' code.
9561 */
be75b157 9562 if (!args || vectorize)
fc36a67e 9563 goto unknown;
211dfcf1 9564 argsv = va_arg(*args, SV*);
7e2040f0
GS
9565 eptr = SvPVx(argsv, elen);
9566 if (DO_UTF8(argsv))
2cf2cfc6 9567 is_utf8 = TRUE;
fc36a67e 9568
46fc3d4c 9569 string:
b22c7a20 9570 vectorize = FALSE;
46fc3d4c 9571 if (has_precis && elen > precis)
9572 elen = precis;
9573 break;
9574
9575 /* INTEGERS */
9576
fc36a67e 9577 case 'p':
5df617be
RB
9578#ifdef CHECK_FORMAT
9579 if (left) {
9580 left = FALSE;
57f5baf2
RB
9581 if (!width)
9582 goto format_sv; /* %-p -> %_ */
57f5baf2
RB
9583 precis = width;
9584 has_precis = TRUE;
9585 width = 0;
9586 goto format_sv; /* %-Np -> %.N_ */
5df617be
RB
9587 }
9588#endif
be75b157 9589 if (alt || vectorize)
c2e66d9e 9590 goto unknown;
211dfcf1 9591 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
fc36a67e 9592 base = 16;
9593 goto integer;
9594
46fc3d4c 9595 case 'D':
29fe7a80 9596#ifdef IV_IS_QUAD
22f3ae8c 9597 intsize = 'q';
29fe7a80 9598#else
46fc3d4c 9599 intsize = 'l';
29fe7a80 9600#endif
46fc3d4c 9601 /* FALL THROUGH */
9602 case 'd':
9603 case 'i':
b22c7a20 9604 if (vectorize) {
ba210ebe 9605 STRLEN ulen;
211dfcf1
HS
9606 if (!veclen)
9607 continue;
2cf2cfc6
A
9608 if (vec_utf8)
9609 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
9610 UTF8_ALLOW_ANYUV);
b22c7a20 9611 else {
e83d50c9 9612 uv = *vecstr;
b22c7a20
GS
9613 ulen = 1;
9614 }
9615 vecstr += ulen;
9616 veclen -= ulen;
e83d50c9
JP
9617 if (plus)
9618 esignbuf[esignlen++] = plus;
b22c7a20
GS
9619 }
9620 else if (args) {
46fc3d4c 9621 switch (intsize) {
9622 case 'h': iv = (short)va_arg(*args, int); break;
46fc3d4c 9623 case 'l': iv = va_arg(*args, long); break;
fc36a67e 9624 case 'V': iv = va_arg(*args, IV); break;
b10c0dba 9625 default: iv = va_arg(*args, int); break;
cf2093f6
JH
9626#ifdef HAS_QUAD
9627 case 'q': iv = va_arg(*args, Quad_t); break;
9628#endif
46fc3d4c 9629 }
9630 }
9631 else {
b10c0dba 9632 IV tiv = SvIVx(argsv); /* work around GCC bug #13488 */
46fc3d4c 9633 switch (intsize) {
b10c0dba
MHM
9634 case 'h': iv = (short)tiv; break;
9635 case 'l': iv = (long)tiv; break;
9636 case 'V':
9637 default: iv = tiv; break;
cf2093f6 9638#ifdef HAS_QUAD
b10c0dba 9639 case 'q': iv = (Quad_t)tiv; break;
cf2093f6 9640#endif
46fc3d4c 9641 }
9642 }
e83d50c9
JP
9643 if ( !vectorize ) /* we already set uv above */
9644 {
9645 if (iv >= 0) {
9646 uv = iv;
9647 if (plus)
9648 esignbuf[esignlen++] = plus;
9649 }
9650 else {
9651 uv = -iv;
9652 esignbuf[esignlen++] = '-';
9653 }
46fc3d4c 9654 }
9655 base = 10;
9656 goto integer;
9657
fc36a67e 9658 case 'U':
29fe7a80 9659#ifdef IV_IS_QUAD
22f3ae8c 9660 intsize = 'q';
29fe7a80 9661#else
fc36a67e 9662 intsize = 'l';
29fe7a80 9663#endif
fc36a67e 9664 /* FALL THROUGH */
9665 case 'u':
9666 base = 10;
9667 goto uns_integer;
9668
4f19785b
WSI
9669 case 'b':
9670 base = 2;
9671 goto uns_integer;
9672
46fc3d4c 9673 case 'O':
29fe7a80 9674#ifdef IV_IS_QUAD
22f3ae8c 9675 intsize = 'q';
29fe7a80 9676#else
46fc3d4c 9677 intsize = 'l';
29fe7a80 9678#endif
46fc3d4c 9679 /* FALL THROUGH */
9680 case 'o':
9681 base = 8;
9682 goto uns_integer;
9683
9684 case 'X':
46fc3d4c 9685 case 'x':
9686 base = 16;
46fc3d4c 9687
9688 uns_integer:
b22c7a20 9689 if (vectorize) {
ba210ebe 9690 STRLEN ulen;
b22c7a20 9691 vector:
211dfcf1
HS
9692 if (!veclen)
9693 continue;
2cf2cfc6
A
9694 if (vec_utf8)
9695 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
9696 UTF8_ALLOW_ANYUV);
b22c7a20 9697 else {
a05b299f 9698 uv = *vecstr;
b22c7a20
GS
9699 ulen = 1;
9700 }
9701 vecstr += ulen;
9702 veclen -= ulen;
9703 }
9704 else if (args) {
46fc3d4c 9705 switch (intsize) {
9706 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
46fc3d4c 9707 case 'l': uv = va_arg(*args, unsigned long); break;
fc36a67e 9708 case 'V': uv = va_arg(*args, UV); break;
b10c0dba 9709 default: uv = va_arg(*args, unsigned); break;
cf2093f6 9710#ifdef HAS_QUAD
9e3321a5 9711 case 'q': uv = va_arg(*args, Uquad_t); break;
cf2093f6 9712#endif
46fc3d4c 9713 }
9714 }
9715 else {
b10c0dba 9716 UV tuv = SvUVx(argsv); /* work around GCC bug #13488 */
46fc3d4c 9717 switch (intsize) {
b10c0dba
MHM
9718 case 'h': uv = (unsigned short)tuv; break;
9719 case 'l': uv = (unsigned long)tuv; break;
9720 case 'V':
9721 default: uv = tuv; break;
cf2093f6 9722#ifdef HAS_QUAD
b10c0dba 9723 case 'q': uv = (Uquad_t)tuv; break;
cf2093f6 9724#endif
46fc3d4c 9725 }
9726 }
9727
9728 integer:
46fc3d4c 9729 eptr = ebuf + sizeof ebuf;
fc36a67e 9730 switch (base) {
9731 unsigned dig;
9732 case 16:
c10ed8b9
HS
9733 if (!uv)
9734 alt = FALSE;
1d7c1841
GS
9735 p = (char*)((c == 'X')
9736 ? "0123456789ABCDEF" : "0123456789abcdef");
fc36a67e 9737 do {
9738 dig = uv & 15;
9739 *--eptr = p[dig];
9740 } while (uv >>= 4);
9741 if (alt) {
46fc3d4c 9742 esignbuf[esignlen++] = '0';
fc36a67e 9743 esignbuf[esignlen++] = c; /* 'x' or 'X' */
46fc3d4c 9744 }
fc36a67e 9745 break;
9746 case 8:
9747 do {
9748 dig = uv & 7;
9749 *--eptr = '0' + dig;
9750 } while (uv >>= 3);
9751 if (alt && *eptr != '0')
9752 *--eptr = '0';
9753 break;
4f19785b
WSI
9754 case 2:
9755 do {
9756 dig = uv & 1;
9757 *--eptr = '0' + dig;
9758 } while (uv >>= 1);
eda88b6d
JH
9759 if (alt) {
9760 esignbuf[esignlen++] = '0';
7481bb52 9761 esignbuf[esignlen++] = 'b';
eda88b6d 9762 }
4f19785b 9763 break;
fc36a67e 9764 default: /* it had better be ten or less */
9765 do {
9766 dig = uv % base;
9767 *--eptr = '0' + dig;
9768 } while (uv /= base);
9769 break;
46fc3d4c 9770 }
9771 elen = (ebuf + sizeof ebuf) - eptr;
c10ed8b9
HS
9772 if (has_precis) {
9773 if (precis > elen)
9774 zeros = precis - elen;
9775 else if (precis == 0 && elen == 1 && *eptr == '0')
9776 elen = 0;
9777 }
46fc3d4c 9778 break;
9779
9780 /* FLOATING POINT */
9781
fc36a67e 9782 case 'F':
9783 c = 'f'; /* maybe %F isn't supported here */
9784 /* FALL THROUGH */
46fc3d4c 9785 case 'e': case 'E':
fc36a67e 9786 case 'f':
46fc3d4c 9787 case 'g': case 'G':
9788
9789 /* This is evil, but floating point is even more evil */
9790
9e5b023a
JH
9791 /* for SV-style calling, we can only get NV
9792 for C-style calling, we assume %f is double;
9793 for simplicity we allow any of %Lf, %llf, %qf for long double
9794 */
9795 switch (intsize) {
9796 case 'V':
9797#if defined(USE_LONG_DOUBLE)
9798 intsize = 'q';
9799#endif
9800 break;
8a2e3f14 9801/* [perl #20339] - we should accept and ignore %lf rather than die */
00e17364
HS
9802 case 'l':
9803 /* FALL THROUGH */
9e5b023a
JH
9804 default:
9805#if defined(USE_LONG_DOUBLE)
9806 intsize = args ? 0 : 'q';
9807#endif
9808 break;
9809 case 'q':
9810#if defined(HAS_LONG_DOUBLE)
9811 break;
9812#else
9813 /* FALL THROUGH */
9814#endif
9815 case 'h':
9e5b023a
JH
9816 goto unknown;
9817 }
9818
9819 /* now we need (long double) if intsize == 'q', else (double) */
be75b157 9820 nv = (args && !vectorize) ?
35fff930
JH
9821#if LONG_DOUBLESIZE > DOUBLESIZE
9822 intsize == 'q' ?
205f51d8
AS
9823 va_arg(*args, long double) :
9824 va_arg(*args, double)
35fff930 9825#else
205f51d8 9826 va_arg(*args, double)
35fff930 9827#endif
9e5b023a 9828 : SvNVx(argsv);
fc36a67e 9829
9830 need = 0;
be75b157 9831 vectorize = FALSE;
fc36a67e 9832 if (c != 'e' && c != 'E') {
9833 i = PERL_INT_MIN;
9e5b023a
JH
9834 /* FIXME: if HAS_LONG_DOUBLE but not USE_LONG_DOUBLE this
9835 will cast our (long double) to (double) */
73b309ea 9836 (void)Perl_frexp(nv, &i);
fc36a67e 9837 if (i == PERL_INT_MIN)
cea2e8a9 9838 Perl_die(aTHX_ "panic: frexp");
c635e13b 9839 if (i > 0)
fc36a67e 9840 need = BIT_DIGITS(i);
9841 }
9842 need += has_precis ? precis : 6; /* known default */
20f6aaab 9843
fc36a67e 9844 if (need < width)
9845 need = width;
9846
20f6aaab
AS
9847#ifdef HAS_LDBL_SPRINTF_BUG
9848 /* This is to try to fix a bug with irix/nonstop-ux/powerux and
205f51d8
AS
9849 with sfio - Allen <allens@cpan.org> */
9850
9851# ifdef DBL_MAX
9852# define MY_DBL_MAX DBL_MAX
9853# else /* XXX guessing! HUGE_VAL may be defined as infinity, so not using */
9854# if DOUBLESIZE >= 8
9855# define MY_DBL_MAX 1.7976931348623157E+308L
9856# else
9857# define MY_DBL_MAX 3.40282347E+38L
9858# endif
9859# endif
9860
9861# ifdef HAS_LDBL_SPRINTF_BUG_LESS1 /* only between -1L & 1L - Allen */
9862# define MY_DBL_MAX_BUG 1L
20f6aaab 9863# else
205f51d8 9864# define MY_DBL_MAX_BUG MY_DBL_MAX
20f6aaab 9865# endif
20f6aaab 9866
205f51d8
AS
9867# ifdef DBL_MIN
9868# define MY_DBL_MIN DBL_MIN
9869# else /* XXX guessing! -Allen */
9870# if DOUBLESIZE >= 8
9871# define MY_DBL_MIN 2.2250738585072014E-308L
9872# else
9873# define MY_DBL_MIN 1.17549435E-38L
9874# endif
9875# endif
20f6aaab 9876
205f51d8
AS
9877 if ((intsize == 'q') && (c == 'f') &&
9878 ((nv < MY_DBL_MAX_BUG) && (nv > -MY_DBL_MAX_BUG)) &&
9879 (need < DBL_DIG)) {
9880 /* it's going to be short enough that
9881 * long double precision is not needed */
9882
9883 if ((nv <= 0L) && (nv >= -0L))
9884 fix_ldbl_sprintf_bug = TRUE; /* 0 is 0 - easiest */
9885 else {
9886 /* would use Perl_fp_class as a double-check but not
9887 * functional on IRIX - see perl.h comments */
9888
9889 if ((nv >= MY_DBL_MIN) || (nv <= -MY_DBL_MIN)) {
9890 /* It's within the range that a double can represent */
9891#if defined(DBL_MAX) && !defined(DBL_MIN)
9892 if ((nv >= ((long double)1/DBL_MAX)) ||
9893 (nv <= (-(long double)1/DBL_MAX)))
20f6aaab 9894#endif
205f51d8 9895 fix_ldbl_sprintf_bug = TRUE;
20f6aaab 9896 }
205f51d8
AS
9897 }
9898 if (fix_ldbl_sprintf_bug == TRUE) {
9899 double temp;
9900
9901 intsize = 0;
9902 temp = (double)nv;
9903 nv = (NV)temp;
9904 }
20f6aaab 9905 }
205f51d8
AS
9906
9907# undef MY_DBL_MAX
9908# undef MY_DBL_MAX_BUG
9909# undef MY_DBL_MIN
9910
20f6aaab
AS
9911#endif /* HAS_LDBL_SPRINTF_BUG */
9912
46fc3d4c 9913 need += 20; /* fudge factor */
80252599
GS
9914 if (PL_efloatsize < need) {
9915 Safefree(PL_efloatbuf);
9916 PL_efloatsize = need + 20; /* more fudge */
9917 New(906, PL_efloatbuf, PL_efloatsize, char);
7d5ea4e7 9918 PL_efloatbuf[0] = '\0';
46fc3d4c 9919 }
9920
4151a5fe
IZ
9921 if ( !(width || left || plus || alt) && fill != '0'
9922 && has_precis && intsize != 'q' ) { /* Shortcuts */
2873255c
NC
9923 /* See earlier comment about buggy Gconvert when digits,
9924 aka precis is 0 */
9925 if ( c == 'g' && precis) {
2e59c212 9926 Gconvert((NV)nv, (int)precis, 0, PL_efloatbuf);
4151a5fe
IZ
9927 if (*PL_efloatbuf) /* May return an empty string for digits==0 */
9928 goto float_converted;
9929 } else if ( c == 'f' && !precis) {
9930 if ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
9931 break;
9932 }
9933 }
46fc3d4c 9934 eptr = ebuf + sizeof ebuf;
9935 *--eptr = '\0';
9936 *--eptr = c;
9e5b023a
JH
9937 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
9938#if defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
9939 if (intsize == 'q') {
e5c81feb
JH
9940 /* Copy the one or more characters in a long double
9941 * format before the 'base' ([efgEFG]) character to
9942 * the format string. */
9943 static char const prifldbl[] = PERL_PRIfldbl;
9944 char const *p = prifldbl + sizeof(prifldbl) - 3;
9945 while (p >= prifldbl) { *--eptr = *p--; }
cf2093f6 9946 }
65202027 9947#endif
46fc3d4c 9948 if (has_precis) {
9949 base = precis;
9950 do { *--eptr = '0' + (base % 10); } while (base /= 10);
9951 *--eptr = '.';
9952 }
9953 if (width) {
9954 base = width;
9955 do { *--eptr = '0' + (base % 10); } while (base /= 10);
9956 }
9957 if (fill == '0')
9958 *--eptr = fill;
84902520
TB
9959 if (left)
9960 *--eptr = '-';
46fc3d4c 9961 if (plus)
9962 *--eptr = plus;
9963 if (alt)
9964 *--eptr = '#';
9965 *--eptr = '%';
9966
ff9121f8
JH
9967 /* No taint. Otherwise we are in the strange situation
9968 * where printf() taints but print($float) doesn't.
bda0f7a5 9969 * --jhi */
9e5b023a
JH
9970#if defined(HAS_LONG_DOUBLE)
9971 if (intsize == 'q')
9972 (void)sprintf(PL_efloatbuf, eptr, nv);
9973 else
9974 (void)sprintf(PL_efloatbuf, eptr, (double)nv);
9975#else
dd8482fc 9976 (void)sprintf(PL_efloatbuf, eptr, nv);
9e5b023a 9977#endif
4151a5fe 9978 float_converted:
80252599
GS
9979 eptr = PL_efloatbuf;
9980 elen = strlen(PL_efloatbuf);
46fc3d4c 9981 break;
9982
fc36a67e 9983 /* SPECIAL */
9984
9985 case 'n':
9986 i = SvCUR(sv) - origlen;
be75b157 9987 if (args && !vectorize) {
c635e13b 9988 switch (intsize) {
9989 case 'h': *(va_arg(*args, short*)) = i; break;
9990 default: *(va_arg(*args, int*)) = i; break;
9991 case 'l': *(va_arg(*args, long*)) = i; break;
9992 case 'V': *(va_arg(*args, IV*)) = i; break;
cf2093f6
JH
9993#ifdef HAS_QUAD
9994 case 'q': *(va_arg(*args, Quad_t*)) = i; break;
9995#endif
c635e13b 9996 }
fc36a67e 9997 }
9dd79c3f 9998 else
211dfcf1 9999 sv_setuv_mg(argsv, (UV)i);
be75b157 10000 vectorize = FALSE;
fc36a67e 10001 continue; /* not "break" */
10002
10003 /* UNKNOWN */
10004
46fc3d4c 10005 default:
fc36a67e 10006 unknown:
599cee73 10007 if (!args && ckWARN(WARN_PRINTF) &&
533c011a 10008 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
c635e13b 10009 SV *msg = sv_newmortal();
35c1215d
NC
10010 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
10011 (PL_op->op_type == OP_PRTF) ? "" : "s");
0f4b6630 10012 if (c) {
0f4b6630 10013 if (isPRINT(c))
1c846c1f 10014 Perl_sv_catpvf(aTHX_ msg,
0f4b6630
JH
10015 "\"%%%c\"", c & 0xFF);
10016 else
10017 Perl_sv_catpvf(aTHX_ msg,
57def98f 10018 "\"%%\\%03"UVof"\"",
0f4b6630 10019 (UV)c & 0xFF);
0f4b6630 10020 } else
c635e13b 10021 sv_catpv(msg, "end of string");
9014280d 10022 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%"SVf, msg); /* yes, this is reentrant */
c635e13b 10023 }
fb73857a 10024
10025 /* output mangled stuff ... */
10026 if (c == '\0')
10027 --q;
46fc3d4c 10028 eptr = p;
10029 elen = q - p;
fb73857a 10030
10031 /* ... right here, because formatting flags should not apply */
10032 SvGROW(sv, SvCUR(sv) + elen + 1);
10033 p = SvEND(sv);
4459522c 10034 Copy(eptr, p, elen, char);
fb73857a 10035 p += elen;
10036 *p = '\0';
b162af07 10037 SvCUR_set(sv, p - SvPVX(sv));
58e33a90 10038 svix = osvix;
fb73857a 10039 continue; /* not "break" */
46fc3d4c 10040 }
10041
6c94ec8b
HS
10042 /* calculate width before utf8_upgrade changes it */
10043 have = esignlen + zeros + elen;
10044
d2876be5
JH
10045 if (is_utf8 != has_utf8) {
10046 if (is_utf8) {
10047 if (SvCUR(sv))
10048 sv_utf8_upgrade(sv);
10049 }
10050 else {
10051 SV *nsv = sv_2mortal(newSVpvn(eptr, elen));
10052 sv_utf8_upgrade(nsv);
10053 eptr = SvPVX(nsv);
10054 elen = SvCUR(nsv);
10055 }
10056 SvGROW(sv, SvCUR(sv) + elen + 1);
10057 p = SvEND(sv);
10058 *p = '\0';
10059 }
6af65485 10060
46fc3d4c 10061 need = (have > width ? have : width);
10062 gap = need - have;
10063
b22c7a20 10064 SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1);
46fc3d4c 10065 p = SvEND(sv);
10066 if (esignlen && fill == '0') {
eb160463 10067 for (i = 0; i < (int)esignlen; i++)
46fc3d4c 10068 *p++ = esignbuf[i];
10069 }
10070 if (gap && !left) {
10071 memset(p, fill, gap);
10072 p += gap;
10073 }
10074 if (esignlen && fill != '0') {
eb160463 10075 for (i = 0; i < (int)esignlen; i++)
46fc3d4c 10076 *p++ = esignbuf[i];
10077 }
fc36a67e 10078 if (zeros) {
10079 for (i = zeros; i; i--)
10080 *p++ = '0';
10081 }
46fc3d4c 10082 if (elen) {
4459522c 10083 Copy(eptr, p, elen, char);
46fc3d4c 10084 p += elen;
10085 }
10086 if (gap && left) {
10087 memset(p, ' ', gap);
10088 p += gap;
10089 }
b22c7a20
GS
10090 if (vectorize) {
10091 if (veclen) {
4459522c 10092 Copy(dotstr, p, dotstrlen, char);
b22c7a20
GS
10093 p += dotstrlen;
10094 }
10095 else
10096 vectorize = FALSE; /* done iterating over vecstr */
10097 }
2cf2cfc6
A
10098 if (is_utf8)
10099 has_utf8 = TRUE;
10100 if (has_utf8)
7e2040f0 10101 SvUTF8_on(sv);
46fc3d4c 10102 *p = '\0';
b162af07 10103 SvCUR_set(sv, p - SvPVX(sv));
b22c7a20
GS
10104 if (vectorize) {
10105 esignlen = 0;
10106 goto vector;
10107 }
46fc3d4c 10108 }
10109}
51371543 10110
645c22ef
DM
10111/* =========================================================================
10112
10113=head1 Cloning an interpreter
10114
10115All the macros and functions in this section are for the private use of
10116the main function, perl_clone().
10117
10118The foo_dup() functions make an exact copy of an existing foo thinngy.
10119During the course of a cloning, a hash table is used to map old addresses
10120to new addresses. The table is created and manipulated with the
10121ptr_table_* functions.
10122
10123=cut
10124
10125============================================================================*/
10126
10127
1d7c1841
GS
10128#if defined(USE_ITHREADS)
10129
1d7c1841
GS
10130#ifndef GpREFCNT_inc
10131# define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
10132#endif
10133
10134
d2d73c3e
AB
10135#define sv_dup_inc(s,t) SvREFCNT_inc(sv_dup(s,t))
10136#define av_dup(s,t) (AV*)sv_dup((SV*)s,t)
10137#define av_dup_inc(s,t) (AV*)SvREFCNT_inc(sv_dup((SV*)s,t))
10138#define hv_dup(s,t) (HV*)sv_dup((SV*)s,t)
10139#define hv_dup_inc(s,t) (HV*)SvREFCNT_inc(sv_dup((SV*)s,t))
10140#define cv_dup(s,t) (CV*)sv_dup((SV*)s,t)
10141#define cv_dup_inc(s,t) (CV*)SvREFCNT_inc(sv_dup((SV*)s,t))
10142#define io_dup(s,t) (IO*)sv_dup((SV*)s,t)
10143#define io_dup_inc(s,t) (IO*)SvREFCNT_inc(sv_dup((SV*)s,t))
10144#define gv_dup(s,t) (GV*)sv_dup((SV*)s,t)
10145#define gv_dup_inc(s,t) (GV*)SvREFCNT_inc(sv_dup((SV*)s,t))
1d7c1841
GS
10146#define SAVEPV(p) (p ? savepv(p) : Nullch)
10147#define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch)
8cf8f3d1 10148
d2d73c3e 10149
d2f185dc
AMS
10150/* Duplicate a regexp. Required reading: pregcomp() and pregfree() in
10151 regcomp.c. AMS 20010712 */
645c22ef 10152
1d7c1841 10153REGEXP *
a8fc9800 10154Perl_re_dup(pTHX_ REGEXP *r, CLONE_PARAMS *param)
1d7c1841 10155{
27da23d5 10156 dVAR;
d2f185dc
AMS
10157 REGEXP *ret;
10158 int i, len, npar;
10159 struct reg_substr_datum *s;
10160
10161 if (!r)
10162 return (REGEXP *)NULL;
10163
10164 if ((ret = (REGEXP *)ptr_table_fetch(PL_ptr_table, r)))
10165 return ret;
10166
10167 len = r->offsets[0];
10168 npar = r->nparens+1;
10169
10170 Newc(0, ret, sizeof(regexp) + (len+1)*sizeof(regnode), char, regexp);
10171 Copy(r->program, ret->program, len+1, regnode);
10172
10173 New(0, ret->startp, npar, I32);
10174 Copy(r->startp, ret->startp, npar, I32);
10175 New(0, ret->endp, npar, I32);
10176 Copy(r->startp, ret->startp, npar, I32);
10177
d2f185dc
AMS
10178 New(0, ret->substrs, 1, struct reg_substr_data);
10179 for (s = ret->substrs->data, i = 0; i < 3; i++, s++) {
10180 s->min_offset = r->substrs->data[i].min_offset;
10181 s->max_offset = r->substrs->data[i].max_offset;
10182 s->substr = sv_dup_inc(r->substrs->data[i].substr, param);
33b8afdf 10183 s->utf8_substr = sv_dup_inc(r->substrs->data[i].utf8_substr, param);
d2f185dc
AMS
10184 }
10185
70612e96 10186 ret->regstclass = NULL;
d2f185dc
AMS
10187 if (r->data) {
10188 struct reg_data *d;
e1ec3a88 10189 const int count = r->data->count;
d2f185dc
AMS
10190
10191 Newc(0, d, sizeof(struct reg_data) + count*sizeof(void *),
10192 char, struct reg_data);
10193 New(0, d->what, count, U8);
10194
10195 d->count = count;
10196 for (i = 0; i < count; i++) {
10197 d->what[i] = r->data->what[i];
10198 switch (d->what[i]) {
a3621e74
YO
10199 /* legal options are one of: sfpont
10200 see also regcomp.h and pregfree() */
d2f185dc
AMS
10201 case 's':
10202 d->data[i] = sv_dup_inc((SV *)r->data->data[i], param);
10203 break;
10204 case 'p':
10205 d->data[i] = av_dup_inc((AV *)r->data->data[i], param);
10206 break;
10207 case 'f':
10208 /* This is cheating. */
10209 New(0, d->data[i], 1, struct regnode_charclass_class);
10210 StructCopy(r->data->data[i], d->data[i],
10211 struct regnode_charclass_class);
70612e96 10212 ret->regstclass = (regnode*)d->data[i];
d2f185dc
AMS
10213 break;
10214 case 'o':
33773810
AMS
10215 /* Compiled op trees are readonly, and can thus be
10216 shared without duplication. */
b34c0dd4 10217 OP_REFCNT_LOCK;
9b978d73 10218 d->data[i] = (void*)OpREFCNT_inc((OP*)r->data->data[i]);
b34c0dd4 10219 OP_REFCNT_UNLOCK;
9b978d73 10220 break;
d2f185dc
AMS
10221 case 'n':
10222 d->data[i] = r->data->data[i];
10223 break;
a3621e74
YO
10224 case 't':
10225 d->data[i] = r->data->data[i];
10226 OP_REFCNT_LOCK;
10227 ((reg_trie_data*)d->data[i])->refcount++;
10228 OP_REFCNT_UNLOCK;
10229 break;
10230 default:
10231 Perl_croak(aTHX_ "panic: re_dup unknown data code '%c'", r->data->what[i]);
d2f185dc
AMS
10232 }
10233 }
10234
10235 ret->data = d;
10236 }
10237 else
10238 ret->data = NULL;
10239
10240 New(0, ret->offsets, 2*len+1, U32);
10241 Copy(r->offsets, ret->offsets, 2*len+1, U32);
10242
e01c5899 10243 ret->precomp = SAVEPVN(r->precomp, r->prelen);
d2f185dc
AMS
10244 ret->refcnt = r->refcnt;
10245 ret->minlen = r->minlen;
10246 ret->prelen = r->prelen;
10247 ret->nparens = r->nparens;
10248 ret->lastparen = r->lastparen;
10249 ret->lastcloseparen = r->lastcloseparen;
10250 ret->reganch = r->reganch;
10251
70612e96
RG
10252 ret->sublen = r->sublen;
10253
10254 if (RX_MATCH_COPIED(ret))
e01c5899 10255 ret->subbeg = SAVEPVN(r->subbeg, r->sublen);
70612e96
RG
10256 else
10257 ret->subbeg = Nullch;
9a26048b
NC
10258#ifdef PERL_COPY_ON_WRITE
10259 ret->saved_copy = Nullsv;
10260#endif
70612e96 10261
d2f185dc
AMS
10262 ptr_table_store(PL_ptr_table, r, ret);
10263 return ret;
1d7c1841
GS
10264}
10265
d2d73c3e 10266/* duplicate a file handle */
645c22ef 10267
1d7c1841 10268PerlIO *
a8fc9800 10269Perl_fp_dup(pTHX_ PerlIO *fp, char type, CLONE_PARAMS *param)
1d7c1841
GS
10270{
10271 PerlIO *ret;
73d840c0
AL
10272 (void)type;
10273
1d7c1841
GS
10274 if (!fp)
10275 return (PerlIO*)NULL;
10276
10277 /* look for it in the table first */
10278 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
10279 if (ret)
10280 return ret;
10281
10282 /* create anew and remember what it is */
ecdeb87c 10283 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
1d7c1841
GS
10284 ptr_table_store(PL_ptr_table, fp, ret);
10285 return ret;
10286}
10287
645c22ef
DM
10288/* duplicate a directory handle */
10289
1d7c1841
GS
10290DIR *
10291Perl_dirp_dup(pTHX_ DIR *dp)
10292{
10293 if (!dp)
10294 return (DIR*)NULL;
10295 /* XXX TODO */
10296 return dp;
10297}
10298
ff276b08 10299/* duplicate a typeglob */
645c22ef 10300
1d7c1841 10301GP *
a8fc9800 10302Perl_gp_dup(pTHX_ GP *gp, CLONE_PARAMS* param)
1d7c1841
GS
10303{
10304 GP *ret;
10305 if (!gp)
10306 return (GP*)NULL;
10307 /* look for it in the table first */
10308 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
10309 if (ret)
10310 return ret;
10311
10312 /* create anew and remember what it is */
10313 Newz(0, ret, 1, GP);
10314 ptr_table_store(PL_ptr_table, gp, ret);
10315
10316 /* clone */
10317 ret->gp_refcnt = 0; /* must be before any other dups! */
d2d73c3e
AB
10318 ret->gp_sv = sv_dup_inc(gp->gp_sv, param);
10319 ret->gp_io = io_dup_inc(gp->gp_io, param);
10320 ret->gp_form = cv_dup_inc(gp->gp_form, param);
10321 ret->gp_av = av_dup_inc(gp->gp_av, param);
10322 ret->gp_hv = hv_dup_inc(gp->gp_hv, param);
10323 ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
10324 ret->gp_cv = cv_dup_inc(gp->gp_cv, param);
1d7c1841
GS
10325 ret->gp_cvgen = gp->gp_cvgen;
10326 ret->gp_flags = gp->gp_flags;
10327 ret->gp_line = gp->gp_line;
10328 ret->gp_file = gp->gp_file; /* points to COP.cop_file */
10329 return ret;
10330}
10331
645c22ef
DM
10332/* duplicate a chain of magic */
10333
1d7c1841 10334MAGIC *
a8fc9800 10335Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS* param)
1d7c1841 10336{
cb359b41
JH
10337 MAGIC *mgprev = (MAGIC*)NULL;
10338 MAGIC *mgret;
1d7c1841
GS
10339 if (!mg)
10340 return (MAGIC*)NULL;
10341 /* look for it in the table first */
10342 mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg);
10343 if (mgret)
10344 return mgret;
10345
10346 for (; mg; mg = mg->mg_moremagic) {
10347 MAGIC *nmg;
10348 Newz(0, nmg, 1, MAGIC);
cb359b41 10349 if (mgprev)
1d7c1841 10350 mgprev->mg_moremagic = nmg;
cb359b41
JH
10351 else
10352 mgret = nmg;
1d7c1841
GS
10353 nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */
10354 nmg->mg_private = mg->mg_private;
10355 nmg->mg_type = mg->mg_type;
10356 nmg->mg_flags = mg->mg_flags;
14befaf4 10357 if (mg->mg_type == PERL_MAGIC_qr) {
d2f185dc 10358 nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj, param);
1d7c1841 10359 }
05bd4103 10360 else if(mg->mg_type == PERL_MAGIC_backref) {
7fc63493 10361 const AV * const av = (AV*) mg->mg_obj;
fdc9a813
AE
10362 SV **svp;
10363 I32 i;
7fc63493 10364 (void)SvREFCNT_inc(nmg->mg_obj = (SV*)newAV());
fdc9a813
AE
10365 svp = AvARRAY(av);
10366 for (i = AvFILLp(av); i >= 0; i--) {
3a81978b 10367 if (!svp[i]) continue;
fdc9a813
AE
10368 av_push((AV*)nmg->mg_obj,sv_dup(svp[i],param));
10369 }
05bd4103 10370 }
1d7c1841
GS
10371 else {
10372 nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED)
d2d73c3e
AB
10373 ? sv_dup_inc(mg->mg_obj, param)
10374 : sv_dup(mg->mg_obj, param);
1d7c1841
GS
10375 }
10376 nmg->mg_len = mg->mg_len;
10377 nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */
14befaf4 10378 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
68795e93 10379 if (mg->mg_len > 0) {
1d7c1841 10380 nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len);
14befaf4
DM
10381 if (mg->mg_type == PERL_MAGIC_overload_table &&
10382 AMT_AMAGIC((AMT*)mg->mg_ptr))
10383 {
1d7c1841
GS
10384 AMT *amtp = (AMT*)mg->mg_ptr;
10385 AMT *namtp = (AMT*)nmg->mg_ptr;
10386 I32 i;
10387 for (i = 1; i < NofAMmeth; i++) {
d2d73c3e 10388 namtp->table[i] = cv_dup_inc(amtp->table[i], param);
1d7c1841
GS
10389 }
10390 }
10391 }
10392 else if (mg->mg_len == HEf_SVKEY)
d2d73c3e 10393 nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr, param);
1d7c1841 10394 }
68795e93
NIS
10395 if ((mg->mg_flags & MGf_DUP) && mg->mg_virtual && mg->mg_virtual->svt_dup) {
10396 CALL_FPTR(nmg->mg_virtual->svt_dup)(aTHX_ nmg, param);
10397 }
1d7c1841
GS
10398 mgprev = nmg;
10399 }
10400 return mgret;
10401}
10402
645c22ef
DM
10403/* create a new pointer-mapping table */
10404
1d7c1841
GS
10405PTR_TBL_t *
10406Perl_ptr_table_new(pTHX)
10407{
10408 PTR_TBL_t *tbl;
10409 Newz(0, tbl, 1, PTR_TBL_t);
10410 tbl->tbl_max = 511;
10411 tbl->tbl_items = 0;
10412 Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
10413 return tbl;
10414}
10415
134ca3d6
DM
10416#if (PTRSIZE == 8)
10417# define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 3)
10418#else
10419# define PTR_TABLE_HASH(ptr) (PTR2UV(ptr) >> 2)
10420#endif
10421
32e691d0
NC
10422
10423
10424STATIC void
10425S_more_pte(pTHX)
10426{
10427 register struct ptr_tbl_ent* pte;
10428 register struct ptr_tbl_ent* pteend;
10429 XPV *ptr;
9c17f24a 10430 New(54, ptr, PERL_ARENA_SIZE/sizeof(XPV), XPV);
32e691d0
NC
10431 ptr->xpv_pv = (char*)PL_pte_arenaroot;
10432 PL_pte_arenaroot = ptr;
10433
10434 pte = (struct ptr_tbl_ent*)ptr;
9c17f24a 10435 pteend = &pte[PERL_ARENA_SIZE / sizeof(struct ptr_tbl_ent) - 1];
32e691d0
NC
10436 PL_pte_root = ++pte;
10437 while (pte < pteend) {
10438 pte->next = pte + 1;
10439 pte++;
10440 }
10441 pte->next = 0;
10442}
10443
10444STATIC struct ptr_tbl_ent*
10445S_new_pte(pTHX)
10446{
10447 struct ptr_tbl_ent* pte;
10448 if (!PL_pte_root)
10449 S_more_pte(aTHX);
10450 pte = PL_pte_root;
10451 PL_pte_root = pte->next;
10452 return pte;
10453}
10454
10455STATIC void
10456S_del_pte(pTHX_ struct ptr_tbl_ent*p)
10457{
10458 p->next = PL_pte_root;
10459 PL_pte_root = p;
10460}
10461
645c22ef
DM
10462/* map an existing pointer using a table */
10463
1d7c1841
GS
10464void *
10465Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv)
10466{
10467 PTR_TBL_ENT_t *tblent;
134ca3d6 10468 UV hash = PTR_TABLE_HASH(sv);
1d7c1841
GS
10469 assert(tbl);
10470 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
10471 for (; tblent; tblent = tblent->next) {
10472 if (tblent->oldval == sv)
10473 return tblent->newval;
10474 }
10475 return (void*)NULL;
10476}
10477
645c22ef
DM
10478/* add a new entry to a pointer-mapping table */
10479
1d7c1841
GS
10480void
10481Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv)
10482{
10483 PTR_TBL_ENT_t *tblent, **otblent;
10484 /* XXX this may be pessimal on platforms where pointers aren't good
10485 * hash values e.g. if they grow faster in the most significant
10486 * bits */
134ca3d6 10487 UV hash = PTR_TABLE_HASH(oldv);
14cade97 10488 bool empty = 1;
1d7c1841
GS
10489
10490 assert(tbl);
10491 otblent = &tbl->tbl_ary[hash & tbl->tbl_max];
14cade97 10492 for (tblent = *otblent; tblent; empty=0, tblent = tblent->next) {
1d7c1841
GS
10493 if (tblent->oldval == oldv) {
10494 tblent->newval = newv;
1d7c1841
GS
10495 return;
10496 }
10497 }
32e691d0 10498 tblent = S_new_pte(aTHX);
1d7c1841
GS
10499 tblent->oldval = oldv;
10500 tblent->newval = newv;
10501 tblent->next = *otblent;
10502 *otblent = tblent;
10503 tbl->tbl_items++;
14cade97 10504 if (!empty && tbl->tbl_items > tbl->tbl_max)
1d7c1841
GS
10505 ptr_table_split(tbl);
10506}
10507
645c22ef
DM
10508/* double the hash bucket size of an existing ptr table */
10509
1d7c1841
GS
10510void
10511Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl)
10512{
10513 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
10514 UV oldsize = tbl->tbl_max + 1;
10515 UV newsize = oldsize * 2;
10516 UV i;
10517
10518 Renew(ary, newsize, PTR_TBL_ENT_t*);
10519 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
10520 tbl->tbl_max = --newsize;
10521 tbl->tbl_ary = ary;
10522 for (i=0; i < oldsize; i++, ary++) {
10523 PTR_TBL_ENT_t **curentp, **entp, *ent;
10524 if (!*ary)
10525 continue;
10526 curentp = ary + oldsize;
10527 for (entp = ary, ent = *ary; ent; ent = *entp) {
134ca3d6 10528 if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
1d7c1841
GS
10529 *entp = ent->next;
10530 ent->next = *curentp;
10531 *curentp = ent;
10532 continue;
10533 }
10534 else
10535 entp = &ent->next;
10536 }
10537 }
10538}
10539
645c22ef
DM
10540/* remove all the entries from a ptr table */
10541
a0739874
DM
10542void
10543Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl)
10544{
10545 register PTR_TBL_ENT_t **array;
10546 register PTR_TBL_ENT_t *entry;
10547 register PTR_TBL_ENT_t *oentry = Null(PTR_TBL_ENT_t*);
10548 UV riter = 0;
10549 UV max;
10550
10551 if (!tbl || !tbl->tbl_items) {
10552 return;
10553 }
10554
10555 array = tbl->tbl_ary;
10556 entry = array[0];
10557 max = tbl->tbl_max;
10558
10559 for (;;) {
10560 if (entry) {
10561 oentry = entry;
10562 entry = entry->next;
32e691d0 10563 S_del_pte(aTHX_ oentry);
a0739874
DM
10564 }
10565 if (!entry) {
10566 if (++riter > max) {
10567 break;
10568 }
10569 entry = array[riter];
10570 }
10571 }
10572
10573 tbl->tbl_items = 0;
10574}
10575
645c22ef
DM
10576/* clear and free a ptr table */
10577
a0739874
DM
10578void
10579Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl)
10580{
10581 if (!tbl) {
10582 return;
10583 }
10584 ptr_table_clear(tbl);
10585 Safefree(tbl->tbl_ary);
10586 Safefree(tbl);
10587}
10588
645c22ef
DM
10589/* attempt to make everything in the typeglob readonly */
10590
5bd07a3d 10591STATIC SV *
59b40662 10592S_gv_share(pTHX_ SV *sstr, CLONE_PARAMS *param)
5bd07a3d
DM
10593{
10594 GV *gv = (GV*)sstr;
59b40662 10595 SV *sv = &param->proto_perl->Isv_no; /* just need SvREADONLY-ness */
5bd07a3d
DM
10596
10597 if (GvIO(gv) || GvFORM(gv)) {
7fb37951 10598 GvUNIQUE_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */
5bd07a3d
DM
10599 }
10600 else if (!GvCV(gv)) {
10601 GvCV(gv) = (CV*)sv;
10602 }
10603 else {
10604 /* CvPADLISTs cannot be shared */
37e20706 10605 if (!SvREADONLY(GvCV(gv)) && !CvXSUB(GvCV(gv))) {
7fb37951 10606 GvUNIQUE_off(gv);
5bd07a3d
DM
10607 }
10608 }
10609
7fb37951 10610 if (!GvUNIQUE(gv)) {
5bd07a3d
DM
10611#if 0
10612 PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n",
10613 HvNAME(GvSTASH(gv)), GvNAME(gv));
10614#endif
10615 return Nullsv;
10616 }
10617
4411f3b6 10618 /*
5bd07a3d
DM
10619 * write attempts will die with
10620 * "Modification of a read-only value attempted"
10621 */
10622 if (!GvSV(gv)) {
10623 GvSV(gv) = sv;
10624 }
10625 else {
10626 SvREADONLY_on(GvSV(gv));
10627 }
10628
10629 if (!GvAV(gv)) {
10630 GvAV(gv) = (AV*)sv;
10631 }
10632 else {
10633 SvREADONLY_on(GvAV(gv));
10634 }
10635
10636 if (!GvHV(gv)) {
10637 GvHV(gv) = (HV*)sv;
10638 }
10639 else {
53c33732 10640 SvREADONLY_on(GvHV(gv));
5bd07a3d
DM
10641 }
10642
10643 return sstr; /* he_dup() will SvREFCNT_inc() */
10644}
10645
645c22ef
DM
10646/* duplicate an SV of any type (including AV, HV etc) */
10647
83841fad
NIS
10648void
10649Perl_rvpv_dup(pTHX_ SV *dstr, SV *sstr, CLONE_PARAMS* param)
10650{
10651 if (SvROK(sstr)) {
b162af07
SP
10652 SvRV_set(dstr, SvWEAKREF(sstr)
10653 ? sv_dup(SvRV(sstr), param)
10654 : sv_dup_inc(SvRV(sstr), param));
f880fe2f 10655
83841fad
NIS
10656 }
10657 else if (SvPVX(sstr)) {
10658 /* Has something there */
10659 if (SvLEN(sstr)) {
68795e93 10660 /* Normal PV - clone whole allocated space */
f880fe2f 10661 SvPV_set(dstr, SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1));
d3d0e6f1
NC
10662 if (SvREADONLY(sstr) && SvFAKE(sstr)) {
10663 /* Not that normal - actually sstr is copy on write.
10664 But we are a true, independant SV, so: */
10665 SvREADONLY_off(dstr);
10666 SvFAKE_off(dstr);
10667 }
68795e93 10668 }
83841fad
NIS
10669 else {
10670 /* Special case - not normally malloced for some reason */
10671 if (SvREADONLY(sstr) && SvFAKE(sstr)) {
10672 /* A "shared" PV - clone it as unshared string */
281b2760 10673 if(SvPADTMP(sstr)) {
5e6160dc
AB
10674 /* However, some of them live in the pad
10675 and they should not have these flags
10676 turned off */
281b2760 10677
f880fe2f
SP
10678 SvPV_set(dstr, sharepvn(SvPVX(sstr), SvCUR(sstr),
10679 SvUVX(sstr)));
607fa7f2 10680 SvUV_set(dstr, SvUVX(sstr));
281b2760
AB
10681 } else {
10682
f880fe2f 10683 SvPV_set(dstr, SAVEPVN(SvPVX(sstr), SvCUR(sstr)));
281b2760
AB
10684 SvFAKE_off(dstr);
10685 SvREADONLY_off(dstr);
5e6160dc 10686 }
83841fad
NIS
10687 }
10688 else {
10689 /* Some other special case - random pointer */
f880fe2f 10690 SvPV_set(dstr, SvPVX(sstr));
d3d0e6f1 10691 }
83841fad
NIS
10692 }
10693 }
10694 else {
10695 /* Copy the Null */
f880fe2f 10696 if (SvTYPE(dstr) == SVt_RV)
b162af07 10697 SvRV_set(dstr, NULL);
f880fe2f
SP
10698 else
10699 SvPV_set(dstr, 0);
83841fad
NIS
10700 }
10701}
10702
1d7c1841 10703SV *
a8fc9800 10704Perl_sv_dup(pTHX_ SV *sstr, CLONE_PARAMS* param)
1d7c1841 10705{
27da23d5 10706 dVAR;
1d7c1841
GS
10707 SV *dstr;
10708
10709 if (!sstr || SvTYPE(sstr) == SVTYPEMASK)
10710 return Nullsv;
10711 /* look for it in the table first */
10712 dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr);
10713 if (dstr)
10714 return dstr;
10715
0405e91e
AB
10716 if(param->flags & CLONEf_JOIN_IN) {
10717 /** We are joining here so we don't want do clone
10718 something that is bad **/
10719
10720 if(SvTYPE(sstr) == SVt_PVHV &&
10721 HvNAME(sstr)) {
10722 /** don't clone stashes if they already exist **/
10723 HV* old_stash = gv_stashpv(HvNAME(sstr),0);
10724 return (SV*) old_stash;
10725 }
10726 }
10727
1d7c1841
GS
10728 /* create anew and remember what it is */
10729 new_SV(dstr);
fd0854ff
DM
10730
10731#ifdef DEBUG_LEAKING_SCALARS
10732 dstr->sv_debug_optype = sstr->sv_debug_optype;
10733 dstr->sv_debug_line = sstr->sv_debug_line;
10734 dstr->sv_debug_inpad = sstr->sv_debug_inpad;
10735 dstr->sv_debug_cloned = 1;
10736# ifdef NETWARE
10737 dstr->sv_debug_file = savepv(sstr->sv_debug_file);
10738# else
10739 dstr->sv_debug_file = savesharedpv(sstr->sv_debug_file);
10740# endif
10741#endif
10742
1d7c1841
GS
10743 ptr_table_store(PL_ptr_table, sstr, dstr);
10744
10745 /* clone */
10746 SvFLAGS(dstr) = SvFLAGS(sstr);
10747 SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */
10748 SvREFCNT(dstr) = 0; /* must be before any other dups! */
10749
10750#ifdef DEBUGGING
10751 if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx)
10752 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
10753 PL_watch_pvx, SvPVX(sstr));
10754#endif
10755
9660f481
DM
10756 /* don't clone objects whose class has asked us not to */
10757 if (SvOBJECT(sstr) && ! (SvFLAGS(SvSTASH(sstr)) & SVphv_CLONEABLE)) {
10758 SvFLAGS(dstr) &= ~SVTYPEMASK;
10759 SvOBJECT_off(dstr);
10760 return dstr;
10761 }
10762
1d7c1841
GS
10763 switch (SvTYPE(sstr)) {
10764 case SVt_NULL:
10765 SvANY(dstr) = NULL;
10766 break;
10767 case SVt_IV:
10768 SvANY(dstr) = new_XIV();
45977657 10769 SvIV_set(dstr, SvIVX(sstr));
1d7c1841
GS
10770 break;
10771 case SVt_NV:
10772 SvANY(dstr) = new_XNV();
9d6ce603 10773 SvNV_set(dstr, SvNVX(sstr));
1d7c1841
GS
10774 break;
10775 case SVt_RV:
10776 SvANY(dstr) = new_XRV();
83841fad 10777 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10778 break;
10779 case SVt_PV:
10780 SvANY(dstr) = new_XPV();
b162af07
SP
10781 SvCUR_set(dstr, SvCUR(sstr));
10782 SvLEN_set(dstr, SvLEN(sstr));
83841fad 10783 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10784 break;
10785 case SVt_PVIV:
10786 SvANY(dstr) = new_XPVIV();
b162af07
SP
10787 SvCUR_set(dstr, SvCUR(sstr));
10788 SvLEN_set(dstr, SvLEN(sstr));
45977657 10789 SvIV_set(dstr, SvIVX(sstr));
83841fad 10790 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10791 break;
10792 case SVt_PVNV:
10793 SvANY(dstr) = new_XPVNV();
b162af07
SP
10794 SvCUR_set(dstr, SvCUR(sstr));
10795 SvLEN_set(dstr, SvLEN(sstr));
45977657 10796 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 10797 SvNV_set(dstr, SvNVX(sstr));
83841fad 10798 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10799 break;
10800 case SVt_PVMG:
10801 SvANY(dstr) = new_XPVMG();
b162af07
SP
10802 SvCUR_set(dstr, SvCUR(sstr));
10803 SvLEN_set(dstr, SvLEN(sstr));
45977657 10804 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 10805 SvNV_set(dstr, SvNVX(sstr));
b162af07
SP
10806 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10807 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10808 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10809 break;
10810 case SVt_PVBM:
10811 SvANY(dstr) = new_XPVBM();
b162af07
SP
10812 SvCUR_set(dstr, SvCUR(sstr));
10813 SvLEN_set(dstr, SvLEN(sstr));
45977657 10814 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 10815 SvNV_set(dstr, SvNVX(sstr));
b162af07
SP
10816 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10817 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10818 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10819 BmRARE(dstr) = BmRARE(sstr);
10820 BmUSEFUL(dstr) = BmUSEFUL(sstr);
10821 BmPREVIOUS(dstr)= BmPREVIOUS(sstr);
10822 break;
10823 case SVt_PVLV:
10824 SvANY(dstr) = new_XPVLV();
b162af07
SP
10825 SvCUR_set(dstr, SvCUR(sstr));
10826 SvLEN_set(dstr, SvLEN(sstr));
45977657 10827 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 10828 SvNV_set(dstr, SvNVX(sstr));
b162af07
SP
10829 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10830 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10831 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10832 LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */
10833 LvTARGLEN(dstr) = LvTARGLEN(sstr);
dd28f7bb
DM
10834 if (LvTYPE(sstr) == 't') /* for tie: unrefcnted fake (SV**) */
10835 LvTARG(dstr) = dstr;
10836 else if (LvTYPE(sstr) == 'T') /* for tie: fake HE */
10837 LvTARG(dstr) = (SV*)he_dup((HE*)LvTARG(sstr), 0, param);
10838 else
10839 LvTARG(dstr) = sv_dup_inc(LvTARG(sstr), param);
1d7c1841
GS
10840 LvTYPE(dstr) = LvTYPE(sstr);
10841 break;
10842 case SVt_PVGV:
7fb37951 10843 if (GvUNIQUE((GV*)sstr)) {
5bd07a3d 10844 SV *share;
59b40662 10845 if ((share = gv_share(sstr, param))) {
5bd07a3d
DM
10846 del_SV(dstr);
10847 dstr = share;
37e20706 10848 ptr_table_store(PL_ptr_table, sstr, dstr);
5bd07a3d
DM
10849#if 0
10850 PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n",
10851 HvNAME(GvSTASH(share)), GvNAME(share));
10852#endif
10853 break;
10854 }
10855 }
1d7c1841 10856 SvANY(dstr) = new_XPVGV();
b162af07
SP
10857 SvCUR_set(dstr, SvCUR(sstr));
10858 SvLEN_set(dstr, SvLEN(sstr));
45977657 10859 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 10860 SvNV_set(dstr, SvNVX(sstr));
b162af07
SP
10861 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10862 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10863 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
1d7c1841
GS
10864 GvNAMELEN(dstr) = GvNAMELEN(sstr);
10865 GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr));
d2d73c3e 10866 GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr), param);
1d7c1841 10867 GvFLAGS(dstr) = GvFLAGS(sstr);
d2d73c3e 10868 GvGP(dstr) = gp_dup(GvGP(sstr), param);
1d7c1841
GS
10869 (void)GpREFCNT_inc(GvGP(dstr));
10870 break;
10871 case SVt_PVIO:
10872 SvANY(dstr) = new_XPVIO();
b162af07
SP
10873 SvCUR_set(dstr, SvCUR(sstr));
10874 SvLEN_set(dstr, SvLEN(sstr));
45977657 10875 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 10876 SvNV_set(dstr, SvNVX(sstr));
b162af07
SP
10877 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10878 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10879 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
a8fc9800 10880 IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr), param);
1d7c1841
GS
10881 if (IoOFP(sstr) == IoIFP(sstr))
10882 IoOFP(dstr) = IoIFP(dstr);
10883 else
a8fc9800 10884 IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr), param);
1d7c1841
GS
10885 /* PL_rsfp_filters entries have fake IoDIRP() */
10886 if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP))
10887 IoDIRP(dstr) = dirp_dup(IoDIRP(sstr));
10888 else
10889 IoDIRP(dstr) = IoDIRP(sstr);
10890 IoLINES(dstr) = IoLINES(sstr);
10891 IoPAGE(dstr) = IoPAGE(sstr);
10892 IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr);
10893 IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr);
7a5fa8a2 10894 if(IoFLAGS(sstr) & IOf_FAKE_DIRP) {
5a37521b
AB
10895 /* I have no idea why fake dirp (rsfps)
10896 should be treaded differently but otherwise
10897 we end up with leaks -- sky*/
10898 IoTOP_GV(dstr) = gv_dup_inc(IoTOP_GV(sstr), param);
10899 IoFMT_GV(dstr) = gv_dup_inc(IoFMT_GV(sstr), param);
10900 IoBOTTOM_GV(dstr) = gv_dup_inc(IoBOTTOM_GV(sstr), param);
10901 } else {
10902 IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr), param);
10903 IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr), param);
10904 IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr), param);
10905 }
1d7c1841 10906 IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr));
1d7c1841 10907 IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr));
1d7c1841 10908 IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr));
1d7c1841
GS
10909 IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr);
10910 IoTYPE(dstr) = IoTYPE(sstr);
10911 IoFLAGS(dstr) = IoFLAGS(sstr);
10912 break;
10913 case SVt_PVAV:
10914 SvANY(dstr) = new_XPVAV();
b162af07
SP
10915 SvCUR_set(dstr, SvCUR(sstr));
10916 SvLEN_set(dstr, SvLEN(sstr));
45977657 10917 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 10918 SvNV_set(dstr, SvNVX(sstr));
b162af07
SP
10919 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10920 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
d2d73c3e 10921 AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr), param);
1d7c1841
GS
10922 AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr);
10923 if (AvARRAY((AV*)sstr)) {
10924 SV **dst_ary, **src_ary;
10925 SSize_t items = AvFILLp((AV*)sstr) + 1;
10926
10927 src_ary = AvARRAY((AV*)sstr);
10928 Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*);
10929 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
f880fe2f 10930 SvPV_set(dstr, (char*)dst_ary);
1d7c1841
GS
10931 AvALLOC((AV*)dstr) = dst_ary;
10932 if (AvREAL((AV*)sstr)) {
10933 while (items-- > 0)
d2d73c3e 10934 *dst_ary++ = sv_dup_inc(*src_ary++, param);
1d7c1841
GS
10935 }
10936 else {
10937 while (items-- > 0)
d2d73c3e 10938 *dst_ary++ = sv_dup(*src_ary++, param);
1d7c1841
GS
10939 }
10940 items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr);
10941 while (items-- > 0) {
10942 *dst_ary++ = &PL_sv_undef;
10943 }
10944 }
10945 else {
f880fe2f 10946 SvPV_set(dstr, Nullch);
1d7c1841
GS
10947 AvALLOC((AV*)dstr) = (SV**)NULL;
10948 }
10949 break;
10950 case SVt_PVHV:
10951 SvANY(dstr) = new_XPVHV();
b162af07
SP
10952 SvCUR_set(dstr, SvCUR(sstr));
10953 SvLEN_set(dstr, SvLEN(sstr));
45977657 10954 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 10955 SvNV_set(dstr, SvNVX(sstr));
b162af07
SP
10956 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10957 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
1d7c1841
GS
10958 HvRITER((HV*)dstr) = HvRITER((HV*)sstr);
10959 if (HvARRAY((HV*)sstr)) {
1d7c1841
GS
10960 STRLEN i = 0;
10961 XPVHV *dxhv = (XPVHV*)SvANY(dstr);
10962 XPVHV *sxhv = (XPVHV*)SvANY(sstr);
10963 Newz(0, dxhv->xhv_array,
10964 PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char);
10965 while (i <= sxhv->xhv_max) {
10966 ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i],
eb160463
GS
10967 (bool)!!HvSHAREKEYS(sstr),
10968 param);
1d7c1841
GS
10969 ++i;
10970 }
eb160463
GS
10971 dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter,
10972 (bool)!!HvSHAREKEYS(sstr), param);
1d7c1841
GS
10973 }
10974 else {
f880fe2f 10975 SvPV_set(dstr, Nullch);
1d7c1841
GS
10976 HvEITER((HV*)dstr) = (HE*)NULL;
10977 }
10978 HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */
10979 HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr));
c43294b8 10980 /* Record stashes for possible cloning in Perl_clone(). */
6676db26 10981 if(HvNAME((HV*)dstr))
d2d73c3e 10982 av_push(param->stashes, dstr);
1d7c1841
GS
10983 break;
10984 case SVt_PVFM:
10985 SvANY(dstr) = new_XPVFM();
10986 FmLINES(dstr) = FmLINES(sstr);
10987 goto dup_pvcv;
10988 /* NOTREACHED */
10989 case SVt_PVCV:
10990 SvANY(dstr) = new_XPVCV();
d2d73c3e 10991 dup_pvcv:
b162af07
SP
10992 SvCUR_set(dstr, SvCUR(sstr));
10993 SvLEN_set(dstr, SvLEN(sstr));
45977657 10994 SvIV_set(dstr, SvIVX(sstr));
9d6ce603 10995 SvNV_set(dstr, SvNVX(sstr));
b162af07
SP
10996 SvMAGIC_set(dstr, mg_dup(SvMAGIC(sstr), param));
10997 SvSTASH_set(dstr, hv_dup_inc(SvSTASH(sstr), param));
83841fad 10998 Perl_rvpv_dup(aTHX_ dstr, sstr, param);
d2d73c3e 10999 CvSTASH(dstr) = hv_dup(CvSTASH(sstr), param); /* NOTE: not refcounted */
1d7c1841 11000 CvSTART(dstr) = CvSTART(sstr);
b34c0dd4 11001 OP_REFCNT_LOCK;
1d7c1841 11002 CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr));
b34c0dd4 11003 OP_REFCNT_UNLOCK;
1d7c1841
GS
11004 CvXSUB(dstr) = CvXSUB(sstr);
11005 CvXSUBANY(dstr) = CvXSUBANY(sstr);
01485f8b
DM
11006 if (CvCONST(sstr)) {
11007 CvXSUBANY(dstr).any_ptr = GvUNIQUE(CvGV(sstr)) ?
11008 SvREFCNT_inc(CvXSUBANY(sstr).any_ptr) :
8f77bfdb 11009 sv_dup_inc((SV *)CvXSUBANY(sstr).any_ptr, param);
01485f8b 11010 }
b23f1a86
DM
11011 /* don't dup if copying back - CvGV isn't refcounted, so the
11012 * duped GV may never be freed. A bit of a hack! DAPM */
11013 CvGV(dstr) = (param->flags & CLONEf_JOIN_IN) ?
11014 Nullgv : gv_dup(CvGV(sstr), param) ;
d2d73c3e
AB
11015 if (param->flags & CLONEf_COPY_STACKS) {
11016 CvDEPTH(dstr) = CvDEPTH(sstr);
11017 } else {
11018 CvDEPTH(dstr) = 0;
11019 }
dd2155a4 11020 PAD_DUP(CvPADLIST(dstr), CvPADLIST(sstr), param);
7dafbf52
DM
11021 CvOUTSIDE_SEQ(dstr) = CvOUTSIDE_SEQ(sstr);
11022 CvOUTSIDE(dstr) =
11023 CvWEAKOUTSIDE(sstr)
11024 ? cv_dup( CvOUTSIDE(sstr), param)
11025 : cv_dup_inc(CvOUTSIDE(sstr), param);
1d7c1841 11026 CvFLAGS(dstr) = CvFLAGS(sstr);
54356c7d 11027 CvFILE(dstr) = CvXSUB(sstr) ? CvFILE(sstr) : SAVEPV(CvFILE(sstr));
1d7c1841
GS
11028 break;
11029 default:
c803eecc 11030 Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(sstr));
1d7c1841
GS
11031 break;
11032 }
11033
11034 if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO)
11035 ++PL_sv_objcount;
11036
11037 return dstr;
d2d73c3e 11038 }
1d7c1841 11039
645c22ef
DM
11040/* duplicate a context */
11041
1d7c1841 11042PERL_CONTEXT *
a8fc9800 11043Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
1d7c1841
GS
11044{
11045 PERL_CONTEXT *ncxs;
11046
11047 if (!cxs)
11048 return (PERL_CONTEXT*)NULL;
11049
11050 /* look for it in the table first */
11051 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
11052 if (ncxs)
11053 return ncxs;
11054
11055 /* create anew and remember what it is */
11056 Newz(56, ncxs, max + 1, PERL_CONTEXT);
11057 ptr_table_store(PL_ptr_table, cxs, ncxs);
11058
11059 while (ix >= 0) {
11060 PERL_CONTEXT *cx = &cxs[ix];
11061 PERL_CONTEXT *ncx = &ncxs[ix];
11062 ncx->cx_type = cx->cx_type;
11063 if (CxTYPE(cx) == CXt_SUBST) {
11064 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
11065 }
11066 else {
11067 ncx->blk_oldsp = cx->blk_oldsp;
11068 ncx->blk_oldcop = cx->blk_oldcop;
1d7c1841
GS
11069 ncx->blk_oldmarksp = cx->blk_oldmarksp;
11070 ncx->blk_oldscopesp = cx->blk_oldscopesp;
11071 ncx->blk_oldpm = cx->blk_oldpm;
11072 ncx->blk_gimme = cx->blk_gimme;
11073 switch (CxTYPE(cx)) {
11074 case CXt_SUB:
11075 ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0
d2d73c3e
AB
11076 ? cv_dup_inc(cx->blk_sub.cv, param)
11077 : cv_dup(cx->blk_sub.cv,param));
1d7c1841 11078 ncx->blk_sub.argarray = (cx->blk_sub.hasargs
d2d73c3e 11079 ? av_dup_inc(cx->blk_sub.argarray, param)
1d7c1841 11080 : Nullav);
d2d73c3e 11081 ncx->blk_sub.savearray = av_dup_inc(cx->blk_sub.savearray, param);
1d7c1841
GS
11082 ncx->blk_sub.olddepth = cx->blk_sub.olddepth;
11083 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
11084 ncx->blk_sub.lval = cx->blk_sub.lval;
f39bc417 11085 ncx->blk_sub.retop = cx->blk_sub.retop;
1d7c1841
GS
11086 break;
11087 case CXt_EVAL:
11088 ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval;
11089 ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type;
b47cad08 11090 ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv, param);
1d7c1841 11091 ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root;
d2d73c3e 11092 ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text, param);
f39bc417 11093 ncx->blk_eval.retop = cx->blk_eval.retop;
1d7c1841
GS
11094 break;
11095 case CXt_LOOP:
11096 ncx->blk_loop.label = cx->blk_loop.label;
11097 ncx->blk_loop.resetsp = cx->blk_loop.resetsp;
11098 ncx->blk_loop.redo_op = cx->blk_loop.redo_op;
11099 ncx->blk_loop.next_op = cx->blk_loop.next_op;
11100 ncx->blk_loop.last_op = cx->blk_loop.last_op;
11101 ncx->blk_loop.iterdata = (CxPADLOOP(cx)
11102 ? cx->blk_loop.iterdata
d2d73c3e 11103 : gv_dup((GV*)cx->blk_loop.iterdata, param));
f3548bdc
DM
11104 ncx->blk_loop.oldcomppad
11105 = (PAD*)ptr_table_fetch(PL_ptr_table,
11106 cx->blk_loop.oldcomppad);
d2d73c3e
AB
11107 ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave, param);
11108 ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval, param);
11109 ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary, param);
1d7c1841
GS
11110 ncx->blk_loop.iterix = cx->blk_loop.iterix;
11111 ncx->blk_loop.itermax = cx->blk_loop.itermax;
11112 break;
11113 case CXt_FORMAT:
d2d73c3e
AB
11114 ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv, param);
11115 ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv, param);
11116 ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv, param);
1d7c1841 11117 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
f39bc417 11118 ncx->blk_sub.retop = cx->blk_sub.retop;
1d7c1841
GS
11119 break;
11120 case CXt_BLOCK:
11121 case CXt_NULL:
11122 break;
11123 }
11124 }
11125 --ix;
11126 }
11127 return ncxs;
11128}
11129
645c22ef
DM
11130/* duplicate a stack info structure */
11131
1d7c1841 11132PERL_SI *
a8fc9800 11133Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
1d7c1841
GS
11134{
11135 PERL_SI *nsi;
11136
11137 if (!si)
11138 return (PERL_SI*)NULL;
11139
11140 /* look for it in the table first */
11141 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
11142 if (nsi)
11143 return nsi;
11144
11145 /* create anew and remember what it is */
11146 Newz(56, nsi, 1, PERL_SI);
11147 ptr_table_store(PL_ptr_table, si, nsi);
11148
d2d73c3e 11149 nsi->si_stack = av_dup_inc(si->si_stack, param);
1d7c1841
GS
11150 nsi->si_cxix = si->si_cxix;
11151 nsi->si_cxmax = si->si_cxmax;
d2d73c3e 11152 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
1d7c1841 11153 nsi->si_type = si->si_type;
d2d73c3e
AB
11154 nsi->si_prev = si_dup(si->si_prev, param);
11155 nsi->si_next = si_dup(si->si_next, param);
1d7c1841
GS
11156 nsi->si_markoff = si->si_markoff;
11157
11158 return nsi;
11159}
11160
11161#define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
11162#define TOPINT(ss,ix) ((ss)[ix].any_i32)
11163#define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
11164#define TOPLONG(ss,ix) ((ss)[ix].any_long)
11165#define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
11166#define TOPIV(ss,ix) ((ss)[ix].any_iv)
38d8b13e
HS
11167#define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool)
11168#define TOPBOOL(ss,ix) ((ss)[ix].any_bool)
1d7c1841
GS
11169#define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
11170#define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
11171#define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
11172#define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
11173#define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
11174#define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
11175
11176/* XXXXX todo */
11177#define pv_dup_inc(p) SAVEPV(p)
11178#define pv_dup(p) SAVEPV(p)
11179#define svp_dup_inc(p,pp) any_dup(p,pp)
11180
645c22ef
DM
11181/* map any object to the new equivent - either something in the
11182 * ptr table, or something in the interpreter structure
11183 */
11184
1d7c1841
GS
11185void *
11186Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl)
11187{
11188 void *ret;
11189
11190 if (!v)
11191 return (void*)NULL;
11192
11193 /* look for it in the table first */
11194 ret = ptr_table_fetch(PL_ptr_table, v);
11195 if (ret)
11196 return ret;
11197
11198 /* see if it is part of the interpreter structure */
11199 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
acfe0abc 11200 ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
05ec9bb3 11201 else {
1d7c1841 11202 ret = v;
05ec9bb3 11203 }
1d7c1841
GS
11204
11205 return ret;
11206}
11207
645c22ef
DM
11208/* duplicate the save stack */
11209
1d7c1841 11210ANY *
a8fc9800 11211Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
1d7c1841
GS
11212{
11213 ANY *ss = proto_perl->Tsavestack;
11214 I32 ix = proto_perl->Tsavestack_ix;
11215 I32 max = proto_perl->Tsavestack_max;
11216 ANY *nss;
11217 SV *sv;
11218 GV *gv;
11219 AV *av;
11220 HV *hv;
11221 void* ptr;
11222 int intval;
11223 long longval;
11224 GP *gp;
11225 IV iv;
11226 I32 i;
c4e33207 11227 char *c = NULL;
1d7c1841 11228 void (*dptr) (void*);
acfe0abc 11229 void (*dxptr) (pTHX_ void*);
e977893f 11230 OP *o;
1d7c1841
GS
11231
11232 Newz(54, nss, max, ANY);
11233
11234 while (ix > 0) {
11235 i = POPINT(ss,ix);
11236 TOPINT(nss,ix) = i;
11237 switch (i) {
11238 case SAVEt_ITEM: /* normal string */
11239 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11240 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 11241 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11242 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
11243 break;
11244 case SAVEt_SV: /* scalar reference */
11245 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11246 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 11247 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 11248 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
1d7c1841 11249 break;
f4dd75d9
GS
11250 case SAVEt_GENERIC_PVREF: /* generic char* */
11251 c = (char*)POPPTR(ss,ix);
11252 TOPPTR(nss,ix) = pv_dup(c);
11253 ptr = POPPTR(ss,ix);
11254 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11255 break;
05ec9bb3
NIS
11256 case SAVEt_SHARED_PVREF: /* char* in shared space */
11257 c = (char*)POPPTR(ss,ix);
11258 TOPPTR(nss,ix) = savesharedpv(c);
11259 ptr = POPPTR(ss,ix);
11260 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11261 break;
1d7c1841
GS
11262 case SAVEt_GENERIC_SVREF: /* generic sv */
11263 case SAVEt_SVREF: /* scalar reference */
11264 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11265 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
11266 ptr = POPPTR(ss,ix);
11267 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
11268 break;
11269 case SAVEt_AV: /* array reference */
11270 av = (AV*)POPPTR(ss,ix);
d2d73c3e 11271 TOPPTR(nss,ix) = av_dup_inc(av, param);
1d7c1841 11272 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 11273 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
11274 break;
11275 case SAVEt_HV: /* hash reference */
11276 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 11277 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841 11278 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 11279 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
11280 break;
11281 case SAVEt_INT: /* int reference */
11282 ptr = POPPTR(ss,ix);
11283 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11284 intval = (int)POPINT(ss,ix);
11285 TOPINT(nss,ix) = intval;
11286 break;
11287 case SAVEt_LONG: /* long reference */
11288 ptr = POPPTR(ss,ix);
11289 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11290 longval = (long)POPLONG(ss,ix);
11291 TOPLONG(nss,ix) = longval;
11292 break;
11293 case SAVEt_I32: /* I32 reference */
11294 case SAVEt_I16: /* I16 reference */
11295 case SAVEt_I8: /* I8 reference */
11296 ptr = POPPTR(ss,ix);
11297 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11298 i = POPINT(ss,ix);
11299 TOPINT(nss,ix) = i;
11300 break;
11301 case SAVEt_IV: /* IV reference */
11302 ptr = POPPTR(ss,ix);
11303 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11304 iv = POPIV(ss,ix);
11305 TOPIV(nss,ix) = iv;
11306 break;
11307 case SAVEt_SPTR: /* SV* reference */
11308 ptr = POPPTR(ss,ix);
11309 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11310 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11311 TOPPTR(nss,ix) = sv_dup(sv, param);
1d7c1841
GS
11312 break;
11313 case SAVEt_VPTR: /* random* reference */
11314 ptr = POPPTR(ss,ix);
11315 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11316 ptr = POPPTR(ss,ix);
11317 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11318 break;
11319 case SAVEt_PPTR: /* char* reference */
11320 ptr = POPPTR(ss,ix);
11321 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11322 c = (char*)POPPTR(ss,ix);
11323 TOPPTR(nss,ix) = pv_dup(c);
11324 break;
11325 case SAVEt_HPTR: /* HV* reference */
11326 ptr = POPPTR(ss,ix);
11327 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11328 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 11329 TOPPTR(nss,ix) = hv_dup(hv, param);
1d7c1841
GS
11330 break;
11331 case SAVEt_APTR: /* AV* reference */
11332 ptr = POPPTR(ss,ix);
11333 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11334 av = (AV*)POPPTR(ss,ix);
d2d73c3e 11335 TOPPTR(nss,ix) = av_dup(av, param);
1d7c1841
GS
11336 break;
11337 case SAVEt_NSTAB:
11338 gv = (GV*)POPPTR(ss,ix);
d2d73c3e 11339 TOPPTR(nss,ix) = gv_dup(gv, param);
1d7c1841
GS
11340 break;
11341 case SAVEt_GP: /* scalar reference */
11342 gp = (GP*)POPPTR(ss,ix);
d2d73c3e 11343 TOPPTR(nss,ix) = gp = gp_dup(gp, param);
1d7c1841
GS
11344 (void)GpREFCNT_inc(gp);
11345 gv = (GV*)POPPTR(ss,ix);
2ed3c8fc 11346 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
1d7c1841
GS
11347 c = (char*)POPPTR(ss,ix);
11348 TOPPTR(nss,ix) = pv_dup(c);
11349 iv = POPIV(ss,ix);
11350 TOPIV(nss,ix) = iv;
11351 iv = POPIV(ss,ix);
11352 TOPIV(nss,ix) = iv;
11353 break;
11354 case SAVEt_FREESV:
26d9b02f 11355 case SAVEt_MORTALIZESV:
1d7c1841 11356 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11357 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
11358 break;
11359 case SAVEt_FREEOP:
11360 ptr = POPPTR(ss,ix);
11361 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
11362 /* these are assumed to be refcounted properly */
11363 switch (((OP*)ptr)->op_type) {
11364 case OP_LEAVESUB:
11365 case OP_LEAVESUBLV:
11366 case OP_LEAVEEVAL:
11367 case OP_LEAVE:
11368 case OP_SCOPE:
11369 case OP_LEAVEWRITE:
e977893f
GS
11370 TOPPTR(nss,ix) = ptr;
11371 o = (OP*)ptr;
11372 OpREFCNT_inc(o);
1d7c1841
GS
11373 break;
11374 default:
11375 TOPPTR(nss,ix) = Nullop;
11376 break;
11377 }
11378 }
11379 else
11380 TOPPTR(nss,ix) = Nullop;
11381 break;
11382 case SAVEt_FREEPV:
11383 c = (char*)POPPTR(ss,ix);
11384 TOPPTR(nss,ix) = pv_dup_inc(c);
11385 break;
11386 case SAVEt_CLEARSV:
11387 longval = POPLONG(ss,ix);
11388 TOPLONG(nss,ix) = longval;
11389 break;
11390 case SAVEt_DELETE:
11391 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 11392 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841
GS
11393 c = (char*)POPPTR(ss,ix);
11394 TOPPTR(nss,ix) = pv_dup_inc(c);
11395 i = POPINT(ss,ix);
11396 TOPINT(nss,ix) = i;
11397 break;
11398 case SAVEt_DESTRUCTOR:
11399 ptr = POPPTR(ss,ix);
11400 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
11401 dptr = POPDPTR(ss,ix);
ef75a179 11402 TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl);
1d7c1841
GS
11403 break;
11404 case SAVEt_DESTRUCTOR_X:
11405 ptr = POPPTR(ss,ix);
11406 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
11407 dxptr = POPDXPTR(ss,ix);
acfe0abc 11408 TOPDXPTR(nss,ix) = (void (*)(pTHX_ void*))any_dup((void *)dxptr, proto_perl);
1d7c1841
GS
11409 break;
11410 case SAVEt_REGCONTEXT:
11411 case SAVEt_ALLOC:
11412 i = POPINT(ss,ix);
11413 TOPINT(nss,ix) = i;
11414 ix -= i;
11415 break;
11416 case SAVEt_STACK_POS: /* Position on Perl stack */
11417 i = POPINT(ss,ix);
11418 TOPINT(nss,ix) = i;
11419 break;
11420 case SAVEt_AELEM: /* array element */
11421 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11422 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841
GS
11423 i = POPINT(ss,ix);
11424 TOPINT(nss,ix) = i;
11425 av = (AV*)POPPTR(ss,ix);
d2d73c3e 11426 TOPPTR(nss,ix) = av_dup_inc(av, param);
1d7c1841
GS
11427 break;
11428 case SAVEt_HELEM: /* hash element */
11429 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11430 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 11431 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11432 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
1d7c1841 11433 hv = (HV*)POPPTR(ss,ix);
d2d73c3e 11434 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
1d7c1841
GS
11435 break;
11436 case SAVEt_OP:
11437 ptr = POPPTR(ss,ix);
11438 TOPPTR(nss,ix) = ptr;
11439 break;
11440 case SAVEt_HINTS:
11441 i = POPINT(ss,ix);
11442 TOPINT(nss,ix) = i;
11443 break;
c4410b1b
GS
11444 case SAVEt_COMPPAD:
11445 av = (AV*)POPPTR(ss,ix);
58ed4fbe 11446 TOPPTR(nss,ix) = av_dup(av, param);
c4410b1b 11447 break;
c3564e5c
GS
11448 case SAVEt_PADSV:
11449 longval = (long)POPLONG(ss,ix);
11450 TOPLONG(nss,ix) = longval;
11451 ptr = POPPTR(ss,ix);
11452 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
11453 sv = (SV*)POPPTR(ss,ix);
d2d73c3e 11454 TOPPTR(nss,ix) = sv_dup(sv, param);
c3564e5c 11455 break;
a1bb4754 11456 case SAVEt_BOOL:
38d8b13e 11457 ptr = POPPTR(ss,ix);
b9609c01 11458 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
38d8b13e 11459 longval = (long)POPBOOL(ss,ix);
b9609c01 11460 TOPBOOL(nss,ix) = (bool)longval;
a1bb4754 11461 break;
8bd2680e
MHM
11462 case SAVEt_SET_SVFLAGS:
11463 i = POPINT(ss,ix);
11464 TOPINT(nss,ix) = i;
11465 i = POPINT(ss,ix);
11466 TOPINT(nss,ix) = i;
11467 sv = (SV*)POPPTR(ss,ix);
11468 TOPPTR(nss,ix) = sv_dup(sv, param);
11469 break;
1d7c1841
GS
11470 default:
11471 Perl_croak(aTHX_ "panic: ss_dup inconsistency");
11472 }
11473 }
11474
11475 return nss;
11476}
11477
9660f481
DM
11478
11479/* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
11480 * flag to the result. This is done for each stash before cloning starts,
11481 * so we know which stashes want their objects cloned */
11482
11483static void
11484do_mark_cloneable_stash(pTHX_ SV *sv)
11485{
11486 if (HvNAME((HV*)sv)) {
11487 GV* cloner = gv_fetchmethod_autoload((HV*)sv, "CLONE_SKIP", 0);
11488 SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
11489 if (cloner && GvCV(cloner)) {
11490 dSP;
11491 UV status;
11492
11493 ENTER;
11494 SAVETMPS;
11495 PUSHMARK(SP);
11496 XPUSHs(sv_2mortal(newSVpv(HvNAME((HV*)sv), 0)));
11497 PUTBACK;
11498 call_sv((SV*)GvCV(cloner), G_SCALAR);
11499 SPAGAIN;
11500 status = POPu;
11501 PUTBACK;
11502 FREETMPS;
11503 LEAVE;
11504 if (status)
11505 SvFLAGS(sv) &= ~SVphv_CLONEABLE;
11506 }
11507 }
11508}
11509
11510
11511
645c22ef
DM
11512/*
11513=for apidoc perl_clone
11514
11515Create and return a new interpreter by cloning the current one.
11516
4be49ee6 11517perl_clone takes these flags as parameters:
6a78b4db 11518
7a5fa8a2
NIS
11519CLONEf_COPY_STACKS - is used to, well, copy the stacks also,
11520without it we only clone the data and zero the stacks,
11521with it we copy the stacks and the new perl interpreter is
11522ready to run at the exact same point as the previous one.
11523The pseudo-fork code uses COPY_STACKS while the
6a78b4db
AB
11524threads->new doesn't.
11525
11526CLONEf_KEEP_PTR_TABLE
7a5fa8a2
NIS
11527perl_clone keeps a ptr_table with the pointer of the old
11528variable as a key and the new variable as a value,
11529this allows it to check if something has been cloned and not
11530clone it again but rather just use the value and increase the
11531refcount. If KEEP_PTR_TABLE is not set then perl_clone will kill
11532the ptr_table using the function
11533C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>,
11534reason to keep it around is if you want to dup some of your own
11535variable who are outside the graph perl scans, example of this
6a78b4db
AB
11536code is in threads.xs create
11537
11538CLONEf_CLONE_HOST
7a5fa8a2
NIS
11539This is a win32 thing, it is ignored on unix, it tells perls
11540win32host code (which is c++) to clone itself, this is needed on
11541win32 if you want to run two threads at the same time,
11542if you just want to do some stuff in a separate perl interpreter
11543and then throw it away and return to the original one,
6a78b4db
AB
11544you don't need to do anything.
11545
645c22ef
DM
11546=cut
11547*/
11548
11549/* XXX the above needs expanding by someone who actually understands it ! */
3fc56081
NK
11550EXTERN_C PerlInterpreter *
11551perl_clone_host(PerlInterpreter* proto_perl, UV flags);
645c22ef 11552
1d7c1841
GS
11553PerlInterpreter *
11554perl_clone(PerlInterpreter *proto_perl, UV flags)
11555{
27da23d5 11556 dVAR;
1d7c1841 11557#ifdef PERL_IMPLICIT_SYS
c43294b8
AB
11558
11559 /* perlhost.h so we need to call into it
11560 to clone the host, CPerlHost should have a c interface, sky */
11561
11562 if (flags & CLONEf_CLONE_HOST) {
11563 return perl_clone_host(proto_perl,flags);
11564 }
11565 return perl_clone_using(proto_perl, flags,
1d7c1841
GS
11566 proto_perl->IMem,
11567 proto_perl->IMemShared,
11568 proto_perl->IMemParse,
11569 proto_perl->IEnv,
11570 proto_perl->IStdIO,
11571 proto_perl->ILIO,
11572 proto_perl->IDir,
11573 proto_perl->ISock,
11574 proto_perl->IProc);
11575}
11576
11577PerlInterpreter *
11578perl_clone_using(PerlInterpreter *proto_perl, UV flags,
11579 struct IPerlMem* ipM, struct IPerlMem* ipMS,
11580 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
11581 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
11582 struct IPerlDir* ipD, struct IPerlSock* ipS,
11583 struct IPerlProc* ipP)
11584{
11585 /* XXX many of the string copies here can be optimized if they're
11586 * constants; they need to be allocated as common memory and just
11587 * their pointers copied. */
11588
11589 IV i;
64aa0685
GS
11590 CLONE_PARAMS clone_params;
11591 CLONE_PARAMS* param = &clone_params;
d2d73c3e 11592
1d7c1841 11593 PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
9660f481
DM
11594 /* for each stash, determine whether its objects should be cloned */
11595 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
ba869deb 11596 PERL_SET_THX(my_perl);
1d7c1841 11597
acfe0abc 11598# ifdef DEBUGGING
a4530404 11599 Poison(my_perl, 1, PerlInterpreter);
fd0854ff 11600 PL_op = Nullop;
c008732b 11601 PL_curcop = (COP *)Nullop;
1d7c1841
GS
11602 PL_markstack = 0;
11603 PL_scopestack = 0;
11604 PL_savestack = 0;
22f7c9c9
JH
11605 PL_savestack_ix = 0;
11606 PL_savestack_max = -1;
66fe0623 11607 PL_sig_pending = 0;
25596c82 11608 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
acfe0abc 11609# else /* !DEBUGGING */
1d7c1841 11610 Zero(my_perl, 1, PerlInterpreter);
acfe0abc 11611# endif /* DEBUGGING */
1d7c1841
GS
11612
11613 /* host pointers */
11614 PL_Mem = ipM;
11615 PL_MemShared = ipMS;
11616 PL_MemParse = ipMP;
11617 PL_Env = ipE;
11618 PL_StdIO = ipStd;
11619 PL_LIO = ipLIO;
11620 PL_Dir = ipD;
11621 PL_Sock = ipS;
11622 PL_Proc = ipP;
1d7c1841
GS
11623#else /* !PERL_IMPLICIT_SYS */
11624 IV i;
64aa0685
GS
11625 CLONE_PARAMS clone_params;
11626 CLONE_PARAMS* param = &clone_params;
1d7c1841 11627 PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
9660f481
DM
11628 /* for each stash, determine whether its objects should be cloned */
11629 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
ba869deb 11630 PERL_SET_THX(my_perl);
1d7c1841
GS
11631
11632# ifdef DEBUGGING
a4530404 11633 Poison(my_perl, 1, PerlInterpreter);
fd0854ff 11634 PL_op = Nullop;
c008732b 11635 PL_curcop = (COP *)Nullop;
1d7c1841
GS
11636 PL_markstack = 0;
11637 PL_scopestack = 0;
11638 PL_savestack = 0;
22f7c9c9
JH
11639 PL_savestack_ix = 0;
11640 PL_savestack_max = -1;
66fe0623 11641 PL_sig_pending = 0;
25596c82 11642 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
1d7c1841
GS
11643# else /* !DEBUGGING */
11644 Zero(my_perl, 1, PerlInterpreter);
11645# endif /* DEBUGGING */
11646#endif /* PERL_IMPLICIT_SYS */
83236556 11647 param->flags = flags;
59b40662 11648 param->proto_perl = proto_perl;
1d7c1841
GS
11649
11650 /* arena roots */
11651 PL_xiv_arenaroot = NULL;
11652 PL_xiv_root = NULL;
612f20c3 11653 PL_xnv_arenaroot = NULL;
1d7c1841 11654 PL_xnv_root = NULL;
612f20c3 11655 PL_xrv_arenaroot = NULL;
1d7c1841 11656 PL_xrv_root = NULL;
612f20c3 11657 PL_xpv_arenaroot = NULL;
1d7c1841 11658 PL_xpv_root = NULL;
612f20c3 11659 PL_xpviv_arenaroot = NULL;
1d7c1841 11660 PL_xpviv_root = NULL;
612f20c3 11661 PL_xpvnv_arenaroot = NULL;
1d7c1841 11662 PL_xpvnv_root = NULL;
612f20c3 11663 PL_xpvcv_arenaroot = NULL;
1d7c1841 11664 PL_xpvcv_root = NULL;
612f20c3 11665 PL_xpvav_arenaroot = NULL;
1d7c1841 11666 PL_xpvav_root = NULL;
612f20c3 11667 PL_xpvhv_arenaroot = NULL;
1d7c1841 11668 PL_xpvhv_root = NULL;
612f20c3 11669 PL_xpvmg_arenaroot = NULL;
1d7c1841 11670 PL_xpvmg_root = NULL;
612f20c3 11671 PL_xpvlv_arenaroot = NULL;
1d7c1841 11672 PL_xpvlv_root = NULL;
612f20c3 11673 PL_xpvbm_arenaroot = NULL;
1d7c1841 11674 PL_xpvbm_root = NULL;
612f20c3 11675 PL_he_arenaroot = NULL;
1d7c1841 11676 PL_he_root = NULL;
32e691d0
NC
11677 PL_pte_arenaroot = NULL;
11678 PL_pte_root = NULL;
1d7c1841
GS
11679 PL_nice_chunk = NULL;
11680 PL_nice_chunk_size = 0;
11681 PL_sv_count = 0;
11682 PL_sv_objcount = 0;
11683 PL_sv_root = Nullsv;
11684 PL_sv_arenaroot = Nullsv;
11685
11686 PL_debug = proto_perl->Idebug;
11687
e5dd39fc 11688#ifdef USE_REENTRANT_API
68853529
SB
11689 /* XXX: things like -Dm will segfault here in perlio, but doing
11690 * PERL_SET_CONTEXT(proto_perl);
11691 * breaks too many other things
11692 */
59bd0823 11693 Perl_reentrant_init(aTHX);
e5dd39fc
AB
11694#endif
11695
1d7c1841
GS
11696 /* create SV map for pointer relocation */
11697 PL_ptr_table = ptr_table_new();
11698
11699 /* initialize these special pointers as early as possible */
11700 SvANY(&PL_sv_undef) = NULL;
11701 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
11702 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL;
11703 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
11704
1d7c1841 11705 SvANY(&PL_sv_no) = new_XPVNV();
1d7c1841 11706 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
0309f36e
NC
11707 SvFLAGS(&PL_sv_no) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
11708 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
f880fe2f 11709 SvPV_set(&PL_sv_no, SAVEPVN(PL_No, 0));
b162af07
SP
11710 SvCUR_set(&PL_sv_no, 0);
11711 SvLEN_set(&PL_sv_no, 1);
45977657 11712 SvIV_set(&PL_sv_no, 0);
9d6ce603 11713 SvNV_set(&PL_sv_no, 0);
1d7c1841
GS
11714 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
11715
1d7c1841 11716 SvANY(&PL_sv_yes) = new_XPVNV();
1d7c1841 11717 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
0309f36e
NC
11718 SvFLAGS(&PL_sv_yes) = SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
11719 |SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
f880fe2f 11720 SvPV_set(&PL_sv_yes, SAVEPVN(PL_Yes, 1));
b162af07
SP
11721 SvCUR_set(&PL_sv_yes, 1);
11722 SvLEN_set(&PL_sv_yes, 2);
45977657 11723 SvIV_set(&PL_sv_yes, 1);
9d6ce603 11724 SvNV_set(&PL_sv_yes, 1);
1d7c1841
GS
11725 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
11726
05ec9bb3 11727 /* create (a non-shared!) shared string table */
1d7c1841
GS
11728 PL_strtab = newHV();
11729 HvSHAREKEYS_off(PL_strtab);
11730 hv_ksplit(PL_strtab, 512);
11731 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
11732
05ec9bb3
NIS
11733 PL_compiling = proto_perl->Icompiling;
11734
11735 /* These two PVs will be free'd special way so must set them same way op.c does */
11736 PL_compiling.cop_stashpv = savesharedpv(PL_compiling.cop_stashpv);
11737 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_stashpv, PL_compiling.cop_stashpv);
11738
11739 PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file);
11740 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file);
11741
1d7c1841
GS
11742 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
11743 if (!specialWARN(PL_compiling.cop_warnings))
d2d73c3e 11744 PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings, param);
ac27b0f5 11745 if (!specialCopIO(PL_compiling.cop_io))
d2d73c3e 11746 PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io, param);
1d7c1841
GS
11747 PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl);
11748
11749 /* pseudo environmental stuff */
11750 PL_origargc = proto_perl->Iorigargc;
e2975953 11751 PL_origargv = proto_perl->Iorigargv;
d2d73c3e 11752
d2d73c3e
AB
11753 param->stashes = newAV(); /* Setup array of objects to call clone on */
11754
a1ea730d 11755#ifdef PERLIO_LAYERS
3a1ee7e8
NIS
11756 /* Clone PerlIO tables as soon as we can handle general xx_dup() */
11757 PerlIO_clone(aTHX_ proto_perl, param);
a1ea730d 11758#endif
d2d73c3e
AB
11759
11760 PL_envgv = gv_dup(proto_perl->Ienvgv, param);
11761 PL_incgv = gv_dup(proto_perl->Iincgv, param);
11762 PL_hintgv = gv_dup(proto_perl->Ihintgv, param);
1d7c1841 11763 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
d2d73c3e
AB
11764 PL_diehook = sv_dup_inc(proto_perl->Idiehook, param);
11765 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param);
1d7c1841
GS
11766
11767 /* switches */
11768 PL_minus_c = proto_perl->Iminus_c;
d2d73c3e 11769 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param);
1d7c1841
GS
11770 PL_localpatches = proto_perl->Ilocalpatches;
11771 PL_splitstr = proto_perl->Isplitstr;
11772 PL_preprocess = proto_perl->Ipreprocess;
11773 PL_minus_n = proto_perl->Iminus_n;
11774 PL_minus_p = proto_perl->Iminus_p;
11775 PL_minus_l = proto_perl->Iminus_l;
11776 PL_minus_a = proto_perl->Iminus_a;
11777 PL_minus_F = proto_perl->Iminus_F;
11778 PL_doswitches = proto_perl->Idoswitches;
11779 PL_dowarn = proto_perl->Idowarn;
11780 PL_doextract = proto_perl->Idoextract;
11781 PL_sawampersand = proto_perl->Isawampersand;
11782 PL_unsafe = proto_perl->Iunsafe;
11783 PL_inplace = SAVEPV(proto_perl->Iinplace);
d2d73c3e 11784 PL_e_script = sv_dup_inc(proto_perl->Ie_script, param);
1d7c1841
GS
11785 PL_perldb = proto_perl->Iperldb;
11786 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
1cbb0781 11787 PL_exit_flags = proto_perl->Iexit_flags;
1d7c1841
GS
11788
11789 /* magical thingies */
11790 /* XXX time(&PL_basetime) when asked for? */
11791 PL_basetime = proto_perl->Ibasetime;
d2d73c3e 11792 PL_formfeed = sv_dup(proto_perl->Iformfeed, param);
1d7c1841
GS
11793
11794 PL_maxsysfd = proto_perl->Imaxsysfd;
11795 PL_multiline = proto_perl->Imultiline;
11796 PL_statusvalue = proto_perl->Istatusvalue;
11797#ifdef VMS
11798 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
11799#endif
0a378802 11800 PL_encoding = sv_dup(proto_perl->Iencoding, param);
1d7c1841 11801
4a4c6fe3 11802 sv_setpvn(PERL_DEBUG_PAD(0), "", 0); /* For regex debugging. */
1f483ca1
JH
11803 sv_setpvn(PERL_DEBUG_PAD(1), "", 0); /* ext/re needs these */
11804 sv_setpvn(PERL_DEBUG_PAD(2), "", 0); /* even without DEBUGGING. */
4a4c6fe3 11805
d2f185dc
AMS
11806 /* Clone the regex array */
11807 PL_regex_padav = newAV();
11808 {
11809 I32 len = av_len((AV*)proto_perl->Iregex_padav);
11810 SV** regexen = AvARRAY((AV*)proto_perl->Iregex_padav);
0f95fc41
AB
11811 av_push(PL_regex_padav,
11812 sv_dup_inc(regexen[0],param));
11813 for(i = 1; i <= len; i++) {
11814 if(SvREPADTMP(regexen[i])) {
11815 av_push(PL_regex_padav, sv_dup_inc(regexen[i], param));
8cf8f3d1 11816 } else {
0f95fc41
AB
11817 av_push(PL_regex_padav,
11818 SvREFCNT_inc(
8cf8f3d1 11819 newSViv(PTR2IV(re_dup(INT2PTR(REGEXP *,
cbfa9890 11820 SvIVX(regexen[i])), param)))
0f95fc41
AB
11821 ));
11822 }
d2f185dc
AMS
11823 }
11824 }
11825 PL_regex_pad = AvARRAY(PL_regex_padav);
1fcf4c12 11826
1d7c1841 11827 /* shortcuts to various I/O objects */
d2d73c3e
AB
11828 PL_stdingv = gv_dup(proto_perl->Istdingv, param);
11829 PL_stderrgv = gv_dup(proto_perl->Istderrgv, param);
11830 PL_defgv = gv_dup(proto_perl->Idefgv, param);
11831 PL_argvgv = gv_dup(proto_perl->Iargvgv, param);
11832 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param);
11833 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param);
1d7c1841
GS
11834
11835 /* shortcuts to regexp stuff */
d2d73c3e 11836 PL_replgv = gv_dup(proto_perl->Ireplgv, param);
1d7c1841
GS
11837
11838 /* shortcuts to misc objects */
d2d73c3e 11839 PL_errgv = gv_dup(proto_perl->Ierrgv, param);
1d7c1841
GS
11840
11841 /* shortcuts to debugging objects */
d2d73c3e
AB
11842 PL_DBgv = gv_dup(proto_perl->IDBgv, param);
11843 PL_DBline = gv_dup(proto_perl->IDBline, param);
11844 PL_DBsub = gv_dup(proto_perl->IDBsub, param);
11845 PL_DBsingle = sv_dup(proto_perl->IDBsingle, param);
11846 PL_DBtrace = sv_dup(proto_perl->IDBtrace, param);
11847 PL_DBsignal = sv_dup(proto_perl->IDBsignal, param);
06492da6 11848 PL_DBassertion = sv_dup(proto_perl->IDBassertion, param);
d2d73c3e
AB
11849 PL_lineary = av_dup(proto_perl->Ilineary, param);
11850 PL_dbargs = av_dup(proto_perl->Idbargs, param);
1d7c1841
GS
11851
11852 /* symbol tables */
d2d73c3e
AB
11853 PL_defstash = hv_dup_inc(proto_perl->Tdefstash, param);
11854 PL_curstash = hv_dup(proto_perl->Tcurstash, param);
d2d73c3e
AB
11855 PL_debstash = hv_dup(proto_perl->Idebstash, param);
11856 PL_globalstash = hv_dup(proto_perl->Iglobalstash, param);
11857 PL_curstname = sv_dup_inc(proto_perl->Icurstname, param);
11858
11859 PL_beginav = av_dup_inc(proto_perl->Ibeginav, param);
ee1c5a4e 11860 PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param);
ece599bd 11861 PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param);
d2d73c3e
AB
11862 PL_endav = av_dup_inc(proto_perl->Iendav, param);
11863 PL_checkav = av_dup_inc(proto_perl->Icheckav, param);
11864 PL_initav = av_dup_inc(proto_perl->Iinitav, param);
1d7c1841
GS
11865
11866 PL_sub_generation = proto_perl->Isub_generation;
11867
11868 /* funky return mechanisms */
11869 PL_forkprocess = proto_perl->Iforkprocess;
11870
11871 /* subprocess state */
d2d73c3e 11872 PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param);
1d7c1841
GS
11873
11874 /* internal state */
11875 PL_tainting = proto_perl->Itainting;
7135f00b 11876 PL_taint_warn = proto_perl->Itaint_warn;
1d7c1841
GS
11877 PL_maxo = proto_perl->Imaxo;
11878 if (proto_perl->Iop_mask)
11879 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
11880 else
11881 PL_op_mask = Nullch;
06492da6 11882 /* PL_asserting = proto_perl->Iasserting; */
1d7c1841
GS
11883
11884 /* current interpreter roots */
d2d73c3e 11885 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param);
1d7c1841
GS
11886 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
11887 PL_main_start = proto_perl->Imain_start;
e977893f 11888 PL_eval_root = proto_perl->Ieval_root;
1d7c1841
GS
11889 PL_eval_start = proto_perl->Ieval_start;
11890
11891 /* runtime control stuff */
11892 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
11893 PL_copline = proto_perl->Icopline;
11894
11895 PL_filemode = proto_perl->Ifilemode;
11896 PL_lastfd = proto_perl->Ilastfd;
11897 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
11898 PL_Argv = NULL;
11899 PL_Cmd = Nullch;
11900 PL_gensym = proto_perl->Igensym;
11901 PL_preambled = proto_perl->Ipreambled;
d2d73c3e 11902 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param);
1d7c1841
GS
11903 PL_laststatval = proto_perl->Ilaststatval;
11904 PL_laststype = proto_perl->Ilaststype;
11905 PL_mess_sv = Nullsv;
11906
d2d73c3e 11907 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param);
1d7c1841
GS
11908 PL_ofmt = SAVEPV(proto_perl->Iofmt);
11909
11910 /* interpreter atexit processing */
11911 PL_exitlistlen = proto_perl->Iexitlistlen;
11912 if (PL_exitlistlen) {
11913 New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
11914 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
11915 }
11916 else
11917 PL_exitlist = (PerlExitListEntry*)NULL;
d2d73c3e 11918 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param);
19e8ce8e
AB
11919 PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param);
11920 PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param);
1d7c1841
GS
11921
11922 PL_profiledata = NULL;
a8fc9800 11923 PL_rsfp = fp_dup(proto_perl->Irsfp, '<', param);
1d7c1841 11924 /* PL_rsfp_filters entries have fake IoDIRP() */
d2d73c3e 11925 PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters, param);
1d7c1841 11926
d2d73c3e 11927 PL_compcv = cv_dup(proto_perl->Icompcv, param);
dd2155a4
DM
11928
11929 PAD_CLONE_VARS(proto_perl, param);
1d7c1841
GS
11930
11931#ifdef HAVE_INTERP_INTERN
11932 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
11933#endif
11934
11935 /* more statics moved here */
11936 PL_generation = proto_perl->Igeneration;
d2d73c3e 11937 PL_DBcv = cv_dup(proto_perl->IDBcv, param);
1d7c1841
GS
11938
11939 PL_in_clean_objs = proto_perl->Iin_clean_objs;
11940 PL_in_clean_all = proto_perl->Iin_clean_all;
11941
11942 PL_uid = proto_perl->Iuid;
11943 PL_euid = proto_perl->Ieuid;
11944 PL_gid = proto_perl->Igid;
11945 PL_egid = proto_perl->Iegid;
11946 PL_nomemok = proto_perl->Inomemok;
11947 PL_an = proto_perl->Ian;
1d7c1841
GS
11948 PL_evalseq = proto_perl->Ievalseq;
11949 PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */
11950 PL_origalen = proto_perl->Iorigalen;
11951 PL_pidstatus = newHV(); /* XXX flag for cloning? */
11952 PL_osname = SAVEPV(proto_perl->Iosname);
5c728af0 11953 PL_sh_path_compat = proto_perl->Ish_path_compat; /* XXX never deallocated */
1d7c1841
GS
11954 PL_sighandlerp = proto_perl->Isighandlerp;
11955
11956
11957 PL_runops = proto_perl->Irunops;
11958
11959 Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char);
11960
11961#ifdef CSH
11962 PL_cshlen = proto_perl->Icshlen;
74f1b2b8 11963 PL_cshname = proto_perl->Icshname; /* XXX never deallocated */
1d7c1841
GS
11964#endif
11965
11966 PL_lex_state = proto_perl->Ilex_state;
11967 PL_lex_defer = proto_perl->Ilex_defer;
11968 PL_lex_expect = proto_perl->Ilex_expect;
11969 PL_lex_formbrack = proto_perl->Ilex_formbrack;
11970 PL_lex_dojoin = proto_perl->Ilex_dojoin;
11971 PL_lex_starts = proto_perl->Ilex_starts;
d2d73c3e
AB
11972 PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff, param);
11973 PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl, param);
1d7c1841
GS
11974 PL_lex_op = proto_perl->Ilex_op;
11975 PL_lex_inpat = proto_perl->Ilex_inpat;
11976 PL_lex_inwhat = proto_perl->Ilex_inwhat;
11977 PL_lex_brackets = proto_perl->Ilex_brackets;
11978 i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets);
11979 PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i);
11980 PL_lex_casemods = proto_perl->Ilex_casemods;
11981 i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods);
11982 PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i);
11983
11984 Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE);
11985 Copy(proto_perl->Inexttype, PL_nexttype, 5, I32);
11986 PL_nexttoke = proto_perl->Inexttoke;
11987
1d773130
TB
11988 /* XXX This is probably masking the deeper issue of why
11989 * SvANY(proto_perl->Ilinestr) can be NULL at this point. For test case:
11990 * http://archive.develooper.com/perl5-porters%40perl.org/msg83298.html
11991 * (A little debugging with a watchpoint on it may help.)
11992 */
389edf32
TB
11993 if (SvANY(proto_perl->Ilinestr)) {
11994 PL_linestr = sv_dup_inc(proto_perl->Ilinestr, param);
11995 i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr);
11996 PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11997 i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr);
11998 PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
11999 i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr);
12000 PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12001 i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr);
12002 PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12003 }
12004 else {
12005 PL_linestr = NEWSV(65,79);
12006 sv_upgrade(PL_linestr,SVt_PVIV);
12007 sv_setpvn(PL_linestr,"",0);
12008 PL_bufptr = PL_oldbufptr = PL_oldoldbufptr = PL_linestart = SvPVX(PL_linestr);
12009 }
1d7c1841 12010 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
1d7c1841
GS
12011 PL_pending_ident = proto_perl->Ipending_ident;
12012 PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */
12013
12014 PL_expect = proto_perl->Iexpect;
12015
12016 PL_multi_start = proto_perl->Imulti_start;
12017 PL_multi_end = proto_perl->Imulti_end;
12018 PL_multi_open = proto_perl->Imulti_open;
12019 PL_multi_close = proto_perl->Imulti_close;
12020
12021 PL_error_count = proto_perl->Ierror_count;
12022 PL_subline = proto_perl->Isubline;
d2d73c3e 12023 PL_subname = sv_dup_inc(proto_perl->Isubname, param);
1d7c1841 12024
1d773130 12025 /* XXX See comment on SvANY(proto_perl->Ilinestr) above */
389edf32
TB
12026 if (SvANY(proto_perl->Ilinestr)) {
12027 i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr);
12028 PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12029 i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr);
12030 PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
12031 PL_last_lop_op = proto_perl->Ilast_lop_op;
12032 }
12033 else {
12034 PL_last_uni = SvPVX(PL_linestr);
12035 PL_last_lop = SvPVX(PL_linestr);
12036 PL_last_lop_op = 0;
12037 }
1d7c1841 12038 PL_in_my = proto_perl->Iin_my;
d2d73c3e 12039 PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash, param);
1d7c1841
GS
12040#ifdef FCRYPT
12041 PL_cryptseen = proto_perl->Icryptseen;
12042#endif
12043
12044 PL_hints = proto_perl->Ihints;
12045
12046 PL_amagic_generation = proto_perl->Iamagic_generation;
12047
12048#ifdef USE_LOCALE_COLLATE
12049 PL_collation_ix = proto_perl->Icollation_ix;
12050 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
12051 PL_collation_standard = proto_perl->Icollation_standard;
12052 PL_collxfrm_base = proto_perl->Icollxfrm_base;
12053 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
12054#endif /* USE_LOCALE_COLLATE */
12055
12056#ifdef USE_LOCALE_NUMERIC
12057 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
12058 PL_numeric_standard = proto_perl->Inumeric_standard;
12059 PL_numeric_local = proto_perl->Inumeric_local;
d2d73c3e 12060 PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param);
1d7c1841
GS
12061#endif /* !USE_LOCALE_NUMERIC */
12062
12063 /* utf8 character classes */
d2d73c3e
AB
12064 PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum, param);
12065 PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc, param);
12066 PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii, param);
12067 PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha, param);
12068 PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space, param);
12069 PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl, param);
12070 PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph, param);
12071 PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit, param);
12072 PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper, param);
12073 PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower, param);
12074 PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print, param);
12075 PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct, param);
12076 PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit, param);
12077 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param);
12078 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param);
12079 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param);
12080 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param);
b4e400f9 12081 PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param);
82686b01
JH
12082 PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param);
12083 PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param);
1d7c1841 12084
6c3182a5 12085 /* Did the locale setup indicate UTF-8? */
9769094f 12086 PL_utf8locale = proto_perl->Iutf8locale;
6c3182a5
JH
12087 /* Unicode features (see perlrun/-C) */
12088 PL_unicode = proto_perl->Iunicode;
12089
12090 /* Pre-5.8 signals control */
12091 PL_signals = proto_perl->Isignals;
12092
12093 /* times() ticks per second */
12094 PL_clocktick = proto_perl->Iclocktick;
12095
12096 /* Recursion stopper for PerlIO_find_layer */
12097 PL_in_load_module = proto_perl->Iin_load_module;
12098
12099 /* sort() routine */
12100 PL_sort_RealCmp = proto_perl->Isort_RealCmp;
12101
57c6e6d2
JH
12102 /* Not really needed/useful since the reenrant_retint is "volatile",
12103 * but do it for consistency's sake. */
12104 PL_reentrant_retint = proto_perl->Ireentrant_retint;
12105
15a5279a
JH
12106 /* Hooks to shared SVs and locks. */
12107 PL_sharehook = proto_perl->Isharehook;
12108 PL_lockhook = proto_perl->Ilockhook;
12109 PL_unlockhook = proto_perl->Iunlockhook;
12110 PL_threadhook = proto_perl->Ithreadhook;
12111
bce260cd
JH
12112 PL_runops_std = proto_perl->Irunops_std;
12113 PL_runops_dbg = proto_perl->Irunops_dbg;
12114
12115#ifdef THREADS_HAVE_PIDS
12116 PL_ppid = proto_perl->Ippid;
12117#endif
12118
1d7c1841
GS
12119 /* swatch cache */
12120 PL_last_swash_hv = Nullhv; /* reinits on demand */
12121 PL_last_swash_klen = 0;
12122 PL_last_swash_key[0]= '\0';
12123 PL_last_swash_tmps = (U8*)NULL;
12124 PL_last_swash_slen = 0;
12125
1d7c1841
GS
12126 PL_glob_index = proto_perl->Iglob_index;
12127 PL_srand_called = proto_perl->Isrand_called;
504f80c1 12128 PL_hash_seed = proto_perl->Ihash_seed;
008fb0c0 12129 PL_rehash_seed = proto_perl->Irehash_seed;
1d7c1841
GS
12130 PL_uudmap['M'] = 0; /* reinits on demand */
12131 PL_bitcount = Nullch; /* reinits on demand */
12132
66fe0623
NIS
12133 if (proto_perl->Ipsig_pend) {
12134 Newz(0, PL_psig_pend, SIG_SIZE, int);
9dd79c3f 12135 }
66fe0623
NIS
12136 else {
12137 PL_psig_pend = (int*)NULL;
12138 }
12139
1d7c1841 12140 if (proto_perl->Ipsig_ptr) {
76d3c696
JH
12141 Newz(0, PL_psig_ptr, SIG_SIZE, SV*);
12142 Newz(0, PL_psig_name, SIG_SIZE, SV*);
76d3c696 12143 for (i = 1; i < SIG_SIZE; i++) {
d2d73c3e
AB
12144 PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i], param);
12145 PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i], param);
1d7c1841
GS
12146 }
12147 }
12148 else {
12149 PL_psig_ptr = (SV**)NULL;
12150 PL_psig_name = (SV**)NULL;
12151 }
12152
12153 /* thrdvar.h stuff */
12154
a0739874 12155 if (flags & CLONEf_COPY_STACKS) {
1d7c1841
GS
12156 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
12157 PL_tmps_ix = proto_perl->Ttmps_ix;
12158 PL_tmps_max = proto_perl->Ttmps_max;
12159 PL_tmps_floor = proto_perl->Ttmps_floor;
12160 Newz(50, PL_tmps_stack, PL_tmps_max, SV*);
12161 i = 0;
12162 while (i <= PL_tmps_ix) {
d2d73c3e 12163 PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i], param);
1d7c1841
GS
12164 ++i;
12165 }
12166
12167 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
12168 i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack;
12169 Newz(54, PL_markstack, i, I32);
12170 PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max
12171 - proto_perl->Tmarkstack);
12172 PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr
12173 - proto_perl->Tmarkstack);
12174 Copy(proto_perl->Tmarkstack, PL_markstack,
12175 PL_markstack_ptr - PL_markstack + 1, I32);
12176
12177 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
12178 * NOTE: unlike the others! */
12179 PL_scopestack_ix = proto_perl->Tscopestack_ix;
12180 PL_scopestack_max = proto_perl->Tscopestack_max;
12181 Newz(54, PL_scopestack, PL_scopestack_max, I32);
12182 Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32);
12183
1d7c1841 12184 /* NOTE: si_dup() looks at PL_markstack */
d2d73c3e 12185 PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo, param);
1d7c1841
GS
12186
12187 /* PL_curstack = PL_curstackinfo->si_stack; */
d2d73c3e
AB
12188 PL_curstack = av_dup(proto_perl->Tcurstack, param);
12189 PL_mainstack = av_dup(proto_perl->Tmainstack, param);
1d7c1841
GS
12190
12191 /* next PUSHs() etc. set *(PL_stack_sp+1) */
12192 PL_stack_base = AvARRAY(PL_curstack);
12193 PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp
12194 - proto_perl->Tstack_base);
12195 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
12196
12197 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
12198 * NOTE: unlike the others! */
12199 PL_savestack_ix = proto_perl->Tsavestack_ix;
12200 PL_savestack_max = proto_perl->Tsavestack_max;
12201 /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/
d2d73c3e 12202 PL_savestack = ss_dup(proto_perl, param);
1d7c1841
GS
12203 }
12204 else {
12205 init_stacks();
985e7056 12206 ENTER; /* perl_destruct() wants to LEAVE; */
1d7c1841
GS
12207 }
12208
12209 PL_start_env = proto_perl->Tstart_env; /* XXXXXX */
12210 PL_top_env = &PL_start_env;
12211
12212 PL_op = proto_perl->Top;
12213
12214 PL_Sv = Nullsv;
12215 PL_Xpv = (XPV*)NULL;
12216 PL_na = proto_perl->Tna;
12217
12218 PL_statbuf = proto_perl->Tstatbuf;
12219 PL_statcache = proto_perl->Tstatcache;
d2d73c3e
AB
12220 PL_statgv = gv_dup(proto_perl->Tstatgv, param);
12221 PL_statname = sv_dup_inc(proto_perl->Tstatname, param);
1d7c1841
GS
12222#ifdef HAS_TIMES
12223 PL_timesbuf = proto_perl->Ttimesbuf;
12224#endif
12225
12226 PL_tainted = proto_perl->Ttainted;
12227 PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */
d2d73c3e
AB
12228 PL_rs = sv_dup_inc(proto_perl->Trs, param);
12229 PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv, param);
12230 PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv, param);
12231 PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv, param);
1d7c1841 12232 PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */
d2d73c3e
AB
12233 PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget, param);
12234 PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget, param);
12235 PL_formtarget = sv_dup(proto_perl->Tformtarget, param);
1d7c1841
GS
12236
12237 PL_restartop = proto_perl->Trestartop;
12238 PL_in_eval = proto_perl->Tin_eval;
12239 PL_delaymagic = proto_perl->Tdelaymagic;
12240 PL_dirty = proto_perl->Tdirty;
12241 PL_localizing = proto_perl->Tlocalizing;
12242
d2d73c3e 12243 PL_errors = sv_dup_inc(proto_perl->Terrors, param);
dd28f7bb 12244 PL_hv_fetch_ent_mh = Nullhe;
1d7c1841
GS
12245 PL_modcount = proto_perl->Tmodcount;
12246 PL_lastgotoprobe = Nullop;
12247 PL_dumpindent = proto_perl->Tdumpindent;
12248
12249 PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl);
d2d73c3e
AB
12250 PL_sortstash = hv_dup(proto_perl->Tsortstash, param);
12251 PL_firstgv = gv_dup(proto_perl->Tfirstgv, param);
12252 PL_secondgv = gv_dup(proto_perl->Tsecondgv, param);
1d7c1841
GS
12253 PL_sortcxix = proto_perl->Tsortcxix;
12254 PL_efloatbuf = Nullch; /* reinits on demand */
12255 PL_efloatsize = 0; /* reinits on demand */
12256
12257 /* regex stuff */
12258
12259 PL_screamfirst = NULL;
12260 PL_screamnext = NULL;
12261 PL_maxscream = -1; /* reinits on demand */
12262 PL_lastscream = Nullsv;
12263
12264 PL_watchaddr = NULL;
12265 PL_watchok = Nullch;
12266
12267 PL_regdummy = proto_perl->Tregdummy;
1d7c1841
GS
12268 PL_regprecomp = Nullch;
12269 PL_regnpar = 0;
12270 PL_regsize = 0;
1d7c1841
GS
12271 PL_colorset = 0; /* reinits PL_colors[] */
12272 /*PL_colors[6] = {0,0,0,0,0,0};*/
1d7c1841
GS
12273 PL_reginput = Nullch;
12274 PL_regbol = Nullch;
12275 PL_regeol = Nullch;
12276 PL_regstartp = (I32*)NULL;
12277 PL_regendp = (I32*)NULL;
12278 PL_reglastparen = (U32*)NULL;
2d862feb 12279 PL_reglastcloseparen = (U32*)NULL;
1d7c1841 12280 PL_regtill = Nullch;
1d7c1841
GS
12281 PL_reg_start_tmp = (char**)NULL;
12282 PL_reg_start_tmpl = 0;
12283 PL_regdata = (struct reg_data*)NULL;
12284 PL_bostr = Nullch;
12285 PL_reg_flags = 0;
12286 PL_reg_eval_set = 0;
12287 PL_regnarrate = 0;
12288 PL_regprogram = (regnode*)NULL;
12289 PL_regindent = 0;
12290 PL_regcc = (CURCUR*)NULL;
12291 PL_reg_call_cc = (struct re_cc_state*)NULL;
12292 PL_reg_re = (regexp*)NULL;
12293 PL_reg_ganch = Nullch;
12294 PL_reg_sv = Nullsv;
53c4c00c 12295 PL_reg_match_utf8 = FALSE;
1d7c1841
GS
12296 PL_reg_magic = (MAGIC*)NULL;
12297 PL_reg_oldpos = 0;
12298 PL_reg_oldcurpm = (PMOP*)NULL;
12299 PL_reg_curpm = (PMOP*)NULL;
12300 PL_reg_oldsaved = Nullch;
12301 PL_reg_oldsavedlen = 0;
ed252734 12302#ifdef PERL_COPY_ON_WRITE
504cff3b 12303 PL_nrs = Nullsv;
ed252734 12304#endif
1d7c1841
GS
12305 PL_reg_maxiter = 0;
12306 PL_reg_leftiter = 0;
12307 PL_reg_poscache = Nullch;
12308 PL_reg_poscache_size= 0;
12309
12310 /* RE engine - function pointers */
12311 PL_regcompp = proto_perl->Tregcompp;
12312 PL_regexecp = proto_perl->Tregexecp;
12313 PL_regint_start = proto_perl->Tregint_start;
12314 PL_regint_string = proto_perl->Tregint_string;
12315 PL_regfree = proto_perl->Tregfree;
12316
12317 PL_reginterp_cnt = 0;
12318 PL_reg_starttry = 0;
12319
a2efc822
SC
12320 /* Pluggable optimizer */
12321 PL_peepp = proto_perl->Tpeepp;
12322
081fc587
AB
12323 PL_stashcache = newHV();
12324
a0739874
DM
12325 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
12326 ptr_table_free(PL_ptr_table);
12327 PL_ptr_table = NULL;
12328 }
8cf8f3d1 12329
f284b03f
AMS
12330 /* Call the ->CLONE method, if it exists, for each of the stashes
12331 identified by sv_dup() above.
12332 */
d2d73c3e
AB
12333 while(av_len(param->stashes) != -1) {
12334 HV* stash = (HV*) av_shift(param->stashes);
f284b03f
AMS
12335 GV* cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
12336 if (cloner && GvCV(cloner)) {
12337 dSP;
12338 ENTER;
12339 SAVETMPS;
12340 PUSHMARK(SP);
9660f481 12341 XPUSHs(sv_2mortal(newSVpv(HvNAME(stash), 0)));
f284b03f
AMS
12342 PUTBACK;
12343 call_sv((SV*)GvCV(cloner), G_DISCARD);
12344 FREETMPS;
12345 LEAVE;
12346 }
4a09accc 12347 }
a0739874 12348
dc507217 12349 SvREFCNT_dec(param->stashes);
dc507217 12350
1d7c1841 12351 return my_perl;
1d7c1841
GS
12352}
12353
1d7c1841 12354#endif /* USE_ITHREADS */
a0ae6670 12355
9f4817db 12356/*
ccfc67b7
JH
12357=head1 Unicode Support
12358
9f4817db
JH
12359=for apidoc sv_recode_to_utf8
12360
5d170f3a
JH
12361The encoding is assumed to be an Encode object, on entry the PV
12362of the sv is assumed to be octets in that encoding, and the sv
12363will be converted into Unicode (and UTF-8).
9f4817db 12364
5d170f3a
JH
12365If the sv already is UTF-8 (or if it is not POK), or if the encoding
12366is not a reference, nothing is done to the sv. If the encoding is not
1768d7eb
JH
12367an C<Encode::XS> Encoding object, bad things will happen.
12368(See F<lib/encoding.pm> and L<Encode>).
9f4817db 12369
5d170f3a 12370The PV of the sv is returned.
9f4817db 12371
5d170f3a
JH
12372=cut */
12373
12374char *
12375Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
12376{
27da23d5 12377 dVAR;
220e2d4e 12378 if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
d0063567
DK
12379 SV *uni;
12380 STRLEN len;
12381 char *s;
12382 dSP;
12383 ENTER;
12384 SAVETMPS;
220e2d4e 12385 save_re_context();
d0063567
DK
12386 PUSHMARK(sp);
12387 EXTEND(SP, 3);
12388 XPUSHs(encoding);
12389 XPUSHs(sv);
7a5fa8a2 12390/*
f9893866
NIS
12391 NI-S 2002/07/09
12392 Passing sv_yes is wrong - it needs to be or'ed set of constants
7a5fa8a2 12393 for Encode::XS, while UTf-8 decode (currently) assumes a true value means
f9893866
NIS
12394 remove converted chars from source.
12395
12396 Both will default the value - let them.
7a5fa8a2 12397
d0063567 12398 XPUSHs(&PL_sv_yes);
f9893866 12399*/
d0063567
DK
12400 PUTBACK;
12401 call_method("decode", G_SCALAR);
12402 SPAGAIN;
12403 uni = POPs;
12404 PUTBACK;
12405 s = SvPV(uni, len);
d0063567
DK
12406 if (s != SvPVX(sv)) {
12407 SvGROW(sv, len + 1);
12408 Move(s, SvPVX(sv), len, char);
12409 SvCUR_set(sv, len);
12410 SvPVX(sv)[len] = 0;
12411 }
12412 FREETMPS;
12413 LEAVE;
d0063567 12414 SvUTF8_on(sv);
95899a2a 12415 return SvPVX(sv);
f9893866 12416 }
95899a2a 12417 return SvPOKp(sv) ? SvPVX(sv) : NULL;
9f4817db
JH
12418}
12419
220e2d4e
IH
12420/*
12421=for apidoc sv_cat_decode
12422
12423The encoding is assumed to be an Encode object, the PV of the ssv is
12424assumed to be octets in that encoding and decoding the input starts
12425from the position which (PV + *offset) pointed to. The dsv will be
12426concatenated the decoded UTF-8 string from ssv. Decoding will terminate
12427when the string tstr appears in decoding output or the input ends on
12428the PV of the ssv. The value which the offset points will be modified
12429to the last input position on the ssv.
68795e93 12430
220e2d4e
IH
12431Returns TRUE if the terminator was found, else returns FALSE.
12432
12433=cut */
12434
12435bool
12436Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
12437 SV *ssv, int *offset, char *tstr, int tlen)
12438{
27da23d5 12439 dVAR;
a73e8557 12440 bool ret = FALSE;
220e2d4e 12441 if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding) && offset) {
220e2d4e
IH
12442 SV *offsv;
12443 dSP;
12444 ENTER;
12445 SAVETMPS;
12446 save_re_context();
12447 PUSHMARK(sp);
12448 EXTEND(SP, 6);
12449 XPUSHs(encoding);
12450 XPUSHs(dsv);
12451 XPUSHs(ssv);
12452 XPUSHs(offsv = sv_2mortal(newSViv(*offset)));
12453 XPUSHs(sv_2mortal(newSVpvn(tstr, tlen)));
12454 PUTBACK;
12455 call_method("cat_decode", G_SCALAR);
12456 SPAGAIN;
12457 ret = SvTRUE(TOPs);
12458 *offset = SvIV(offsv);
12459 PUTBACK;
12460 FREETMPS;
12461 LEAVE;
220e2d4e 12462 }
a73e8557
JH
12463 else
12464 Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
12465 return ret;
220e2d4e 12466}
f9893866 12467
241d1a3b
NC
12468/*
12469 * Local variables:
12470 * c-indentation-style: bsd
12471 * c-basic-offset: 4
12472 * indent-tabs-mode: t
12473 * End:
12474 *
edf815fd 12475 * vim: shiftwidth=4:
241d1a3b 12476*/