public void Collect(List <uint> roots) { // first, unset every allocation's Mark flag if it isn't pinned, otherwise treat as root and scan if pinned foreach (var alloc in _heap) { alloc.Mark = alloc.Pin; if (alloc.Pin) { Scan(alloc.Handle); } } // next, scan each of the given roots foreach (var ptr in roots) { Scan(ptr); } // at this point, any allocation we failed to mark is unused and may be collected foreach (var alloc in _heap) { if (!alloc.Mark) { _usedMem -= alloc.Memory.Memory.Length; _allocator.Free(alloc.Memory); alloc.Memory = new MemorySpan(null); alloc.Type = null; } } }
public void Release() { if (--_useCount == 0) { _memoryAllocator.Free(this); } Debug.Assert(_useCount >= 0); }