public sealed override MemoryHandle Pin(int elementIndex = 0)
            {
                unsafe
                {
                    while (true)
                    {
                        int currentCount = Volatile.Read(ref _refCount);
                        if (currentCount <= 0)
                        {
                            ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer();
                        }
                        if (Interlocked.CompareExchange(ref _refCount, currentCount + 1, currentCount) == currentCount)
                        {
                            break;
                        }
                    }

                    try
                    {
                        if ((uint)elementIndex > (uint)_array.Length)
                        {
                            ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.elementIndex);
                        }

                        GCHandle handle = GCHandle.Alloc(_array, GCHandleType.Pinned);

                        return(new MemoryHandle(Unsafe.Add <T>(((void *)handle.AddrOfPinnedObject()), elementIndex), handle, this));
                    }
                    catch
                    {
                        Unpin();
                        throw;
                    }
                }
            }
            public sealed override Span <T> GetSpan()
            {
                if (IsDisposed)
                {
                    ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer();
                }

                return(_array);
            }
Example #3
0
            public sealed override void Retain()
            {
                if (IsDisposed)
                {
                    ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer();
                }

                _refCount++;
            }
Example #4
0
            sealed override bool TryGetArray(out ArraySegment <T> arraySegment)
            {
                if (IsDisposed)
                {
                    ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer();
                }

                arraySegment = new ArraySegment <T>(_array);
                return(true);
            }
Example #5
0
 public sealed override void Retain()
 {
     while (true)
     {
         int currentCount = Volatile.Read(ref _refCount);
         if (currentCount <= 0)
         {
             ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer();
         }
         if (Interlocked.CompareExchange(ref _refCount, currentCount + 1, currentCount) == currentCount)
         {
             break;
         }
     }
 }
Example #6
0
            public sealed override bool Release()
            {
                if (IsDisposed)
                {
                    ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer();
                }

                int newRefCount = --_refCount;

                if (newRefCount < 0)
                {
                    ThrowHelper.ThrowInvalidOperationException();
                }

                return(newRefCount != 0);
            }
Example #7
0
 public sealed override bool Release()
 {
     while (true)
     {
         int currentCount = Volatile.Read(ref _refCount);
         if (currentCount <= 0)
         {
             ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer();
         }
         if (Interlocked.CompareExchange(ref _refCount, currentCount - 1, currentCount) == currentCount)
         {
             if (currentCount == 1)
             {
                 Dispose();
                 return(false);
             }
             return(true);
         }
     }
 }