mirror of https://github.com/ogoun/Zero.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.5 KiB
54 lines
1.5 KiB
using MemoryPools.Memory;
|
|
using System;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
/*https://github.com/sidristij/memory-pools*/
|
|
|
|
namespace MemoryPools
|
|
{
|
|
public static class Pool<T> where T : class, new()
|
|
{
|
|
private static readonly DefaultObjectPool<T> _freeObjectsQueue = new DefaultObjectPool<T>(new DefaultPooledObjectPolicy<T>());
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static T Get()
|
|
{
|
|
return _freeObjectsQueue.Get();
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void Return<T1>(T1 instance) where T1 : T
|
|
{
|
|
if (instance != null)
|
|
_freeObjectsQueue.Return(instance);
|
|
}
|
|
}
|
|
|
|
public static class Pool
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static T Get<T>() where T : class, new()
|
|
{
|
|
return Pool<T>.Get();
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void Return<T>(T instance) where T : class, new()
|
|
{
|
|
Pool<T>.Return(instance);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static CountdownMemoryOwner<T> GetBuffer<T>(int size)
|
|
{
|
|
return InternalArraysPool.Rent<T>(size, false);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static CountdownMemoryOwner<T> GetBufferFrom<T>(ReadOnlySpan<T> source)
|
|
{
|
|
return InternalArraysPool.RentFrom(source, false);
|
|
}
|
|
}
|
|
}
|