3 * Copyright (C) 2022 by Larry Wall and others
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
10 /* This file contains the newSV_type and newSV_type_mortal functions, as well as
11 * the various struct and macro definitions they require. In the main, these
12 * definitions were moved from sv.c, where many of them continue to also be used.
13 * (In Perl_more_bodies, Perl_sv_upgrade and Perl_sv_clear, for example.) Code
14 * comments associated with definitions and functions were also copied across
17 * The rationale for having these as inline functions, rather than in sv.c, is
18 * that the target type is very often known at compile time, and therefore
19 * optimum code can be emitted by the compiler, rather than having all calls
20 * traverse the many branches of Perl_sv_upgrade at runtime.
23 /* This definition came from perl.h*/
25 /* The old value was hard coded at 1008. (4096-16) seems to be a bit faster,
26 at least on FreeBSD. YMMV, so experiment. */
27 #ifndef PERL_ARENA_SIZE
28 #define PERL_ARENA_SIZE 4080
31 /* All other pre-existing definitions and functions that were moved into this
32 * file originally came from sv.c. */
35 # define SvARENA_CHAIN(sv) ((sv)->sv_u.svu_rv)
36 # define SvARENA_CHAIN_SET(sv,val) (sv)->sv_u.svu_rv = MUTABLE_SV((val))
37 /* Whilst I'd love to do this, it seems that things like to check on
39 # define POISON_SV_HEAD(sv) PoisonNew(sv, 1, struct STRUCT_SV)
41 # define POISON_SV_HEAD(sv) PoisonNew(&SvANY(sv), 1, void *), \
42 PoisonNew(&SvREFCNT(sv), 1, U32)
44 # define SvARENA_CHAIN(sv) SvANY(sv)
45 # define SvARENA_CHAIN_SET(sv,val) SvANY(sv) = (void *)(val)
46 # define POISON_SV_HEAD(sv)
50 # define MEM_LOG_NEW_SV(sv, file, line, func) \
51 Perl_mem_log_new_sv(sv, file, line, func)
52 # define MEM_LOG_DEL_SV(sv, file, line, func) \
53 Perl_mem_log_del_sv(sv, file, line, func)
55 # define MEM_LOG_NEW_SV(sv, file, line, func) NOOP
56 # define MEM_LOG_DEL_SV(sv, file, line, func) NOOP
59 #define uproot_SV(p) \
62 PL_sv_root = MUTABLE_SV(SvARENA_CHAIN(p)); \
66 /* Perl_more_sv lives in sv.c, we don't want to inline it.
67 * but the function declaration seems to be needed. */
68 SV* Perl_more_sv(pTHX);
70 /* new_SV(): return a new, empty SV head */
71 PERL_STATIC_INLINE SV*
72 Perl_new_sv(pTHX_ const char *file, int line, const char *func)
75 #if !defined(DEBUG_LEAKING_SCALARS) || \
76 (!defined(DEBUGGING) && !defined(PERL_MEM_LOG))
77 PERL_UNUSED_ARG(file);
78 PERL_UNUSED_ARG(line);
79 PERL_UNUSED_ARG(func);
85 sv = Perl_more_sv(aTHX);
89 #ifdef DEBUG_LEAKING_SCALARS
90 sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
91 sv->sv_debug_line = (U16) (PL_parser && PL_parser->copline != NOLINE
97 sv->sv_debug_inpad = 0;
98 sv->sv_debug_parent = NULL;
99 sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL;
101 sv->sv_debug_serial = PL_sv_serial++;
103 MEM_LOG_NEW_SV(sv, file, line, func);
104 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) new_SV (from %s:%d [%s])\n",
105 PTR2UV(sv), (long)sv->sv_debug_serial, file, line, func));
109 # define new_SV(p) (p)=Perl_new_sv(aTHX_ __FILE__, __LINE__, FUNCTION__)
111 typedef struct xpvhv_with_aux XPVHV_WITH_AUX;
113 struct body_details {
114 U8 body_size; /* Size to allocate */
115 U8 copy; /* Size of structure to copy (may be shorter) */
116 U8 offset; /* Size of unalloced ghost fields to first alloced field*/
117 PERL_BITFIELD8 type : 5; /* We have space for a sanity check. */
118 PERL_BITFIELD8 cant_upgrade : 1;/* Cannot upgrade this type */
119 PERL_BITFIELD8 zero_nv : 1; /* zero the NV when upgrading from this */
120 PERL_BITFIELD8 arena : 1; /* Allocated from an arena */
121 U32 arena_size; /* Size of arena to allocate */
124 #define ALIGNED_TYPE_NAME(name) name##_aligned
125 #define ALIGNED_TYPE(name) \
130 } ALIGNED_TYPE_NAME(name)
132 ALIGNED_TYPE(regexp);
137 ALIGNED_TYPE(XPVHV_WITH_AUX);
141 ALIGNED_TYPE(XPVOBJ);
148 /* With -DPURFIY we allocate everything directly, and don't use arenas.
149 This seems a rather elegant way to simplify some of the code below. */
150 #define HASARENA FALSE
152 #define HASARENA TRUE
154 #define NOARENA FALSE
156 /* Size the arenas to exactly fit a given number of bodies. A count
157 of 0 fits the max number bodies into a PERL_ARENA_SIZE.block,
158 simplifying the default. If count > 0, the arena is sized to fit
159 only that many bodies, allowing arenas to be used for large, rare
160 bodies (XPVFM, XPVIO) without undue waste. The arena size is
161 limited by PERL_ARENA_SIZE, so we can safely oversize the
164 #define FIT_ARENA0(body_size) \
165 ((size_t)(PERL_ARENA_SIZE / body_size) * body_size)
166 #define FIT_ARENAn(count,body_size) \
167 ( count * body_size <= PERL_ARENA_SIZE) \
168 ? count * body_size \
169 : FIT_ARENA0 (body_size)
170 #define FIT_ARENA(count,body_size) \
172 ? FIT_ARENAn (count, body_size) \
173 : FIT_ARENA0 (body_size))
175 /* Calculate the length to copy. Specifically work out the length less any
176 final padding the compiler needed to add. See the comment in sv_upgrade
177 for why copying the padding proved to be a bug. */
179 #define copy_length(type, last_member) \
180 STRUCT_OFFSET(type, last_member) \
181 + sizeof (((type*)SvANY((const SV *)0))->last_member)
183 static const struct body_details bodies_by_type[] = {
184 /* HEs use this offset for their arena. */
185 { 0, 0, 0, SVt_NULL, FALSE, NONV, NOARENA, 0 },
187 /* IVs are in the head, so the allocation size is 0. */
189 sizeof(IV), /* This is used to copy out the IV body. */
190 STRUCT_OFFSET(XPVIV, xiv_iv), SVt_IV, FALSE, NONV,
191 NOARENA /* IVS don't need an arena */, 0
196 STRUCT_OFFSET(XPVNV, xnv_u),
197 SVt_NV, FALSE, HADNV, NOARENA, 0 },
199 { sizeof(NV), sizeof(NV),
200 STRUCT_OFFSET(XPVNV, xnv_u),
201 SVt_NV, FALSE, HADNV, HASARENA, FIT_ARENA(0, sizeof(NV)) },
204 { sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur),
205 copy_length(XPV, xpv_len) - STRUCT_OFFSET(XPV, xpv_cur),
206 + STRUCT_OFFSET(XPV, xpv_cur),
207 SVt_PV, FALSE, NONV, HASARENA,
208 FIT_ARENA(0, sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur)) },
210 { sizeof(XINVLIST) - STRUCT_OFFSET(XPV, xpv_cur),
211 copy_length(XINVLIST, is_offset) - STRUCT_OFFSET(XPV, xpv_cur),
212 + STRUCT_OFFSET(XPV, xpv_cur),
213 SVt_INVLIST, TRUE, NONV, HASARENA,
214 FIT_ARENA(0, sizeof(XINVLIST) - STRUCT_OFFSET(XPV, xpv_cur)) },
216 { sizeof(XPVIV) - STRUCT_OFFSET(XPV, xpv_cur),
217 copy_length(XPVIV, xiv_u) - STRUCT_OFFSET(XPV, xpv_cur),
218 + STRUCT_OFFSET(XPV, xpv_cur),
219 SVt_PVIV, FALSE, NONV, HASARENA,
220 FIT_ARENA(0, sizeof(XPVIV) - STRUCT_OFFSET(XPV, xpv_cur)) },
222 #if NVSIZE > 8 && PTRSIZE < 8 && MEM_ALIGNBYTES > 8
223 /* NV may need strict 16 byte alignment.
225 On 64-bit systems the NV ends up aligned despite the hack
226 avoiding allocation of xmg_stash and xmg_u, so only do this
232 SVt_PVNV, FALSE, HADNV, HASARENA,
233 FIT_ARENA(0, sizeof(XPVNV)) },
235 { sizeof(XPVNV) - STRUCT_OFFSET(XPV, xpv_cur),
236 copy_length(XPVNV, xnv_u) - STRUCT_OFFSET(XPV, xpv_cur),
237 + STRUCT_OFFSET(XPV, xpv_cur),
238 SVt_PVNV, FALSE, HADNV, HASARENA,
239 FIT_ARENA(0, sizeof(XPVNV) - STRUCT_OFFSET(XPV, xpv_cur)) },
241 { sizeof(XPVMG), copy_length(XPVMG, xnv_u), 0, SVt_PVMG, FALSE, HADNV,
242 HASARENA, FIT_ARENA(0, sizeof(XPVMG)) },
244 { sizeof(ALIGNED_TYPE_NAME(regexp)),
247 SVt_REGEXP, TRUE, NONV, HASARENA,
248 FIT_ARENA(0, sizeof(ALIGNED_TYPE_NAME(regexp)))
251 { sizeof(ALIGNED_TYPE_NAME(XPVGV)), sizeof(XPVGV), 0, SVt_PVGV, TRUE, HADNV,
252 HASARENA, FIT_ARENA(0, sizeof(ALIGNED_TYPE_NAME(XPVGV))) },
254 { sizeof(ALIGNED_TYPE_NAME(XPVLV)), sizeof(XPVLV), 0, SVt_PVLV, TRUE, HADNV,
255 HASARENA, FIT_ARENA(0, sizeof(ALIGNED_TYPE_NAME(XPVLV))) },
257 { sizeof(ALIGNED_TYPE_NAME(XPVAV)),
258 copy_length(XPVAV, xav_alloc),
260 SVt_PVAV, TRUE, NONV, HASARENA,
261 FIT_ARENA(0, sizeof(ALIGNED_TYPE_NAME(XPVAV))) },
263 { sizeof(ALIGNED_TYPE_NAME(XPVHV)),
264 copy_length(XPVHV, xhv_max),
266 SVt_PVHV, TRUE, NONV, HASARENA,
267 FIT_ARENA(0, sizeof(ALIGNED_TYPE_NAME(XPVHV))) },
269 { sizeof(ALIGNED_TYPE_NAME(XPVCV)),
272 SVt_PVCV, TRUE, NONV, HASARENA,
273 FIT_ARENA(0, sizeof(ALIGNED_TYPE_NAME(XPVCV))) },
275 { sizeof(ALIGNED_TYPE_NAME(XPVFM)),
278 SVt_PVFM, TRUE, NONV, NOARENA,
279 FIT_ARENA(20, sizeof(ALIGNED_TYPE_NAME(XPVFM))) },
281 { sizeof(ALIGNED_TYPE_NAME(XPVIO)),
284 SVt_PVIO, TRUE, NONV, HASARENA,
285 FIT_ARENA(24, sizeof(ALIGNED_TYPE_NAME(XPVIO))) },
287 { sizeof(ALIGNED_TYPE_NAME(XPVOBJ)),
288 copy_length(XPVOBJ, xobject_fields),
290 SVt_PVOBJ, TRUE, NONV, HASARENA,
291 FIT_ARENA(0, sizeof(ALIGNED_TYPE_NAME(XPVOBJ))) },
294 #define new_body_allocated(sv_type) \
295 (void *)((char *)S_new_body(aTHX_ sv_type) \
296 - bodies_by_type[sv_type].offset)
299 #if !(NVSIZE <= IVSIZE)
300 # define new_XNV() safemalloc(sizeof(XPVNV))
302 #define new_XPVNV() safemalloc(sizeof(XPVNV))
303 #define new_XPVMG() safemalloc(sizeof(XPVMG))
305 #define del_body_by_type(p, type) safefree(p)
309 #if !(NVSIZE <= IVSIZE)
310 # define new_XNV() new_body_allocated(SVt_NV)
312 #define new_XPVNV() new_body_allocated(SVt_PVNV)
313 #define new_XPVMG() new_body_allocated(SVt_PVMG)
315 #define del_body_by_type(p, type) \
316 del_body(p + bodies_by_type[(type)].offset, \
317 &PL_body_roots[(type)])
321 /* no arena for you! */
323 #define new_NOARENA(details) \
324 safemalloc((details)->body_size + (details)->offset)
325 #define new_NOARENAZ(details) \
326 safecalloc((details)->body_size + (details)->offset, 1)
330 /* grab a new thing from the arena's free list, allocating more if necessary. */
331 #define new_body_from_arena(xpv, root_index, type_meta) \
333 void ** const r3wt = &PL_body_roots[root_index]; \
334 xpv = (PTR_TBL_ENT_t*) (*((void **)(r3wt)) \
335 ? *((void **)(r3wt)) : Perl_more_bodies(aTHX_ root_index, \
336 type_meta.body_size,\
337 type_meta.arena_size)); \
338 *(r3wt) = *(void**)(xpv); \
341 PERL_STATIC_INLINE void *
342 S_new_body(pTHX_ const svtype sv_type)
345 new_body_from_arena(xpv, sv_type, bodies_by_type[sv_type]);
351 static const struct body_details fake_rv =
352 { 0, 0, 0, SVt_IV, FALSE, NONV, NOARENA, 0 };
354 static const struct body_details fake_hv_with_aux =
355 /* The SVt_IV arena is used for (larger) PVHV bodies. */
356 { sizeof(ALIGNED_TYPE_NAME(XPVHV_WITH_AUX)),
357 copy_length(XPVHV, xhv_max),
359 SVt_PVHV, TRUE, NONV, HASARENA,
360 FIT_ARENA(0, sizeof(ALIGNED_TYPE_NAME(XPVHV_WITH_AUX))) };
363 =for apidoc newSV_type
365 Creates a new SV, of the type specified. The reference count for the new SV
371 PERL_STATIC_INLINE SV *
372 Perl_newSV_type(pTHX_ const svtype type)
376 const struct body_details *type_details;
380 type_details = bodies_by_type + type;
382 SvFLAGS(sv) &= ~SVTYPEMASK;
389 SET_SVANY_FOR_BODYLESS_IV(sv);
394 SET_SVANY_FOR_BODYLESS_NV(sv);
396 SvANY(sv) = new_XNV();
403 assert(type_details->body_size);
406 assert(type_details->arena);
407 assert(type_details->arena_size);
408 /* This points to the start of the allocated area. */
409 new_body = S_new_body(aTHX_ type);
410 /* xpvav and xpvhv have no offset, so no need to adjust new_body */
411 assert(!(type_details->offset));
413 /* We always allocated the full length item with PURIFY. To do this
414 we fake things so that arena is false for all 16 types.. */
415 new_body = new_NOARENAZ(type_details);
417 SvANY(sv) = new_body;
419 SvSTASH_set(sv, NULL);
420 SvMAGIC_set(sv, NULL);
432 /* start with PERL_HASH_DEFAULT_HvMAX+1 buckets: */
433 HvMAX(sv) = PERL_HASH_DEFAULT_HvMAX;
437 #ifndef NODEFAULT_SHAREKEYS
438 HvSHAREKEYS_on(sv); /* key-sharing on by default */
440 /* start with PERL_HASH_DEFAULT_HvMAX+1 buckets: */
441 HvMAX(sv) = PERL_HASH_DEFAULT_HvMAX;
444 ObjectMAXFIELD(sv) = -1;
445 ObjectFIELDS(sv) = NULL;
451 sv->sv_u.svu_array = NULL; /* or svu_hash */
464 /* For a type known at compile time, it should be possible for the
465 * compiler to deduce the value of (type_details->arena), resolve
466 * that branch below, and inline the relevant values from
467 * bodies_by_type. Except, at least for gcc, it seems not to do that.
468 * We help it out here with two deviations from sv_upgrade:
469 * (1) Minor rearrangement here, so that PVFM - the only type at this
470 * point not to be allocated from an array appears last, not PV.
471 * (2) The ASSUME() statement here for everything that isn't PVFM.
472 * Obviously this all only holds as long as it's a true reflection of
473 * the bodies_by_type lookup table. */
475 ASSUME(type_details->arena);
480 assert(type_details->body_size);
481 /* We always allocated the full length item with PURIFY. To do this
482 we fake things so that arena is false for all 16 types.. */
484 if(type_details->arena) {
485 /* This points to the start of the allocated area. */
486 new_body = S_new_body(aTHX_ type);
487 Zero(new_body, type_details->body_size, char);
488 new_body = ((char *)new_body) - type_details->offset;
492 new_body = new_NOARENAZ(type_details);
494 SvANY(sv) = new_body;
496 if (UNLIKELY(type == SVt_PVIO)) {
497 IO * const io = MUTABLE_IO(sv);
498 GV *iogv = gv_fetchpvs("IO::File::", GV_ADD, SVt_PVHV);
501 /* Clear the stashcache because a new IO could overrule a package
503 DEBUG_o(Perl_deb(aTHX_ "sv_upgrade clearing PL_stashcache\n"));
504 hv_clear(PL_stashcache);
506 SvSTASH_set(io, MUTABLE_HV(SvREFCNT_inc(GvHV(iogv))));
510 sv->sv_u.svu_rv = NULL;
513 Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu",
514 (unsigned long)type);
521 =for apidoc newSV_type_mortal
523 Creates a new mortal SV, of the type specified. The reference count for the
526 This is equivalent to
527 SV* sv = sv_2mortal(newSV_type(<some type>))
529 SV* sv = sv_newmortal();
530 sv_upgrade(sv, <some_type>)
531 but should be more efficient than both of them. (Unless sv_2mortal is inlined
532 at some point in the future.)
537 PERL_STATIC_INLINE SV *
538 Perl_newSV_type_mortal(pTHX_ const svtype type)
540 SV *sv = newSV_type(type);
541 SSize_t ix = ++PL_tmps_ix;
542 if (UNLIKELY(ix >= PL_tmps_max))
543 ix = Perl_tmps_grow_p(aTHX_ ix);
544 PL_tmps_stack[ix] = (sv);
549 /* The following functions started out in sv.h and then moved to inline.h. They
550 * moved again into this file during the 5.37.x development cycle. */
553 =for apidoc_section $SV
554 =for apidoc SvPVXtrue
556 Returns a boolean as to whether or not C<sv> contains a PV that is considered
557 TRUE. FALSE is returned if C<sv> doesn't contain a PV, or if the PV it does
558 contain is zero length, or consists of just the single character '0'. Every
559 other PV value is considered TRUE.
561 As of Perl v5.37.1, C<sv> is evaluated exactly once; in earlier releases, it
562 could be evaluated more than once.
567 PERL_STATIC_INLINE bool
568 Perl_SvPVXtrue(pTHX_ SV *sv)
570 PERL_ARGS_ASSERT_SVPVXTRUE;
574 if (! (XPV *) SvANY(sv)) {
578 if ( ((XPV *) SvANY(sv))->xpv_cur > 1) { /* length > 1 */
582 if (( (XPV *) SvANY(sv))->xpv_cur == 0) {
586 return *sv->sv_u.svu_pv != '0';
590 =for apidoc SvGETMAGIC
591 Invokes C<L</mg_get>> on an SV if it has 'get' magic. For example, this
592 will call C<FETCH> on a tied variable. As of 5.37.1, this function is
593 guaranteed to evaluate its argument exactly once.
598 PERL_STATIC_INLINE void
599 Perl_SvGETMAGIC(pTHX_ SV *sv)
601 PERL_ARGS_ASSERT_SVGETMAGIC;
603 if (UNLIKELY(SvGMAGICAL(sv))) {
608 PERL_STATIC_INLINE bool
609 Perl_SvTRUE(pTHX_ SV *sv)
611 PERL_ARGS_ASSERT_SVTRUE;
613 if (UNLIKELY(sv == NULL))
616 return SvTRUE_nomg_NN(sv);
619 PERL_STATIC_INLINE bool
620 Perl_SvTRUE_nomg(pTHX_ SV *sv)
622 PERL_ARGS_ASSERT_SVTRUE_NOMG;
624 if (UNLIKELY(sv == NULL))
626 return SvTRUE_nomg_NN(sv);
629 PERL_STATIC_INLINE bool
630 Perl_SvTRUE_NN(pTHX_ SV *sv)
632 PERL_ARGS_ASSERT_SVTRUE_NN;
635 return SvTRUE_nomg_NN(sv);
638 PERL_STATIC_INLINE bool
639 Perl_SvTRUE_common(pTHX_ SV * sv, const bool sv_2bool_is_fallback)
641 PERL_ARGS_ASSERT_SVTRUE_COMMON;
643 if (UNLIKELY(SvIMMORTAL_INTERP(sv)))
644 return SvIMMORTAL_TRUE(sv);
650 return SvPVXtrue(sv);
653 return SvIVX(sv) != 0; /* casts to bool */
655 if (SvROK(sv) && !(SvOBJECT(SvRV(sv)) && HvAMAGIC(SvSTASH(SvRV(sv)))))
658 if (sv_2bool_is_fallback)
659 return sv_2bool_nomg(sv);
661 return isGV_with_GP(sv);
664 PERL_STATIC_INLINE SV *
665 Perl_SvREFCNT_inc(SV *sv)
667 if (LIKELY(sv != NULL))
672 PERL_STATIC_INLINE SV *
673 Perl_SvREFCNT_inc_NN(SV *sv)
675 PERL_ARGS_ASSERT_SVREFCNT_INC_NN;
681 PERL_STATIC_INLINE void
682 Perl_SvREFCNT_inc_void(SV *sv)
684 if (LIKELY(sv != NULL))
688 PERL_STATIC_INLINE void
689 Perl_SvREFCNT_dec(pTHX_ SV *sv)
691 if (LIKELY(sv != NULL)) {
692 U32 rc = SvREFCNT(sv);
694 SvREFCNT(sv) = rc - 1;
696 Perl_sv_free2(aTHX_ sv, rc);
700 PERL_STATIC_INLINE SV *
701 Perl_SvREFCNT_dec_ret_NULL(pTHX_ SV *sv)
703 PERL_ARGS_ASSERT_SVREFCNT_DEC_RET_NULL;
704 Perl_SvREFCNT_dec(aTHX_ sv);
709 PERL_STATIC_INLINE void
710 Perl_SvREFCNT_dec_NN(pTHX_ SV *sv)
712 U32 rc = SvREFCNT(sv);
714 PERL_ARGS_ASSERT_SVREFCNT_DEC_NN;
717 SvREFCNT(sv) = rc - 1;
719 Perl_sv_free2(aTHX_ sv, rc);
723 =for apidoc SvAMAGIC_on
725 Indicate that C<sv> has overloading (active magic) enabled.
730 PERL_STATIC_INLINE void
731 Perl_SvAMAGIC_on(SV *sv)
733 PERL_ARGS_ASSERT_SVAMAGIC_ON;
736 if (SvOBJECT(SvRV(sv))) HvAMAGIC_on(SvSTASH(SvRV(sv)));
740 =for apidoc SvAMAGIC_off
742 Indicate that C<sv> has overloading (active magic) disabled.
747 PERL_STATIC_INLINE void
748 Perl_SvAMAGIC_off(SV *sv)
750 PERL_ARGS_ASSERT_SVAMAGIC_OFF;
752 if (SvROK(sv) && SvOBJECT(SvRV(sv)))
753 HvAMAGIC_off(SvSTASH(SvRV(sv)));
756 PERL_STATIC_INLINE U32
757 Perl_SvPADSTALE_on(SV *sv)
759 assert(!(SvFLAGS(sv) & SVs_PADTMP));
760 return SvFLAGS(sv) |= SVs_PADSTALE;
762 PERL_STATIC_INLINE U32
763 Perl_SvPADSTALE_off(SV *sv)
765 assert(!(SvFLAGS(sv) & SVs_PADTMP));
766 return SvFLAGS(sv) &= ~SVs_PADSTALE;
770 =for apidoc_section $SV
772 =for apidoc_item SvIV_nomg
773 =for apidoc_item m||SvIVx
775 These each coerce the given SV to IV and return it. The returned value in many
776 circumstances will get stored in C<sv>'s IV slot, but not in all cases. (Use
777 C<L</sv_setiv>> to make sure it does).
779 As of 5.37.1, all are guaranteed to evaluate C<sv> only once.
781 C<SvIVx> is now identical to C<SvIV>, but prior to 5.37.1, it was the only form
782 guaranteed to evaluate C<sv> only once.
784 C<SvIV_nomg> is the same as C<SvIV>, but does not perform 'get' magic.
787 =for apidoc_item SvNV_nomg
788 =for apidoc_item m||SvNVx
790 These each coerce the given SV to NV and return it. The returned value in many
791 circumstances will get stored in C<sv>'s NV slot, but not in all cases. (Use
792 C<L</sv_setnv>> to make sure it does).
794 As of 5.37.1, all are guaranteed to evaluate C<sv> only once.
796 C<SvNVx> is now identical to C<SvNV>, but prior to 5.37.1, it was the only form
797 guaranteed to evaluate C<sv> only once.
799 C<SvNV_nomg> is the same as C<SvNV>, but does not perform 'get' magic.
802 =for apidoc_item SvUV_nomg
803 =for apidoc_item m||SvUVx
805 These each coerce the given SV to UV and return it. The returned value in many
806 circumstances will get stored in C<sv>'s UV slot, but not in all cases. (Use
807 C<L</sv_setuv>> to make sure it does).
809 As of 5.37.1, all are guaranteed to evaluate C<sv> only once.
811 C<SvUVx> is now identical to C<SvUV>, but prior to 5.37.1, it was the only form
812 guaranteed to evaluate C<sv> only once.
817 PERL_STATIC_INLINE IV
818 Perl_SvIV(pTHX_ SV *sv) {
819 PERL_ARGS_ASSERT_SVIV;
826 PERL_STATIC_INLINE UV
827 Perl_SvUV(pTHX_ SV *sv) {
828 PERL_ARGS_ASSERT_SVUV;
835 PERL_STATIC_INLINE NV
836 Perl_SvNV(pTHX_ SV *sv) {
837 PERL_ARGS_ASSERT_SVNV;
844 PERL_STATIC_INLINE IV
845 Perl_SvIV_nomg(pTHX_ SV *sv) {
846 PERL_ARGS_ASSERT_SVIV_NOMG;
850 return sv_2iv_flags(sv, 0);
853 PERL_STATIC_INLINE UV
854 Perl_SvUV_nomg(pTHX_ SV *sv) {
855 PERL_ARGS_ASSERT_SVUV_NOMG;
859 return sv_2uv_flags(sv, 0);
862 PERL_STATIC_INLINE NV
863 Perl_SvNV_nomg(pTHX_ SV *sv) {
864 PERL_ARGS_ASSERT_SVNV_NOMG;
868 return sv_2nv_flags(sv, 0);
871 #if defined(PERL_CORE) || defined (PERL_EXT)
872 PERL_STATIC_INLINE STRLEN
873 S_sv_or_pv_pos_u2b(pTHX_ SV *sv, const char *pv, STRLEN pos, STRLEN *lenp)
875 PERL_ARGS_ASSERT_SV_OR_PV_POS_U2B;
877 U8 *hopped = utf8_hop((U8 *)pv, pos);
878 if (lenp) *lenp = (STRLEN)(utf8_hop(hopped, *lenp) - hopped);
879 return (STRLEN)(hopped - (U8 *)pv);
881 return sv_pos_u2b_flags(sv,pos,lenp,SV_CONST_RETURN);
885 PERL_STATIC_INLINE char *
886 Perl_sv_pvutf8n_force_wrapper(pTHX_ SV * const sv, STRLEN * const lp, const U32 dummy)
888 /* This is just so can be passed to Perl_SvPV_helper() as a function
889 * pointer with the same signature as all the other such pointers, and
890 * having hence an unused parameter */
891 PERL_ARGS_ASSERT_SV_PVUTF8N_FORCE_WRAPPER;
892 PERL_UNUSED_ARG(dummy);
894 return sv_pvutf8n_force(sv, lp);
897 PERL_STATIC_INLINE char *
898 Perl_sv_pvbyten_force_wrapper(pTHX_ SV * const sv, STRLEN * const lp, const U32 dummy)
900 /* This is just so can be passed to Perl_SvPV_helper() as a function
901 * pointer with the same signature as all the other such pointers, and
902 * having hence an unused parameter */
903 PERL_ARGS_ASSERT_SV_PVBYTEN_FORCE_WRAPPER;
904 PERL_UNUSED_ARG(dummy);
906 return sv_pvbyten_force(sv, lp);
909 PERL_STATIC_INLINE char *
910 Perl_SvPV_helper(pTHX_
914 const PL_SvPVtype type,
915 char * (*non_trivial)(pTHX_ SV *, STRLEN * const, const U32),
917 const U32 return_flags
920 /* 'type' should be known at compile time, so this is reduced to a single
921 * conditional at runtime */
922 if ( (type == SvPVbyte_type_ && SvPOK_byte_nog(sv))
923 || (type == SvPVforce_type_ && SvPOK_pure_nogthink(sv))
924 || (type == SvPVutf8_type_ && SvPOK_utf8_nog(sv))
925 || (type == SvPVnormal_type_ && SvPOK_nog(sv))
926 || (type == SvPVutf8_pure_type_ && SvPOK_utf8_pure_nogthink(sv))
927 || (type == SvPVbyte_pure_type_ && SvPOK_byte_pure_nogthink(sv))
933 /* Similarly 'return_flags is known at compile time, so this becomes
935 if (return_flags & SV_MUTABLE_RETURN) {
936 return SvPVX_mutable(sv);
938 else if(return_flags & SV_CONST_RETURN) {
939 return (char *) SvPVX_const(sv);
946 if (or_null) { /* This is also known at compile time */
947 if (flags & SV_GMAGIC) { /* As is this */
952 if (lp) { /* As is this */
960 /* Can't trivially handle this, call the function */
961 return non_trivial(aTHX_ sv, lp, (flags|return_flags));
965 =for apidoc newRV_noinc
967 Creates an RV wrapper for an SV. The reference count for the original
968 SV is B<not> incremented.
973 PERL_STATIC_INLINE SV *
974 Perl_newRV_noinc(pTHX_ SV *const tmpRef)
976 SV *sv = newSV_type(SVt_IV);
978 PERL_ARGS_ASSERT_NEWRV_NOINC;
982 /* inlined, simplified sv_setrv_noinc(sv, tmpRef); */
983 SvRV_set(sv, tmpRef);
989 PERL_STATIC_INLINE char *
990 Perl_sv_setpv_freshbuf(pTHX_ SV *const sv)
992 PERL_ARGS_ASSERT_SV_SETPV_FRESHBUF;
993 assert(SvTYPE(sv) >= SVt_PV);
994 assert(SvTYPE(sv) <= SVt_PVMG);
995 assert(!SvTHINKFIRST(sv));
999 (void)SvPOK_only_UTF8(sv); /* UTF-8 flag will be 0; This is used instead
1000 of 'SvPOK_only' because the other sv_setpv
1007 * ex: set ts=8 sts=4 sw=4 et: