Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^4: Dynaloader/XS: sharing C symbols across shared objects

by creamygoodness (Curate)
on Jun 11, 2008 at 00:09 UTC ( [id://691361]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Dynaloader/XS: sharing C symbols across shared objects
in thread Dynaloader/XS: sharing C symbols across shared objects

Pointers to functions in C ? ... then wrapped in an SV ?

C function pointers are directly analogous to perl subroutine references. The big differences are...

  • They're typed, so function signatures must match.
  • The syntax for declaring them is bloody awful. In fact, it's so awful that many people use typedefs whenever possible.
  • You can't verify that a non-NULL function pointer actually points to anything valid, unlike in Perl where you can verify a sub ref with ref($sub).
Here's a demo for passing a function pointer around via SV.
use strict; use warnings; use Inline C => <<'END_C'; typedef void (*hello_func_t)(); void hola_mundo() { printf("Hola, mundo!\n"); } SV* get_hola_mundo_func_ptr() { return newSViv( PTR2IV(hola_mundo) ); } void do_hello(SV *sv_with_func_ptr) { IV temp = SvIV(sv_with_func_ptr); hello_func_t hello = INT2PTR(hello_func_t, temp); hello(); } END_C my $func_ptr = get_hola_mundo_func_ptr(); do_hello($func_ptr);
--
Marvin Humphrey
Rectangular Research ― http://www.rectangular.com

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-24 12:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found