Don't use ThreadStatic in Helpers

This commit is contained in:
Garry Newman 2020-05-07 11:47:46 +01:00
parent 04d1c86954
commit b813ee7a17

View File

@ -9,37 +9,35 @@ namespace Steamworks
{
public const int MemoryBufferSize = 1024 * 32;
[ThreadStatic] private static IntPtr[] MemoryPool;
[ThreadStatic] private static int MemoryPoolIndex;
private static IntPtr[] MemoryPool = new IntPtr[]
{
Marshal.AllocHGlobal( MemoryBufferSize ),
Marshal.AllocHGlobal( MemoryBufferSize ),
Marshal.AllocHGlobal( MemoryBufferSize ),
Marshal.AllocHGlobal( MemoryBufferSize )
};
private static int MemoryPoolIndex;
public static unsafe IntPtr TakeMemory()
{
if ( MemoryPool == null )
lock ( MemoryPool )
{
//
// The pool has 4 items. This should be safe because we shouldn't really
// ever be using more than 2 memory pools
//
MemoryPool = new IntPtr[4];
MemoryPoolIndex++;
for ( int i = 0; i < MemoryPool.Length; i++ )
MemoryPool[i] = Marshal.AllocHGlobal( MemoryBufferSize );
if ( MemoryPoolIndex >= MemoryPool.Length )
MemoryPoolIndex = 0;
var take = MemoryPool[MemoryPoolIndex];
((byte*)take)[0] = 0;
return take;
}
MemoryPoolIndex++;
if ( MemoryPoolIndex >= MemoryPool.Length )
MemoryPoolIndex = 0;
var take = MemoryPool[MemoryPoolIndex];
((byte*)take)[0] = 0;
return take;
}
[ThreadStatic] private static byte[][] BufferPool;
[ThreadStatic] private static int BufferPoolIndex;
private static byte[][] BufferPool = new byte[4][];
private static int BufferPoolIndex;
/// <summary>
/// Returns a buffer. This will get returned and reused later on.
@ -47,27 +45,23 @@ namespace Steamworks
/// </summary>
public static byte[] TakeBuffer( int minSize )
{
if ( BufferPool == null )
lock ( BufferPool )
{
//
// The pool has 4 items.
//
BufferPool = new byte[4][];
BufferPoolIndex++;
for ( int i = 0; i < BufferPool.Length; i++ )
BufferPool[i] = new byte[ 1024 * 128 ];
if ( BufferPoolIndex >= BufferPool.Length )
BufferPoolIndex = 0;
if ( BufferPool[BufferPoolIndex] == null )
BufferPool[BufferPoolIndex] = new byte[1024 * 256];
if ( BufferPool[BufferPoolIndex].Length < minSize )
{
BufferPool[BufferPoolIndex] = new byte[minSize + 1024];
}
return BufferPool[BufferPoolIndex];
}
BufferPoolIndex++;
if ( BufferPoolIndex >= BufferPool.Length )
BufferPoolIndex = 0;
if ( BufferPool[BufferPoolIndex].Length < minSize )
{
BufferPool[BufferPoolIndex] = new byte[minSize + 1024];
}
return BufferPool[BufferPoolIndex];
}
internal unsafe static string MemoryToString( IntPtr ptr )