(965c31410a) Unstable v0.10.4.0
This commit is contained in:
@@ -7,86 +7,70 @@ namespace Steamworks
|
||||
{
|
||||
internal static class Helpers
|
||||
{
|
||||
public const int MaxStringSize = 1024 * 32;
|
||||
public const int MemoryBufferSize = 1024 * 32;
|
||||
|
||||
private static IntPtr[] MemoryPool = new IntPtr[]
|
||||
{
|
||||
Marshal.AllocHGlobal( MemoryBufferSize ),
|
||||
Marshal.AllocHGlobal( MemoryBufferSize ),
|
||||
Marshal.AllocHGlobal( MemoryBufferSize ),
|
||||
Marshal.AllocHGlobal( MemoryBufferSize )
|
||||
};
|
||||
|
||||
private static object mutex = new object();
|
||||
private static IntPtr[] MemoryPool;
|
||||
private static int MemoryPoolIndex;
|
||||
|
||||
public static unsafe IntPtr TakeMemory()
|
||||
{
|
||||
IntPtr take = IntPtr.Zero;
|
||||
lock (mutex)
|
||||
lock ( MemoryPool )
|
||||
{
|
||||
if (MemoryPool == null)
|
||||
{
|
||||
//
|
||||
// The pool has 5 items. This should be safe because we shouldn't really
|
||||
// ever be using more than 2 memory pools
|
||||
//
|
||||
MemoryPool = new IntPtr[5];
|
||||
|
||||
for (int i = 0; i < MemoryPool.Length; i++)
|
||||
MemoryPool[i] = Marshal.AllocHGlobal(MaxStringSize);
|
||||
}
|
||||
|
||||
MemoryPoolIndex++;
|
||||
if (MemoryPoolIndex >= MemoryPool.Length)
|
||||
|
||||
if ( MemoryPoolIndex >= MemoryPool.Length )
|
||||
MemoryPoolIndex = 0;
|
||||
|
||||
take = MemoryPool[MemoryPoolIndex];
|
||||
var take = MemoryPool[MemoryPoolIndex];
|
||||
|
||||
((byte*)take)[0] = 0;
|
||||
}
|
||||
|
||||
return take;
|
||||
return take;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static byte[][] BufferPool;
|
||||
private static byte[][] BufferPool = new byte[4][];
|
||||
private static int BufferPoolIndex;
|
||||
private static object BufferMutex = new object();
|
||||
|
||||
/// <summary>
|
||||
/// Returns a buffer. This will get returned and reused later on.
|
||||
/// We shouldn't really be using this anymore.
|
||||
/// </summary>
|
||||
public static byte[] TakeBuffer( int minSize )
|
||||
{
|
||||
int bufferPoolIndex;
|
||||
lock (BufferMutex)
|
||||
lock ( BufferPool )
|
||||
{
|
||||
if (BufferPool == null)
|
||||
{
|
||||
//
|
||||
// The pool has 8 items.
|
||||
//
|
||||
BufferPool = new byte[8][];
|
||||
|
||||
for (int i = 0; i < BufferPool.Length; i++)
|
||||
BufferPool[i] = new byte[1024 * 128];
|
||||
}
|
||||
|
||||
BufferPoolIndex++;
|
||||
|
||||
if (BufferPoolIndex < 0 || BufferPoolIndex >= BufferPool.Length)
|
||||
if ( BufferPoolIndex >= BufferPool.Length )
|
||||
BufferPoolIndex = 0;
|
||||
|
||||
bufferPoolIndex = BufferPoolIndex;
|
||||
}
|
||||
if ( BufferPool[BufferPoolIndex] == null )
|
||||
BufferPool[BufferPoolIndex] = new byte[1024 * 256];
|
||||
|
||||
if ( BufferPool[bufferPoolIndex].Length < minSize )
|
||||
{
|
||||
BufferPool[bufferPoolIndex] = new byte[minSize + 1024];
|
||||
}
|
||||
if ( BufferPool[BufferPoolIndex].Length < minSize )
|
||||
{
|
||||
BufferPool[BufferPoolIndex] = new byte[minSize + 1024];
|
||||
}
|
||||
|
||||
return BufferPool[bufferPoolIndex];
|
||||
return BufferPool[BufferPoolIndex];
|
||||
}
|
||||
}
|
||||
|
||||
internal unsafe static string MemoryToString( IntPtr ptr )
|
||||
{
|
||||
var len = 0;
|
||||
|
||||
for( len = 0; len < MaxStringSize; len++ )
|
||||
for( len = 0; len < MemoryBufferSize; len++ )
|
||||
{
|
||||
if ( ((byte*)ptr)[len] == 0 )
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user