Esempio n. 1
0
        private void CopyToBuffer(byte *value, int size)
        {
            if (_buffer == null ||
                _bufferPos + size > _buffer.SizeInBytes)
            {
                var newBuffer = _buffersPool.Allocate(Bits.NextPowerOf2(_bufferPos + size));
                if (_buffer != null)
                {
                    Memory.Copy(newBuffer.Address, _buffer.Address, _buffer.SizeInBytes);
                    _buffersPool.Return(_buffer);
                }
                _buffer = newBuffer;
            }

            Memory.Copy(_buffer.Address + _bufferPos, value, size);
            _bufferPos += size;
        }
Esempio n. 2
0
 public void SerialAllocationAndRelease()
 {
     using (var pool = new UnmanagedBuffersPoolWithLowMemoryHandling(string.Empty))
     {
         var allocatedMemory = new List <AllocatedMemoryData>();
         for (var i = 0; i < 1000; i++)
         {
             allocatedMemory.Add(pool.Allocate(i));
         }
         foreach (var data in allocatedMemory)
         {
             pool.Return(data);
         }
     }
 }
Esempio n. 3
0
        public void ParallelAllocationAndReleaseSeperately()
        {
            using (var pool = new UnmanagedBuffersPoolWithLowMemoryHandling(string.Empty))
            {
                var allocatedMemory = new global::Sparrow.Collections.ConcurrentSet <AllocatedMemoryData>();
                Parallel.For(0, 100, RavenTestHelper.DefaultParallelOptions, x =>
                {
                    for (var i = 0; i < 10; i++)
                    {
                        allocatedMemory.Add(pool.Allocate(i));
                    }
                });

                Parallel.ForEach(allocatedMemory, RavenTestHelper.DefaultParallelOptions, item =>
                {
                    pool.Return(item);
                });
            }
        }
Esempio n. 4
0
        public void ParallelSerialAllocationAndRelease()
        {
            using (var pool = new UnmanagedBuffersPoolWithLowMemoryHandling(string.Empty))
            {
                var allocatedMemory = new BlockingCollection <AllocatedMemoryData>();
                Task.Run(() =>
                {
                    for (var i = 0; i < 100; i++)
                    {
                        allocatedMemory.Add(pool.Allocate(i));
                    }
                    allocatedMemory.CompleteAdding();
                });

                while (allocatedMemory.IsCompleted == false)
                {
                    AllocatedMemoryData tuple;
                    if (allocatedMemory.TryTake(out tuple, 100))
                    {
                        pool.Return(tuple);
                    }
                }
            }
        }