Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^5: Passing integer pointer in XS? (simpler)

by tye (Sage)
on Jul 18, 2016 at 22:14 UTC ( #1168013=note: print w/replies, xml ) Need Help??


in reply to Re^4: Passing integer pointer in XS? (XS << Perl)
in thread Passing integer pointer in XS?

If the first 4 arguments are for specifying ways to output 2 strings, then you can get simpler than that!

int tdSensor( protocol, protocolLen, model, modelLen, id, dataTypes ) SV * protocol int protocolLen SV * model int modelLen int & id int & dataTypes CODE: { STRLEN n_a; (void) SvPV_force( protocol, n_a ); (void) SvPV_force( model, n_a ); SvGROW( protocol, protocolLen ); SvGROW( model, modelLen ); } RETVAL = tdSensor( SvPV(protocol), protocolLen, SvPV(model), modelLen, &id, &data +Types ); SvCUR_set( protocol, strlen(SvPV(protocol)) ); SvCUR_set( model, strlen(SvPV(model)) ); OUTPUT: RETVAL id dataTypes

But much simpler (and less likely to be buggy) than even that is:

int _tdSensor( protocol, protocolLen, model, modelLen, id, dataTypes ) char * protocol int protocolLen char * model int modelLen int & id int & dataTypes CODE: RETVAL = tdSensor( protocol, protocolLen, model, modelLen, &id, &d +ataTypes ); OUTPUT: RETVAL id dataTypes

Plus

sub tdSensor { my( $protocolLen, $modelLen ) = @_; $protcolLen ||= 1024; $modelLen ||= 1024; my $protocol = '\0' x $protcolLen; my $model = '\0' x $modelLen; my $id = 0; my $dataTypes = 0; my $ret = _tdSensor( $protocol, $protocolLen, $model, $modelLen, $id, $dataTypes ); s/\0.*// for $protocol, $model; return( $ret, $protocol, $model, $id, $dataTypes ); }

Which has the significant advantage of making it much easier to improve the interface without having to futz with the super finicky XS part of the code.

- tye        

Replies are listed 'Best First'.
Re^6: Passing integer pointer in XS? (simpler)
by ikegami (Pope) on Jul 19, 2016 at 07:02 UTC

    But much simpler (and less likely to be buggy) than even that is:

    Didn't I already demonstrate that it's quite buggy? (Ok, so you're relying on the fact that manipulating the internal buffer of the scalar works in this situation even though you're not allowed to do it. As far as I'm concerned, you can't call simple something that relies on this level of knowledge of internals.)

    If the first 4 arguments are for specifying ways to output 2 strings, then you can get simpler than that!

    Really? Cause I would need to perform some testing to verify that it's correct. All that magic is far far more complicated than just returning the values.

      Check PP(pp_sysread), it does pretty much the same. (Utf8 blah-blah is probably not relevant here.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1168013]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2020-10-29 07:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (269 votes). Check out past polls.

    Notices?