[Haskell-cafe] Stable pointers: use of cast to/from Ptr

Donn Cave donn at avvanta.com
Mon Feb 13 02:28:15 CET 2012

Quoth Yves Pares,
> If StablePtrs cannot have their pointed value modified (either C or
> Haskell-side), that mostly limits their interest, doesn't it?

I'm not sure I follow what's happening in the two cases you mention,
but in case it helps, I use StablePtr as a way to smuggle Haskell
values through a non-Haskell layer.  On the other side, they are
just things - not really pointers, so while void* is all right as
long as it's the right size, it is a little misleading.  "Tag" might
be a better word.

castStablePtrToPtr doesn't change that - you still get a tag, not
a memory address.  I can think of no use for it, unless you want
to call a function that happens to use a Ptr () without expecting
it to point somewhere in the memory address sense.

Since they're Haskell values, it would be perverse to modify them.
I just want them back, in a Haskell function dispatched by the
other layer.  If I want to pass data per se to the other side,
I have to marshal it to be readable CInts and such, and then pass
it as a Ptr.  And/or if the data is to be modified by the other
side, same deal, I have to un-marshal it back into Haskell.

I actually use them with a C++ layer, where of course the dispatch
happens through C++ object member functions.  That allows me to
map Haskell functions to the C++ API in a C++ derived class that
just maintains a table of the actual Haskell `member functions',
and a StablePtr for the actual Haskell data.


More information about the Haskell-Cafe mailing list