Front page | perl.perl5.porters |
Postings from June 2013
[perl #116407] SvPVutf8 != SvPVX, and sv_2pvutf8
Thread Next
From:
Father Chrysostomos via RT
Date:
June 9, 2013 18:22
Subject:
[perl #116407] SvPVutf8 != SvPVX, and sv_2pvutf8
Message ID:
rt-3.6.HEAD-2552-1370802114-1190.116407-15-0@perl.org
On Wed May 22 11:36:42 2013, davem wrote:
> On Fri, May 17, 2013 at 03:14:16PM +0100, Dave Mitchell wrote:
> > As a shortcut, if you've already done any get magic processing
> that was
> > required, and if the SV is SvPOK, then its okay to use SvCUR and
> to
> > directly read from and write to the SvPVX buffer, and to use
> SvGROW()
> > if you want to write beyond the end of the string. You'll still
> need
> > to call SvSETMAGIC afterwards.
>
> I don't think that's true. COW vars have SvPOK set, but you still
> shouldn't directly write to them. This has always been true, since it
> used
> to apply to shared-hash keys as well: although we've changed in it in
> 5.18
> so that the RO flag is no longer set:
>
> $ perl5163 -MDevel::Peek -e '%h=qw(a 1); Dump $_ for keys %h'
> SV = PV(0xd0c140) at 0xd0dfe0
> REFCNT = 2
> FLAGS = (POK,FAKE,READONLY,pPOK)
> PV = 0xd244d0 "a"
> CUR = 1
> LEN = 0
> $ perl5180 -MDevel::Peek -e '%h=qw(a 1); Dump $_ for keys %h'
> SV = PV(0x19a9e30) at 0x19a91c0
> REFCNT = 2
> FLAGS = (POK,IsCOW,pPOK)
> PV = 0x19c0250 "a"
> CUR = 1
> LEN = 0
>
> So, I *suspect* that the correct answer is that if POK is set, you
> still
> need to test SvTHINKFIRST() (rather than SvREADONLY()) ,then call
> sv_force_normal() or similar (or use SV_CHECK_THINKFIRST or
> SV_CHECK_THINKFIRST_COW_DROP), although frankly this is where I start
> to
> get confused and become unclear exactly what we should be recommending
> to
> XS writers.
Currently sv_force_normal is already part of the API, but anything
involving THINKFIRST is not.
Since SvPV_force will itself call sv_force_normal, we can simply
recommend SvPV_force if the existing string needs to be modified.
If the XS author wants to use the existing buffer (for speed) but drop
if it would have to be copied, then sv_force_normal_flags(sv,
SV_COW_DROP_PV) followed by sv_grow is probably the right thing to
recommend.
Now, do we want to add THINKFIRST to the API? I really don’t know. But
I’ve demonstrated that we can provide the ‘right way’ to do things
without changing the current API at all.
--
Father Chrysostomos
---
via perlbug: queue: perl5 status: resolved
https://rt.perl.org:443/rt3/Ticket/Display.html?id=116407
Thread Next
-
[perl #116407] SvPVutf8 != SvPVX, and sv_2pvutf8
by Father Chrysostomos via RT