Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^2: Mixing C and perl

by jpl (Monk)
on Jan 17, 2012 at 18:51 UTC ( #948379=note: print w/replies, xml ) Need Help??

in reply to Re: Mixing C and perl
in thread Mixing C and perl

I want to be able to get directly to the XS methods using
If $amd is just a blessed hash reference, I think I'd have to "indirect through" the perl wrapper:
# In sub method { my $self = shift; my $Cptr = $self->{__c_ptr}; return $Cptr->xs_method(); }
Also not too onerous. But will h2xs do all the right stuff to do the dynamic linking? I confess to considerable uncertainty about how the linking is made to work. I have tested the original model, and it appears to be doing the right thing with linking and reference counts.

Replies are listed 'Best First'.
Re^3: Mixing C and perl
by tsee (Curate) on Jan 17, 2012 at 19:06 UTC

    In the Perl-hash-wraps-C-struct setup, an object looks like {foo => 'bar', ... '__cptr' => \2131231231}. Well, with- or without the scalar reference to the number-that-is-a-pointer

    What you can do to make the XS methods work appropriately on this construct while being able to store stuff regularly from Perl as well, is to define a typemap that will a) check it got a hashref b) check that the class inherits from your package, c) fetch the __cptr slot of your hash-object, d) check it's a scalar ref, e) use the integer as a pointer, f) cast that into your C type. b), d), e), f) are done in T_PTROBJ already. This would allow you to write XS like with T_PTROBJ. The only downside is that you'd lose the ability to access the outer hash from XS.

      The only downside is that you'd lose the ability to access the outer hash from XSM

      Not really, just keep a pointer to the wrapping hash also inside the C structure, as in the OP sample code.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://948379]
[Eily]: what, not a Bishop t-shirt ?
[holli]: a pope t-shirt? are the words "fuck the" printed on it?
[Eily]: ...
[Eily]: I just realized that there's an onion in your eye
[marto]: bishop t-shirt?
LanX zooming holli s picture seeking onion
[holli]: Oh, seems like LanX' team has won the fourth World Cup ... awesome!
LanX grins
[marto]: anyway, don't come to Glasgow for the football :)
[holli]: the hell? i have a scriptblocker on.

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2017-12-14 16:10 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (398 votes). Check out past polls.