/// <summary> /// Gets a {@code short} at memory address {@code p} by reading byte for byte, instead of the whole value /// in one go. This can be useful, even necessary in some scenarios where <seealso cref="allowUnalignedMemoryAccess"/> /// is {@code false} and {@code p} isn't aligned properly. Values read with this method should have been /// previously put using <seealso cref="putShortByteWiseLittleEndian(long, short)"/>. /// </summary> /// <param name="p"> address pointer to start reading at. </param> /// <returns> the read value, which was read byte for byte. </returns> public static short GetShortByteWiseLittleEndian(long p) { short a = ( short )(UnsafeUtil.GetByte(p) & 0xFF); short b = ( short )(UnsafeUtil.GetByte(p + 1) & 0xFF); return(( short )((b << 8) | a)); }
/// <summary> /// Puts a {@code int} at memory address {@code p} by writing byte for byte, instead of the whole value /// in one go. This can be useful, even necessary in some scenarios where <seealso cref="allowUnalignedMemoryAccess"/> /// is {@code false} and {@code p} isn't aligned properly. Values written with this method should be /// read using <seealso cref="getIntByteWiseLittleEndian(long)"/>. /// </summary> /// <param name="p"> address pointer to start writing at. </param> /// <param name="value"> value to write byte for byte. </param> public static void PutIntByteWiseLittleEndian(long p, int value) { UnsafeUtil.PutByte(p, ( sbyte )value); UnsafeUtil.PutByte(p + 1, ( sbyte )(value >> 8)); UnsafeUtil.PutByte(p + 2, ( sbyte )(value >> 16)); UnsafeUtil.PutByte(p + 3, ( sbyte )(value >> 24)); }
/// <summary> /// Gets a {@code int} at memory address {@code p} by reading byte for byte, instead of the whole value /// in one go. This can be useful, even necessary in some scenarios where <seealso cref="allowUnalignedMemoryAccess"/> /// is {@code false} and {@code p} isn't aligned properly. Values read with this method should have been /// previously put using <seealso cref="putIntByteWiseLittleEndian(long, int)"/>. /// </summary> /// <param name="p"> address pointer to start reading at. </param> /// <returns> the read value, which was read byte for byte. </returns> public static int GetIntByteWiseLittleEndian(long p) { int a = UnsafeUtil.GetByte(p) & 0xFF; int b = UnsafeUtil.GetByte(p + 1) & 0xFF; int c = UnsafeUtil.GetByte(p + 2) & 0xFF; int d = UnsafeUtil.GetByte(p + 3) & 0xFF; return((d << 24) | (c << 16) | (b << 8) | a); }
/// <summary> /// Puts a {@code long} at memory address {@code p} by writing byte for byte, instead of the whole value /// in one go. This can be useful, even necessary in some scenarios where <seealso cref="allowUnalignedMemoryAccess"/> /// is {@code false} and {@code p} isn't aligned properly. Values written with this method should be /// read using <seealso cref="getShortByteWiseLittleEndian(long)"/>. /// </summary> /// <param name="p"> address pointer to start writing at. </param> /// <param name="value"> value to write byte for byte. </param> public static void PutLongByteWiseLittleEndian(long p, long value) { UnsafeUtil.PutByte(p, ( sbyte )value); UnsafeUtil.PutByte(p + 1, ( sbyte )(value >> 8)); UnsafeUtil.PutByte(p + 2, ( sbyte )(value >> 16)); UnsafeUtil.PutByte(p + 3, ( sbyte )(value >> 24)); UnsafeUtil.PutByte(p + 4, ( sbyte )(value >> 32)); UnsafeUtil.PutByte(p + 5, ( sbyte )(value >> 40)); UnsafeUtil.PutByte(p + 6, ( sbyte )(value >> 48)); UnsafeUtil.PutByte(p + 7, ( sbyte )(value >> 56)); }
/// <summary> /// Atomically set field or array element to a maximum between current value and provided <code>newValue</code> /// </summary> public static void CompareAndSetMaxLong(object @object, long fieldOffset, long newValue) { long currentValue; do { currentValue = UnsafeUtil.GetLongVolatile(@object, fieldOffset); if (currentValue >= newValue) { return; } } while (!UnsafeUtil.CompareAndSwapLong(@object, fieldOffset, currentValue, newValue)); }
/// <summary> /// Gets a {@code long} at memory address {@code p} by reading byte for byte, instead of the whole value /// in one go. This can be useful, even necessary in some scenarios where <seealso cref="allowUnalignedMemoryAccess"/> /// is {@code false} and {@code p} isn't aligned properly. Values read with this method should have been /// previously put using <seealso cref="putLongByteWiseLittleEndian(long, long)"/>. /// </summary> /// <param name="p"> address pointer to start reading at. </param> /// <returns> the read value, which was read byte for byte. </returns> public static long GetLongByteWiseLittleEndian(long p) { long a = UnsafeUtil.GetByte(p) & 0xFF; long b = UnsafeUtil.GetByte(p + 1) & 0xFF; long c = UnsafeUtil.GetByte(p + 2) & 0xFF; long d = UnsafeUtil.GetByte(p + 3) & 0xFF; long e = UnsafeUtil.GetByte(p + 4) & 0xFF; long f = UnsafeUtil.GetByte(p + 5) & 0xFF; long g = UnsafeUtil.GetByte(p + 6) & 0xFF; long h = UnsafeUtil.GetByte(p + 7) & 0xFF; return((h << 56) | (g << 48) | (f << 40) | (e << 32) | (d << 24) | (c << 16) | (b << 8) | a); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test void shouldPutAndGetByteWiseLittleEndianLong() internal virtual void ShouldPutAndGetByteWiseLittleEndianLong() { // GIVEN int sizeInBytes = 8; GlobalMemoryTracker tracker = GlobalMemoryTracker.INSTANCE; long p = allocateMemory(sizeInBytes, tracker); long value = 0b11001100_10101010_10011001_01100110__10001000_01000100_00100010_00010001L; // WHEN UnsafeUtil.PutLongByteWiseLittleEndian(p, value); long readValue = UnsafeUtil.GetLongByteWiseLittleEndian(p); // THEN free(p, sizeInBytes, tracker); assertEquals(value, readValue); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test void shouldPutAndGetByteWiseLittleEndianShort() internal virtual void ShouldPutAndGetByteWiseLittleEndianShort() { // GIVEN int sizeInBytes = 2; GlobalMemoryTracker tracker = GlobalMemoryTracker.INSTANCE; long p = allocateMemory(sizeInBytes, tracker); short value = ( short )0b11001100_10101010; // WHEN UnsafeUtil.PutShortByteWiseLittleEndian(p, value); short readValue = UnsafeUtil.GetShortByteWiseLittleEndian(p); // THEN free(p, sizeInBytes, tracker); assertEquals(value, readValue); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test void shouldAlignMemoryTo4ByteBoundary() internal virtual void ShouldAlignMemoryTo4ByteBoundary() { // GIVEN long allocatedMemory = currentTimeMillis(); int alignBy = 4; // WHEN for (int i = 0; i < 10; i++) { // THEN long alignedMemory = UnsafeUtil.AlignedMemory(allocatedMemory, alignBy); assertTrue(alignedMemory >= allocatedMemory); assertEquals(0, alignedMemory % Integer.BYTES); assertTrue(alignedMemory - allocatedMemory <= 3); allocatedMemory++; } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test void getAddressOfDirectByteBuffer() internal virtual void getAddressOfDirectByteBuffer() { ByteBuffer buf = ByteBuffer.allocateDirect(8); long address = UnsafeUtil.GetDirectByteBufferAddress(buf); long expected = ThreadLocalRandom.current().nextLong(); // Disable native access checking, because UnsafeUtil doesn't know about the memory allocation in the // ByteBuffer.allocateDirect( … ) call. bool nativeAccessCheckEnabled = UnsafeUtil.ExchangeNativeAccessCheckEnabled(false); try { UnsafeUtil.PutLong(address, expected); long actual = buf.Long; assertThat(actual, isOneOf(expected, Long.reverseBytes(expected))); } finally { UnsafeUtil.ExchangeNativeAccessCheckEnabled(nativeAccessCheckEnabled); } }
/// <summary> /// Puts a {@code short} at memory address {@code p} by writing byte for byte, instead of the whole value /// in one go. This can be useful, even necessary in some scenarios where <seealso cref="allowUnalignedMemoryAccess"/> /// is {@code false} and {@code p} isn't aligned properly. Values written with this method should be /// read using <seealso cref="getShortByteWiseLittleEndian(long)"/>. /// </summary> /// <param name="p"> address pointer to start writing at. </param> /// <param name="value"> value to write byte for byte. </param> public static void PutShortByteWiseLittleEndian(long p, short value) { UnsafeUtil.PutByte(p, ( sbyte )value); UnsafeUtil.PutByte(p + 1, ( sbyte )(value >> 8)); }