mallocForeignPtr vs. C
Axel Simon
Axel.Simon at in.tum.de
Tue Jul 13 16:03:01 EDT 2010
Hi Evan, Ed,
On Jul 12, 2010, at 22:53, Edward Z. Yang wrote:
> Excerpts from Evan Laforge's message of Mon Jul 12 16:43:45 -0400
> 2010:
>> Yeah, that's definitely the safest and simplest. But the copying
>> defeats the purpose of passing a pointer in the first place, which
>> was
>> to not have to copy the giant array just to pass it.
>
> Well, if your C code wasn't squirreling away the pointer, you could
> have
> avoided the copy. Memory copies in C are suprisingly cheap; I'm
> really
> surprised how often I see memcpy() in high performance code (though,
> it may be
> the case that the details of the algorithm are a much more important
> influence
> on performance.)
If your C code has a way to properly unref a pointer then you could
wrap your ForeignPtr in a StablePtr and pass that to C land. Once C
has freed the StablePtr the ForeignPtr can become dead when Haskell
has dropped all references and it will be garbage collected.
The downside is that your C code needs to hold the StablePtr and do
the accesses through the Ptr in the ForeignPtr, so it needs to be able
to store two pointers at once.
Axel
More information about the Glasgow-haskell-users
mailing list