develooper Front page | perl.perl5.porters | Postings from April 2014

[perl #116925] document/publicize THINKFIRST

Thread Previous
From:
bulk88 via RT
Date:
April 16, 2014 09:47
Subject:
[perl #116925] document/publicize THINKFIRST
Message ID:
rt-4.0.18-22099-1397641631-1255.116925-15-0@perl.org
On Tue Mar 25 22:19:43 2014, tonyc wrote:
> On Tue Feb 04 20:24:36 2014, tonyc wrote:
> >  - L<perlguts/Working with SVs> should probably discuss how to
> >    work with the SV's buffer, including SvPV_force(),
> >    SvPVbyte_force(), SvPVutf8_force(), SvGROW(). I'm not sure if
> >    this belongs after the SvGROW discussion, or after the
> >    sv_cat*() discussion.
> 
> Attached a potential patch.
> 
> Tony
 
+If you want to write to an existing SV's buffer and set its value to a
+string, use SvPV_force() or one of its variants to force the SV to be
+a PV.  This will remove any of various types of non-stringness from
+the SV, including copy-on-write and being a reference.  This can be
+used to implement something like read():
+
+    (void)SvPVbyte_force(sv, len);
+    s = SvGROW(sv, offset + needlen + 1);

WHy the "(void)SvPVbyte_force(sv, len);" line?  Won't SvGROW drop the COW and RVs?


+If you don't need the existing content of the SV, you can avoid some
+copying with:
+
+    if (SvTHINKFIRST(sv))
+        sv_force_normal_flags(sv, SV_COW_DROP_PV);
+    SvUPGRADE(sv, SVt_PV);
+    s = SvGROW(sv, needlen + 1);

Maybe the API needs to be reworks. WOuldn't sv_force_normal_flags create a SVPV without a buffer already? So why the SvUPGRADE? I Kno the SvUPGRADE is protection so the SvGROW doesn't segv because its an SVIV with no body, only a head. Instread of SvUPGRADE(sv, SVt_PV); SvGROW(sv, needlen + 1); try this 


----------------------
if(SvTYPE(sv) >= SVt_PV) {
    if( SvIsCOW(sv) ) {
        needgrow:
        buffer = sv_grow(sv,len);
    } else if (SvLEN(sv) < (len) {
        goto needgrow;
    } else {
        buffer = SvPVX(sv);
    }
} else {
    goto needgrow;
}
-----------------------


+If you want to write to an existing SV's buffer and set its value to a
+string, use SvPV_force() or one of its variants to force the SV to be
+a PV.  This will remove any of various types of non-stringness from
+the SV, including copy-on-write and being a reference.  This can be
+used to implement something like read():


Is this for read buffer, then you can then write to it? Where the buffer will be read, and conditionally written to in the same C library call? If so it needs to explain better that this produces the Perl level contents for you to read and write to. And this does not supply uninit memory, but only initiated memory. Use "If you want to read and write to an string" . also mention " If you are ONLY WRITTING, see the next paragraph for how to do it faster."

-- 
bulk88 ~ bulk88 at hotmail.com

---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=116925

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About