garbage-collectiond

Is it possible, in D, to tell the garbage collector to not scan a particular pointer (or anything below it)?


I have a very large tree structure that 1) does not often have nodes removed during the duration of the program, and 2) gets its nodes from a custom allocator that has its own reference to the block of memory the nodes are broken off from.

Is there any way to prevent the GC from scanning the tree? Ideally, I'd want something like core.memory.GC.removeRoot() that works on roots not previously added via core.memory.GC.addRoot(), but I'd be fine with anything that gives me that effect (perhaps a way to turn off the GC for a particular module (except if explicitly called via GC.collect())?).


Solution

  • When you allocate the memory, if you do it yourself with GC.malloc instead of normal new operator, you can pass a NO_SCAN flag to it, then emplace your structure on top. Use extreme caution with this though since if it isn't scanned things it points to are liable to disappear on you and lead to pain.