Example #1
0
        /// <summary>
        /// Return an array of the underlying storage from <paramref name="buf"/> into a byte array.
        /// The copy will start at {@code start} and copy {@code length} bytes.
        /// If <paramref name="copy"/> is true a copy will be made of the memory.
        /// If <paramref name="copy"/> is false the underlying storage will be shared, if possible.
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="copy"></param>
        /// <returns></returns>
        public static byte[] GetBytes(IByteBuffer buf, int start, int length, bool copy)
        {
            var capacity = buf.Capacity;

            if (MathUtil.IsOutOfBounds(start, length, capacity))
            {
                ThrowHelper.ThrowIndexOutOfRangeException_Expected(start, length, capacity);
            }

            if (buf.HasArray)
            {
                if (copy || start != 0 || length != capacity)
                {
                    int baseOffset = buf.ArrayOffset + start;
                    var bytes      = new byte[length];
                    PlatformDependent.CopyMemory(buf.Array, baseOffset, bytes, 0, length);
                    return(bytes);
                }
                else
                {
                    return(buf.Array);
                }
            }

            byte[] v = new byte[length];
            _ = buf.GetBytes(start, v);
            return(v);
        }
        internal static IByteBuffer Copy(AbstractByteBuffer buf, byte *addr, int index, int length)
        {
            IByteBuffer copy = buf.Allocator.DirectBuffer(length, buf.MaxCapacity);

            if (length != 0)
            {
                if (copy.HasMemoryAddress)
                {
                    IntPtr ptr = copy.AddressOfPinnedMemory();
                    if (ptr != IntPtr.Zero)
                    {
                        PlatformDependent.CopyMemory(addr, (byte *)ptr, length);
                    }
                    else
                    {
                        fixed(byte *dst = &copy.GetPinnableMemoryAddress())
                        {
                            PlatformDependent.CopyMemory(addr, dst, length);
                        }
                    }
                    copy.SetIndex(0, length);
                }
                else
                {
                    copy.WriteBytes(buf, index, length);
                }
            }
            return(copy);
        }
Example #3
0
        //internal static int SetBytes(AbstractByteBuffer buf, byte* addr, int index, Stream input, int length)
        //{
        //    IByteBuffer tmpBuf = buf.Allocator.HeapBuffer(length);
        //    try
        //    {
        //        int readTotal = 0;
        //        int readBytes;
        //        byte[] tmp = tmpBuf.Array;
        //        int offset = tmpBuf.ArrayOffset;
        //        do
        //        {
        //            readBytes = input.Read(tmp, offset + readTotal, length - readTotal);
        //            readTotal += readBytes;
        //        }
        //        while (readBytes > 0 && readTotal < length);

        //        //if (readTotal > 0)
        //        //{
        //        PlatformDependent.CopyMemory(tmp, offset, addr, readTotal);
        //        //}

        //        return readTotal;
        //    }
        //    finally
        //    {
        //        tmpBuf.Release();
        //    }
        //}

        internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer dst, int dstIndex, int length)
        {
            //if (dst is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.dst); }

            //if (MathUtil.IsOutOfBounds(dstIndex, length, dst.Capacity))
            //{
            //    ThrowHelper.ThrowIndexOutOfRangeException_DstIndex(dstIndex);
            //}
            if (0u >= (uint)length)
            {
                return;
            }

            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);
                    }
                }
                return;
            }

            GetBytes0(buf, addr, index, dst, dstIndex, length);
        }
        internal static int SetBytes(AbstractByteBuffer buf, byte *addr, int index, Stream input, int length)
        {
            if (length == 0)
            {
                return(0);
            }

            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();
            }
        }
        internal static Task <int> SetBytesAsync(AbstractByteBuffer buf, byte *addr, int index, Stream input, int length, CancellationToken cancellationToken)
        {
            if (length == 0)
            {
                return(TaskEx.Zero);
            }

            IByteBuffer tmpBuf = buf.Allocator.HeapBuffer(length);

            return(tmpBuf.SetBytesAsync(0, input, length, cancellationToken)
                   .ContinueWith(t => {
                try
                {
                    var read = t.Result;
                    if (read > 0)
                    {
                        PlatformDependent.CopyMemory(tmpBuf.Array, tmpBuf.ArrayOffset, addr, read);
                    }
                    return read;
                }
                finally
                {
                    tmpBuf.Release();
                }
            }));
        }
Example #6
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);
            }
        }
Example #7
0
        internal static void SetBytes(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer src, int srcIndex, int length)
        {
            //if (src is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.src); }

            //if (MathUtil.IsOutOfBounds(srcIndex, length, src.Capacity))
            //{
            //    ThrowHelper.ThrowIndexOutOfRangeException_SrcIndex(srcIndex);
            //}
            if (0u >= (uint)length)
            {
                return;
            }

            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);
                    }
                }
                return;
            }

            SetBytes0(buf, addr, index, src, srcIndex, length);
        }
