Ejemplo 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);
        }
Ejemplo n.º 2
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);
        }