On Jul 6, 2004, at 5:55 PM, Tim Pushor wrote:

First of all, I am completely overwhelmed with XS, so this may be a dumb question ;-)

Good or bad, I want to be able to share a double with C and Perl. The complicated part is that I need to be able to take the address of the double in C (as a global) and use it through various functions. i.e.

your example code stores a pointer to the client code's double -- does the real code do that? this will be problematic for dealing with more than one value. from your desire to do stuff like


  setval ($a);
  process (5);

i would presume you also want to be able to do

  setval ($a);
  process (5);
  setval ($b);
  process (7);

and have both $a and $b be separate values, holding the results of process on each.

it is indeed possible to get to the double stored inside an SV, but since perl really likes to be able to copy other wise temporary scalars, you'll have a tough time implementing that in a way that doesn't segfault. (i tried taking the address of the scalar's internal nv after SvNOK_only, but the next time you do print "$a" it gets its POK flag set, and the NV address is no longer valid, resulting in a segfault later in the program. in other words, it doesn't want to work.)

any other solution would involve storing a static/global double in your XS module, and using xsubs as accessors which are used in a tie() to make the perl interface work right, and this will be clumsy at best, since the call to setvar() will alter the object and you may not have a way to remove its magical properties. the same goes if you try to add a magic virtual table to it, with the added question of where do you store the double without leaking it?

i humbly suggest that either redesigning the C interface or designing a new and sane perl interface around it would be a better approach. at the very least, having each operation in C code take a pointer to a structure containing execution context (the double and any other state information (think of a C++ object)) would give you a cleaner API with free reentrancy.


-- elysse (in labor): is the head the biggest part? midwife: yes. elysse: oh, good.



Reply via email to