Example #8
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);
                }
            }
        }
Example #9
0
        /// <summary>
        ///     Creates a new <see cref="IResourceLeakTracker" /> which is expected to be closed
        ///     when the
        ///     related resource is deallocated.
        /// </summary>
        /// <returns>the <see cref="IResourceLeakTracker" /> or <c>null</c></returns>
        public IResourceLeakTracker Track(object obj)
        {
            DetectionLevel level = Level;

            if (level == DetectionLevel.Disabled)
            {
                return(null);
            }

            if (level < DetectionLevel.Paranoid)
            {
                if (0u >= (uint)(PlatformDependent.GetThreadLocalRandom().Next(this.samplingInterval)))
                {
                    return(new DefaultResourceLeak(this, obj));
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(new DefaultResourceLeak(this, obj));
            }
        }
        public sealed override IByteBuffer AdjustCapacity(int newCapacity)
        {
            CheckNewCapacity(newCapacity);

            uint unewCapacity = (uint)newCapacity;
            uint oldCapacity  = (uint)_capacity;

            if (oldCapacity == unewCapacity)
            {
                return(this);
            }
            int bytesToCopy;

            if (unewCapacity > oldCapacity)
            {
                bytesToCopy = _capacity;
            }
            else
            {
                TrimIndicesToCapacity(newCapacity);
                bytesToCopy = newCapacity;
            }
            byte[] oldBuffer = _buffer;
            byte[] newBuffer = AllocateDirect(newCapacity);
            PlatformDependent.CopyMemory(oldBuffer, 0, newBuffer, 0, bytesToCopy);
            SetByteBuffer(newBuffer, true);
            return(this);
        }
Example #11
0
        public sealed override IByteBuffer AdjustCapacity(int newCapacity)
        {
            CheckNewCapacity(newCapacity);

            uint unewCapacity = (uint)newCapacity;
            uint oldCapacity  = (uint)_capacity;

            if (oldCapacity == unewCapacity)
            {
                return(this);
            }
            int bytesToCopy;

            if (unewCapacity > oldCapacity)
            {
                bytesToCopy = _capacity;
            }
            else
            {
                TrimIndicesToCapacity(newCapacity);
                bytesToCopy = newCapacity;
            }
            byte[] oldArray = Memory;
            byte[] newArray = AllocateArray(newCapacity);
            PlatformDependent.CopyMemory(oldArray, 0, newArray, 0, bytesToCopy);

            SetArray(newArray, MaxCapacity);
            FreeArray(oldArray);
            return(this);
        }
Example #12
0
 internal static void SetZero(byte *addr, int length)
 {
     //if (0u >= (uint)length)
     //{
     //    return;
     //}
     PlatformDependent.SetMemory(addr, length, Zero);
 }
Example #13
0
 internal static void SetZero(byte[] array, int index, int length)
 {
     //if (0u >= (uint)length)
     //{
     //    return;
     //}
     PlatformDependent.SetMemory(array, index, length, Zero);
 }
Example #14
0
 internal static void SetZero(byte[] array, int index, int length)
 {
     if (length == 0)
     {
         return;
     }
     PlatformDependent.SetMemory(array, index, length, Zero);
 }
Example #15
0
 internal static void SetZero(byte *addr, int length)
 {
     if (length == 0)
     {
         return;
     }
     PlatformDependent.SetMemory(addr, length, Zero);
 }
Example #16
0
        public byte[] ToByteArray(int start, int end)
        {
            int count = end - start;
            var bytes = new byte[count];

            PlatformDependent.CopyMemory(this.value, this.offset + start, bytes, 0, count);

            return(bytes);
        }
Example #17
0
        protected override void MemoryCopy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int length)
        {
            if (length == 0)
            {
                return;
            }

            PlatformDependent.CopyMemory(src, srcOffset, dst, dstOffset, length);
        }
Example #18
0
        public override IByteBuffer Copy(int index, int length)
        {
            this.CheckIndex(index, length);
            var copiedArray = new byte[length];

            PlatformDependent.CopyMemory(this.array, index, copiedArray, 0, length);

            return(new UnpooledHeapByteBuffer(this.Allocator, copiedArray, this.MaxCapacity));
        }
Example #19
0
 public sealed override IByteBuffer GetBytes(int index, byte[] dst, int dstIndex, int length)
 {
     if (dst is null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.dst);
     }
     CheckDstIndex(index, length, dstIndex, dst.Length);
     PlatformDependent.CopyMemory(Memory, Idx(index), dst, dstIndex, length);
     return(this);
 }
