Understanding GC memory ranges and roots

Steven Schveighoffer schveiguy at gmail.com
Tue Jul 7 11:50:59 UTC 2020


On 7/6/20 7:03 PM, Per Nordlöw wrote:
> I'm experimenting with an alternative GC implementation at
> 
> https://github.com/nordlow/phobos-next/blob/master/benchmarks/gc-benchmark/source/segregated_gc.d 
> 
> 
> I now wonder if the parameters passed by `GC.addRange` and `GC.addRoot` 
> are the only memory entry blocks I need to implement to correctly scan 
> the stack and static memory storage?
> 
> I doesn't seem like that because when I add prints to these functions 
> the only call I get is
> 
> addRange(0x556d8d118ea0, 93200, nil)
> 
> Is this the stack or the static storage?
> I'm guessing static storage because printing the address of the first 
> variable in a function prints 0x7ffe094fa014 which is very different 
> from 0x556d8d118ea0.
> 

addRoot and addRange are called when allocations happen outside the 
stack or static storage. This might be the global (non-TLS) storage? It 
could also be something else that the runtime uses (could be on the C heap).

> How do I catch both the stack and static storage?

The stack and Thread Local Storage (i.e. static storage) are scanned 
using a specialized interface. See here: 
https://github.com/dlang/druntime/blob/eb279cf69c2e56e38b865409226f22e1d2465e72/src/core/thread/threadbase.d#L1024-L1042

> And why am I not getting any calls to GC.addRoot()? When is GC.addRoot() 
> supposed to be called and by who?
> 

If you are adding prints, you may as well throw an exception, catch it, 
and print the stack trace if you want to know who's calling what.

-Steve


More information about the Digitalmars-d mailing list