in reply to Re^4: XS efficient copy (SvCUR_set)
in thread XS efficient copy
Thanks for the corrections. My memory was rusty and I didn't double check it. Mea culpa.
Having double checked now, I'd still do things differently than you did but I realized more of the reasons for my reluctance to using SvPVX() and SvPOK_on() and those had to do with dealing with scalars passed in rather than one freshly created right there. So I now agree that your suggestion is safe (for this particular case) (and mine was fatally flawed, of course).
I'd personally still avoid using SvPVX() and SvPOK_on() as sticking with techniques that work in both cases makes sense to me. But, I see I used SvPV_force() and getting the "use of undefined value" warning was actually a desired feature. If I was creating a new scalar, then I'd sv_setpvn(sv,"",0) before doing SvPV_force() and thus not get the warning when it wasn't appropriate (in part because these two steps would likely be in separate macros "of my own design"). But that has a trivial amount of extra overhead that some might dislike. I'd also use SvPOK_only() but do that and the SvCUR_set() only after the call to fill the allocated buffer had succeeded.
So I guess I'd do something like this in 3 steps: 1) Allocate a scalar containing an empty string (preferably with the desired size of buffer), 2) Prepare the scalar to have a large enough buffer and extract a pointer to it (using a macro that doesn't assume a pristine SV), 3) (after the buffer has been "filled") Mark the scalar as containing the string of the proper length. I find that layer of abstraction makes the XS subroutine easier to understand and each of my three macros easier to understand in isolation. In fact, I spent some concerted study on each of my macros, carefully verifying implementation details of the "sv" macros that they use to make sure my macros were correct and safe. And then I hapilly forgot most of the niggling details of all of those "sv" macros which I'd never faithfully remember anyway.
Thanks again and sorry for the misinformation.
- tye
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: XS efficient copy (SvCUR_set)
by creamygoodness (Curate) on Apr 07, 2006 at 18:36 UTC |