ln.biginteger/ln.biginteger/BigIntegerAllocator.cs

42 lines
907 B
C#

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
namespace ln.biginteger
{
public static class BigIntegerAllocator
{
static int _mpz_size = 24;
static int _allocate_count = 40960000;
static Stack<IntPtr> pool = new Stack<IntPtr>();
public static IntPtr Pop()
{
lock (pool)
{
if (!pool.TryPop(out IntPtr ptr))
{
ptr = Marshal.AllocHGlobal(_mpz_size * _allocate_count);
for (int n=1;n<_allocate_count;n++)
pool.Push(ptr + (n*_mpz_size));
}
return ptr;
}
}
public static void Push(IntPtr ptr)
{
lock (pool)
{
pool.Push(ptr);
}
}
}
}