mirror of
https://github.com/aphrody-labs/NuGet.Client.git
synced 2026-10-14 05:19:04 +00:00
42 lines
945 B
C#
42 lines
945 B
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace NuGet
|
|
{
|
|
internal class SimplePool<T> where T : class
|
|
{
|
|
private readonly object _lockObj = new();
|
|
private readonly Stack<T> _values = new();
|
|
private readonly Func<T> _allocate;
|
|
|
|
public SimplePool(Func<T> allocate)
|
|
{
|
|
_allocate = allocate;
|
|
}
|
|
|
|
public T Allocate()
|
|
{
|
|
lock (_lockObj)
|
|
{
|
|
if (_values.Count > 0)
|
|
{
|
|
return _values.Pop();
|
|
}
|
|
}
|
|
|
|
return _allocate();
|
|
}
|
|
|
|
public void Free(T value)
|
|
{
|
|
lock (_lockObj)
|
|
{
|
|
_values.Push(value);
|
|
}
|
|
}
|
|
}
|
|
}
|