internal T Init <T>(
            AbstractByteBuffer unwrapped, IByteBuffer wrapped, int readerIndex, int writerIndex, int maxCapacity)
            where T : AbstractPooledDerivedByteBuffer
        {
            wrapped.Retain(); // Retain up front to ensure the parent is accessible before doing more work.
            this.parent     = wrapped;
            this.rootParent = unwrapped;

            try
            {
                this.SetMaxCapacity(maxCapacity);
                this.SetIndex0(readerIndex, writerIndex); // It is assumed the bounds checking is done by the caller.
                this.SetReferenceCount(1);

                wrapped = null;
                return((T)this);
            }
            finally
            {
                if (wrapped != null)
                {
                    this.parent = this.rootParent = null;
                    wrapped.Release();
                }
            }
        }
示例#2
0
        internal static void SetBytes(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer src, int srcIndex, int length)
        {
            if (MathUtil.IsOutOfBounds(srcIndex, length, src.Capacity))
            {
                throw new IndexOutOfRangeException($"srcIndex: {srcIndex}");
            }

            if (length != 0)
            {
                if (src.HasMemoryAddress)
                {
                    IntPtr ptr = src.AddressOfPinnedMemory();
                    if (ptr != IntPtr.Zero)
                    {
                        PlatformDependent.CopyMemory((byte *)(ptr + srcIndex), addr, length);
                    }
                    else
                    {
                        fixed(byte *source = &src.GetPinnableMemoryAddress())
                        {
                            PlatformDependent.CopyMemory(source + srcIndex, addr, length);
                        }
                    }
                }
                else if (src.HasArray)
                {
                    PlatformDependent.CopyMemory(src.Array, src.ArrayOffset + srcIndex, addr, length);
                }
                else
                {
                    src.GetBytes(srcIndex, buf, index, length);
                }
            }
        }
示例#3
0
        internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer dst, int dstIndex, int length)
        {
            if (MathUtil.IsOutOfBounds(dstIndex, length, dst.Capacity))
            {
                throw new IndexOutOfRangeException($"dstIndex: {dstIndex}");
            }

            if (dst.HasMemoryAddress)
            {
                IntPtr ptr = dst.AddressOfPinnedMemory();
                if (ptr != IntPtr.Zero)
                {
                    PlatformDependent.CopyMemory(addr, (byte *)(ptr + dstIndex), length);
                }
                else
                {
                    fixed(byte *destination = &dst.GetPinnableMemoryAddress())
                    {
                        PlatformDependent.CopyMemory(addr, destination + dstIndex, length);
                    }
                }
            }
            else if (dst.HasArray)
            {
                PlatformDependent.CopyMemory(addr, dst.Array, dst.ArrayOffset + dstIndex, length);
            }
            else
            {
                dst.SetBytes(dstIndex, buf, index, length);
            }
        }
示例#4
0
        static PooledSlicedByteBuffer NewInstance0(AbstractByteBuffer unwrapped, IByteBuffer wrapped, int adjustment, int length)
        {
            PooledSlicedByteBuffer slice = Recycler.Take();

            slice.Init <PooledSlicedByteBuffer>(unwrapped, wrapped, 0, length, length);
            slice.DiscardMarks();
            slice.adjustment = adjustment;

            return(slice);
        }
示例#5
0
        internal static PooledDuplicatedByteBuffer NewInstance(AbstractByteBuffer unwrapped, IByteBuffer wrapped, int readerIndex, int writerIndex)
        {
            PooledDuplicatedByteBuffer duplicate = Recycler.Take();

            duplicate.Init <PooledDuplicatedByteBuffer>(unwrapped, wrapped, readerIndex, writerIndex, unwrapped.MaxCapacity);
            duplicate.MarkReaderIndex();
            duplicate.MarkWriterIndex();

            return(duplicate);
        }
