Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

14798 lines (12836 sloc) 406.623 kB
/* sv.c
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
* 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall
* and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* 'I wonder what the Entish is for "yes" and "no",' he thought.
* --Pippin
*
* [p.480 of _The Lord of the Rings_, III/iv: "Treebeard"]
*/
/*
*
*
* This file contains the code that creates, manipulates and destroys
* scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the
* structure of an SV, so their creation and destruction is handled
* here; higher-level functions are in av.c, hv.c, and so on. Opcode
* level functions (eg. substr, split, join) for each of the types are
* in the pp*.c files.
*/
#include "EXTERN.h"
#define PERL_IN_SV_C
#include "perl.h"
#include "regcomp.h"
#ifndef HAS_C99
# if __STDC_VERSION__ >= 199901L && !defined(VMS)
# define HAS_C99 1
# endif
#endif
#if HAS_C99
# include <stdint.h>
#endif
#ifdef __Lynx__
/* Missing proto on LynxOS */
char *gconvert(double, int, int, char *);
#endif
#ifdef PERL_UTF8_CACHE_ASSERT
/* if adding more checks watch out for the following tests:
* t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
* lib/utf8.t lib/Unicode/Collate/t/index.t
* --jhi
*/
# define ASSERT_UTF8_CACHE(cache) \
STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); \
assert((cache)[2] <= (cache)[3]); \
assert((cache)[3] <= (cache)[1]);} \
} STMT_END
#else
# define ASSERT_UTF8_CACHE(cache) NOOP
#endif
#ifdef PERL_OLD_COPY_ON_WRITE
#define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
#define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next))
#endif
/* ============================================================================
=head1 Allocation and deallocation of SVs.
An SV (or AV, HV, etc.) is allocated in two parts: the head (struct
sv, av, hv...) contains type and reference count information, and for
many types, a pointer to the body (struct xrv, xpv, xpviv...), which
contains fields specific to each type. Some types store all they need
in the head, so don't have a body.
In all but the most memory-paranoid configurations (ex: PURIFY), heads
and bodies are allocated out of arenas, which by default are
approximately 4K chunks of memory parcelled up into N heads or bodies.
Sv-bodies are allocated by their sv-type, guaranteeing size
consistency needed to allocate safely from arrays.
For SV-heads, the first slot in each arena is reserved, and holds a
link to the next arena, some flags, and a note of the number of slots.
Snaked through each arena chain is a linked list of free items; when
this becomes empty, an extra arena is allocated and divided up into N
items which are threaded into the free list.
SV-bodies are similar, but they use arena-sets by default, which
separate the link and info from the arena itself, and reclaim the 1st
slot in the arena. SV-bodies are further described later.
The following global variables are associated with arenas:
PL_sv_arenaroot pointer to list of SV arenas
PL_sv_root pointer to list of free SV structures
PL_body_arenas head of linked-list of body arenas
PL_body_roots[] array of pointers to list of free bodies of svtype
arrays are indexed by the svtype needed
A few special SV heads are not allocated from an arena, but are
instead directly created in the interpreter structure, eg PL_sv_undef.
The size of arenas can be changed from the default by setting
PERL_ARENA_SIZE appropriately at compile time.
The SV arena serves the secondary purpose of allowing still-live SVs
to be located and destroyed during final cleanup.
At the lowest level, the macros new_SV() and del_SV() grab and free
an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
to return the SV to the free list with error checking.) new_SV() calls
more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
SVs in the free list have their SvTYPE field set to all ones.
At the time of very final cleanup, sv_free_arenas() is called from
perl_destruct() to physically free all the arenas allocated since the
start of the interpreter.
The function visit() scans the SV arenas list, and calls a specified
function for each SV it finds which is still live - ie which has an SvTYPE
other than all 1's, and a non-zero SvREFCNT. visit() is used by the
following functions (specified as [function that calls visit()] / [function
called by visit() for each SV]):
sv_report_used() / do_report_used()
dump all remaining SVs (debugging aid)
sv_clean_objs() / do_clean_objs(),do_clean_named_objs(),
do_clean_named_io_objs(),do_curse()
Attempt to free all objects pointed to by RVs,
try to do the same for all objects indir-
ectly referenced by typeglobs too, and
then do a final sweep, cursing any
objects that remain. Called once from
perl_destruct(), prior to calling sv_clean_all()
below.
sv_clean_all() / do_clean_all()
SvREFCNT_dec(sv) each remaining SV, possibly
triggering an sv_free(). It also sets the
SVf_BREAK flag on the SV to indicate that the
refcnt has been artificially lowered, and thus
stopping sv_free() from giving spurious warnings
about SVs which unexpectedly have a refcnt
of zero. called repeatedly from perl_destruct()
until there are no SVs left.
=head2 Arena allocator API Summary
Private API to rest of sv.c
new_SV(), del_SV(),
new_XPVNV(), del_XPVGV(),
etc
Public API:
sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
=cut
* ========================================================================= */
/*
* "A time to plant, and a time to uproot what was planted..."
*/
#ifdef PERL_MEM_LOG
# define MEM_LOG_NEW_SV(sv, file, line, func) \
Perl_mem_log_new_sv(sv, file, line, func)
# define MEM_LOG_DEL_SV(sv, file, line, func) \
Perl_mem_log_del_sv(sv, file, line, func)
#else
# define MEM_LOG_NEW_SV(sv, file, line, func) NOOP
# define MEM_LOG_DEL_SV(sv, file, line, func) NOOP
#endif
#ifdef DEBUG_LEAKING_SCALARS
# define FREE_SV_DEBUG_FILE(sv) STMT_START { \
if ((sv)->sv_debug_file) PerlMemShared_free((sv)->sv_debug_file); \
} STMT_END
# define DEBUG_SV_SERIAL(sv) \
DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) del_SV\n", \
PTR2UV(sv), (long)(sv)->sv_debug_serial))
#else
# define FREE_SV_DEBUG_FILE(sv)
# define DEBUG_SV_SERIAL(sv) NOOP
#endif
#ifdef PERL_POISON
# define SvARENA_CHAIN(sv) ((sv)->sv_u.svu_rv)
# define SvARENA_CHAIN_SET(sv,val) (sv)->sv_u.svu_rv = MUTABLE_SV((val))
/* Whilst I'd love to do this, it seems that things like to check on
unreferenced scalars
# define POSION_SV_HEAD(sv) PoisonNew(sv, 1, struct STRUCT_SV)
*/
# define POSION_SV_HEAD(sv) PoisonNew(&SvANY(sv), 1, void *), \
PoisonNew(&SvREFCNT(sv), 1, U32)
#else
# define SvARENA_CHAIN(sv) SvANY(sv)
# define SvARENA_CHAIN_SET(sv,val) SvANY(sv) = (void *)(val)
# define POSION_SV_HEAD(sv)
#endif
/* Mark an SV head as unused, and add to free list.
*
* If SVf_BREAK is set, skip adding it to the free list, as this SV had
* its refcount artificially decremented during global destruction, so
* there may be dangling pointers to it. The last thing we want in that
* case is for it to be reused. */
#define plant_SV(p) \
STMT_START { \
const U32 old_flags = SvFLAGS(p); \
MEM_LOG_DEL_SV(p, __FILE__, __LINE__, FUNCTION__); \
DEBUG_SV_SERIAL(p); \
FREE_SV_DEBUG_FILE(p); \
POSION_SV_HEAD(p); \
SvFLAGS(p) = SVTYPEMASK; \
if (!(old_flags & SVf_BREAK)) { \
SvARENA_CHAIN_SET(p, PL_sv_root); \
PL_sv_root = (p); \
} \
--PL_sv_count; \
} STMT_END
#define uproot_SV(p) \
STMT_START { \
(p) = PL_sv_root; \
PL_sv_root = MUTABLE_SV(SvARENA_CHAIN(p)); \
++PL_sv_count; \
} STMT_END
/* make some more SVs by adding another arena */
STATIC SV*
S_more_sv(pTHX)
{
dVAR;
SV* sv;
char *chunk; /* must use New here to match call to */
Newx(chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
uproot_SV(sv);
return sv;
}
/* new_SV(): return a new, empty SV head */
#ifdef DEBUG_LEAKING_SCALARS
/* provide a real function for a debugger to play with */
STATIC SV*
S_new_SV(pTHX_ const char *file, int line, const char *func)
{
SV* sv;
if (PL_sv_root)
uproot_SV(sv);
else
sv = S_more_sv(aTHX);
SvANY(sv) = 0;
SvREFCNT(sv) = 1;
SvFLAGS(sv) = 0;
sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
sv->sv_debug_line = (U16) (PL_parser && PL_parser->copline != NOLINE
? PL_parser->copline
: PL_curcop
? CopLINE(PL_curcop)
: 0
);
sv->sv_debug_inpad = 0;
sv->sv_debug_parent = NULL;
sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL;
sv->sv_debug_serial = PL_sv_serial++;
MEM_LOG_NEW_SV(sv, file, line, func);
DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) new_SV (from %s:%d [%s])\n",
PTR2UV(sv), (long)sv->sv_debug_serial, file, line, func));
return sv;
}
# define new_SV(p) (p)=S_new_SV(aTHX_ __FILE__, __LINE__, FUNCTION__)
#else
# define new_SV(p) \
STMT_START { \
if (PL_sv_root) \
uproot_SV(p); \
else \
(p) = S_more_sv(aTHX); \
SvANY(p) = 0; \
SvREFCNT(p) = 1; \
SvFLAGS(p) = 0; \
MEM_LOG_NEW_SV(p, __FILE__, __LINE__, FUNCTION__); \
} STMT_END
#endif
/* del_SV(): return an empty SV head to the free list */
#ifdef DEBUGGING
#define del_SV(p) \
STMT_START { \
if (DEBUG_D_TEST) \
del_sv(p); \
else \
plant_SV(p); \
} STMT_END
STATIC void
S_del_sv(pTHX_ SV *p)
{
dVAR;
PERL_ARGS_ASSERT_DEL_SV;
if (DEBUG_D_TEST) {
SV* sva;
bool ok = 0;
for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
const SV * const sv = sva + 1;
const SV * const svend = &sva[SvREFCNT(sva)];
if (p >= sv && p < svend) {
ok = 1;
break;
}
}
if (!ok) {
Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
"Attempt to free non-arena SV: 0x%"UVxf
pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
return;
}
}
plant_SV(p);
}
#else /* ! DEBUGGING */
#define del_SV(p) plant_SV(p)
#endif /* DEBUGGING */
/*
=head1 SV Manipulation Functions
=for apidoc sv_add_arena
Given a chunk of memory, link it to the head of the list of arenas,
and split it into a list of free SVs.
=cut
*/
static void
S_sv_add_arena(pTHX_ char *const ptr, const U32 size, const U32 flags)
{
dVAR;
SV *const sva = MUTABLE_SV(ptr);
SV* sv;
SV* svend;
PERL_ARGS_ASSERT_SV_ADD_ARENA;
/* The first SV in an arena isn't an SV. */
SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
SvFLAGS(sva) = flags; /* FAKE if not to be freed */
PL_sv_arenaroot = sva;
PL_sv_root = sva + 1;
svend = &sva[SvREFCNT(sva) - 1];
sv = sva + 1;
while (sv < svend) {
SvARENA_CHAIN_SET(sv, (sv + 1));
#ifdef DEBUGGING
SvREFCNT(sv) = 0;
#endif
/* Must always set typemask because it's always checked in on cleanup
when the arenas are walked looking for objects. */
SvFLAGS(sv) = SVTYPEMASK;
sv++;
}
SvARENA_CHAIN_SET(sv, 0);
#ifdef DEBUGGING
SvREFCNT(sv) = 0;
#endif
SvFLAGS(sv) = SVTYPEMASK;
}
/* visit(): call the named function for each non-free SV in the arenas
* whose flags field matches the flags/mask args. */
STATIC I32
S_visit(pTHX_ SVFUNC_t f, const U32 flags, const U32 mask)
{
dVAR;
SV* sva;
I32 visited = 0;
PERL_ARGS_ASSERT_VISIT;
for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
const SV * const svend = &sva[SvREFCNT(sva)];
SV* sv;
for (sv = sva + 1; sv < svend; ++sv) {
if (SvTYPE(sv) != (svtype)SVTYPEMASK
&& (sv->sv_flags & mask) == flags
&& SvREFCNT(sv))
{
(*f)(aTHX_ sv);
++visited;
}
}
}
return visited;
}
#ifdef DEBUGGING
/* called by sv_report_used() for each live SV */
static void
do_report_used(pTHX_ SV *const sv)
{
if (SvTYPE(sv) != (svtype)SVTYPEMASK) {
PerlIO_printf(Perl_debug_log, "****\n");
sv_dump(sv);
}
}
#endif
/*
=for apidoc sv_report_used
Dump the contents of all SVs not yet freed (debugging aid).
=cut
*/
void
Perl_sv_report_used(pTHX)
{
#ifdef DEBUGGING
visit(do_report_used, 0, 0);
#else
PERL_UNUSED_CONTEXT;
#endif
}
/* called by sv_clean_objs() for each live SV */
static void
do_clean_objs(pTHX_ SV *const ref)
{
dVAR;
assert (SvROK(ref));
{
SV * const target = SvRV(ref);
if (SvOBJECT(target)) {
DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(ref)));
if (SvWEAKREF(ref)) {
sv_del_backref(target, ref);
SvWEAKREF_off(ref);
SvRV_set(ref, NULL);
} else {
SvROK_off(ref);
SvRV_set(ref, NULL);
SvREFCNT_dec_NN(target);
}
}
}
}
/* clear any slots in a GV which hold objects - except IO;
* called by sv_clean_objs() for each live GV */
static void
do_clean_named_objs(pTHX_ SV *const sv)
{
dVAR;
SV *obj;
assert(SvTYPE(sv) == SVt_PVGV);
assert(isGV_with_GP(sv));
if (!GvGP(sv))
return;
/* freeing GP entries may indirectly free the current GV;
* hold onto it while we mess with the GP slots */
SvREFCNT_inc(sv);
if ( ((obj = GvSV(sv) )) && SvOBJECT(obj)) {
DEBUG_D((PerlIO_printf(Perl_debug_log,
"Cleaning named glob SV object:\n "), sv_dump(obj)));
GvSV(sv) = NULL;
SvREFCNT_dec_NN(obj);
}
if ( ((obj = MUTABLE_SV(GvAV(sv)) )) && SvOBJECT(obj)) {
DEBUG_D((PerlIO_printf(Perl_debug_log,
"Cleaning named glob AV object:\n "), sv_dump(obj)));
GvAV(sv) = NULL;
SvREFCNT_dec_NN(obj);
}
if ( ((obj = MUTABLE_SV(GvHV(sv)) )) && SvOBJECT(obj)) {
DEBUG_D((PerlIO_printf(Perl_debug_log,
"Cleaning named glob HV object:\n "), sv_dump(obj)));
GvHV(sv) = NULL;
SvREFCNT_dec_NN(obj);
}
if ( ((obj = MUTABLE_SV(GvCV(sv)) )) && SvOBJECT(obj)) {
DEBUG_D((PerlIO_printf(Perl_debug_log,
"Cleaning named glob CV object:\n "), sv_dump(obj)));
GvCV_set(sv, NULL);
SvREFCNT_dec_NN(obj);
}
SvREFCNT_dec_NN(sv); /* undo the inc above */
}
/* clear any IO slots in a GV which hold objects (except stderr, defout);
* called by sv_clean_objs() for each live GV */
static void
do_clean_named_io_objs(pTHX_ SV *const sv)
{
dVAR;
SV *obj;
assert(SvTYPE(sv) == SVt_PVGV);
assert(isGV_with_GP(sv));
if (!GvGP(sv) || sv == (SV*)PL_stderrgv || sv == (SV*)PL_defoutgv)
return;
SvREFCNT_inc(sv);
if ( ((obj = MUTABLE_SV(GvIO(sv)) )) && SvOBJECT(obj)) {
DEBUG_D((PerlIO_printf(Perl_debug_log,
"Cleaning named glob IO object:\n "), sv_dump(obj)));
GvIOp(sv) = NULL;
SvREFCNT_dec_NN(obj);
}
SvREFCNT_dec_NN(sv); /* undo the inc above */
}
/* Void wrapper to pass to visit() */
static void
do_curse(pTHX_ SV * const sv) {
if ((PL_stderrgv && GvGP(PL_stderrgv) && (SV*)GvIO(PL_stderrgv) == sv)
|| (PL_defoutgv && GvGP(PL_defoutgv) && (SV*)GvIO(PL_defoutgv) == sv))
return;
(void)curse(sv, 0);
}
/*
=for apidoc sv_clean_objs
Attempt to destroy all objects not yet freed.
=cut
*/
void
Perl_sv_clean_objs(pTHX)
{
dVAR;
GV *olddef, *olderr;
PL_in_clean_objs = TRUE;
visit(do_clean_objs, SVf_ROK, SVf_ROK);
/* Some barnacles may yet remain, clinging to typeglobs.
* Run the non-IO destructors first: they may want to output
* error messages, close files etc */
visit(do_clean_named_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
visit(do_clean_named_io_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
/* And if there are some very tenacious barnacles clinging to arrays,
closures, or what have you.... */
visit(do_curse, SVs_OBJECT, SVs_OBJECT);
olddef = PL_defoutgv;
PL_defoutgv = NULL; /* disable skip of PL_defoutgv */
if (olddef && isGV_with_GP(olddef))
do_clean_named_io_objs(aTHX_ MUTABLE_SV(olddef));
olderr = PL_stderrgv;
PL_stderrgv = NULL; /* disable skip of PL_stderrgv */
if (olderr && isGV_with_GP(olderr))
do_clean_named_io_objs(aTHX_ MUTABLE_SV(olderr));
SvREFCNT_dec(olddef);
PL_in_clean_objs = FALSE;
}
/* called by sv_clean_all() for each live SV */
static void
do_clean_all(pTHX_ SV *const sv)
{
dVAR;
if (sv == (const SV *) PL_fdpid || sv == (const SV *)PL_strtab) {
/* don't clean pid table and strtab */
return;
}
DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
SvFLAGS(sv) |= SVf_BREAK;
SvREFCNT_dec_NN(sv);
}
/*
=for apidoc sv_clean_all
Decrement the refcnt of each remaining SV, possibly triggering a
cleanup. This function may have to be called multiple times to free
SVs which are in complex self-referential hierarchies.
=cut
*/
I32
Perl_sv_clean_all(pTHX)
{
dVAR;
I32 cleaned;
PL_in_clean_all = TRUE;
cleaned = visit(do_clean_all, 0,0);
return cleaned;
}
/*
ARENASETS: a meta-arena implementation which separates arena-info
into struct arena_set, which contains an array of struct
arena_descs, each holding info for a single arena. By separating
the meta-info from the arena, we recover the 1st slot, formerly
borrowed for list management. The arena_set is about the size of an
arena, avoiding the needless malloc overhead of a naive linked-list.
The cost is 1 arena-set malloc per ~320 arena-mallocs, + the unused
memory in the last arena-set (1/2 on average). In trade, we get
back the 1st slot in each arena (ie 1.7% of a CV-arena, less for
smaller types). The recovery of the wasted space allows use of
small arenas for large, rare body types, by changing array* fields
in body_details_by_type[] below.
*/
struct arena_desc {
char *arena; /* the raw storage, allocated aligned */
size_t size; /* its size ~4k typ */
svtype utype; /* bodytype stored in arena */
};
struct arena_set;
/* Get the maximum number of elements in set[] such that struct arena_set
will fit within PERL_ARENA_SIZE, which is probably just under 4K, and
therefore likely to be 1 aligned memory page. */
#define ARENAS_PER_SET ((PERL_ARENA_SIZE - sizeof(struct arena_set*) \
- 2 * sizeof(int)) / sizeof (struct arena_desc))
struct arena_set {
struct arena_set* next;
unsigned int set_size; /* ie ARENAS_PER_SET */
unsigned int curr; /* index of next available arena-desc */
struct arena_desc set[ARENAS_PER_SET];
};
/*
=for apidoc sv_free_arenas
Deallocate the memory used by all arenas. Note that all the individual SV
heads and bodies within the arenas must already have been freed.
=cut
*/
void
Perl_sv_free_arenas(pTHX)
{
dVAR;
SV* sva;
SV* svanext;
unsigned int i;
/* Free arenas here, but be careful about fake ones. (We assume
contiguity of the fake ones with the corresponding real ones.) */
for (sva = PL_sv_arenaroot; sva; sva = svanext) {
svanext = MUTABLE_SV(SvANY(sva));
while (svanext && SvFAKE(svanext))
svanext = MUTABLE_SV(SvANY(svanext));
if (!SvFAKE(sva))
Safefree(sva);
}
{
struct arena_set *aroot = (struct arena_set*) PL_body_arenas;
while (aroot) {
struct arena_set *current = aroot;
i = aroot->curr;
while (i--) {
assert(aroot->set[i].arena);
Safefree(aroot->set[i].arena);
}
aroot = aroot->next;
Safefree(current);
}
}
PL_body_arenas = 0;
i = PERL_ARENA_ROOTS_SIZE;
while (i--)
PL_body_roots[i] = 0;
PL_sv_arenaroot = 0;
PL_sv_root = 0;
}
/*
Here are mid-level routines that manage the allocation of bodies out
of the various arenas. There are 5 kinds of arenas:
1. SV-head arenas, which are discussed and handled above
2. regular body arenas
3. arenas for reduced-size bodies
4. Hash-Entry arenas
Arena types 2 & 3 are chained by body-type off an array of
arena-root pointers, which is indexed by svtype. Some of the
larger/less used body types are malloced singly, since a large
unused block of them is wasteful. Also, several svtypes dont have
bodies; the data fits into the sv-head itself. The arena-root
pointer thus has a few unused root-pointers (which may be hijacked
later for arena types 4,5)
3 differs from 2 as an optimization; some body types have several
unused fields in the front of the structure (which are kept in-place
for consistency). These bodies can be allocated in smaller chunks,
because the leading fields arent accessed. Pointers to such bodies
are decremented to point at the unused 'ghost' memory, knowing that
the pointers are used with offsets to the real memory.
=head1 SV-Body Allocation
Allocation of SV-bodies is similar to SV-heads, differing as follows;
the allocation mechanism is used for many body types, so is somewhat
more complicated, it uses arena-sets, and has no need for still-live
SV detection.
At the outermost level, (new|del)_X*V macros return bodies of the
appropriate type. These macros call either (new|del)_body_type or
(new|del)_body_allocated macro pairs, depending on specifics of the
type. Most body types use the former pair, the latter pair is used to
allocate body types with "ghost fields".
"ghost fields" are fields that are unused in certain types, and
consequently don't need to actually exist. They are declared because
they're part of a "base type", which allows use of functions as
methods. The simplest examples are AVs and HVs, 2 aggregate types
which don't use the fields which support SCALAR semantics.
For these types, the arenas are carved up into appropriately sized
chunks, we thus avoid wasted memory for those unaccessed members.
When bodies are allocated, we adjust the pointer back in memory by the
size of the part not allocated, so it's as if we allocated the full
structure. (But things will all go boom if you write to the part that
is "not there", because you'll be overwriting the last members of the
preceding structure in memory.)
We calculate the correction using the STRUCT_OFFSET macro on the first
member present. If the allocated structure is smaller (no initial NV
actually allocated) then the net effect is to subtract the size of the NV
from the pointer, to return a new pointer as if an initial NV were actually
allocated. (We were using structures named *_allocated for this, but
this turned out to be a subtle bug, because a structure without an NV
could have a lower alignment constraint, but the compiler is allowed to
optimised accesses based on the alignment constraint of the actual pointer
to the full structure, for example, using a single 64 bit load instruction
because it "knows" that two adjacent 32 bit members will be 8-byte aligned.)
This is the same trick as was used for NV and IV bodies. Ironically it
doesn't need to be used for NV bodies any more, because NV is now at
the start of the structure. IV bodies don't need it either, because
they are no longer allocated.
In turn, the new_body_* allocators call S_new_body(), which invokes
new_body_inline macro, which takes a lock, and takes a body off the
linked list at PL_body_roots[sv_type], calling Perl_more_bodies() if
necessary to refresh an empty list. Then the lock is released, and
the body is returned.
Perl_more_bodies allocates a new arena, and carves it up into an array of N
bodies, which it strings into a linked list. It looks up arena-size
and body-size from the body_details table described below, thus
supporting the multiple body-types.
If PURIFY is defined, or PERL_ARENA_SIZE=0, arenas are not used, and
the (new|del)_X*V macros are mapped directly to malloc/free.
For each sv-type, struct body_details bodies_by_type[] carries
parameters which control these aspects of SV handling:
Arena_size determines whether arenas are used for this body type, and if
so, how big they are. PURIFY or PERL_ARENA_SIZE=0 set this field to
zero, forcing individual mallocs and frees.
Body_size determines how big a body is, and therefore how many fit into
each arena. Offset carries the body-pointer adjustment needed for
"ghost fields", and is used in *_allocated macros.
But its main purpose is to parameterize info needed in
Perl_sv_upgrade(). The info here dramatically simplifies the function
vs the implementation in 5.8.8, making it table-driven. All fields
are used for this, except for arena_size.
For the sv-types that have no bodies, arenas are not used, so those
PL_body_roots[sv_type] are unused, and can be overloaded. In
something of a special case, SVt_NULL is borrowed for HE arenas;
PL_body_roots[HE_SVSLOT=SVt_NULL] is filled by S_more_he, but the
bodies_by_type[SVt_NULL] slot is not used, as the table is not
available in hv.c.
*/
struct body_details {
U8 body_size; /* Size to allocate */
U8 copy; /* Size of structure to copy (may be shorter) */
U8 offset;
unsigned int type : 4; /* We have space for a sanity check. */
unsigned int cant_upgrade : 1; /* Cannot upgrade this type */
unsigned int zero_nv : 1; /* zero the NV when upgrading from this */
unsigned int arena : 1; /* Allocated from an arena */
size_t arena_size; /* Size of arena to allocate */
};
#define HADNV FALSE
#define NONV TRUE
#ifdef PURIFY
/* With -DPURFIY we allocate everything directly, and don't use arenas.
This seems a rather elegant way to simplify some of the code below. */
#define HASARENA FALSE
#else
#define HASARENA TRUE
#endif
#define NOARENA FALSE
/* Size the arenas to exactly fit a given number of bodies. A count
of 0 fits the max number bodies into a PERL_ARENA_SIZE.block,
simplifying the default. If count > 0, the arena is sized to fit
only that many bodies, allowing arenas to be used for large, rare
bodies (XPVFM, XPVIO) without undue waste. The arena size is
limited by PERL_ARENA_SIZE, so we can safely oversize the
declarations.
*/
#define FIT_ARENA0(body_size) \
((size_t)(PERL_ARENA_SIZE / body_size) * body_size)
#define FIT_ARENAn(count,body_size) \
( count * body_size <= PERL_ARENA_SIZE) \
? count * body_size \
: FIT_ARENA0 (body_size)
#define FIT_ARENA(count,body_size) \
count \
? FIT_ARENAn (count, body_size) \
: FIT_ARENA0 (body_size)
/* Calculate the length to copy. Specifically work out the length less any
final padding the compiler needed to add. See the comment in sv_upgrade
for why copying the padding proved to be a bug. */
#define copy_length(type, last_member) \
STRUCT_OFFSET(type, last_member) \
+ sizeof (((type*)SvANY((const SV *)0))->last_member)
static const struct body_details bodies_by_type[] = {
/* HEs use this offset for their arena. */
{ 0, 0, 0, SVt_NULL, FALSE, NONV, NOARENA, 0 },
/* IVs are in the head, so the allocation size is 0. */
{ 0,
sizeof(IV), /* This is used to copy out the IV body. */
STRUCT_OFFSET(XPVIV, xiv_iv), SVt_IV, FALSE, NONV,
NOARENA /* IVS don't need an arena */, 0
},
{ sizeof(NV), sizeof(NV),
STRUCT_OFFSET(XPVNV, xnv_u),
SVt_NV, FALSE, HADNV, HASARENA, FIT_ARENA(0, sizeof(NV)) },
{ sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur),
copy_length(XPV, xpv_len) - STRUCT_OFFSET(XPV, xpv_cur),
+ STRUCT_OFFSET(XPV, xpv_cur),
SVt_PV, FALSE, NONV, HASARENA,
FIT_ARENA(0, sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur)) },
{ sizeof(XINVLIST) - STRUCT_OFFSET(XPV, xpv_cur),
copy_length(XINVLIST, is_offset) - STRUCT_OFFSET(XPV, xpv_cur),
+ STRUCT_OFFSET(XPV, xpv_cur),
SVt_INVLIST, TRUE, NONV, HASARENA,
FIT_ARENA(0, sizeof(XINVLIST) - STRUCT_OFFSET(XPV, xpv_cur)) },
{ sizeof(XPVIV) - STRUCT_OFFSET(XPV, xpv_cur),
copy_length(XPVIV, xiv_u) - STRUCT_OFFSET(XPV, xpv_cur),
+ STRUCT_OFFSET(XPV, xpv_cur),
SVt_PVIV, FALSE, NONV, HASARENA,
FIT_ARENA(0, sizeof(XPVIV) - STRUCT_OFFSET(XPV, xpv_cur)) },
{ sizeof(XPVNV) - STRUCT_OFFSET(XPV, xpv_cur),
copy_length(XPVNV, xnv_u) - STRUCT_OFFSET(XPV, xpv_cur),
+ STRUCT_OFFSET(XPV, xpv_cur),
SVt_PVNV, FALSE, HADNV, HASARENA,
FIT_ARENA(0, sizeof(XPVNV) - STRUCT_OFFSET(XPV, xpv_cur)) },
{ sizeof(XPVMG), copy_length(XPVMG, xnv_u), 0, SVt_PVMG, FALSE, HADNV,
HASARENA, FIT_ARENA(0, sizeof(XPVMG)) },
{ sizeof(regexp),
sizeof(regexp),
0,
SVt_REGEXP, TRUE, NONV, HASARENA,
FIT_ARENA(0, sizeof(regexp))
},
{ sizeof(XPVGV), sizeof(XPVGV), 0, SVt_PVGV, TRUE, HADNV,
HASARENA, FIT_ARENA(0, sizeof(XPVGV)) },
{ sizeof(XPVLV), sizeof(XPVLV), 0, SVt_PVLV, TRUE, HADNV,
HASARENA, FIT_ARENA(0, sizeof(XPVLV)) },
{ sizeof(XPVAV),
copy_length(XPVAV, xav_alloc),
0,
SVt_PVAV, TRUE, NONV, HASARENA,
FIT_ARENA(0, sizeof(XPVAV)) },
{ sizeof(XPVHV),
copy_length(XPVHV, xhv_max),
0,
SVt_PVHV, TRUE, NONV, HASARENA,
FIT_ARENA(0, sizeof(XPVHV)) },
{ sizeof(XPVCV),
sizeof(XPVCV),
0,
SVt_PVCV, TRUE, NONV, HASARENA,
FIT_ARENA(0, sizeof(XPVCV)) },
{ sizeof(XPVFM),
sizeof(XPVFM),
0,
SVt_PVFM, TRUE, NONV, NOARENA,
FIT_ARENA(20, sizeof(XPVFM)) },
{ sizeof(XPVIO),
sizeof(XPVIO),
0,
SVt_PVIO, TRUE, NONV, HASARENA,
FIT_ARENA(24, sizeof(XPVIO)) },
};
#define new_body_allocated(sv_type) \
(void *)((char *)S_new_body(aTHX_ sv_type) \
- bodies_by_type[sv_type].offset)
/* return a thing to the free list */
#define del_body(thing, root) \
STMT_START { \
void ** const thing_copy = (void **)thing; \
*thing_copy = *root; \
*root = (void*)thing_copy; \
} STMT_END
#ifdef PURIFY
#define new_XNV() safemalloc(sizeof(XPVNV))
#define new_XPVNV() safemalloc(sizeof(XPVNV))
#define new_XPVMG() safemalloc(sizeof(XPVMG))
#define del_XPVGV(p) safefree(p)
#else /* !PURIFY */
#define new_XNV() new_body_allocated(SVt_NV)
#define new_XPVNV() new_body_allocated(SVt_PVNV)
#define new_XPVMG() new_body_allocated(SVt_PVMG)
#define del_XPVGV(p) del_body(p + bodies_by_type[SVt_PVGV].offset, \
&PL_body_roots[SVt_PVGV])
#endif /* PURIFY */
/* no arena for you! */
#define new_NOARENA(details) \
safemalloc((details)->body_size + (details)->offset)
#define new_NOARENAZ(details) \
safecalloc((details)->body_size + (details)->offset, 1)
void *
Perl_more_bodies (pTHX_ const svtype sv_type, const size_t body_size,
const size_t arena_size)
{
dVAR;
void ** const root = &PL_body_roots[sv_type];
struct arena_desc *adesc;
struct arena_set *aroot = (struct arena_set *) PL_body_arenas;
unsigned int curr;
char *start;
const char *end;
const size_t good_arena_size = Perl_malloc_good_size(arena_size);
#if defined(DEBUGGING) && !defined(PERL_GLOBAL_STRUCT_PRIVATE)
static bool done_sanity_check;
/* PERL_GLOBAL_STRUCT_PRIVATE cannot coexist with global
* variables like done_sanity_check. */
if (!done_sanity_check) {
unsigned int i = SVt_LAST;
done_sanity_check = TRUE;
while (i--)
assert (bodies_by_type[i].type == i);
}
#endif
assert(arena_size);
/* may need new arena-set to hold new arena */
if (!aroot || aroot->curr >= aroot->set_size) {
struct arena_set *newroot;
Newxz(newroot, 1, struct arena_set);
newroot->set_size = ARENAS_PER_SET;
newroot->next = aroot;
aroot = newroot;
PL_body_arenas = (void *) newroot;
DEBUG_m(PerlIO_printf(Perl_debug_log, "new arenaset %p\n", (void*)aroot));
}
/* ok, now have arena-set with at least 1 empty/available arena-desc */
curr = aroot->curr++;
adesc = &(aroot->set[curr]);
assert(!adesc->arena);
Newx(adesc->arena, good_arena_size, char);
adesc->size = good_arena_size;
adesc->utype = sv_type;
DEBUG_m(PerlIO_printf(Perl_debug_log, "arena %d added: %p size %"UVuf"\n",
curr, (void*)adesc->arena, (UV)good_arena_size));
start = (char *) adesc->arena;
/* Get the address of the byte after the end of the last body we can fit.
Remember, this is integer division: */
end = start + good_arena_size / body_size * body_size;
/* computed count doesn't reflect the 1st slot reservation */
#if defined(MYMALLOC) || defined(HAS_MALLOC_GOOD_SIZE)
DEBUG_m(PerlIO_printf(Perl_debug_log,
"arena %p end %p arena-size %d (from %d) type %d "
"size %d ct %d\n",
(void*)start, (void*)end, (int)good_arena_size,
(int)arena_size, sv_type, (int)body_size,
(int)good_arena_size / (int)body_size));
#else
DEBUG_m(PerlIO_printf(Perl_debug_log,
"arena %p end %p arena-size %d type %d size %d ct %d\n",
(void*)start, (void*)end,
(int)arena_size, sv_type, (int)body_size,
(int)good_arena_size / (int)body_size));
#endif
*root = (void *)start;
while (1) {
/* Where the next body would start: */
char * const next = start + body_size;
if (next >= end) {
/* This is the last body: */
assert(next == end);
*(void **)start = 0;
return *root;
}
*(void**) start = (void *)next;
start = next;
}
}
/* grab a new thing from the free list, allocating more if necessary.
The inline version is used for speed in hot routines, and the
function using it serves the rest (unless PURIFY).
*/
#define new_body_inline(xpv, sv_type) \
STMT_START { \
void ** const r3wt = &PL_body_roots[sv_type]; \
xpv = (PTR_TBL_ENT_t*) (*((void **)(r3wt)) \
? *((void **)(r3wt)) : Perl_more_bodies(aTHX_ sv_type, \
bodies_by_type[sv_type].body_size,\
bodies_by_type[sv_type].arena_size)); \
*(r3wt) = *(void**)(xpv); \
} STMT_END
#ifndef PURIFY
STATIC void *
S_new_body(pTHX_ const svtype sv_type)
{
dVAR;
void *xpv;
new_body_inline(xpv, sv_type);
return xpv;
}
#endif
static const struct body_details fake_rv =
{ 0, 0, 0, SVt_IV, FALSE, NONV, NOARENA, 0 };
/*
=for apidoc sv_upgrade
Upgrade an SV to a more complex form. Generally adds a new body type to the
SV, then copies across as much information as possible from the old body.
It croaks if the SV is already in a more complex form than requested. You
generally want to use the C<SvUPGRADE> macro wrapper, which checks the type
before calling C<sv_upgrade>, and hence does not croak. See also
C<svtype>.
=cut
*/
void
Perl_sv_upgrade(pTHX_ SV *const sv, svtype new_type)
{
dVAR;
void* old_body;
void* new_body;
const svtype old_type = SvTYPE(sv);
const struct body_details *new_type_details;
const struct body_details *old_type_details
= bodies_by_type + old_type;
SV *referant = NULL;
PERL_ARGS_ASSERT_SV_UPGRADE;
if (old_type == new_type)
return;
/* This clause was purposefully added ahead of the early return above to
the shared string hackery for (sort {$a <=> $b} keys %hash), with the
inference by Nick I-S that it would fix other troublesome cases. See
changes 7162, 7163 (f130fd4589cf5fbb24149cd4db4137c8326f49c1 and parent)
Given that shared hash key scalars are no longer PVIV, but PV, there is
no longer need to unshare so as to free up the IVX slot for its proper
purpose. So it's safe to move the early return earlier. */
if (new_type > SVt_PVMG && SvIsCOW(sv)) {
sv_force_normal_flags(sv, 0);
}
old_body = SvANY(sv);
/* Copying structures onto other structures that have been neatly zeroed
has a subtle gotcha. Consider XPVMG
+------+------+------+------+------+-------+-------+
| NV | CUR | LEN | IV | MAGIC | STASH |
+------+------+------+------+------+-------+-------+
0 4 8 12 16 20 24 28
where NVs are aligned to 8 bytes, so that sizeof that structure is
actually 32 bytes long, with 4 bytes of padding at the end:
+------+------+------+------+------+-------+-------+------+
| NV | CUR | LEN | IV | MAGIC | STASH | ??? |
+------+------+------+------+------+-------+-------+------+
0 4 8 12 16 20 24 28 32
so what happens if you allocate memory for this structure:
+------+------+------+------+------+-------+-------+------+------+...
| NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME |
+------+------+------+------+------+-------+-------+------+------+...
0 4 8 12 16 20 24 28 32 36
zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
expect, because you copy the area marked ??? onto GP. Now, ??? may have
started out as zero once, but it's quite possible that it isn't. So now,
rather than a nicely zeroed GP, you have it pointing somewhere random.
Bugs ensue.
(In fact, GP ends up pointing at a previous GP structure, because the
principle cause of the padding in XPVMG getting garbage is a copy of
sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob. Right now
this happens to be moot because XPVGV has been re-ordered, with GP
no longer after STASH)
So we are careful and work out the size of used parts of all the
structures. */
switch (old_type) {
case SVt_NULL:
break;
case SVt_IV:
if (SvROK(sv)) {
referant = SvRV(sv);
old_type_details = &fake_rv;
if (new_type == SVt_NV)
new_type = SVt_PVNV;
} else {
if (new_type < SVt_PVIV) {
new_type = (new_type == SVt_NV)
? SVt_PVNV : SVt_PVIV;
}
}
break;
case SVt_NV:
if (new_type < SVt_PVNV) {
new_type = SVt_PVNV;
}
break;
case SVt_PV:
assert(new_type > SVt_PV);
assert(SVt_IV < SVt_PV);
assert(SVt_NV < SVt_PV);
break;
case SVt_PVIV:
break;
case SVt_PVNV:
break;
case SVt_PVMG:
/* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
there's no way that it can be safely upgraded, because perl.c
expects to Safefree(SvANY(PL_mess_sv)) */
assert(sv != PL_mess_sv);
/* This flag bit is used to mean other things in other scalar types.
Given that it only has meaning inside the pad, it shouldn't be set
on anything that can get upgraded. */
assert(!SvPAD_TYPED(sv));
break;
default:
if (UNLIKELY(old_type_details->cant_upgrade))
Perl_croak(aTHX_ "Can't upgrade %s (%" UVuf ") to %" UVuf,
sv_reftype(sv, 0), (UV) old_type, (UV) new_type);
}
if (UNLIKELY(old_type > new_type))
Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
(int)old_type, (int)new_type);
new_type_details = bodies_by_type + new_type;
SvFLAGS(sv) &= ~SVTYPEMASK;
SvFLAGS(sv) |= new_type;
/* This can't happen, as SVt_NULL is <= all values of new_type, so one of
the return statements above will have triggered. */
assert (new_type != SVt_NULL);
switch (new_type) {
case SVt_IV:
assert(old_type == SVt_NULL);
SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
SvIV_set(sv, 0);
return;
case SVt_NV:
assert(old_type == SVt_NULL);
SvANY(sv) = new_XNV();
SvNV_set(sv, 0);
return;
case SVt_PVHV:
case SVt_PVAV:
assert(new_type_details->body_size);
#ifndef PURIFY
assert(new_type_details->arena);
assert(new_type_details->arena_size);
/* This points to the start of the allocated area. */
new_body_inline(new_body, new_type);
Zero(new_body, new_type_details->body_size, char);
new_body = ((char *)new_body) - new_type_details->offset;
#else
/* We always allocated the full length item with PURIFY. To do this
we fake things so that arena is false for all 16 types.. */
new_body = new_NOARENAZ(new_type_details);
#endif
SvANY(sv) = new_body;
if (new_type == SVt_PVAV) {
AvMAX(sv) = -1;
AvFILLp(sv) = -1;
AvREAL_only(sv);
if (old_type_details->body_size) {
AvALLOC(sv) = 0;
} else {
/* It will have been zeroed when the new body was allocated.
Lets not write to it, in case it confuses a write-back
cache. */
}
} else {
assert(!SvOK(sv));
SvOK_off(sv);
#ifndef NODEFAULT_SHAREKEYS
HvSHAREKEYS_on(sv); /* key-sharing on by default */
#endif
/* start with PERL_HASH_DEFAULT_HvMAX+1 buckets: */
HvMAX(sv) = PERL_HASH_DEFAULT_HvMAX;
}
/* SVt_NULL isn't the only thing upgraded to AV or HV.
The target created by newSVrv also is, and it can have magic.
However, it never has SvPVX set.
*/
if (old_type == SVt_IV) {
assert(!SvROK(sv));
} else if (old_type >= SVt_PV) {
assert(SvPVX_const(sv) == 0);
}
if (old_type >= SVt_PVMG) {
SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_u.xmg_magic);
SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
} else {
sv->sv_u.svu_array = NULL; /* or svu_hash */
}
break;
case SVt_PVIV:
/* XXX Is this still needed? Was it ever needed? Surely as there is
no route from NV to PVIV, NOK can never be true */
assert(!SvNOKp(sv));
assert(!SvNOK(sv));
case SVt_PVIO:
case SVt_PVFM:
case SVt_PVGV:
case SVt_PVCV:
case SVt_PVLV:
case SVt_INVLIST:
case SVt_REGEXP:
case SVt_PVMG:
case SVt_PVNV:
case SVt_PV:
assert(new_type_details->body_size);
/* We always allocated the full length item with PURIFY. To do this
we fake things so that arena is false for all 16 types.. */
if(new_type_details->arena) {
/* This points to the start of the allocated area. */
new_body_inline(new_body, new_type);
Zero(new_body, new_type_details->body_size, char);
new_body = ((char *)new_body) - new_type_details->offset;
} else {
new_body = new_NOARENAZ(new_type_details);
}
SvANY(sv) = new_body;
if (old_type_details->copy) {
/* There is now the potential for an upgrade from something without
an offset (PVNV or PVMG) to something with one (PVCV, PVFM) */
int offset = old_type_details->offset;
int length = old_type_details->copy;
if (new_type_details->offset > old_type_details->offset) {
const int difference
= new_type_details->offset - old_type_details->offset;
offset += difference;
length -= difference;
}
assert (length >= 0);
Copy((char *)old_body + offset, (char *)new_body + offset, length,
char);
}
#ifndef NV_ZERO_IS_ALLBITS_ZERO
/* If NV 0.0 is stores as all bits 0 then Zero() already creates a
* correct 0.0 for us. Otherwise, if the old body didn't have an
* NV slot, but the new one does, then we need to initialise the
* freshly created NV slot with whatever the correct bit pattern is
* for 0.0 */
if (old_type_details->zero_nv && !new_type_details->zero_nv
&& !isGV_with_GP(sv))
SvNV_set(sv, 0);
#endif
if (UNLIKELY(new_type == SVt_PVIO)) {
IO * const io = MUTABLE_IO(sv);
GV *iogv = gv_fetchpvs("IO::File::", GV_ADD, SVt_PVHV);
SvOBJECT_on(io);
/* Clear the stashcache because a new IO could overrule a package
name */
DEBUG_o(Perl_deb(aTHX_ "sv_upgrade clearing PL_stashcache\n"));
hv_clear(PL_stashcache);
SvSTASH_set(io, MUTABLE_HV(SvREFCNT_inc(GvHV(iogv))));
IoPAGE_LEN(sv) = 60;
}
if (UNLIKELY(new_type == SVt_REGEXP))
sv->sv_u.svu_rx = (regexp *)new_body;
else if (old_type < SVt_PV) {
/* referant will be NULL unless the old type was SVt_IV emulating
SVt_RV */
sv->sv_u.svu_rv = referant;
}
break;
default:
Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu",
(unsigned long)new_type);
}
if (old_type > SVt_IV) {
#ifdef PURIFY
safefree(old_body);
#else
/* Note that there is an assumption that all bodies of types that
can be upgraded came from arenas. Only the more complex non-
upgradable types are allowed to be directly malloc()ed. */
assert(old_type_details->arena);
del_body((void*)((char*)old_body + old_type_details->offset),
&PL_body_roots[old_type]);
#endif
}
}
/*
=for apidoc sv_backoff
Remove any string offset. You should normally use the C<SvOOK_off> macro
wrapper instead.
=cut
*/
int
Perl_sv_backoff(pTHX_ SV *const sv)
{
STRLEN delta;
const char * const s = SvPVX_const(sv);
PERL_ARGS_ASSERT_SV_BACKOFF;
PERL_UNUSED_CONTEXT;
assert(SvOOK(sv));
assert(SvTYPE(sv) != SVt_PVHV);
assert(SvTYPE(sv) != SVt_PVAV);
SvOOK_offset(sv, delta);
SvLEN_set(sv, SvLEN(sv) + delta);
SvPV_set(sv, SvPVX(sv) - delta);
Move(s, SvPVX(sv), SvCUR(sv)+1, char);
SvFLAGS(sv) &= ~SVf_OOK;
return 0;
}
/*
=for apidoc sv_grow
Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
Use the C<SvGROW> wrapper instead.
=cut
*/
static void S_sv_uncow(pTHX_ SV * const sv, const U32 flags);
char *
Perl_sv_grow(pTHX_ SV *const sv, STRLEN newlen)
{
char *s;
PERL_ARGS_ASSERT_SV_GROW;
if (SvROK(sv))
sv_unref(sv);
if (SvTYPE(sv) < SVt_PV) {
sv_upgrade(sv, SVt_PV);
s = SvPVX_mutable(sv);
}
else if (SvOOK(sv)) { /* pv is offset? */
sv_backoff(sv);
s = SvPVX_mutable(sv);
if (newlen > SvLEN(sv))
newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
}
else
{
if (SvIsCOW(sv)) S_sv_uncow(aTHX_ sv, 0);
s = SvPVX_mutable(sv);
}
#ifdef PERL_NEW_COPY_ON_WRITE
/* the new COW scheme uses SvPVX(sv)[SvLEN(sv)-1] (if spare)
* to store the COW count. So in general, allocate one more byte than
* asked for, to make it likely this byte is always spare: and thus
* make more strings COW-able.
* If the new size is a big power of two, don't bother: we assume the
* caller wanted a nice 2^N sized block and will be annoyed at getting
* 2^N+1 */
if (newlen & 0xff)
newlen++;
#endif
if (newlen > SvLEN(sv)) { /* need more room? */
STRLEN minlen = SvCUR(sv);
minlen += (minlen >> PERL_STRLEN_EXPAND_SHIFT) + 10;
if (newlen < minlen)
newlen = minlen;
#ifndef Perl_safesysmalloc_size
newlen = PERL_STRLEN_ROUNDUP(newlen);
#endif
if (SvLEN(sv) && s) {
s = (char*)saferealloc(s, newlen);
}
else {
s = (char*)safemalloc(newlen);
if (SvPVX_const(sv) && SvCUR(sv)) {
Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
}
}
SvPV_set(sv, s);
#ifdef Perl_safesysmalloc_size
/* Do this here, do it once, do it right, and then we will never get
called back into sv_grow() unless there really is some growing
needed. */
SvLEN_set(sv, Perl_safesysmalloc_size(s));
#else
SvLEN_set(sv, newlen);
#endif
}
return s;
}
/*
=for apidoc sv_setiv
Copies an integer into the given SV, upgrading first if necessary.
Does not handle 'set' magic. See also C<sv_setiv_mg>.
=cut
*/
void
Perl_sv_setiv(pTHX_ SV *const sv, const IV i)
{
dVAR;
PERL_ARGS_ASSERT_SV_SETIV;
SV_CHECK_THINKFIRST_COW_DROP(sv);
switch (SvTYPE(sv)) {
case SVt_NULL:
case SVt_NV:
sv_upgrade(sv, SVt_IV);
break;
case SVt_PV:
sv_upgrade(sv, SVt_PVIV);
break;
case SVt_PVGV:
if (!isGV_with_GP(sv))
break;
case SVt_PVAV:
case SVt_PVHV:
case SVt_PVCV:
case SVt_PVFM:
case SVt_PVIO:
/* diag_listed_as: Can't coerce %s to %s in %s */
Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
OP_DESC(PL_op));
default: NOOP;
}
(void)SvIOK_only(sv); /* validate number */
SvIV_set(sv, i);
SvTAINT(sv);
}
/*
=for apidoc sv_setiv_mg
Like C<sv_setiv>, but also handles 'set' magic.
=cut
*/
void
Perl_sv_setiv_mg(pTHX_ SV *const sv, const IV i)
{
PERL_ARGS_ASSERT_SV_SETIV_MG;
sv_setiv(sv,i);
SvSETMAGIC(sv);
}
/*
=for apidoc sv_setuv
Copies an unsigned integer into the given SV, upgrading first if necessary.
Does not handle 'set' magic. See also C<sv_setuv_mg>.
=cut
*/
void
Perl_sv_setuv(pTHX_ SV *const sv, const UV u)
{
PERL_ARGS_ASSERT_SV_SETUV;
/* With the if statement to ensure that integers are stored as IVs whenever
possible:
u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
without
u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
If you wish to remove the following if statement, so that this routine
(and its callers) always return UVs, please benchmark to see what the
effect is. Modern CPUs may be different. Or may not :-)
*/
if (u <= (UV)IV_MAX) {
sv_setiv(sv, (IV)u);
return;
}
sv_setiv(sv, 0);
SvIsUV_on(sv);
SvUV_set(sv, u);
}
/*
=for apidoc sv_setuv_mg
Like C<sv_setuv>, but also handles 'set' magic.
=cut
*/
void
Perl_sv_setuv_mg(pTHX_ SV *const sv, const UV u)
{
PERL_ARGS_ASSERT_SV_SETUV_MG;
sv_setuv(sv,u);
SvSETMAGIC(sv);
}
/*
=for apidoc sv_setnv
Copies a double into the given SV, upgrading first if necessary.
Does not handle 'set' magic. See also C<sv_setnv_mg>.
=cut
*/
void
Perl_sv_setnv(pTHX_ SV *const sv, const NV num)
{
dVAR;
PERL_ARGS_ASSERT_SV_SETNV;
SV_CHECK_THINKFIRST_COW_DROP(sv);
switch (SvTYPE(sv)) {
case SVt_NULL:
case SVt_IV:
sv_upgrade(sv, SVt_NV);
break;
case SVt_PV:
case SVt_PVIV:
sv_upgrade(sv, SVt_PVNV);
break;
case SVt_PVGV:
if (!isGV_with_GP(sv))
break;
case SVt_PVAV:
case SVt_PVHV:
case SVt_PVCV:
case SVt_PVFM:
case SVt_PVIO:
/* diag_listed_as: Can't coerce %s to %s in %s */
Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
OP_DESC(PL_op));
default: NOOP;
}
SvNV_set(sv, num);
(void)SvNOK_only(sv); /* validate number */
SvTAINT(sv);
}
/*
=for apidoc sv_setnv_mg
Like C<sv_setnv>, but also handles 'set' magic.
=cut
*/
void
Perl_sv_setnv_mg(pTHX_ SV *const sv, const NV num)
{
PERL_ARGS_ASSERT_SV_SETNV_MG;
sv_setnv(sv,num);
SvSETMAGIC(sv);
}
/* Return a cleaned-up, printable version of sv, for non-numeric, or
* not incrementable warning display.
* Originally part of S_not_a_number().
* The return value may be != tmpbuf.
*/
STATIC const char *
S_sv_display(pTHX_ SV *const sv, char *tmpbuf, STRLEN tmpbuf_size) {
const char *pv;
PERL_ARGS_ASSERT_SV_DISPLAY;
if (DO_UTF8(sv)) {
SV *dsv = newSVpvs_flags("", SVs_TEMP);
pv = sv_uni_display(dsv, sv, 10, UNI_DISPLAY_ISPRINT);
} else {
char *d = tmpbuf;
const char * const limit = tmpbuf + tmpbuf_size - 8;
/* each *s can expand to 4 chars + "...\0",
i.e. need room for 8 chars */
const char *s = SvPVX_const(sv);
const char * const end = s + SvCUR(sv);
for ( ; s < end && d < limit; s++ ) {
int ch = *s & 0xFF;
if (! isASCII(ch) && !isPRINT_LC(ch)) {
*d++ = 'M';
*d++ = '-';
/* Map to ASCII "equivalent" of Latin1 */
ch = LATIN1_TO_NATIVE(NATIVE_TO_LATIN1(ch) & 127);
}
if (ch == '\n') {
*d++ = '\\';
*d++ = 'n';
}
else if (ch == '\r') {
*d++ = '\\';
*d++ = 'r';
}
else if (ch == '\f') {
*d++ = '\\';
*d++ = 'f';
}
else if (ch == '\\') {
*d++ = '\\';
*d++ = '\\';
}
else if (ch == '\0') {
*d++ = '\\';
*d++ = '0';
}
else if (isPRINT_LC(ch))
*d++ = ch;
else {
*d++ = '^';
*d++ = toCTRL(ch);
}
}
if (s < end) {
*d++ = '.';
*d++ = '.';
*d++ = '.';
}
*d = '\0';
pv = tmpbuf;
}
return pv;
}
/* Print an "isn't numeric" warning, using a cleaned-up,
* printable version of the offending string
*/
STATIC void
S_not_a_number(pTHX_ SV *const sv)
{
dVAR;
char tmpbuf[64];
const char *pv;
PERL_ARGS_ASSERT_NOT_A_NUMBER;
pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
if (PL_op)
Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
/* diag_listed_as: Argument "%s" isn't numeric%s */
"Argument \"%s\" isn't numeric in %s", pv,
OP_DESC(PL_op));
else
Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
/* diag_listed_as: Argument "%s" isn't numeric%s */
"Argument \"%s\" isn't numeric", pv);
}
STATIC void
S_not_incrementable(pTHX_ SV *const sv) {
dVAR;
char tmpbuf[64];
const char *pv;
PERL_ARGS_ASSERT_NOT_INCREMENTABLE;
pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
"Argument \"%s\" treated as 0 in increment (++)", pv);
}
/*
=for apidoc looks_like_number
Test if the content of an SV looks like a number (or is a number).
C<Inf> and C<Infinity> are treated as numbers (so will not issue a
non-numeric warning), even if your atof() doesn't grok them. Get-magic is
ignored.
=cut
*/
I32
Perl_looks_like_number(pTHX_ SV *const sv)
{
const char *sbegin;
STRLEN len;
PERL_ARGS_ASSERT_LOOKS_LIKE_NUMBER;
if (SvPOK(sv) || SvPOKp(sv)) {
sbegin = SvPV_nomg_const(sv, len);
}
else
return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
return grok_number(sbegin, len, NULL);
}
STATIC bool
S_glob_2number(pTHX_ GV * const gv)
{
PERL_ARGS_ASSERT_GLOB_2NUMBER;
/* We know that all GVs stringify to something that is not-a-number,
so no need to test that. */
if (ckWARN(WARN_NUMERIC))
{
SV *const buffer = sv_newmortal();
gv_efullname3(buffer, gv, "*");
not_a_number(buffer);
}
/* We just want something true to return, so that S_sv_2iuv_common
can tail call us and return true. */
return TRUE;
}
/* Actually, ISO C leaves conversion of UV to IV undefined, but
until proven guilty, assume that things are not that bad... */
/*
NV_PRESERVES_UV:
As 64 bit platforms often have an NV that doesn't preserve all bits of
an IV (an assumption perl has been based on to date) it becomes necessary
to remove the assumption that the NV always carries enough precision to
recreate the IV whenever needed, and that the NV is the canonical form.
Instead, IV/UV and NV need to be given equal rights. So as to not lose
precision as a side effect of conversion (which would lead to insanity
and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1) to distinguish between IV/UV/NV slots that have cached a valid
conversion where precision was lost and IV/UV/NV slots that have a
valid conversion which has lost no precision
2) to ensure that if a numeric conversion to one form is requested that
would lose precision, the precise conversion (or differently
imprecise conversion) is also performed and cached, to prevent
requests for different numeric formats on the same SV causing
lossy conversion chains. (lossless conversion chains are perfectly
acceptable (still))
flags are used:
SvIOKp is true if the IV slot contains a valid value
SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
SvNOKp is true if the NV slot contains a valid value
SvNOK is true only if the NV value is accurate
so
while converting from PV to NV, check to see if converting that NV to an
IV(or UV) would lose accuracy over a direct conversion from PV to
IV(or UV). If it would, cache both conversions, return NV, but mark
SV as IOK NOKp (ie not NOK).
While converting from PV to IV, check to see if converting that IV to an
NV would lose accuracy over a direct conversion from PV to NV. If it
would, cache both conversions, flag similarly.
Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
correctly because if IV & NV were set NV *always* overruled.
Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
changes - now IV and NV together means that the two are interchangeable:
SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
The benefit of this is that operations such as pp_add know that if
SvIOK is true for both left and right operands, then integer addition
can be used instead of floating point (for cases where the result won't
overflow). Before, floating point was always used, which could lead to
loss of precision compared with integer addition.
* making IV and NV equal status should make maths accurate on 64 bit
platforms
* may speed up maths somewhat if pp_add and friends start to use
integers when possible instead of fp. (Hopefully the overhead in
looking for SvIOK and checking for overflow will not outweigh the
fp to integer speedup)
* will slow down integer operations (callers of SvIV) on "inaccurate"
values, as the change from SvIOK to SvIOKp will cause a call into
sv_2iv each time rather than a macro access direct to the IV slot
* should speed up number->string conversion on integers as IV is
favoured when IV and NV are equally accurate
####################################################################
You had better be using SvIOK_notUV if you want an IV for arithmetic:
SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
On the other hand, SvUOK is true iff UV.
####################################################################
Your mileage will vary depending your CPU's relative fp to integer
performance ratio.
*/
#ifndef NV_PRESERVES_UV
# define IS_NUMBER_UNDERFLOW_IV 1
# define IS_NUMBER_UNDERFLOW_UV 2
# define IS_NUMBER_IV_AND_UV 2
# define IS_NUMBER_OVERFLOW_IV 4
# define IS_NUMBER_OVERFLOW_UV 5
/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
/* For sv_2nv these three cases are "SvNOK and don't bother casting" */
STATIC int
S_sv_2iuv_non_preserve(pTHX_ SV *const sv
# ifdef DEBUGGING
, I32 numtype
# endif
)
{
dVAR;
PERL_ARGS_ASSERT_SV_2IUV_NON_PRESERVE;
DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%"NVgf" inttype=%"UVXf"\n", SvPVX_const(sv), SvIVX(sv), SvNVX(sv), (UV)numtype));
if (SvNVX(sv) < (NV)IV_MIN) {
(void)SvIOKp_on(sv);
(void)SvNOK_on(sv);
SvIV_set(sv, IV_MIN);
return IS_NUMBER_UNDERFLOW_IV;
}
if (SvNVX(sv) > (NV)UV_MAX) {
(void)SvIOKp_on(sv);
(void)SvNOK_on(sv);
SvIsUV_on(sv);
SvUV_set(sv, UV_MAX);
return IS_NUMBER_OVERFLOW_UV;
}
(void)SvIOKp_on(sv);
(void)SvNOK_on(sv);
/* Can't use strtol etc to convert this string. (See truth table in
sv_2iv */
if (SvNVX(sv) <= (UV)IV_MAX) {
SvIV_set(sv, I_V(SvNVX(sv)));
if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
SvIOK_on(sv); /* Integer is precise. NOK, IOK */
} else {
/* Integer is imprecise. NOK, IOKp */
}
return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
}
SvIsUV_on(sv);
SvUV_set(sv, U_V(SvNVX(sv)));
if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
if (SvUVX(sv) == UV_MAX) {
/* As we know that NVs don't preserve UVs, UV_MAX cannot
possibly be preserved by NV. Hence, it must be overflow.
NOK, IOKp */
return IS_NUMBER_OVERFLOW_UV;
}
SvIOK_on(sv); /* Integer is precise. NOK, UOK */
} else {
/* Integer is imprecise. NOK, IOKp */
}
return IS_NUMBER_OVERFLOW_IV;
}
#endif /* !NV_PRESERVES_UV*/
STATIC bool
S_sv_2iuv_common(pTHX_ SV *const sv)
{
dVAR;
PERL_ARGS_ASSERT_SV_2IUV_COMMON;
if (SvNOKp(sv)) {
/* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
* without also getting a cached IV/UV from it at the same time
* (ie PV->NV conversion should detect loss of accuracy and cache
* IV or UV at same time to avoid this. */
/* IV-over-UV optimisation - choose to cache IV if possible */
if (SvTYPE(sv) == SVt_NV)
sv_upgrade(sv, SVt_PVNV);
(void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
/* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
certainly cast into the IV range at IV_MAX, whereas the correct
answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
cases go to UV */
#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
if (Perl_isnan(SvNVX(sv))) {
SvUV_set(sv, 0);
SvIsUV_on(sv);
return FALSE;
}
#endif
if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
SvIV_set(sv, I_V(SvNVX(sv)));
if (SvNVX(sv) == (NV) SvIVX(sv)
#ifndef NV_PRESERVES_UV
&& (((UV)1 << NV_PRESERVES_UV_BITS) >
(UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
/* Don't flag it as "accurately an integer" if the number
came from a (by definition imprecise) NV operation, and
we're outside the range of NV integer precision */
#endif
) {
if (SvNOK(sv))
SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
else {
/* scalar has trailing garbage, eg "42a" */
}
DEBUG_c(PerlIO_printf(Perl_debug_log,
"0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
PTR2UV(sv),
SvNVX(sv),
SvIVX(sv)));
} else {
/* IV not precise. No need to convert from PV, as NV
conversion would already have cached IV if it detected
that PV->IV would be better than PV->NV->IV
flags already correct - don't set public IOK. */
DEBUG_c(PerlIO_printf(Perl_debug_log,
"0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
PTR2UV(sv),
SvNVX(sv),
SvIVX(sv)));
}
/* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
but the cast (NV)IV_MIN rounds to a the value less (more
negative) than IV_MIN which happens to be equal to SvNVX ??
Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
(NV)UVX == NVX are both true, but the values differ. :-(
Hopefully for 2s complement IV_MIN is something like
0x8000000000000000 which will be exact. NWC */
}
else {
SvUV_set(sv, U_V(SvNVX(sv)));
if (
(SvNVX(sv) == (NV) SvUVX(sv))
#ifndef NV_PRESERVES_UV
/* Make sure it's not 0xFFFFFFFFFFFFFFFF */
/*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
&& (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
/* Don't flag it as "accurately an integer" if the number
came from a (by definition imprecise) NV operation, and
we're outside the range of NV integer precision */
#endif
&& SvNOK(sv)
)
SvIOK_on(sv);
SvIsUV_on(sv);
DEBUG_c(PerlIO_printf(Perl_debug_log,
"0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
PTR2UV(sv),
SvUVX(sv),
SvUVX(sv)));
}
}
else if (SvPOKp(sv)) {
UV value;
const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
/* We want to avoid a possible problem when we cache an IV/ a UV which
may be later translated to an NV, and the resulting NV is not
the same as the direct translation of the initial string
(eg 123.456 can shortcut to the IV 123 with atol(), but we must
be careful to ensure that the value with the .456 is around if the
NV value is requested in the future).
This means that if we cache such an IV/a UV, we need to cache the
NV as well. Moreover, we trade speed for space, and do not
cache the NV if we are sure it's not needed.
*/
/* SVt_PVNV is one higher than SVt_PVIV, hence this order */
if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
== IS_NUMBER_IN_UV) {
/* It's definitely an integer, only upgrade to PVIV */
if (SvTYPE(sv) < SVt_PVIV)
sv_upgrade(sv, SVt_PVIV);
(void)SvIOK_on(sv);
} else if (SvTYPE(sv) < SVt_PVNV)
sv_upgrade(sv, SVt_PVNV);
/* If NVs preserve UVs then we only use the UV value if we know that
we aren't going to call atof() below. If NVs don't preserve UVs
then the value returned may have more precision than atof() will
return, even though value isn't perfectly accurate. */
if ((numtype & (IS_NUMBER_IN_UV
#ifdef NV_PRESERVES_UV
| IS_NUMBER_NOT_INT
#endif
)) == IS_NUMBER_IN_UV) {
/* This won't turn off the public IOK flag if it was set above */
(void)SvIOKp_on(sv);
if (!(numtype & IS_NUMBER_NEG)) {
/* positive */;
if (value <= (UV)IV_MAX) {
SvIV_set(sv, (IV)value);
} else {
/* it didn't overflow, and it was positive. */
SvUV_set(sv, value);
SvIsUV_on(sv);
}
} else {
/* 2s complement assumption */
if (value <= (UV)IV_MIN) {
SvIV_set(sv, -(IV)value);
} else {
/* Too negative for an IV. This is a double upgrade, but
I'm assuming it will be rare. */
if (SvTYPE(sv) < SVt_PVNV)
sv_upgrade(sv, SVt_PVNV);
SvNOK_on(sv);
SvIOK_off(sv);
SvIOKp_on(sv);
SvNV_set(sv, -(NV)value);
SvIV_set(sv, IV_MIN);
}
}
}
/* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
will be in the previous block to set the IV slot, and the next
block to set the NV slot. So no else here. */
if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
!= IS_NUMBER_IN_UV) {
/* It wasn't an (integer that doesn't overflow the UV). */
SvNV_set(sv, Atof(SvPVX_const(sv)));
if (! numtype && ckWARN(WARN_NUMERIC))
not_a_number(sv);
#if defined(USE_LONG_DOUBLE)
DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
PTR2UV(sv), SvNVX(sv)));
#else
DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
PTR2UV(sv), SvNVX(sv)));
#endif
#ifdef NV_PRESERVES_UV
(void)SvIOKp_on(sv);
(void)SvNOK_on(sv);
if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
SvIV_set(sv, I_V(SvNVX(sv)));
if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
SvIOK_on(sv);
} else {
NOOP; /* Integer is imprecise. NOK, IOKp */
}
/* UV will not work better than IV */
} else {
if (SvNVX(sv) > (NV)UV_MAX) {
SvIsUV_on(sv);
/* Integer is inaccurate. NOK, IOKp, is UV */
SvUV_set(sv, UV_MAX);
} else {
SvUV_set(sv, U_V(SvNVX(sv)));
/* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
NV preservse UV so can do correct comparison. */
if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
SvIOK_on(sv);
} else {
NOOP; /* Integer is imprecise. NOK, IOKp, is UV */
}
}
SvIsUV_on(sv);
}
#else /* NV_PRESERVES_UV */
if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
== (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
/* The IV/UV slot will have been set from value returned by
grok_number above. The NV slot has just been set using
Atof. */
SvNOK_on(sv);
assert (SvIOKp(sv));
} else {
if (((UV)1 << NV_PRESERVES_UV_BITS) >
U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
/* Small enough to preserve all bits. */
(void)SvIOKp_on(sv);
SvNOK_on(sv);
SvIV_set(sv, I_V(SvNVX(sv)));
if ((NV)(SvIVX(sv)) == SvNVX(sv))
SvIOK_on(sv);
/* Assumption: first non-preserved integer is < IV_MAX,
this NV is in the preserved range, therefore: */
if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
< (UV)IV_MAX)) {
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);
}
} else {
/* IN_UV NOT_INT
0 0 already failed to read UV.
0 1 already failed to read UV.
1 0 you won't get here in this case. IV/UV
slot set, public IOK, Atof() unneeded.
1 1 already read UV.
so there's no point in sv_2iuv_non_preserve() attempting
to use atol, strtol, strtoul etc. */
# ifdef DEBUGGING
sv_2iuv_non_preserve (sv, numtype);
# else
sv_2iuv_non_preserve (sv);
# endif
}
}
#endif /* NV_PRESERVES_UV */
/* It might be more code efficient to go through the entire logic above
and conditionally set with SvIOKp_on() rather than SvIOK(), but it
gets complex and potentially buggy, so more programmer efficient
to do it this way, by turning off the public flags: */
if (!numtype)
SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
}
}
else {
if (isGV_with_GP(sv))
return glob_2number(MUTABLE_GV(sv));
if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
report_uninit(sv);
if (SvTYPE(sv) < SVt_IV)
/* Typically the caller expects that sv_any is not NULL now. */
sv_upgrade(sv, SVt_IV);
/* Return 0 from the caller. */
return TRUE;
}
return FALSE;
}
/*
=for apidoc sv_2iv_flags
Return the integer value of an SV, doing any necessary string
conversion. If flags includes SV_GMAGIC, does an mg_get() first.
Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
=cut
*/
IV
Perl_sv_2iv_flags(pTHX_ SV *const sv, const I32 flags)
{
dVAR;
if (!sv)
return 0;
assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
&& SvTYPE(sv) != SVt_PVFM);
if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
mg_get(sv);
if (SvROK(sv)) {
if (SvAMAGIC(sv)) {
SV * tmpstr;
if (flags & SV_SKIP_OVERLOAD)
return 0;
tmpstr = AMG_CALLunary(sv, numer_amg);
if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
return SvIV(tmpstr);
}
}
return PTR2IV(SvRV(sv));
}
if (SvVALID(sv) || isREGEXP(sv)) {
/* FBMs use the space for SvIVX and SvNVX for other purposes, and use
the same flag bit as SVf_IVisUV, so must not let them cache IVs.
In practice they are extremely unlikely to actually get anywhere
accessible by user Perl code - the only way that I'm aware of is when
a constant subroutine which is used as the second argument to index.
Regexps have no SvIVX and SvNVX fields.
*/
assert(isREGEXP(sv) || SvPOKp(sv));
{
UV value;
const char * const ptr =
isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
const int numtype
= grok_number(ptr, SvCUR(sv), &value);
if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
== IS_NUMBER_IN_UV) {
/* It's definitely an integer */
if (numtype & IS_NUMBER_NEG) {
if (value < (UV)IV_MIN)
return -(IV)value;
} else {
if (value < (UV)IV_MAX)
return (IV)value;
}
}
if (!numtype) {
if (ckWARN(WARN_NUMERIC))
not_a_number(sv);
}
return I_V(Atof(ptr));
}
}
if (SvTHINKFIRST(sv)) {
#ifdef PERL_OLD_COPY_ON_WRITE
if (SvIsCOW(sv)) {
sv_force_normal_flags(sv, 0);
}
#endif
if (SvREADONLY(sv) && !SvOK(sv)) {
if (ckWARN(WARN_UNINITIALIZED))
report_uninit(sv);
return 0;
}
}
if (!SvIOKp(sv)) {
if (S_sv_2iuv_common(aTHX_ sv))
return 0;
}
DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
PTR2UV(sv),SvIVX(sv)));
return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
}
/*
=for apidoc sv_2uv_flags
Return the unsigned integer value of an SV, doing any necessary string
conversion. If flags includes SV_GMAGIC, does an mg_get() first.
Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
=cut
*/
UV
Perl_sv_2uv_flags(pTHX_ SV *const sv, const I32 flags)
{
dVAR;
if (!sv)
return 0;
if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
mg_get(sv);
if (SvROK(sv)) {
if (SvAMAGIC(sv)) {
SV *tmpstr;
if (flags & SV_SKIP_OVERLOAD)
return 0;
tmpstr = AMG_CALLunary(sv, numer_amg);
if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
return SvUV(tmpstr);
}
}
return PTR2UV(SvRV(sv));
}
if (SvVALID(sv) || isREGEXP(sv)) {
/* FBMs use the space for SvIVX and SvNVX for other purposes, and use
the same flag bit as SVf_IVisUV, so must not let them cache IVs.
Regexps have no SvIVX and SvNVX fields. */
assert(isREGEXP(sv) || SvPOKp(sv));
{
UV value;
const char * const ptr =
isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
const int numtype
= grok_number(ptr, SvCUR(sv), &value);
if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
== IS_NUMBER_IN_UV) {
/* It's definitely an integer */
if (!(numtype & IS_NUMBER_NEG))
return value;
}
if (!numtype) {
if (ckWARN(WARN_NUMERIC))
not_a_number(sv);
}
return U_V(Atof(ptr));
}
}
if (SvTHINKFIRST(sv)) {
#ifdef PERL_OLD_COPY_ON_WRITE
if (SvIsCOW(sv)) {
sv_force_normal_flags(sv, 0);
}
#endif
if (SvREADONLY(sv) && !SvOK(sv)) {
if (ckWARN(WARN_UNINITIALIZED))
report_uninit(sv);
return 0;
}
}
if (!SvIOKp(sv)) {
if (S_sv_2iuv_common(aTHX_ sv))
return 0;
}
DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
PTR2UV(sv),SvUVX(sv)));
return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
}
/*
=for apidoc sv_2nv_flags
Return the num value of an SV, doing any necessary string or integer
conversion. If flags includes SV_GMAGIC, does an mg_get() first.
Normally used via the C<SvNV(sv)> and C<SvNVx(sv)> macros.
=cut
*/
NV
Perl_sv_2nv_flags(pTHX_ SV *const sv, const I32 flags)
{
dVAR;
if (!sv)
return 0.0;
assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
&& SvTYPE(sv) != SVt_PVFM);
if (SvGMAGICAL(sv) || SvVALID(sv) || isREGEXP(sv)) {
/* FBMs use the space for SvIVX and SvNVX for other purposes, and use
the same flag bit as SVf_IVisUV, so must not let them cache NVs.
Regexps have no SvIVX and SvNVX fields. */
const char *ptr;
if (flags & SV_GMAGIC)
mg_get(sv);
if (SvNOKp(sv))
return SvNVX(sv);
if (SvPOKp(sv) && !SvIOKp(sv)) {
ptr = SvPVX_const(sv);
grokpv:
if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) &&
!grok_number(ptr, SvCUR(sv), NULL))
not_a_number(sv);
return Atof(ptr);
}
if (SvIOKp(sv)) {
if (SvIsUV(sv))
return (NV)SvUVX(sv);
else
return (NV)SvIVX(sv);
}
if (SvROK(sv)) {
goto return_rok;
}
if (isREGEXP(sv)) {
ptr = RX_WRAPPED((REGEXP *)sv);
goto grokpv;
}
assert(SvTYPE(sv) >= SVt_PVMG);
/* This falls through to the report_uninit near the end of the
function. */
} else if (SvTHINKFIRST(sv)) {
if (SvROK(sv)) {
return_rok:
if (SvAMAGIC(sv)) {
SV *tmpstr;
if (flags & SV_SKIP_OVERLOAD)
return 0;
tmpstr = AMG_CALLunary(sv, numer_amg);
if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
return SvNV(tmpstr);
}
}
return PTR2NV(SvRV(sv));
}
#ifdef PERL_OLD_COPY_ON_WRITE
if (SvIsCOW(sv)) {
sv_force_normal_flags(sv, 0);
}
#endif
if (SvREADONLY(sv) && !SvOK(sv)) {
if (ckWARN(WARN_UNINITIALIZED))
report_uninit(sv);
return 0.0;
}
}
if (SvTYPE(sv) < SVt_NV) {
/* The logic to use SVt_PVNV if necessary is in sv_upgrade. */
sv_upgrade(sv, SVt_NV);
#ifdef USE_LONG_DOUBLE
DEBUG_c({
STORE_NUMERIC_LOCAL_SET_STANDARD();
PerlIO_printf(Perl_debug_log,
"0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
PTR2UV(sv), SvNVX(sv));
RESTORE_NUMERIC_LOCAL();
});
#else
DEBUG_c({
STORE_NUMERIC_LOCAL_SET_STANDARD();
PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
PTR2UV(sv), SvNVX(sv));
RESTORE_NUMERIC_LOCAL();
});
#endif
}
else if (SvTYPE(sv) < SVt_PVNV)
sv_upgrade(sv, SVt_PVNV);
if (SvNOKp(sv)) {
return SvNVX(sv);
}
if (SvIOKp(sv)) {
SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
#ifdef NV_PRESERVES_UV
if (SvIOK(sv))
SvNOK_on(sv);
else
SvNOKp_on(sv);
#else
/* Only set the public NV OK flag if this NV preserves the IV */
/* Check it's not 0xFFFFFFFFFFFFFFFF */
if (SvIOK(sv) &&
SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
: (SvIVX(sv) == I_V(SvNVX(sv))))
SvNOK_on(sv);
else
SvNOKp_on(sv);
#endif
}
else if (SvPOKp(sv)) {
UV value;
const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC))
not_a_number(sv);
#ifdef NV_PRESERVES_UV
if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
== IS_NUMBER_IN_UV) {
/* It's definitely an integer */
SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
} else
SvNV_set(sv, Atof(SvPVX_const(sv)));
if (numtype)
SvNOK_on(sv);
else
SvNOKp_on(sv);
#else
SvNV_set(sv, Atof(SvPVX_const(sv)));
/* Only set the public NV OK flag if this NV preserves the value in
the PV at least as well as an IV/UV would.
Not sure how to do this 100% reliably. */
/* if that shift count is out of range then Configure's test is
wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
UV_BITS */
if (((UV)1 << NV_PRESERVES_UV_BITS) >
U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
SvNOK_on(sv); /* Definitely small enough to preserve all bits */
} else if (!(numtype & IS_NUMBER_IN_UV)) {
/* Can't use strtol etc to convert this string, so don't try.
sv_2iv and sv_2uv will use the NV to convert, not the PV. */
SvNOK_on(sv);
} else {
/* value has been set. It may not be precise. */
if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
/* 2s complement assumption for (UV)IV_MIN */
SvNOK_on(sv); /* Integer is too negative. */
} else {
SvNOKp_on(sv);
SvIOKp_on(sv);
if (numtype & IS_NUMBER_NEG) {
SvIV_set(sv, -(IV)value);
} else if (value <= (UV)IV_MAX) {
SvIV_set(sv, (IV)value);
} else {
SvUV_set(sv, value);
SvIsUV_on(sv);
}
if (numtype & IS_NUMBER_NOT_INT) {
/* I believe that even if the original PV had decimals,
they are lost beyond the limit of the FP precision.
However, neither is canonical, so both only get p
flags. NWC, 2000/11/25 */
/* Both already have p flags, so do nothing */
} else {
const NV nv = SvNVX(sv);
if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
if (SvIVX(sv) == I_V(nv)) {
SvNOK_on(sv);
} else {
/* It had no "." so it must be integer. */
}
SvIOK_on(sv);
} else {
/* between IV_MAX and NV(UV_MAX).
Could be slightly > UV_MAX */
if (numtype & IS_NUMBER_NOT_INT) {
/* UV and NV both imprecise. */
} else {
const UV nv_as_uv = U_V(nv);
if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
SvNOK_on(sv);
}
SvIOK_on(sv);
}
}
}
}
}
/* It might be more code efficient to go through the entire logic above
and conditionally set with SvNOKp_on() rather than SvNOK(), but it
gets complex and potentially buggy, so more programmer efficient
to do it this way, by turning off the public flags: */
if (!numtype)
SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
#endif /* NV_PRESERVES_UV */
}
else {
if (isGV_with_GP(sv)) {
glob_2number(MUTABLE_GV(sv));
return 0.0;
}
if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
report_uninit(sv);
assert (SvTYPE(sv) >= SVt_NV);
/* Typically the caller expects that sv_any is not NULL now. */
/* XXX Ilya implies that this is a bug in callers that assume this
and ideally should be fixed. */
return 0.0;
}
#if defined(USE_LONG_DOUBLE)
DEBUG_c({
STORE_NUMERIC_LOCAL_SET_STANDARD();
PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
PTR2UV(sv), SvNVX(sv));
RESTORE_NUMERIC_LOCAL();
});
#else
DEBUG_c({
STORE_NUMERIC_LOCAL_SET_STANDARD();
PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
PTR2UV(sv), SvNVX(sv));
RESTORE_NUMERIC_LOCAL();
});
#endif
return SvNVX(sv);
}
/*
=for apidoc sv_2num
Return an SV with the numeric value of the source SV, doing any necessary
reference or overload conversion. You must use the C<SvNUM(sv)> macro to
access this function.
=cut
*/
SV *
Perl_sv_2num(pTHX_ SV *const sv)
{
PERL_ARGS_ASSERT_SV_2NUM;
if (!SvROK(sv))
return sv;
if (SvAMAGIC(sv)) {
SV * const tmpsv = AMG_CALLunary(sv, numer_amg);
TAINT_IF(tmpsv && SvTAINTED(tmpsv));
if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
return sv_2num(tmpsv);
}
return sv_2mortal(newSVuv(PTR2UV(SvRV(sv))));
}
/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
* UV as a string towards the end of buf, and return pointers to start and
* end of it.
*
* We assume that buf is at least TYPE_CHARS(UV) long.
*/
static char *
S_uiv_2buf(char *const buf, const IV iv, UV uv, const int is_uv, char **const peob)
{
char *ptr = buf + TYPE_CHARS(UV);
char * const ebuf = ptr;
int sign;
PERL_ARGS_ASSERT_UIV_2BUF;
if (is_uv)
sign = 0;
else if (iv >= 0) {
uv = iv;
sign = 0;
} else {
uv = -iv;
sign = 1;
}
do {
*--ptr = '0' + (char)(uv % 10);
} while (uv /= 10);
if (sign)
*--ptr = '-';
*peob = ebuf;
return ptr;
}
/*
=for apidoc sv_2pv_flags
Returns a pointer to the string value of an SV, and sets *lp to its length.
If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a
string if necessary. Normally invoked via the C<SvPV_flags> macro.
C<sv_2pv()> and C<sv_2pv_nomg> usually end up here too.
=cut
*/
char *
Perl_sv_2pv_flags(pTHX_ SV *const sv, STRLEN *const lp, const I32 flags)
{
dVAR;
char *s;
if (!sv) {
if (lp)
*lp = 0;
return (char *)"";
}
assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
&& SvTYPE(sv) != SVt_PVFM);
if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
mg_get(sv);
if (SvROK(sv)) {
if (SvAMAGIC(sv)) {
SV *tmpstr;
if (flags & SV_SKIP_OVERLOAD)
return NULL;
tmpstr = AMG_CALLunary(sv, string_amg);
TAINT_IF(tmpstr && SvTAINTED(tmpstr));
if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
/* Unwrap this: */
/* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr);
*/
char *pv;
if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
if (flags & SV_CONST_RETURN) {
pv = (char *) SvPVX_const(tmpstr);
} else {
pv = (flags & SV_MUTABLE_RETURN)
? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
}
if (lp)
*lp = SvCUR(tmpstr);
} else {
pv = sv_2pv_flags(tmpstr, lp, flags);
}
if (SvUTF8(tmpstr))
SvUTF8_on(sv);
else
SvUTF8_off(sv);
return pv;
}
}
{
STRLEN len;
char *retval;
char *buffer;
SV *const referent = SvRV(sv);
if (!referent) {
len = 7;
retval = buffer = savepvn("NULLREF", len);
} else if (SvTYPE(referent) == SVt_REGEXP &&
(!(PL_curcop->cop_hints & HINT_NO_AMAGIC) ||
amagic_is_enabled(string_amg))) {
REGEXP * const re = (REGEXP *)MUTABLE_PTR(referent);
assert(re);
/* If the regex is UTF-8 we want the containing scalar to
have an UTF-8 flag too */
if (RX_UTF8(re))
SvUTF8_on(sv);
else
SvUTF8_off(sv);
if (lp)
*lp = RX_WRAPLEN(re);
return RX_WRAPPED(re);
} else {
const char *const typestr = sv_reftype(referent, 0);
const STRLEN typelen = strlen(typestr);
UV addr = PTR2UV(referent);
const char *stashname = NULL;
STRLEN stashnamelen = 0; /* hush, gcc */
const char *buffer_end;
if (SvOBJECT(referent)) {
const HEK *const name = HvNAME_HEK(SvSTASH(referent));
if (name) {
stashname = HEK_KEY(name);
stashnamelen = HEK_LEN(name);
if (HEK_UTF8(name)) {
SvUTF8_on(sv);
} else {
SvUTF8_off(sv);
}
} else {
stashname = "__ANON__";
stashnamelen = 8;
}
len = stashnamelen + 1 /* = */ + typelen + 3 /* (0x */
+ 2 * sizeof(UV) + 2 /* )\0 */;
} else {
len = typelen + 3 /* (0x */
+ 2 * sizeof(UV) + 2 /* )\0 */;
}
Newx(buffer, len, char);
buffer_end = retval = buffer + len;
/* Working backwards */
*--retval = '\0';
*--retval = ')';
do {
*--retval = PL_hexdigit[addr & 15];
} while (addr >>= 4);
*--retval = 'x';
*--retval = '0';
*--retval = '(';
retval -= typelen;
memcpy(retval, typestr, typelen);
if (stashname) {
*--retval = '=';
retval -= stashnamelen;
memcpy(retval, stashname, stashnamelen);
}
/* retval may not necessarily have reached the start of the
buffer here. */
assert (retval >= buffer);
len = buffer_end - retval - 1; /* -1 for that \0 */
}
if (lp)
*lp = len;
SAVEFREEPV(buffer);
return retval;
}
}
if (SvPOKp(sv)) {
if (lp)
*lp = SvCUR(sv);
if (flags & SV_MUTABLE_RETURN)
return SvPVX_mutable(sv);
if (flags & SV_CONST_RETURN)
return (char *)SvPVX_const(sv);
return SvPVX(sv);
}
if (SvIOK(sv)) {
/* I'm assuming that if both IV and NV are equally valid then
converting the IV is going to be more efficient */
const U32 isUIOK = SvIsUV(sv);
char buf[TYPE_CHARS(UV)];
char *ebuf, *ptr;
STRLEN len;
if (SvTYPE(sv) < SVt_PVIV)
sv_upgrade(sv, SVt_PVIV);
ptr = uiv_2buf(buf, SvIVX(sv), SvUVX(sv), isUIOK, &ebuf);
len = ebuf - ptr;
/* inlined from sv_setpvn */
s = SvGROW_mutable(sv, len + 1);
Move(ptr, s, len, char);
s += len;
*s = '\0';
SvPOK_on(sv);
}
else if (SvNOK(sv)) {
if (SvTYPE(sv) < SVt_PVNV)
sv_upgrade(sv, SVt_PVNV);
if (SvNVX(sv) == 0.0) {
s = SvGROW_mutable(sv, 2);
*s++ = '0';
*s = '\0';
} else {
dSAVE_ERRNO;
/* The +20 is pure guesswork. Configure test needed. --jhi */
s = SvGROW_mutable(sv, NV_DIG + 20);
/* some Xenix systems wipe out errno here */
#ifndef USE_LOCALE_NUMERIC
Gconvert(SvNVX(sv), NV_DIG, 0, s);
SvPOK_on(sv);
#else
/* Gconvert always uses the current locale. That's the right thing
* to do if we're supposed to be using locales. But otherwise, we
* want the result to be based on the C locale, so we need to
* change to the C locale during the Gconvert and then change back.
* But if we're already in the C locale (PL_numeric_standard is
* TRUE in that case), no need to do any changing */
if (PL_numeric_standard || IN_SOME_LOCALE_FORM_RUNTIME) {
Gconvert(SvNVX(sv), NV_DIG, 0, s);
/* If the radix character is UTF-8, and actually is in the
* output, turn on the UTF-8 flag for the scalar */
if (! PL_numeric_standard
&& PL_numeric_radix_sv && SvUTF8(PL_numeric_radix_sv)
&& instr(s, SvPVX_const(PL_numeric_radix_sv)))
{
SvUTF8_on(sv);
}
}
else {
char *loc = savepv(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "C");
Gconvert(SvNVX(sv), NV_DIG, 0, s);
setlocale(LC_NUMERIC, loc);
Safefree(loc);
}
/* We don't call SvPOK_on(), because it may come to pass that the
* locale changes so that the stringification we just did is no
* longer correct. We will have to re-stringify every time it is
* needed */
#endif
RESTORE_ERRNO;
while (*s) s++;
}
}
else if (isGV_with_GP(sv)) {
GV *const gv = MUTABLE_GV(sv);
SV *const buffer = sv_newmortal();
gv_efullname3(buffer, gv, "*");
assert(SvPOK(buffer));
if (SvUTF8(buffer))
SvUTF8_on(sv);
if (lp)
*lp = SvCUR(buffer);
return SvPVX(buffer);
}
else if (isREGEXP(sv)) {
if (lp) *lp = RX_WRAPLEN((REGEXP *)sv);
return RX_WRAPPED((REGEXP *)sv);
}
else {
if (lp)
*lp = 0;
if (flags & SV_UNDEF_RETURNS_NULL)
return NULL;
if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
report_uninit(sv);
/* Typically the caller expects that sv_any is not NULL now. */
if (!SvREADONLY(sv) && SvTYPE(sv) < SVt_PV)
sv_upgrade(sv, SVt_PV);
return (char *)"";
}
{
const STRLEN len = s - SvPVX_const(sv);
if (lp)
*lp = len;
SvCUR_set(sv, len);
}
DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
PTR2UV(sv),SvPVX_const(sv)));
if (flags & SV_CONST_RETURN)
return (char *)SvPVX_const(sv);
if (flags & SV_MUTABLE_RETURN)
return SvPVX_mutable(sv);
return SvPVX(sv);
}
/*
=for apidoc sv_copypv
Copies a stringified representation of the source SV into the
destination SV. Automatically performs any necessary mg_get and
coercion of numeric values into strings. Guaranteed to preserve
UTF8 flag even from overloaded objects. Similar in nature to
sv_2pv[_flags] but operates directly on an SV instead of just the
string. Mostly uses sv_2pv_flags to do its work, except when that
would lose the UTF-8'ness of the PV.
=for apidoc sv_copypv_nomg
Like sv_copypv, but doesn't invoke get magic first.
=for apidoc sv_copypv_flags
Implementation of sv_copypv and sv_copypv_nomg. Calls get magic iff flags
include SV_GMAGIC.
=cut
*/
void
Perl_sv_copypv(pTHX_ SV *const dsv, SV *const ssv)
{
PERL_ARGS_ASSERT_SV_COPYPV;
sv_copypv_flags(dsv, ssv, 0);
}
void
Perl_sv_copypv_flags(pTHX_ SV *const dsv, SV *const ssv, const I32 flags)
{
STRLEN len;
const char *s;
PERL_ARGS_ASSERT_SV_COPYPV_FLAGS;
if ((flags & SV_GMAGIC) && SvGMAGICAL(ssv))
mg_get(ssv);
s = SvPV_nomg_const(ssv,len);
sv_setpvn(dsv,s,len);
if (SvUTF8(ssv))
SvUTF8_on(dsv);
else
SvUTF8_off(dsv);
}
/*
=for apidoc sv_2pvbyte
Return a pointer to the byte-encoded representation of the SV, and set *lp
to its length. May cause the SV to be downgraded from UTF-8 as a
side-effect.
Usually accessed via the C<SvPVbyte> macro.
=cut
*/
char *
Perl_sv_2pvbyte(pTHX_ SV *sv, STRLEN *const lp)
{
PERL_ARGS_ASSERT_SV_2PVBYTE;
SvGETMAGIC(sv);
if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
|| isGV_with_GP(sv) || SvROK(sv)) {
SV *sv2 = sv_newmortal();
sv_copypv_nomg(sv2,sv);
sv = sv2;
}
sv_utf8_downgrade(sv,0);
return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
}
/*
=for apidoc sv_2pvutf8
Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
Usually accessed via the C<SvPVutf8> macro.
=cut
*/
char *
Perl_sv_2pvutf8(pTHX_ SV *sv, STRLEN *const lp)
{
PERL_ARGS_ASSERT_SV_2PVUTF8;
if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
|| isGV_with_GP(sv) || SvROK(sv))
sv = sv_mortalcopy(sv);
else
SvGETMAGIC(sv);
sv_utf8_upgrade_nomg(sv);
return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
}
/*
=for apidoc sv_2bool
This macro is only used by sv_true() or its macro equivalent, and only if
the latter's argument is neither SvPOK, SvIOK nor SvNOK.
It calls sv_2bool_flags with the SV_GMAGIC flag.
=for apidoc sv_2bool_flags
This function is only used by sv_true() and friends, and only if
the latter's argument is neither SvPOK, SvIOK nor SvNOK. If the flags
contain SV_GMAGIC, then it does an mg_get() first.
=cut
*/
bool
Perl_sv_2bool_flags(pTHX_ SV *const sv, const I32 flags)
{
dVAR;
PERL_ARGS_ASSERT_SV_2BOOL_FLAGS;
if(flags & SV_GMAGIC) SvGETMAGIC(sv);
if (!SvOK(sv))
return 0;
if (SvROK(sv)) {
if (SvAMAGIC(sv)) {
SV * const tmpsv = AMG_CALLunary(sv, bool__amg);
if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
return cBOOL(SvTRUE(tmpsv));
}
return SvRV(sv) != 0;
}
if (isREGEXP(sv))
return
RX_WRAPLEN(sv) > 1 || (RX_WRAPLEN(sv) && *RX_WRAPPED(sv) != '0');
return SvTRUE_common(sv, isGV_with_GP(sv) ? 1 : 0);
}
/*
=for apidoc sv_utf8_upgrade
Converts the PV of an SV to its UTF-8-encoded form.
Forces the SV to string form if it is not already.
Will C<mg_get> on C<sv> if appropriate.
Always sets the SvUTF8 flag to avoid future validity checks even
if the whole string is the same in UTF-8 as not.
Returns the number of bytes in the converted string
This is not a general purpose byte encoding to Unicode interface:
use the Encode extension for that.
=for apidoc sv_utf8_upgrade_nomg
Like sv_utf8_upgrade, but doesn't do magic on C<sv>.
=for apidoc sv_utf8_upgrade_flags
Converts the PV of an SV to its UTF-8-encoded form.
Forces the SV to string form if it is not already.
Always sets the SvUTF8 flag to avoid future validity checks even
if all the bytes are invariant in UTF-8.
If C<flags> has C<SV_GMAGIC> bit set,
will C<mg_get> on C<sv> if appropriate, else not.
Returns the number of bytes in the converted string
C<sv_utf8_upgrade> and
C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
This is not a general purpose byte encoding to Unicode interface:
use the Encode extension for that.
=cut
The grow version is currently not externally documented. It adds a parameter,
extra, which is the number of unused bytes the string of 'sv' is guaranteed to
have free after it upon return. This allows the caller to reserve extra space
that it intends to fill, to avoid extra grows.
Also externally undocumented for the moment is the flag SV_FORCE_UTF8_UPGRADE,
which can be used to tell this function to not first check to see if there are
any characters that are different in UTF-8 (variant characters) which would
force it to allocate a new string to sv, but to assume there are. Typically
this flag is used by a routine that has already parsed the string to find that
there are such characters, and passes this information on so that the work
doesn't have to be repeated.
(One might think that the calling routine could pass in the position of the
first such variant, so it wouldn't have to be found again. But that is not the
case, because typically when the caller is likely to use this flag, it won't be
calling this routine unless it finds something that won't fit into a byte.
Otherwise it tries to not upgrade and just use bytes. But some things that
do fit into a byte are variants in utf8, and the caller may not have been
keeping track of these.)
If the routine itself changes the string, it adds a trailing NUL. Such a NUL
isn't guaranteed due to having other routines do the work in some input cases,
or if the input is already flagged as being in utf8.
The speed of this could perhaps be improved for many cases if someone wanted to
write a fast function that counts the number of variant characters in a string,
especially if it could return the position of the first one.
*/
STRLEN
Perl_sv_utf8_upgrade_flags_grow(pTHX_ SV *const sv, const I32 flags, STRLEN extra)
{
dVAR;
PERL_ARGS_ASSERT_SV_UTF8_UPGRADE_FLAGS_GROW;
if (sv == &PL_sv_undef)
return 0;
if (!SvPOK_nog(sv)) {
STRLEN len = 0;
if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
(void) sv_2pv_flags(sv,&len, flags);
if (SvUTF8(sv)) {
if (extra) SvGROW(sv, SvCUR(sv) + extra);
return len;
}
} else {
(void) SvPV_force_flags(sv,len,flags & SV_GMAGIC);
}
}
if (SvUTF8(sv)) {
if (extra) SvGROW(sv, SvCUR(sv) + extra);
return SvCUR(sv);
}
if (SvIsCOW(sv)) {
S_sv_uncow(aTHX_ sv, 0);
}
if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING)) {
sv_recode_to_utf8(sv, PL_encoding);
if (extra) SvGROW(sv, SvCUR(sv) + extra);
return SvCUR(sv);
}
if (SvCUR(sv) == 0) {
if (extra) SvGROW(sv, extra);
} else { /* Assume Latin-1/EBCDIC */
/* This function could be much more efficient if we
* had a FLAG in SVs to signal if there are any variant
* chars in the PV. Given that there isn't such a flag
* make the loop as fast as possible (although there are certainly ways
* to speed this up, eg. through vectorization) */
U8 * s = (U8 *) SvPVX_const(sv);
U8 * e = (U8 *) SvEND(sv);
U8 *t = s;
STRLEN two_byte_count = 0;
if (flags & SV_FORCE_UTF8_UPGRADE) goto must_be_utf8;
/* See if really will need to convert to utf8. We mustn't rely on our
* incoming SV being well formed and having a trailing '\0', as certain
* code in pp_formline can send us partially built SVs. */
while (t < e) {
const U8 ch = *t++;
if (NATIVE_BYTE_IS_INVARIANT(ch)) continue;
t--; /* t already incremented; re-point to first variant */
two_byte_count = 1;
goto must_be_utf8;
}
/* utf8 conversion not needed because all are invariants. Mark as
* UTF-8 even if no variant - saves scanning loop */
SvUTF8_on(sv);
if (extra) SvGROW(sv, SvCUR(sv) + extra);
return SvCUR(sv);
must_be_utf8:
/* Here, the string should be converted to utf8, either because of an
* input flag (two_byte_count = 0), or because a character that
* requires 2 bytes was found (two_byte_count = 1). t points either to
* the beginning of the string (if we didn't examine anything), or to
* the first variant. In either case, everything from s to t - 1 will
* occupy only 1 byte each on output.
*
* There are two main ways to convert. One is to create a new string
* and go through the input starting from the beginning, appending each
* converted value onto the new string as we go along. It's probably
* best to allocate enough space in the string for the worst possible
* case rather than possibly running out of space and having to
* reallocate and then copy what we've done so far. Since everything
* from s to t - 1 is invariant, the destination can be initialized
* with these using a fast memory copy
*
* The other way is to figure out exactly how big the string should be
* by parsing the entire input. Then you don't have to make it big
* enough to handle the worst possible case, and more importantly, if
* the string you already have is large enough, you don't have to
* allocate a new string, you can copy the last character in the input
* string to the final position(s) that will be occupied by the
* converted string and go backwards, stopping at t, since everything
* before that is invariant.
*
* There are advantages and disadvantages to each method.
*
* In the first method, we can allocate a new string, do the memory
* copy from the s to t - 1, and then proceed through the rest of the
* string byte-by-byte.
*
* In the second method, we proceed through the rest of the input
* string just calculating how big the converted string will be. Then
* there are two cases:
* 1) if the string has enough extra space to handle the converted
* value. We go backwards through the string, converting until we
* get to the position we are at now, and then stop. If this
* position is far enough along in the string, this method is
* faster than the other method. If the memory copy were the same
* speed as the byte-by-byte loop, that position would be about
* half-way, as at the half-way mark, parsing to the end and back
* is one complete string's parse, the same amount as starting
* over and going all the way through. Actually, it would be
* somewhat less than half-way, as it's faster to just count bytes
* than to also copy, and we don't have the overhead of allocating
* a new string, changing the scalar to use it, and freeing the
* existing one. But if the memory copy is fast, the break-even
* point is somewhere after half way. The counting loop could be
* sped up by vectorization, etc, to move the break-even point
* further towards the beginning.
* 2) if the string doesn't have enough space to handle the converted
* value. A new string will have to be allocated, and one might
* as well, given that, start from the beginning doing the first
* method. We've spent extra time parsing the string and in
* exchange all we've gotten is that we know precisely how big to
* make the new one. Perl is more optimized for time than space,
* so this case is a loser.
* So what I've decided to do is not use the 2nd method unless it is
* guaranteed that a new string won't have to be allocated, assuming
* the worst case. I also decided not to put any more conditions on it
* than this, for now. It seems likely that, since the worst case is
* twice as big as the unknown portion of the string (plus 1), we won't
* be guaranteed enough space, causing us to go to the first method,
* unless the string is short, or the first variant character is near
* the end of it. In either of these cases, it seems best to use the
* 2nd method. The only circumstance I can think of where this would
* be really slower is if the string had once had much more data in it
* than it does now, but there is still a substantial amount in it */
{
STRLEN invariant_head = t - s;
STRLEN size = invariant_head + (e - t) * 2 + 1 + extra;
if (SvLEN(sv) < size) {
/* Here, have decided to allocate a new string */
U8 *dst;
U8 *d;
Newx(dst, size, U8);
/* If no known invariants at the beginning of the input string,
* set so starts from there. Otherwise, can use memory copy to
* get up to where we are now, and then start from here */
if (invariant_head <= 0) {
d = dst;
} else {
Copy(s, dst, invariant_head, char);
d = dst + invariant_head;
}
while (t < e) {
append_utf8_from_native_byte(*t, &d);
t++;
}
*d = '\0';
SvPV_free(sv); /* No longer using pre-existing string */
SvPV_set(sv, (char*)dst);
SvCUR_set(sv, d - dst);
SvLEN_set(sv, size);
} else {
/* Here, have decided to get the exact size of the string.
* Currently this happens only when we know that there is
* guaranteed enough space to fit the converted string, so
* don't have to worry about growing. If two_byte_count is 0,
* then t points to the first byte of the string which hasn't
* been examined yet. Otherwise two_byte_count is 1, and t
* points to the first byte in the string that will expand to
* two. Depending on this, start examining at t or 1 after t.
* */
U8 *d = t + two_byte_count;
/* Count up the remaining bytes that expand to two */
while (d < e) {
const U8 chr = *d++;
if (! NATIVE_BYTE_IS_INVARIANT(chr)) two_byte_count++;
}
/* The string will expand by just the number of bytes that
* occupy two positions. But we are one afterwards because of
* the increment just above. This is the place to put the
* trailing NUL, and to set the length before we decrement */
d += two_byte_count;
SvCUR_set(sv, d - s);
*d-- = '\0';
/* Having decremented d, it points to the position to put the
* very last byte of the expanded string. Go backwards through
* the string, copying and expanding as we go, stopping when we
* get to the part that is invariant the rest of the way down */
e--;
while (e >= t) {
if (NATIVE_BYTE_IS_INVARIANT(*e)) {
*d-- = *e;
} else {
*d-- = UTF8_EIGHT_BIT_LO(*e);
*d-- = UTF8_EIGHT_BIT_HI(*e);
}
e--;
}
}
if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
/* Update pos. We do it at the end rather than during
* the upgrade, to avoid slowing down the common case
* (upgrade without pos).
* pos can be stored as either bytes or characters. Since
* this was previously a byte string we can just turn off
* the bytes flag. */
MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
if (mg) {
mg->mg_flags &= ~MGf_BYTES;
}
if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
magic_setutf8(sv,mg); /* clear UTF8 cache */
}
}
}
/* Mark as UTF-8 even if no variant - saves scanning loop */
SvUTF8_on(sv);
return SvCUR(sv);
}
/*
=for apidoc sv_utf8_downgrade
Attempts to convert the PV of an SV from characters to bytes.
If the PV contains a character that cannot fit
in a byte, this conversion will fail;
in this case, either returns false or, if C<fail_ok> is not
true, croaks.
This is not a general purpose Unicode to byte encoding interface:
use the Encode extension for that.
=cut
*/
bool
Perl_sv_utf8_downgrade(pTHX_ SV *const sv, const bool fail_ok)
{
dVAR;
PERL_ARGS_ASSERT_SV_UTF8_DOWNGRADE;
if (SvPOKp(sv) && SvUTF8(sv)) {
if (SvCUR(sv)) {
U8 *s;
STRLEN len;
int mg_flags = SV_GMAGIC;
if (SvIsCOW(sv)) {
S_sv_uncow(aTHX_ sv, 0);
}
if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
/* update pos */
MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
if (mg && mg->mg_len > 0 && mg->mg_flags & MGf_BYTES) {
mg->mg_len = sv_pos_b2u_flags(sv, mg->mg_len,
SV_GMAGIC|SV_CONST_RETURN);
mg_flags = 0; /* sv_pos_b2u does get magic */
}
if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
magic_setutf8(sv,mg); /* clear UTF8 cache */
}
s = (U8 *) SvPV_flags(sv, len, mg_flags);
if (!utf8_to_bytes(s, &len)) {
if (fail_ok)
return FALSE;
else {
if (PL_op)
Perl_croak(aTHX_ "Wide character in %s",
OP_DESC(PL_op));
else
Perl_croak(aTHX_ "Wide character");
}
}
SvCUR_set(sv, len);
}
}
SvUTF8_off(sv);
return TRUE;
}
/*
=for apidoc sv_utf8_encode
Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
flag off so that it looks like octets again.
=cut
*/
void
Perl_sv_utf8_encode(pTHX_ SV *const sv)
{
PERL_ARGS_ASSERT_SV_UTF8_ENCODE;
if (SvREADONLY(sv)) {
sv_force_normal_flags(sv, 0);
}
(void) sv_utf8_upgrade(sv);
SvUTF8_off(sv);
}
/*
=for apidoc sv_utf8_decode
If the PV of the SV is an octet sequence in UTF-8
and contains a multiple-byte character, the C<SvUTF8> flag is turned on
so that it looks like a character. If the PV contains only single-byte
characters, the C<SvUTF8> flag stays off.
Scans PV for validity and returns false if the PV is invalid UTF-8.
=cut
*/
bool
Perl_sv_utf8_decode(pTHX_ SV *const sv)
{
PERL_ARGS_ASSERT_SV_UTF8_DECODE;
if (SvPOKp(sv)) {
const U8 *start, *c;
const U8 *e;
/* The octets may have got themselves encoded - get them back as
* bytes
*/
if (!sv_utf8_downgrade(sv, TRUE))
return FALSE;
/* it is actually just a matter of turning the utf8 flag on, but
* we want to make sure everything inside is valid utf8 first.
*/
c = start = (const U8 *) SvPVX_const(sv);
if (!is_utf8_string(c, SvCUR(sv)))
return FALSE;
e = (const U8 *) SvEND(sv);
while (c < e) {
const U8 ch = *c++;
if (!UTF8_IS_INVARIANT(ch)) {
SvUTF8_on(sv);
break;
}
}
if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
/* XXX Is this dead code? XS_utf8_decode calls SvSETMAGIC
after this, clearing pos. Does anything on CPAN
need this? */
/* adjust pos to the start of a UTF8 char sequence */
MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
if (mg) {
I32 pos = mg->mg_len;
if (pos > 0) {
for (c = start + pos; c > start; c--) {
if (UTF8_IS_START(*c))
break;
}
mg->mg_len = c - start;
}
}
if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
magic_setutf8(sv,mg); /* clear UTF8 cache */
}
}
return TRUE;
}
/*
=for apidoc sv_setsv
Copies the contents of the source SV C<ssv> into the destination SV
C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
function if the source SV needs to be reused. Does not handle 'set' magic.
Loosely speaking, it performs a copy-by-value, obliterating any previous
content of the destination.
You probably want to use one of the assortment of wrappers, such as
C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
C<SvSetMagicSV_nosteal>.
=for apidoc sv_setsv_flags
Copies the contents of the source SV C<ssv> into the destination SV
C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
function if the source SV needs to be reused. Does not handle 'set' magic.
Loosely speaking, it performs a copy-by-value, obliterating any previous
content of the destination.
If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
C<ssv> if appropriate, else not. If the C<flags>
parameter has the C<NOSTEAL> bit set then the
buffers of temps will not be stolen. <sv_setsv>
and C<sv_setsv_nomg> are implemented in terms of this function.
You probably want to use one of the assortment of wrappers, such as
C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
C<SvSetMagicSV_nosteal>.
This is the primary function for copying scalars, and most other
copy-ish functions and macros use this underneath.
=cut
*/
static void
S_glob_assign_glob(pTHX_ SV *const dstr, SV *const sstr, const int dtype)
{
I32 mro_changes = 0; /* 1 = method, 2 = isa, 3 = recursive isa */
HV *old_stash = NULL;
PERL_ARGS_ASSERT_GLOB_ASSIGN_GLOB;
if (dtype != SVt_PVGV && !isGV_with_GP(dstr)) {
const char * const name = GvNAME(sstr);
const STRLEN len = GvNAMELEN(sstr);
{
if (dtype >= SVt_PV) {
SvPV_free(dstr);
SvPV_set(dstr, 0);
SvLEN_set(dstr, 0);
SvCUR_set(dstr, 0);
}
SvUPGRADE(dstr, SVt_PVGV);
(void)SvOK_off(dstr);
/* We have to turn this on here, even though we turn it off
below, as GvSTASH will fail an assertion otherwise. */
isGV_with_GP_on(dstr);
}
GvSTASH(dstr) = GvSTASH(sstr);
if (GvSTASH(dstr))
Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dstr)), dstr);
gv_name_set(MUTABLE_GV(dstr), name, len,
GV_ADD | (GvNAMEUTF8(sstr) ? SVf_UTF8 : 0 ));
SvFAKE_on(dstr); /* can coerce to non-glob */
}
if(GvGP(MUTABLE_GV(sstr))) {
/* If source has method cache entry, clear it */
if(GvCVGEN(sstr)) {
SvREFCNT_dec(GvCV(sstr));
GvCV_set(sstr, NULL);
GvCVGEN(sstr) = 0;
}
/* If source has a real method, then a method is
going to change */
else if(
GvCV((const GV *)sstr) && GvSTASH(dstr) && HvENAME(GvSTASH(dstr))
) {
mro_changes = 1;
}
}
/* If dest already had a real method, that's a change as well */
if(
!mro_changes && GvGP(MUTABLE_GV(dstr)) && GvCVu((const GV *)dstr)
&& GvSTASH(dstr) && HvENAME(GvSTASH(dstr))
) {
mro_changes = 1;
}
/* We don't need to check the name of the destination if it was not a
glob to begin with. */
if(dtype == SVt_PVGV) {
const char * const name = GvNAME((const GV *)dstr);
if(
strEQ(name,"ISA")
/* The stash may have been detached from the symbol table, so
check its name. */
&& GvSTASH(dstr) && HvENAME(GvSTASH(dstr))
)
mro_changes = 2;
else {
const STRLEN len = GvNAMELEN(dstr);
if ((len > 1 && name[len-2] == ':' && name[len-1] == ':')
|| (len == 1 && name[0] == ':')) {
mro_changes = 3;
/* Set aside the old stash, so we can reset isa caches on
its subclasses. */
if((old_stash = GvHV(dstr)))
/* Make sure we do not lose it early. */
SvREFCNT_inc_simple_void_NN(
sv_2mortal((SV *)old_stash)
);
}
}
}
gp_free(MUTABLE_GV(dstr));
isGV_with_GP_off(dstr); /* SvOK_off does not like globs. */
(void)SvOK_off(dstr);
isGV_with_GP_on(dstr);
GvINTRO_off(dstr); /* one-shot flag */
GvGP_set(dstr, gp_ref(GvGP(sstr)));
if (SvTAINTED(sstr))
SvTAINT(dstr);
if (GvIMPORTED(dstr) != GVf_IMPORTED
&& CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
{
GvIMPORTED_on(dstr);
}
GvMULTI_on(dstr);
if(mro_changes == 2) {
if (GvAV((const GV *)sstr)) {
MAGIC *mg;
SV * const sref = (SV *)GvAV((const GV *)dstr);
if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) {
if (SvTYPE(mg->mg_obj) != SVt_PVAV) {
AV * const ary = newAV();
av_push(ary, mg->mg_obj); /* takes the refcount */
mg->mg_obj = (SV *)ary;
}
av_push((AV *)mg->mg_obj, SvREFCNT_inc_simple_NN(dstr));
}
else sv_magic(sref, dstr, PERL_MAGIC_isa, NULL, 0);
}
mro_isa_changed_in(GvSTASH(dstr));
}
else if(mro_changes == 3) {
HV * const stash = GvHV(dstr);
if(old_stash ? (HV *)HvENAME_get(old_stash) : stash)
mro_package_moved(
stash, old_stash,
(GV *)dstr, 0
);
}
else if(mro_changes) mro_method_changed_in(GvSTASH(dstr));
if (GvIO(dstr) && dtype == SVt_PVGV) {
DEBUG_o(Perl_deb(aTHX_
"glob_assign_glob clearing PL_stashcache\n"));
/* It's a cache. It will rebuild itself quite happily.
It's a lot of effort to work out exactly which key (or keys)
might be invalidated by the creation of the this file handle.
*/
hv_clear(PL_stashcache);
}
return;
}
static void
S_glob_assign_ref(pTHX_ SV *const dstr, SV *const sstr)
{
SV * const sref = SvRV(sstr);
SV *dref;
const int intro = GvINTRO(dstr);
SV **location;
U8 import_flag = 0;
const U32 stype = SvTYPE(sref);
PERL_ARGS_ASSERT_GLOB_ASSIGN_REF;
if (intro) {
GvINTRO_off(dstr); /* one-shot flag */
GvLINE(dstr) = CopLINE(PL_curcop);
GvEGV(dstr) = MUTABLE_GV(dstr);
}
GvMULTI_on(dstr);
switch (stype) {
case SVt_PVCV:
location = (SV **) &(GvGP(dstr)->gp_cv); /* XXX bypassing GvCV_set */
import_flag = GVf_IMPORTED_CV;
goto common;
case SVt_PVHV:
location = (SV **) &GvHV(dstr);
import_flag = GVf_IMPORTED_HV;
goto common;
case SVt_PVAV:
location = (SV **) &GvAV(dstr);
import_flag = GVf_IMPORTED_AV;
goto common;
case SVt_PVIO:
location = (SV **) &GvIOp(dstr);
goto common;
case SVt_PVFM:
location = (SV **) &GvFORM(dstr);
goto common;
default:
location = &GvSV(dstr);
import_flag = GVf_IMPORTED_SV;
common:
if (intro) {
if (stype == SVt_PVCV) {
/*if (GvCVGEN(dstr) && (GvCV(dstr) != (const CV *)sref || GvCVGEN(dstr))) {*/
if (GvCVGEN(dstr)) {
SvREFCNT_dec(GvCV(dstr));
GvCV_set(dstr, NULL);
GvCVGEN(dstr) = 0; /* Switch off cacheness. */
}
}
/* SAVEt_GVSLOT takes more room on the savestack and has more
overhead in leave_scope than SAVEt_GENERIC_SV. But for CVs
leave_scope needs access to the GV so it can reset method
caches. We must use SAVEt_GVSLOT whenever the type is
SVt_PVCV, even if the stash is anonymous, as the stash may
gain a name somehow before leave_scope. */
if (stype == SVt_PVCV) {
/* There is no save_pushptrptrptr. Creating it for this
one call site would be overkill. So inline the ss add
routines here. */
dSS_ADD;
SS_ADD_PTR(dstr);
SS_ADD_PTR(location);
SS_ADD_PTR(SvREFCNT_inc(*location));
SS_ADD_UV(SAVEt_GVSLOT);
SS_ADD_END(4);
}
else SAVEGENERICSV(*location);
}
dref = *location;
if (stype == SVt_PVCV && (*location != sref || GvCVGEN(dstr))) {
CV* const cv = MUTABLE_CV(*location);
if (cv) {
if (!GvCVGEN((const GV *)dstr) &&
(CvROOT(cv) || CvXSUB(cv)) &&
/* redundant check that avoids creating the extra SV
most of the time: */
(CvCONST(cv) || ckWARN(WARN_REDEFINE)))
{
SV * const new_const_sv =
CvCONST((const CV *)sref)
? cv_const_sv((const CV *)sref)
: NULL;
report_redefined_cv(
sv_2mortal(Perl_newSVpvf(aTHX_
"%"HEKf"::%"HEKf,
HEKfARG(
HvNAME_HEK(GvSTASH((const GV *)dstr))
),
HEKfARG(GvENAME_HEK(MUTABLE_GV(dstr)))
)),
cv,
CvCONST((const CV *)sref) ? &new_const_sv : NULL
);
}
if (!intro)
cv_ckproto_len_flags(cv, (const GV *)dstr,
SvPOK(sref) ? CvPROTO(sref) : NULL,
SvPOK(sref) ? CvPROTOLEN(sref) : 0,
SvPOK(sref) ? SvUTF8(sref) : 0);
}
GvCVGEN(dstr) = 0; /* Switch off cacheness. */
GvASSUMECV_on(dstr);
if(GvSTASH(dstr)) gv_method_changed(dstr); /* sub foo { 1 } sub bar { 2 } *bar = \&foo */
}
*location = SvREFCNT_inc_simple_NN(sref);
if (import_flag && !(GvFLAGS(dstr) & import_flag)
&& CopSTASH_ne(PL_curcop, GvSTASH(dstr))) {
GvFLAGS(dstr) |= import_flag;
}
if (stype == SVt_PVHV) {
const char * const name = GvNAME((GV*)dstr);
const STRLEN len = GvNAMELEN(dstr);
if (
(
(len > 1 && name[len-2] == ':' && name[len-1] == ':')
|| (len == 1 && name[0] == ':')
)
&& (!dref || HvENAME_get(dref))
) {
mro_package_moved(
(HV *)sref, (HV *)dref,
(GV *)dstr, 0
);
}
}
else if (
stype == SVt_PVAV && sref != dref
&& strEQ(GvNAME((GV*)dstr), "ISA")
/* The stash may have been detached from the symbol table, so
check its name before doing anything. */
&& GvSTASH(dstr) && HvENAME(GvSTASH(dstr))
) {
MAGIC *mg;
MAGIC * const omg = dref && SvSMAGICAL(dref)
? mg_find(dref, PERL_MAGIC_isa)
: NULL;
if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) {
if (SvTYPE(mg->mg_obj) != SVt_PVAV) {
AV * const ary = newAV();
av_push(ary, mg->mg_obj); /* takes the refcount */
mg->mg_obj = (SV *)ary;
}
if (omg) {
if (SvTYPE(omg->mg_obj) == SVt_PVAV) {
SV **svp = AvARRAY((AV *)omg->mg_obj);
I32 items = AvFILLp((AV *)omg->mg_obj) + 1;
while (items--)
av_push(
(AV *)mg->mg_obj,
SvREFCNT_inc_simple_NN(*svp++)
);
}
else
av_push(
(AV *)mg->mg_obj,
SvREFCNT_inc_simple_NN(omg->mg_obj)
);
}
else
av_push((AV *)mg->mg_obj,SvREFCNT_inc_simple_NN(dstr));
}
else
{
sv_magic(
sref, omg ? omg->mg_obj : dstr, PERL_MAGIC_isa, NULL, 0
);
mg = mg_find(sref, PERL_MAGIC_isa);
}
/* Since the *ISA assignment could have affected more than
one stash, don't call mro_isa_changed_in directly, but let
magic_clearisa do it for us, as it already has the logic for
dealing with globs vs arrays of globs. */
assert(mg);
Perl_magic_clearisa(aTHX_ NULL, mg);
}
else if (stype == SVt_PVIO) {
DEBUG_o(Perl_deb(aTHX_ "glob_assign_ref clearing PL_stashcache\n"));
/* It's a cache. It will rebuild itself quite happily.
It's a lot of effort to work out exactly which key (or keys)
might be invalidated by the creation of the this file handle.
*/
hv_clear(PL_stashcache);
}
break;
}
if (!intro) SvREFCNT_dec(dref);
if (SvTAINTED(sstr))
SvTAINT(dstr);
return;
}
/* Work around compiler warnings about unsigned >= THRESHOLD when thres-
hold is 0. */
#if SV_COW_THRESHOLD
# define GE_COW_THRESHOLD(len) ((len) >= SV_COW_THRESHOLD)
#else
# define GE_COW_THRESHOLD(len) 1
#endif
#if SV_COWBUF_THRESHOLD
# define GE_COWBUF_THRESHOLD(len) ((len) >= SV_COWBUF_THRESHOLD)
#else
# define GE_COWBUF_THRESHOLD(len) 1
#endif
void
Perl_sv_setsv_flags(pTHX_ SV *dstr, SV* sstr, const I32 flags)
{
dVAR;
U32 sflags;
int dtype;
svtype stype;
PERL_ARGS_ASSERT_SV_SETSV_FLAGS;
if (sstr == dstr)
return;
if (SvIS_FREED(dstr)) {
Perl_croak(aTHX_ "panic: attempt to copy value %" SVf
" to a freed scalar %p", SVfARG(sstr), (void *)dstr);
}
SV_CHECK_THINKFIRST_COW_DROP(dstr);
if (!sstr)
sstr = &PL_sv_undef;
if (SvIS_FREED(sstr)) {
Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p to %p",
(void*)sstr, (void*)dstr);
}
stype = SvTYPE(sstr);
dtype = SvTYPE(dstr);
/* There's a lot of redundancy below but we're going for speed here */
switch (stype) {
case SVt_NULL:
undef_sstr:
if (dtype != SVt_PVGV && dtype != SVt_PVLV) {
(void)SvOK_off(dstr);
return;
}
break;
case SVt_IV:
if (SvIOK(sstr)) {
switch (dtype) {
case SVt_NULL:
sv_upgrade(dstr, SVt_IV);
break;
case SVt_NV:
case SVt_PV:
sv_upgrade(dstr, SVt_PVIV);
break;
case SVt_PVGV:
case SVt_PVLV:
goto end_of_first_switch;
}
(void)SvIOK_only(dstr);
SvIV_set(dstr, SvIVX(sstr));
if (SvIsUV(sstr))
SvIsUV_on(dstr);
/* SvTAINTED can only be true if the SV has taint magic, which in
turn means that the SV type is PVMG (or greater). This is the
case statement for SVt_IV, so this cannot be true (whatever gcov
may say). */
assert(!SvTAINTED(sstr));
return;
}
if (!SvROK(sstr))
goto undef_sstr;
if (dtype < SVt_PV && dtype != SVt_IV)
sv_upgrade(dstr, SVt_IV);
break;
case SVt_NV:
if (SvNOK(sstr)) {
switch (dtype) {
case SVt_NULL:
case SVt_IV:
sv_upgrade(dstr, SVt_NV);
break;
case SVt_PV:
case SVt_PVIV:
sv_upgrade(dstr, SVt_PVNV);
break;
case SVt_PVGV:
case SVt_PVLV:
goto end_of_first_switch;
}
SvNV_set(dstr, SvNVX(sstr));
(void)SvNOK_only(dstr);
/* SvTAINTED can only be true if the SV has taint magic, which in
turn means that the SV type is PVMG (or greater). This is the
case statement for SVt_NV, so this cannot be true (whatever gcov
may say). */
assert(!SvTAINTED(sstr));
return;
}
goto undef_sstr;
case SVt_PV:
if (dtype < SVt_PV)
sv_upgrade(dstr, SVt_PV);
break;
case SVt_PVIV:
if (dtype < SVt_PVIV)
sv_upgrade(dstr, SVt_PVIV);
break;
case SVt_PVNV:
if (dtype < SVt_PVNV)
sv_upgrade(dstr, SVt_PVNV);
break;
default:
{
const char * const type = sv_reftype(sstr,0);
if (PL_op)
/* diag_listed_as: Bizarre copy of %s */
Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_DESC(PL_op));
else
Perl_croak(aTHX_ "Bizarre copy of %s", type);
}
break;
case SVt_REGEXP:
upgregexp:
if (dtype < SVt_REGEXP)
{
if (dtype >= SVt_PV) {
SvPV_free(dstr);
SvPV_set(dstr, 0);
SvLEN_set(dstr, 0);
SvCUR_set(dstr, 0);
}
sv_upgrade(dstr, SVt_REGEXP);
}
break;
case SVt_INVLIST:
case SVt_PVLV:
case SVt_PVGV:
case SVt_PVMG:
if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
mg_get(sstr);
if (SvTYPE(sstr) != stype)
stype = SvTYPE(sstr);
}
if (isGV_with_GP(sstr) && dtype <= SVt_PVLV) {
glob_assign_glob(dstr, sstr, dtype);
return;
}
if (stype == SVt_PVLV)
{