Example #20
0
        public LocalServerChannel()
        {
            _inboundBuffer = PlatformDependent.NewMpscQueue <object>();
            _shutdownHook  = () => Unsafe.Close(Unsafe.VoidPromise());

            var config = new DefaultChannelConfiguration(this);

            config.Allocator = new PreferHeapByteBufAllocator(config.Allocator);
            Configuration    = config;
        }
Example #21
0
 public sealed override IByteBuffer SetBytes(int index, byte[] src, int srcIndex, int length)
 {
     if (src is null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.src);
     }
     CheckSrcIndex(index, length, srcIndex, src.Length);
     PlatformDependent.CopyMemory(src, srcIndex, Memory, Idx(index), length);
     return(this);
 }
Example #22
0
 private static void GetBytes0(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer dst, int dstIndex, int length)
 {
     if (dst.HasArray)
     {
         PlatformDependent.CopyMemory(addr, dst.Array, dst.ArrayOffset + dstIndex, length);
     }
     else
     {
         _ = dst.SetBytes(dstIndex, buf, index, length);
     }
 }
Example #23
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);
     }
 }
Example #24
0
 private static void SetBytes0(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer src, int srcIndex, int length)
 {
     if (src.HasArray)
     {
         PlatformDependent.CopyMemory(src.Array, src.ArrayOffset + srcIndex, addr, length);
     }
     else
     {
         _ = src.GetBytes(srcIndex, buf, index, length);
     }
 }
        /// <summary>
        ///     Creates a new big-endian buffer whose content is a copy of the specified array.
        ///     The new buffer's <see cref="IByteBuffer.ReaderIndex" /> and <see cref="IByteBuffer.WriterIndex" />
        ///     are <c>0</c> and <see cref="Array.Length" /> respectively.
        /// </summary>
        /// <param name="array">A buffer we're going to copy.</param>
        /// <param name="offset">The index offset from which we're going to read array.</param>
        /// <param name="length">
        ///     The number of bytes we're going to read from array beginning from position offset.
        /// </param>
        /// <returns>The new buffer that copies the contents of array.</returns>
        public static IByteBuffer CopiedBuffer(byte[] array, int offset, int length)
        {
            if (length == 0)
            {
                return(Empty);
            }

            var copy = new byte[length];

            PlatformDependent.CopyMemory(array, offset, copy, 0, length);
            return(WrappedBuffer(copy));
        }
Example #26
0
        // ReSharper disable NonReadonlyMemberInGetHashCode
        public override int GetHashCode()
        {
            int h = this.hash;

            if (0u >= (uint)h)
            {
                h         = PlatformDependent.HashCodeAscii(this.value, this.offset, this.length);
                this.hash = h;
            }

            return(h);
        }
Example #27
0
        protected sealed override IByteBuffer NewBuffer(int length, int maxCapacity)
        {
            this.AssumedMaxCapacity = maxCapacity == int.MaxValue;

            int         offset = length == 0 ? 0 : PlatformDependent.GetThreadLocalRandom().Next(length);
            IByteBuffer buffer = Unpooled.Buffer(length * 2);
            IByteBuffer slice  = this.NewSlice(buffer, offset, length);

            Assert.Equal(0, slice.ReaderIndex);
            Assert.Equal(length, slice.WriterIndex);

            return(slice);
        }
        /// <summary>
        ///     Creates a new big-endian buffer whose content is a copy of the specified array
        ///     The new buffer's <see cref="IByteBuffer.ReaderIndex" /> and <see cref="IByteBuffer.WriterIndex" />
        ///     are <c>0</c> and <see cref="Array.Length" /> respectively.
        /// </summary>
        /// <param name="array">A buffer we're going to copy.</param>
        /// <returns>The new buffer that copies the contents of array.</returns>
        public static IByteBuffer CopiedBuffer(byte[] array)
        {
            if (array.Length == 0)
            {
                return(Empty);
            }

            var newArray = new byte[array.Length];

            PlatformDependent.CopyMemory(array, 0, newArray, 0, array.Length);

            return(WrappedBuffer(newArray));
        }
        internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, byte[] dst, int dstIndex, int length)
        {
            Contract.Requires(dst != null);

            if (MathUtil.IsOutOfBounds(dstIndex, length, dst.Length))
            {
                ThrowHelper.ThrowIndexOutOfRangeException_DstIndex(dstIndex);
            }
            if (length != 0)
            {
                PlatformDependent.CopyMemory(addr, dst, dstIndex, length);
            }
        }
Example #30
0
        internal static void GetBytes(byte *addr, byte[] dst, int dstIndex, int length)
        {
            //if (dst is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.dst); }

            //if (MathUtil.IsOutOfBounds(dstIndex, length, dst.Length))
            //{
            //    ThrowHelper.ThrowIndexOutOfRangeException_DstIndex(dstIndex);
            //}
            //if (length != 0)
            //{
            PlatformDependent.CopyMemory(addr, dst, dstIndex, length);
            //}
        }