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); } }
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 = ©.GetPinnableMemoryAddress()) { PlatformDependent.CopyMemory(addr, dst, length); } } copy.SetIndex(0, length); } else { copy.WriteBytes(buf, index, length); } } return(copy); }
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); }
//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); }
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(); } }
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(); }
public ref byte GetPinnableMemoryAddress() => ref Buf.GetPinnableMemoryAddress();