Esempio n. 1
0
        public Span <byte> Rent(int numberOfElements, bool clearBuffer = false)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("NativeBufferPool");
            }

            Span <byte> buffer;

            if (numberOfElements > _maxElements)
            {
#if DEBUG
                System.Diagnostics.Debugger.Break();
#endif
                buffer = new Span <byte>(Marshal.AllocHGlobal(numberOfElements * Marshal.SizeOf(typeof(byte))).ToPointer(), numberOfElements);
            }
            else
            {
                int index = BufferUtilities.SelectBucketIndex(numberOfElements);
                NativeBufferBucket bucket = Volatile.Read(ref _buckets[index]);
                if (bucket == null)
                {
                    NativeBufferBucket newBucket = new NativeBufferBucket(BufferUtilities.GetMaxSizeForBucket(index), NUMBER_OF_BUFFERS_IN_BUCKET);
                    if (Interlocked.CompareExchange(ref _buckets[index], newBucket, null) != null)
                    {
                        newBucket.Dispose();
                    }

                    bucket = _buckets[index];
                }

                var rented = bucket.Rent();
                buffer = new Span <byte>(rented._memory, rented._length);
            }

            if (clearBuffer)
            {
                BufferUtilities.ClearSpan(buffer);
            }
            return(buffer);
        }
Esempio n. 2
0
        public void Return(Span <byte> buffer, bool clearBuffer = false)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("NativeBufferPool");
            }

            if (clearBuffer)
            {
                BufferUtilities.ClearSpan(buffer);
            }

            if (buffer.Length > _maxElements)
            {
                Marshal.FreeHGlobal(new IntPtr(buffer.UnsafePointer));
            }
            else
            {
                _buckets[BufferUtilities.SelectBucketIndex(buffer.Length)].Return(new NativeBuffer(buffer.UnsafePointer, 0));
            }
        }
Esempio n. 3
0
        public T[] RentBuffer(int size, bool clearBuffer = false)
        {
            T[] buffer;
            if (size > _maxBufferSize)
            {
                buffer = new T[size];
            }
            else
            {
                int index = BufferUtilities.SelectBucketIndex(size);
                ManagedBufferBucket <T> bucket = Volatile.Read(ref _buckets[index]);
                if (bucket == null)
                {
                    Interlocked.CompareExchange(ref _buckets[index], new ManagedBufferBucket <T>(BufferUtilities.GetMaxSizeForBucket(index), NUMBER_OF_BUFFERS_IN_BUCKET), null);
                    bucket = _buckets[index];
                }
                buffer = bucket.Rent();
                if (clearBuffer)
                {
                    Array.Clear(buffer, 0, buffer.Length);
                }
            }

            return(buffer);
        }