mallocForeignPtr vs. C

Simon Marlow marlowsd at gmail.com
Tue Jul 13 05:34:15 EDT 2010


On 13/07/2010 05:49, Evan Laforge wrote:
> On Mon, Jul 12, 2010 at 6:54 PM, John Meacham<john at repetae.net>  wrote:
>> Hi, is a StablePtr what you are after?
>
> Indeed, it looks like StablePtr will get me what I want.  It's a
> little less convenient than FunPtr because I'm already doing some
> finalization of FunPtrs and I can reuse the same callback, but it
> looks like it's specifically documented to do what I want, which is
> reassuring.
>
> In any case, memcpy still wins out on simplicity.  If I ever need to
> pass a pointer larger than 32mb and have both haskell and C manage the
> memory, I'll look into StablePtr.

FYI, when you call a foreign import "wrapper" to make a FunPtr, a 
StablePtr gets created behind the scenes to point to the closure 
representing the function in Haskell, and freeHaskellFunctionPtr calls 
freeStablePtr on that StablePtr.

Rougly speaking, ForeignPtrs are pointers from Haskell to C, and 
StablePtrs are pointers from C to Haskell.  However, we realised that we 
could optimise the case of a ForeignPtr allocated in Haskell by using 
heap-allocated memory and replacing the finalizer with the GC, which is 
what mallocForeignPtr does.

Cheers,
	Simon


More information about the Glasgow-haskell-users mailing list