using System; using System.Collections.Generic; namespace Unity.VisualScripting { public static class ManualPool where T : class { private static readonly object @lock = new object(); private static readonly Stack free = new Stack(); private static readonly HashSet busy = new HashSet(); public static T New(Func constructor) { lock (@lock) { if (free.Count == 0) { free.Push(constructor()); } var item = free.Pop(); busy.Add(item); return item; } } public static void Free(T item) { lock (@lock) { if (!busy.Contains(item)) { throw new ArgumentException("The item to free is not in use by the pool.", nameof(item)); } busy.Remove(item); free.Push(item); } } } }