Scott/Muppet

Thanks for the response ;-)

muppet wrote:


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

<snip>

It does something like that. I just made up that example as I didn't think I could describe it in English ;-)


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.

You are right, that was not a very good way of going about it. I felt that from the start, but this code is ISAM database code, and the variable in question is the block pointer. This pointer gets updated every time an ISAM operation is done - which is at least once for each record scanned/loaded. I wanted to avoid copying variables back and forth on each ISAM operation, for more efficiency.

I don't know enough to do it, but I did make it work with a scalar reference. Unfortunately I had to do what I set out not too, but at this point - I'm just glad it seems to work ;-) This can be complicated.

Thanks,
Tim


Reply via email to