in reply to Re^8: XS: SvPVLV examples?
in thread XS: SvPVLV examples?
use strict; use warnings; use Inline C => <<'__EOI__'; SV* f(SV* sv, I32 pos, I32 len) { SV* targ = newSV_type(SVt_PVLV); sv_magic(targ, NULL, PERL_MAGIC_substr, NULL, 0); LvTYPE(targ) = 'x'; LvTARG(targ) = SvREFCNT_inc_simple(sv); LvTARGOFF(targ) = pos; LvTARGLEN(targ) = len; return targ; } __EOI__ use Devel::Peek qw( Dump ); my $x = "abcde"; $_=uc for f($x,1,3); print("$x\n"); # aBCDe
If not, just copy the two functions relating to substr in mg.c
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^10: XS: SvPVLV examples?
by BrowserUk (Patriarch) on Sep 25, 2009 at 04:20 UTC | |
Yep! Bin there, done that. Trouble is, the lvalueness doesn't survive assignment:
The effect I am after is persistant lvalueness as demonstrated here (and back up there somewhere):
But I'd like to avoid the need for taking a reference and using indirection. And to keep this all in one thread: No to "Are you really asking about making an lvalue function?" Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
by ikegami (Patriarch) on Sep 25, 2009 at 04:37 UTC | |
Magic isn't copied on assignment, not even for substr magic. You did $r = func for my function, but $r = \func for substr. This difference in the code accounts for the difference in the results. If I take your "persistant lvalueness" example and simply change substr to f, I get the same output as before the change. Feel free to return a reference to targ instead of targ itself if that's what you want. | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Sep 25, 2009 at 08:19 UTC | |
Actually, the more I think about this, I think you may have been half right back there when you talked about lvalue subs. What I want is both. An lvalue sub that returns a PVLV that I can take a reference to if I need to. What I'm seeking is the lowest cost way to provide direct access to memory (mapped to a file) from Perl code. Starting from the beginning. When I map a view of a file, I get given an address. If I set that address into the PV of a scalar and return the scalar to perl, I can directly read from and write to an theoretical 128GB of disk as if it were a simple string in memory. Obviously, if you write sequentially to the whole address space, it will be little quicker than using normal IO because of swapping. But if you are randomly accessing sparsely, letting the OS & hardware take care of the loading the required pages on demand, and remembering to flush back any that you change, makes for very efficient access. So, to increment a 32-bit integer somewhere in that address space, you might do something like:
which works as is. But besides being rather unwieldy, it falls foul of the substr limitations you helped identify earlier in the week which means that you cannot use offsets >2GB. So, my goal is to write a sub (or series of subs) that address both the integer limitations of substr and the unwieldiness of the expression above. Further inspiration comes from some code I used a while ago that incremented substrings in place:
Which looks okay until you look closely. And gets positively bizarre (though completely logical!):
And in any case, incrementing bytes isn't particularly useful. However, looking into this PVLV business led me to look at and think about vec in new ways:
The requirement for persistent lvalueness arises because whilst a sub that takes an offset and size is (reasonably) convenient when dealing with arrays of data or when the offsets are otherwise programmically generated:
For regularly accessed fields at fixed offsets, it would be far more convenient to be able to use named variables:
But I guess that dereference syntax wouldn't be so inconvenient:
Anyway, that's where I think I'm headed. And I think your snippet will be usable with the addition of the lvalue attribute on the sub:
and some custom set/get magic methods which will essentially be a combination of simplified versions of the existing substr and vec ones. To a) restrict sets to like for like rather than allowing the lvalue to be shrunk or grown through the assignments; b) to only address multiples of bytes/words/dwords/quads, signed and unsigned. And maybe reals; c) do native endianess. Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
by ikegami (Patriarch) on Sep 25, 2009 at 14:42 UTC | |