// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; using System.Diagnostics; using System.Text; using Microsoft.CodeAnalysis.PooledObjects; namespace Microsoft.CodeAnalysis { internal static class SharedPoolExtensions { private const int Threshold = 512; public static PooledObject GetPooledObject(this ObjectPool pool) { return PooledObject.Create(pool); } public static PooledObject GetPooledObject(this ObjectPool pool) { return PooledObject.Create(pool); } public static PooledObject> GetPooledObject(this ObjectPool> pool) { return PooledObject>.Create(pool); } public static PooledObject> GetPooledObject(this ObjectPool> pool) { return PooledObject>.Create(pool); } public static PooledObject> GetPooledObject(this ObjectPool> pool) { return PooledObject>.Create(pool); } public static PooledObject> GetPooledObject(this ObjectPool> pool) { return PooledObject>.Create(pool); } public static PooledObject> GetPooledObject(this ObjectPool> pool) { return PooledObject>.Create(pool); } public static PooledObject> GetPooledObject(this ObjectPool> pool, out List list) { var pooledObject = PooledObject>.Create(pool); list = pooledObject.Object; return pooledObject; } public static PooledObject GetPooledObject(this ObjectPool pool) where T : class { return new PooledObject(pool, p => p.Allocate(), (p, o) => p.Free(o)); } public static StringBuilder AllocateAndClear(this ObjectPool pool) { var sb = pool.Allocate(); sb.Clear(); return sb; } public static Stopwatch AllocateAndClear(this ObjectPool pool) { var watch = pool.Allocate(); watch.Reset(); return watch; } public static Stack AllocateAndClear(this ObjectPool> pool) { var set = pool.Allocate(); set.Clear(); return set; } public static Queue AllocateAndClear(this ObjectPool> pool) { var set = pool.Allocate(); set.Clear(); return set; } public static HashSet AllocateAndClear(this ObjectPool> pool) { var set = pool.Allocate(); set.Clear(); return set; } public static Dictionary AllocateAndClear(this ObjectPool> pool) { var map = pool.Allocate(); map.Clear(); return map; } public static List AllocateAndClear(this ObjectPool> pool) { var list = pool.Allocate(); list.Clear(); return list; } public static void ClearAndFree(this ObjectPool pool, StringBuilder sb) { if (sb == null) { return; } sb.Clear(); if (sb.Capacity > Threshold) { sb.Capacity = Threshold; } pool.Free(sb); } public static void ClearAndFree(this ObjectPool pool, Stopwatch watch) { if (watch == null) { return; } watch.Reset(); pool.Free(watch); } public static void ClearAndFree(this ObjectPool> pool, HashSet set) { if (set == null) { return; } var count = set.Count; set.Clear(); if (count > Threshold) { set.TrimExcess(); } pool.Free(set); } public static void ClearAndFree(this ObjectPool> pool, Stack set) { if (set == null) { return; } var count = set.Count; set.Clear(); if (count > Threshold) { set.TrimExcess(); } pool.Free(set); } public static void ClearAndFree(this ObjectPool> pool, Queue set) { if (set == null) { return; } var count = set.Count; set.Clear(); if (count > Threshold) { set.TrimExcess(); } pool.Free(set); } public static void ClearAndFree(this ObjectPool> pool, Dictionary map) { if (map == null) { return; } // if map grew too big, don't put it back to pool if (map.Count > Threshold) { pool.ForgetTrackedObject(map); return; } map.Clear(); pool.Free(map); } public static void ClearAndFree(this ObjectPool> pool, List list) { if (list == null) { return; } list.Clear(); if (list.Capacity > Threshold) { list.Capacity = Threshold; } pool.Free(list); } } }