summaryrefslogtreecommitdiff
path: root/sv.h
diff options
context:
space:
mode:
authorFather Chrysostomos <sprout@cpan.org>2012-10-08 00:20:21 -0700
committerFather Chrysostomos <sprout@cpan.org>2012-11-27 07:05:01 -0800
commitdb2c6cb33ec067c880a2cb3c4efdb33f7e3e3d0f (patch)
tree2460f0a21a4cfde265cd5fd481296eee2515c150 /sv.h
parent08bf00be470db7b367e14733226d4fddc004c796 (diff)
downloadperl-db2c6cb33ec067c880a2cb3c4efdb33f7e3e3d0f.tar.gz
New COW mechanism
This was discussed in ticket #114820. This new copy-on-write mechanism stores a reference count for the PV inside the PV itself, at the very end. (I was using SvEND+1 at first, but parts of the regexp engine expect to be able to do SvCUR_set(sv,0), which causes the wrong byte of the string to be used as the reference count.) Only 256 SVs can share the same PV this way. Also, only strings with allocated space after the trailing null can be used for copy-on-write. Much of the code is shared with PERL_OLD_COPY_ON_WRITE. The restric- tion against doing copy-on-write with magical variables has hence been inherited, though it is not necessary. A future commit will take care of that. I had to modify _core_swash_init to handle $@ differently. The exist- ing mechanism of copying $@ to a new scalar and back again was very fragile. With copy-on-write, $@ =~ s/// can cause pp_subst’s string pointers to become stale. So now we remove the scalar from *@ and allow the utf8-table-loading code to autovivify a new one. Then we restore the untouched $@ afterwards if all goes well.
Diffstat (limited to 'sv.h')
-rw-r--r--sv.h19
1 files changed, 18 insertions, 1 deletions
diff --git a/sv.h b/sv.h
index 25ceff93bf..c6c05e3e7b 100644
--- a/sv.h
+++ b/sv.h
@@ -1835,6 +1835,8 @@ Like sv_utf8_upgrade, but doesn't do magic on C<sv>.
((SvIsCOW(sv) ? sv_force_normal_flags(sv, 0) : (void) 0), 0)
# define SvIsCOW_normal(sv) (SvIsCOW(sv) && SvLEN(sv))
# define SvRELEASE_IVX_(sv) SvRELEASE_IVX(sv),
+# define SvCANCOW(sv) \
+ (SvIsCOW(sv) || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)
#else
# define SvRELEASE_IVX(sv) 0
/* This little game brought to you by the need to shut this warning up:
@@ -1842,6 +1844,16 @@ mg.c: In function 'Perl_magic_get':
mg.c:1024: warning: left-hand operand of comma expression has no effect
*/
# define SvRELEASE_IVX_(sv) /**/
+# ifdef PERL_NEW_COPY_ON_WRITE
+# define SvCANCOW(sv) \
+ (SvIsCOW(sv) \
+ ? SvLEN(sv) ? CowREFCNT(sv) != SV_COW_REFCNT_MAX : 1 \
+ : (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS \
+ && SvCUR(sv)+1 < SvLEN(sv))
+ /* Note: To allow 256 COW "copies", a refcnt of 0 means 1. */
+# define CowREFCNT(sv) (*(U8 *)(SvPVX(sv)+SvLEN(sv)-1))
+# define SV_COW_REFCNT_MAX ((1 << sizeof(U8)*8) - 1)
+# endif
#endif /* PERL_OLD_COPY_ON_WRITE */
/* This is a pessimistic view. Scalar must be purely a read-write PV to copy-
@@ -2062,7 +2074,12 @@ See also C<PL_sv_yes> and C<PL_sv_no>.
== (SVt_PVLV|SVf_FAKE))
-#define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
+#ifdef PERL_NEW_COPY_ON_WRITE
+# define SvGROW(sv,len) \
+ (SvIsCOW(sv) || SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
+#else
+# define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
+#endif
#define SvGROW_mutable(sv,len) \
(SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX_mutable(sv))
#define Sv_Grow sv_grow