What I'm seeking is the lowest cost way to provide direct access to memory (mapped to a file) from Perl code.

Right off the bat, that eliminates magic. Let's compare

substr($s, $pos, $len) = $x;
to
substr($s, $pos, $len, $x);

This is what occurs in the assignment version:

  1. A var is created, upgraded and made magical.
  2. The value of $x is copied into the PV of the magical var
  3. The magic handler of the magical var is called.
  4. It copies the the value of the PV into $s (resizing if necessary).
  5. The magical var is freed.

In contrast, the following occurs for the 4-arg version:

  1. The value of $x is copied into $s (resizing if necessary).

None of the extra steps in the first version are particularly cheap either.

But let's say you're ok with the overhead.

it falls foul of the substr limitations you helped identify earlier in the week which means that you cannot use offsets ≥2GB.

Then substr magic is definitely not acceptable, since suffers from the same problem.

int Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg) { STRLEN len; SV * const lsv = LvTARG(sv); const char * const tmps = SvPV_const(lsv,len); I32 offs = LvTARGOFF(sv); I32 rem = LvTARGLEN(sv); ... } int Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg) { dVAR; STRLEN len; const char * const tmps = SvPV_const(sv, len); SV * const lsv = LvTARG(sv); I32 lvoff = LvTARGOFF(sv); I32 lvlen = LvTARGLEN(sv); ... }

Actually, the more I think about this, I think you may have been half right back there when you talked about lvalue subs

I looked in perlxs quickly, and I didn't see anything about this. Maybe adding one of the following to the associated .pm will work:

sub xs_func1 :lvalue; sub xs_func2 :lvalue;

or

use attributes (); for (qw( xs_func1 xs_func2 )) { no strict 'refs'; attributes->import(__PACKAGE__, \&{$_}, 'lvalue'); }

And in any case, incrementing bytes isn't particularly useful

Sounds like you want your function to return numbers.

my $mmap = $class->new( ..., endianess => ENDIAN_NATIVE ); ++( $mmap->uint8(0x1234) );

In reply to Re: XS: SvPVLV examples? by ikegami
in thread XS: SvPVLV examples? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.