[Haskell-cafe] Sometimes pinned memory?

Duncan Coutts duncan.coutts at googlemail.com
Fri Nov 13 05:26:05 EST 2009

On Wed, 2009-11-11 at 13:14 -0800, Gregory Crosswhite wrote:
> Hey everyone!  Do you have any suggestions for how I might allocate an  
> aligned block of memory that I can pin while making foreign calls, but  
> leave unpinned the rest of the time to potentially improve allocation  
> and garbage collector performance?  Or is this even a good idea?

GHC's memory management does not have any support for temporarily
pinning a heap object. Heap objects must start out pinned or unpinned
and that cannot be changed later (the current impl uses separate
sections of the heap for pinned vs unpinned).

As Don says, you can copy from an unpinned to a fresh pinned ByteArray#.
If you're prepared to rely on internal properties of GHC's memory
manager, then you can use the fact that currently ByteArray#s over a
certain size are always pinned. This means that for small unpinned
arrays you can just copy when it comes to a foreign call, and for larger
ones you can rely on them being pinned. As I said though, this is not a
portable technique.


More information about the Haskell-Cafe mailing list