예제 #1
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);
            }
        }
예제 #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);
                }
            }
        }
        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);
        }
예제 #4
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);
        }
예제 #5
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);
        }
예제 #6
0
        public void TestHasNoMemoryAddressWhenMultipleBuffers()
        {
            IByteBuffer buf1 = Unpooled.DirectBuffer(10);

            if (!buf1.HasMemoryAddress)
            {
                buf1.Release();
                return;
            }

            IByteBuffer buf2 = Unpooled.DirectBuffer(10);
            IByteBuffer buf  = NewBuffer(buf1, buf2);

            Assert.False(buf.HasMemoryAddress);
            try
            {
                buf.GetPinnableMemoryAddress();
                Assert.False(true);
            }
            catch (NotSupportedException)
            {
                // expected
            }
            finally
            {
                buf.Release();
            }
        }
예제 #7
0
        public void TestHasMemoryAddressWithSingleBuffer()
        {
            IByteBuffer buf1 = Unpooled.DirectBuffer(10);

            if (!buf1.HasMemoryAddress)
            {
                buf1.Release();
                return;
            }
            IByteBuffer buf = NewBuffer(buf1);

            Assert.True(buf.HasMemoryAddress);
            Assert.Equal(buf1.GetPinnableMemoryAddress(), buf.GetPinnableMemoryAddress());
            buf.Release();
        }
예제 #8
0
 public ref byte GetPinnableMemoryAddress() => ref Buf.GetPinnableMemoryAddress();