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

2012-02-13 Thread Donn Cave
Quoth =?ISO-8859-1?Q?Yves_Par=E8s?= yves.pa...@gmail.com, You mean I have to use a type like StablePtr (IORef Stuff)? Because there I can only peek (deRefStablePtr) the pointer, not poke it. I take it I should return to C a StablePtr to the new value if I don't want to use IORefs... Yes ...

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

2012-02-12 Thread Yves Parès
Hello, According to the documentation ( http://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Foreign-StablePtr.html), StablePtrs aims at being opaque on C-side. But they provide functions to be casted to/from regular *void**'s. Does that mean if for instance you have a StablePtr CInt

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

2012-02-12 Thread Antoine Latter
On Sun, Feb 12, 2012 at 8:18 AM, Yves Parès yves.pa...@gmail.com wrote: Hello, According to the documentation (http://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Foreign-StablePtr.html), StablePtrs aims at being opaque on C-side. But they provide functions to be casted to/from

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

2012-02-12 Thread Albert Y. C. Lai
On 12-02-12 09:18 AM, Yves Parès wrote: According to the documentation (http://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Foreign-StablePtr.html), StablePtrs aims at being opaque on C-side. The doc multiply warns again and again that StablePtr, as well as whatever Ptr you get

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

2012-02-12 Thread Yves Parès
Yes, but from C side, a StablePtr* is **already a **void**. (See HsFFI.h which typedefs HsStablePtr to void*) So its sufficient for a use as an opaque pointer, no need to cast it. So what is the use of casting it to a Ptr () if this doesn't allow to access the memory space addressed? 2012/2/12

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

2012-02-12 Thread Yves Parès
Thanks for your explanation Albert, it makes things clearer. So StablePtrs are just useful so that C code can: 1) call back into Haskell (through a foreign exported function like doSomethingWithTheObjectIGaveYou :: StablePtr MyObjectType - Stuff - IO ()) 2) store them to return them later to

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

2012-02-12 Thread Antoine Latter
On Sun, Feb 12, 2012 at 3:09 PM, Yves Parès yves.pa...@gmail.com wrote: But then, In use case 1), how can a Haskell function modify the data addressed? http://hackage.haskell.org/packages/archive/base/latest/doc/html/Foreign-StablePtr.html#v:deRefStablePtr Antoine

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

2012-02-12 Thread Donn Cave
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

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

2012-02-12 Thread John Meacham
No, you can do nothing with the pointer on the C side other than pass it back into haskell. It may not even be a pointer, it may be an index into an array deep within the RTS for instance. The reason they can be cast to void *'s is so you can store them in C data structures that don't know about

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

2012-02-12 Thread Yves Parès
You mean I have to use a type like StablePtr (IORef Stuff)? Because there I can only peek (deRefStablePtr) the pointer, not poke it. I take it I should return to C a StablePtr to the new value if I don't want to use IORefs... Or else I have to use regular Ptrs with Foreign.Marshall.Alloc.malloc