develooper Front page | perl.perl5.porters | Postings from October 2013

Storing a C function pointer in an SV

Thread Next
From:
wolfsage
Date:
October 22, 2013 11:10
Subject:
Storing a C function pointer in an SV
Message ID:
CAJ0K8bgxksmNDaj9KEQE5t7vn6pbG8rD431PQ8gPzWd83UKNmg@mail.gmail.com
Howdy folks,

Is there a recommended/safe/sane way to store a C function pointer in an SV?

(Attached is Example.tar.gz which contains a module with the full code
for example)

This works (sample from Example.xs):


  SV * store_func_in_sv(void *addr) {
          return newSViv( (IV)addr );
  }

  void call_func_from_sv(SV *sv) {
          void (*func)() = (void *)SvIV(sv);
          func();
  }

  void func() {
          warn("Called!\n");
  }

  MODULE = Example                PACKAGE = Example

  void
  test()
      CODE:
          SV *ptr = store_func_in_sv(func);
          call_func_from_sv(ptr);


But I'm not sure that's portable or safe.

Thanks,

-- Matthew Horsfall (alh)

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About