using System; using System.Reflection; namespace Glass.Testbed { /// /// Reads how many bytes the calling thread has allocated, for the frame readout. /// /// /// This project targets netstandard2.0, which predates GC.GetAllocatedBytesForCurrentThread, so it's bound by /// reflection once at startup instead (see UnitTestSharp.AllocationCounter for the same trick). /// public static class AllocationCounter { private static readonly Func source = BindToRuntime(); private static Func BindToRuntime() { MethodInfo counter = typeof(GC).GetMethod("GetAllocatedBytesForCurrentThread", BindingFlags.Public | BindingFlags.Static, null, Type.EmptyTypes, null); return counter == null ? null : (Func)counter.CreateDelegate(typeof(Func)); } /// /// Bytes the calling thread has allocated since it started, or zero on a runtime that won't say. /// public static long BytesAllocatedOnThisThread() { return source?.Invoke() ?? 0; } } }