Changes between Initial Version and Version 1 of Commentary/Rts/Storage/GC/Pinned


Ignore:
Timestamp:
Dec 9, 2009 4:20:49 PM (4 years ago)
Author:
simonmar
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Commentary/Rts/Storage/GC/Pinned

    v1 v1  
     1 
     2 
     3= Pinned Objects = 
     4 
     5The GC does not support pinning arbitrary objects.  Only objects that have no pointer fields can be pinned.  Nevertheless, this is a useful case, because we often want to allocate garbage-collectable memory that can be passed to foreign functions via the FFI, and we want to be able to run the GC while the foreign function is still executing (for a `safe` foreign call).  Hence, the memory we allocated must not move. 
     6 
     7Bytestrings are currently allocated as pinned memory, so that the bytestring contents can be passed to FFI calls if necessary. 
     8 
     9The RTS provides an API for allocating pinned memory, in [[GhcFile(includes/rts/storage/GC.h)]]: 
     10 
     11{{{ 
     12StgPtr  allocatePinned  ( Capability *cap, lnat n ); 
     13}}} 
     14 
     15This allocates memory from the given Capability's nursery. 
     16 
     17Pinned objects work in the GC as follows: 
     18 
     19 * Pinned objects are allocated into a block of their own, not mixed up with unpinned objects. 
     20 * The block containing pinned objects is marked as a ''large block'', i.e. the `BF_LARGE` bit is set in `bd->flags`. 
     21 * When encountering a live object in a `BF_LARGE` block, the GC never copies the object, instead it just re-links the whole block onto the `large_objects` list of the destination generation. 
     22 * The GC doesn't have to scavenge the pinned object, since it does not contain any pointers.  This is just as well, because we cannot scan blocks for live pinned objects, due to [wiki:Commentary/Rts/Storage/Slop slop].  Hence the restriction that pinned objects do not contain pointers. 
     23 
     24This means that using pinned objects may lead to memory fragmentation, since a single pinned object keeps alive the whole block in which it resides.  If we were to implement a non-moving collector such as [wiki:Commentary/Rts/Storage/GC/Sweeping mark-region], then we would be able to reduce the impact of fragmentation due to pinned objects.