public void Case1()
        {
#if DEBUG
            var l     = new Stack <IntPtr>();
            var stack = new StackedMemoryPool(12, 100);
            stack.GarbageCollectionDelay = 1000;
            for (int i = 0; i < 100; i++)
            {
                l.Push(stack.Take());
            }

            for (int i = 0; i < 34; i++)
            {
                l.Push(stack.Take());
            }

            for (int i = 0; i < 133; i++)
            {
                stack.Return(l.Pop());
            }

            var manual = new ManualResetEventSlim();
            stack.GCInvoked += () =>
            {
                stack.Available.Should().Be(100);
                manual.Set();
            };

            stack.Return(l.Pop());
            manual.Wait(5000).Should().BeTrue();
#endif
        }
        public static UnmanagedMemoryBlock <T> FromPool(StackedMemoryPool manager)
        {
            Debug.Assert(manager.SingleSize / InfoOf <T> .Size > 0);
            var buffer = manager.Take();

            return(new UnmanagedMemoryBlock <T>((T *)buffer, 1, () => manager.Return(buffer)));
        }
示例#3
0
        public void TakeExceedStored()
        {
            var pool = new StackedMemoryPool(4, 10);

            pool.Available.Should().Be(10);
            var l = new List <IntPtr>();

            for (int i = 0; i < 10; i++)
            {
                l.Add(pool.Take());
            }
            pool.Available.Should().Be(0);
            var next = pool.Take();

            pool.Available.Should().Be(0);
            l.Add(next);
            next.Should().NotBe(IntPtr.Zero);

            for (int i = 0; i < 11; i++)
            {
                pool.Return(l[i]);
            }

            pool.Available.Should().Be(11);
        }