RFC: Pinning interface for the GC

Alex Rønne Petersen alex at lycus.org
Sat Oct 13 11:58:24 PDT 2012


Hi,

With precise garbage collection coming up, and most likely compacting 
garbage collection in the future, I think it's time we start thinking 
about an API to pin garbage collector-managed objects.

A typical approach that people use to 'pin' objects today is to allocate 
a chunk of memory from the C heap, add it as a root [range], and store a 
reference in it. That, or just global variables.

This is kind of terrible because adding the chunk of memory as a root 
forces the GC to actually scan it, which is unnecessary when what you 
really want is to pin the object in place and tell the GC "I know what 
I'm doing, don't touch this".

I propose the following functions in core.memory.GC:

     static bool pin(const(void)* p) nothrow;
     static bool unpin(const(void)* p) nothrow;

The pin function shall pin the object pointed to by p in place such that 
it is not allowed to be moved nor collected until unpinned. The function 
shall return true if the object was successfully pinned or false if the 
object was already pinned or didn't belong to the garbage collector in 
the first place.

The unpin function shall unpin the object pointed to by p such that it 
is once again eligible for moving and collection as usual. The function 
shall return true if the object was successfully unpinned or false if 
the object was not pinned or didn't belong to the garbage collector in 
the first place.

Destroy!

-- 
Alex Rønne Petersen
alex at lycus.org
http://lycus.org


More information about the Digitalmars-d mailing list