示例#6
0
 internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, byte[] dst, int dstIndex, int length)
 {
     if (MathUtil.IsOutOfBounds(dstIndex, length, dst.Length))
     {
         throw new IndexOutOfRangeException($"dstIndex: {dstIndex}");
     }
     if (length != 0)
     {
         PlatformDependent.CopyMemory(addr, dst, dstIndex, length);
     }
 }
示例#7
0
 internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, Stream output, int length)
 {
     if (length != 0)
     {
         IByteBuffer tmpBuf = buf.Allocator.Buffer(length);
         try
         {
             byte[] tmp    = tmpBuf.Array;
             int    offset = tmpBuf.ArrayOffset;
             PlatformDependent.CopyMemory(addr, tmp, offset, length);
             output.Write(tmp, offset, length);
         }
         finally
         {
             tmpBuf.Release();
         }
     }
 }
        internal UnpooledDuplicatedByteBuffer(AbstractByteBuffer buffer, int readerIndex, int writerIndex)
            : base(buffer.MaxCapacity)
        {
            if (buffer is UnpooledDuplicatedByteBuffer duplicated)
            {
                this.buffer = duplicated.buffer;
            }
            else if (buffer is AbstractPooledDerivedByteBuffer)
            {
                this.buffer = (AbstractByteBuffer)buffer.Unwrap();
            }
            else
            {
                this.buffer = buffer;
            }

            this.SetIndex0(readerIndex, writerIndex);
            this.MarkIndex(); // Mark read and writer index
        }
示例#9
0
        internal static int SetBytes(AbstractByteBuffer buf, byte *addr, int index, Stream input, int length)
        {
            IByteBuffer tmpBuf = buf.Allocator.HeapBuffer(length);

            try
            {
                byte[] tmp       = tmpBuf.Array;
                int    offset    = tmpBuf.ArrayOffset;
                int    readBytes = input.Read(tmp, offset, length);
                if (readBytes > 0)
                {
                    PlatformDependent.CopyMemory(tmp, offset, addr, readBytes);
                }

                return(readBytes);
            }
            finally
            {
                tmpBuf.Release();
            }
        }
示例#10
0
        internal static IByteBuffer Copy(AbstractByteBuffer buf, byte *addr, int index, int length)
        {
            IByteBuffer copy = buf.Allocator.Buffer(length, buf.MaxCapacity);

            if (length != 0)
            {
                if (copy.HasMemoryAddress)
                {
                    fixed(byte *dst = &copy.GetPinnableMemoryAddress())
                    {
                        PlatformDependent.CopyMemory(addr, dst, length);
                    }

                    copy.SetIndex(0, length);
                }
                else
                {
                    copy.WriteBytes(buf, index, length);
                }
            }
            return(copy);
        }
示例#11
0
 internal static PooledSlicedByteBuffer NewInstance(AbstractByteBuffer unwrapped, IByteBuffer wrapped, int index, int length)
 {
     CheckSliceOutOfBounds(index, length, unwrapped);
     return(NewInstance0(unwrapped, wrapped, index, length));
 }
示例#12
0
 // No need to check length zero, the calling method already done it
 internal static void SetBytes(AbstractByteBuffer buf, byte *addr, int index, byte[] src, int srcIndex, int length) =>
 PlatformDependent.CopyMemory(src, srcIndex, addr, length);
 public PooledNonRetainedSlicedByteBuffer(IReferenceCounted referenceCountDelegate, AbstractByteBuffer buffer, int index, int length)
     : base(buffer, index, length)
 {
     this.referenceCountDelegate = referenceCountDelegate;
 }
 internal PooledNonRetainedDuplicateByteBuffer(IReferenceCounted referenceCountDelegate, AbstractByteBuffer buffer)
     : base(buffer)
 {
     this.referenceCountDelegate = referenceCountDelegate;
 }
 public UnpooledDuplicatedByteBuffer(AbstractByteBuffer buffer)
     : this(buffer, buffer.ReaderIndex, buffer.WriterIndex)
 {
 }