using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using System.Threading;
using Microsoft.AspNetCore.Shared;
using Microsoft.CodeAnalysis;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: InternalsVisibleTo("Microsoft.Extensions.ObjectPool.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: AssemblyMetadata("CommitHash", "954f61dd38b33caa2b736c73530bd5a294174437")]
[assembly: AssemblyMetadata("SourceCommitUrl", "https://github.com/dotnet/aspnetcore/tree/954f61dd38b33caa2b736c73530bd5a294174437")]
[assembly: AssemblyMetadata("Serviceable", "True")]
[assembly: AssemblyCompany("Microsoft Corporation")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("© Microsoft Corporation. All rights reserved.")]
[assembly: AssemblyDescription("A simple object pool implementation.")]
[assembly: AssemblyFileVersion("8.0.824.36908")]
[assembly: AssemblyInformationalVersion("8.0.8+954f61dd38b33caa2b736c73530bd5a294174437")]
[assembly: AssemblyProduct("Microsoft ASP.NET Core")]
[assembly: AssemblyTitle("Microsoft.Extensions.ObjectPool")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/dotnet/aspnetcore")]
[assembly: NeutralResourcesLanguage("en-US")]
[assembly: AssemblyVersion("8.0.0.0")]
[module: RefSafetyRules(11)]
[module: System.Runtime.CompilerServices.NullablePublicOnly(true)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
internal sealed class NullableAttribute : Attribute
{
public readonly byte[] NullableFlags;
public NullableAttribute(byte P_0)
{
NullableFlags = new byte[1] { P_0 };
}
public NullableAttribute(byte[] P_0)
{
NullableFlags = P_0;
}
}
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
internal sealed class NullableContextAttribute : Attribute
{
public readonly byte Flag;
public NullableContextAttribute(byte P_0)
{
Flag = P_0;
}
}
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class NullablePublicOnlyAttribute : Attribute
{
public readonly bool IncludesInternals;
public NullablePublicOnlyAttribute(bool P_0)
{
IncludesInternals = P_0;
}
}
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, Inherited = false)]
internal sealed class AllowNullAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, Inherited = false)]
internal sealed class DisallowNullAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue, Inherited = false)]
internal sealed class MaybeNullAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue, Inherited = false)]
internal sealed class NotNullAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
internal sealed class MaybeNullWhenAttribute : Attribute
{
public bool ReturnValue { get; }
public MaybeNullWhenAttribute(bool returnValue)
{
ReturnValue = returnValue;
}
}
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
internal sealed class NotNullWhenAttribute : Attribute
{
public bool ReturnValue { get; }
public NotNullWhenAttribute(bool returnValue)
{
ReturnValue = returnValue;
}
}
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
internal sealed class NotNullIfNotNullAttribute : Attribute
{
public string ParameterName { get; }
public NotNullIfNotNullAttribute(string parameterName)
{
ParameterName = parameterName;
}
}
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
internal sealed class DoesNotReturnAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
internal sealed class DoesNotReturnIfAttribute : Attribute
{
public bool ParameterValue { get; }
public DoesNotReturnIfAttribute(bool parameterValue)
{
ParameterValue = parameterValue;
}
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
internal sealed class MemberNotNullAttribute : Attribute
{
public string[] Members { get; }
public MemberNotNullAttribute(string member)
{
Members = new string[1] { member };
}
public MemberNotNullAttribute(params string[] members)
{
Members = members;
}
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
internal sealed class MemberNotNullWhenAttribute : Attribute
{
public bool ReturnValue { get; }
public string[] Members { get; }
public MemberNotNullWhenAttribute(bool returnValue, string member)
{
ReturnValue = returnValue;
Members = new string[1] { member };
}
public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
{
ReturnValue = returnValue;
Members = members;
}
}
}
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
internal sealed class CallerArgumentExpressionAttribute : Attribute
{
public string ParameterName { get; }
public CallerArgumentExpressionAttribute(string parameterName)
{
ParameterName = parameterName;
}
}
}
namespace Microsoft.AspNetCore.Shared
{
internal static class ArgumentNullThrowHelper
{
public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression("argument")] string? paramName = null)
{
if (argument == null)
{
Throw(paramName);
}
}
[DoesNotReturn]
internal static void Throw(string? paramName)
{
throw new ArgumentNullException(paramName);
}
}
}
namespace Microsoft.Extensions.ObjectPool
{
public class DefaultObjectPool<T> : ObjectPool<T> where T : class
{
private readonly Func<T> _createFunc;
private readonly Func<T, bool> _returnFunc;
private readonly int _maxCapacity;
private int _numItems;
private protected readonly ConcurrentQueue<T> _items = new ConcurrentQueue<T>();
private protected T? _fastItem;
public DefaultObjectPool(IPooledObjectPolicy<T> policy)
: this(policy, Environment.ProcessorCount * 2)
{
}
public DefaultObjectPool(IPooledObjectPolicy<T> policy, int maximumRetained)
{
_createFunc = policy.Create;
_returnFunc = policy.Return;
_maxCapacity = maximumRetained - 1;
}
public override T Get()
{
T result = _fastItem;
if (result == null || Interlocked.CompareExchange(ref _fastItem, null, result) != result)
{
if (_items.TryDequeue(out result))
{
Interlocked.Decrement(ref _numItems);
return result;
}
return _createFunc();
}
return result;
}
public override void Return(T obj)
{
ReturnCore(obj);
}
private protected bool ReturnCore(T obj)
{
if (!_returnFunc(obj))
{
return false;
}
if (_fastItem != null || Interlocked.CompareExchange(ref _fastItem, obj, null) != null)
{
if (Interlocked.Increment(ref _numItems) <= _maxCapacity)
{
_items.Enqueue(obj);
return true;
}
Interlocked.Decrement(ref _numItems);
return false;
}
return true;
}
}
public class DefaultObjectPoolProvider : ObjectPoolProvider
{
public int MaximumRetained { get; set; } = Environment.ProcessorCount * 2;
public override ObjectPool<T> Create<T>(IPooledObjectPolicy<T> policy)
{
ArgumentNullThrowHelper.ThrowIfNull(policy, "policy");
if (typeof(IDisposable).IsAssignableFrom(typeof(T)))
{
return new DisposableObjectPool<T>(policy, MaximumRetained);
}
return new DefaultObjectPool<T>(policy, MaximumRetained);
}
}
public class DefaultPooledObjectPolicy<T> : PooledObjectPolicy<T> where T : class, new()
{
public override T Create()
{
return new T();
}
public override bool Return(T obj)
{
if (obj is IResettable resettable)
{
return resettable.TryReset();
}
return true;
}
}
internal sealed class DisposableObjectPool<T> : DefaultObjectPool<T>, IDisposable where T : class
{
private volatile bool _isDisposed;
public DisposableObjectPool(IPooledObjectPolicy<T> policy)
: base(policy)
{
}
public DisposableObjectPool(IPooledObjectPolicy<T> policy, int maximumRetained)
: base(policy, maximumRetained)
{
}
public override T Get()
{
if (_isDisposed)
{
ThrowObjectDisposedException();
}
return base.Get();
void ThrowObjectDisposedException()
{
throw new ObjectDisposedException(GetType().Name);
}
}
public override void Return(T obj)
{
if (_isDisposed || !ReturnCore(obj))
{
DisposeItem(obj);
}
}
public void Dispose()
{
_isDisposed = true;
DisposeItem(_fastItem);
_fastItem = null;
T result;
while (_items.TryDequeue(out result))
{
DisposeItem(result);
}
}
private static void DisposeItem(T item)
{
if (item is IDisposable disposable)
{
disposable.Dispose();
}
}
}
public interface IPooledObjectPolicy<T> where T : notnull
{
T Create();
bool Return(T obj);
}
public interface IResettable
{
bool TryReset();
}
[Obsolete("LeakTrackingObjectPool<T> was only intended for internal use in diagnostic builds of .NET. It never functioned in publicly shipped .NET versions and may be removed in a future release.")]
public class LeakTrackingObjectPool<T> : ObjectPool<T> where T : class
{
private sealed class Tracker : IDisposable
{
private readonly string _stack;
private bool _disposed;
public Tracker()
{
_stack = Environment.StackTrace;
}
public void Dispose()
{
_disposed = true;
GC.SuppressFinalize(this);
}
~Tracker()
{
if (!_disposed)
{
_ = Environment.HasShutdownStarted;
}
}
}
private readonly ConditionalWeakTable<T, Tracker> _trackers = new ConditionalWeakTable<T, Tracker>();
private readonly ObjectPool<T> _inner;
public LeakTrackingObjectPool(ObjectPool<T> inner)
{
ArgumentNullThrowHelper.ThrowIfNull(inner, "inner");
_inner = inner;
}
public override T Get()
{
T val = _inner.Get();
_trackers.Add(val, new Tracker());
return val;
}
public override void Return(T obj)
{
if (_trackers.TryGetValue(obj, out var value))
{
_trackers.Remove(obj);
value.Dispose();
}
_inner.Return(obj);
}
}
[Obsolete("LeakTrackingObjectPoolProvider was only intended for internal use in diagnostic builds of .NET. It never functioned in publicly shipped .NET versions and may be removed in a future release.")]
public class LeakTrackingObjectPoolProvider : ObjectPoolProvider
{
private readonly ObjectPoolProvider _inner;
public LeakTrackingObjectPoolProvider(ObjectPoolProvider inner)
{
ArgumentNullThrowHelper.ThrowIfNull(inner, "inner");
_inner = inner;
}
public override ObjectPool<T> Create<T>(IPooledObjectPolicy<T> policy)
{
return new LeakTrackingObjectPool<T>(_inner.Create(policy));
}
}
public abstract class ObjectPool<T> where T : class
{
public abstract T Get();
public abstract void Return(T obj);
}
public static class ObjectPool
{
public static ObjectPool<T> Create<T>(IPooledObjectPolicy<T>? policy = null) where T : class, new()
{
return new DefaultObjectPoolProvider().Create(policy ?? new DefaultPooledObjectPolicy<T>());
}
}
public abstract class ObjectPoolProvider
{
public ObjectPool<T> Create<T>() where T : class, new()
{
return Create(new DefaultPooledObjectPolicy<T>());
}
public abstract ObjectPool<T> Create<T>(IPooledObjectPolicy<T> policy) where T : class;
}
public static class ObjectPoolProviderExtensions
{
public static ObjectPool<StringBuilder> CreateStringBuilderPool(this ObjectPoolProvider provider)
{
return provider.Create(new StringBuilderPooledObjectPolicy());
}
public static ObjectPool<StringBuilder> CreateStringBuilderPool(this ObjectPoolProvider provider, int initialCapacity, int maximumRetainedCapacity)
{
StringBuilderPooledObjectPolicy policy = new StringBuilderPooledObjectPolicy
{
InitialCapacity = initialCapacity,
MaximumRetainedCapacity = maximumRetainedCapacity
};
return provider.Create(policy);
}
}
public abstract class PooledObjectPolicy<T> : IPooledObjectPolicy<T> where T : notnull
{
public abstract T Create();
public abstract bool Return(T obj);
}
public class StringBuilderPooledObjectPolicy : PooledObjectPolicy<StringBuilder>
{
public int InitialCapacity { get; set; } = 100;
public int MaximumRetainedCapacity { get; set; } = 4096;
public override StringBuilder Create()
{
return new StringBuilder(InitialCapacity);
}
public override bool Return(StringBuilder obj)
{
if (obj.Capacity > MaximumRetainedCapacity)
{
return false;
}
obj.Clear();
return true;
}
}
}