public char ReadChar(Int64 position)
{
- int sizeOfType = sizeof(char);
- EnsureSafeToRead(position, sizeOfType);
+ EnsureSafeToRead(position, sizeof(char));
char result;
-
unsafe
{
byte* pointer = null;
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- result = *((char*)(pointer));
-#if ALIGN_ACCESS
- }
- else {
- result = (char)( *pointer | *(pointer + 1) << 8 ) ;
- }
-#endif
+ result = Unsafe.ReadUnaligned<char>(pointer + _offset + position);
}
finally
{
// See comment above.
public Int16 ReadInt16(Int64 position)
{
- int sizeOfType = sizeof(Int16);
- EnsureSafeToRead(position, sizeOfType);
+ EnsureSafeToRead(position, sizeof(Int16));
Int16 result;
-
unsafe
{
byte* pointer = null;
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- result = *((Int16*)(pointer));
-#if ALIGN_ACCESS
- }
- else {
- result = (Int16)( *pointer | *(pointer + 1) << 8 );
- }
-#endif
+ result = Unsafe.ReadUnaligned<Int16>(pointer + _offset + position);
}
finally
{
public Int32 ReadInt32(Int64 position)
{
- int sizeOfType = sizeof(Int32);
- EnsureSafeToRead(position, sizeOfType);
+ EnsureSafeToRead(position, sizeof(Int32));
Int32 result;
unsafe
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- result = *((Int32*)(pointer));
-#if ALIGN_ACCESS
- }
- else {
- result = (Int32)( *pointer | *(pointer + 1) << 8 | *(pointer + 2) << 16 | *(pointer + 3) << 24 );
- }
-#endif
+ result = Unsafe.ReadUnaligned<Int32>(pointer + _offset + position);
}
finally
{
public Int64 ReadInt64(Int64 position)
{
- int sizeOfType = sizeof(Int64);
- EnsureSafeToRead(position, sizeOfType);
+ EnsureSafeToRead(position, sizeof(Int64));
Int64 result;
unsafe
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- result = *((Int64*)(pointer));
-#if ALIGN_ACCESS
- }
- else {
- int lo = *pointer | *(pointer + 1) << 8 | *(pointer + 2) << 16 | *(pointer + 3) << 24;
- int hi = *(pointer + 4) | *(pointer + 5) << 8 | *(pointer + 6) << 16 | *(pointer + 7) << 24;
- result = (Int64)(((Int64)hi << 32) | (UInt32)lo);
- }
-#endif
+ result = Unsafe.ReadUnaligned<Int64>(pointer + _offset + position);
}
finally
{
return result;
}
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private unsafe Int32 UnsafeReadInt32(byte* pointer)
- {
- Int32 result;
- // check if pointer is aligned
- if (((int)pointer & (sizeof(Int32) - 1)) == 0)
- {
- result = *((Int32*)pointer);
- }
- else
- {
- result = (Int32)(*(pointer) | *(pointer + 1) << 8 | *(pointer + 2) << 16 | *(pointer + 3) << 24);
- }
-
- return result;
- }
public Decimal ReadDecimal(Int64 position)
{
const int ScaleMask = 0x00FF0000;
const int SignMask = unchecked((int)0x80000000);
- int sizeOfType = sizeof(Decimal);
- EnsureSafeToRead(position, sizeOfType);
+ EnsureSafeToRead(position, sizeof(Decimal));
+
+ int lo, mid, hi, flags;
unsafe
{
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
- int lo = UnsafeReadInt32(pointer);
- int mid = UnsafeReadInt32(pointer + 4);
- int hi = UnsafeReadInt32(pointer + 8);
- int flags = UnsafeReadInt32(pointer + 12);
-
- // Check for invalid Decimal values
- if (!((flags & ~(SignMask | ScaleMask)) == 0 && (flags & ScaleMask) <= (28 << 16)))
- {
- throw new ArgumentException(Environment.GetResourceString("Arg_BadDecimal")); // Throw same Exception type as Decimal(int[]) ctor for compat
- }
-
- bool isNegative = (flags & SignMask) != 0;
- byte scale = (byte)(flags >> 16);
-
- return new decimal(lo, mid, hi, isNegative, scale);
+
+ lo = Unsafe.ReadUnaligned<Int32>(pointer + _offset + position);
+ mid = Unsafe.ReadUnaligned<Int32>(pointer + _offset + position + 4);
+ hi = Unsafe.ReadUnaligned<Int32>(pointer + _offset + position + 8);
+ flags = Unsafe.ReadUnaligned<Int32>(pointer + _offset + position + 12);
}
finally
{
}
}
}
+
+ // Check for invalid Decimal values
+ if (!((flags & ~(SignMask | ScaleMask)) == 0 && (flags & ScaleMask) <= (28 << 16)))
+ {
+ throw new ArgumentException(Environment.GetResourceString("Arg_BadDecimal")); // Throw same Exception type as Decimal(int[]) ctor for compat
+ }
+
+ bool isNegative = (flags & SignMask) != 0;
+ byte scale = (byte)(flags >> 16);
+
+ return new decimal(lo, mid, hi, isNegative, scale);
}
public Single ReadSingle(Int64 position)
{
- int sizeOfType = sizeof(Single);
- EnsureSafeToRead(position, sizeOfType);
+ EnsureSafeToRead(position, sizeof(Single));
Single result;
unsafe
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- result = BitConverter.Int32BitsToSingle(*((int*)(pointer)));
-#if ALIGN_ACCESS
- }
- else {
- UInt32 tempResult = (UInt32)( *pointer | *(pointer + 1) << 8 | *(pointer + 2) << 16 | *(pointer + 3) << 24 );
- result = *((float*)&tempResult);
- }
-#endif
+ result = Unsafe.ReadUnaligned<Single>(pointer + _offset + position);
}
finally
{
public Double ReadDouble(Int64 position)
{
- int sizeOfType = sizeof(Double);
- EnsureSafeToRead(position, sizeOfType);
+ EnsureSafeToRead(position, sizeof(Double));
Double result;
unsafe
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- result = BitConverter.Int64BitsToDouble(*((long*)(pointer)));
-#if ALIGN_ACCESS
- }
- else {
-
- UInt32 lo = (UInt32)( *pointer | *(pointer + 1) << 8 | *(pointer + 2) << 16 | *(pointer + 3) << 24 );
- UInt32 hi = (UInt32)( *(pointer + 4) | *(pointer + 5) << 8 | *(pointer + 6) << 16 | *(pointer + 7) << 24 );
- UInt64 tempResult = ((UInt64)hi) << 32 | lo;
- result = *((double*)&tempResult);
-
- }
-#endif
+ result = Unsafe.ReadUnaligned<Double>(pointer + _offset + position);
}
finally
{
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
- result = *((SByte*)pointer);
+ result = *((SByte*)(pointer + _offset + position));
}
finally
{
[CLSCompliant(false)]
public UInt16 ReadUInt16(Int64 position)
{
- int sizeOfType = sizeof(UInt16);
- EnsureSafeToRead(position, sizeOfType);
+ EnsureSafeToRead(position, sizeof(UInt16));
UInt16 result;
unsafe
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- result = *((UInt16*)(pointer));
-#if ALIGN_ACCESS
- }
- else {
- result = (UInt16)( *pointer | *(pointer + 1) << 8 );
- }
-#endif
-
+ result = Unsafe.ReadUnaligned<UInt16>(pointer + _offset + position);
}
finally
{
[CLSCompliant(false)]
public UInt32 ReadUInt32(Int64 position)
{
- int sizeOfType = sizeof(UInt32);
- EnsureSafeToRead(position, sizeOfType);
+ EnsureSafeToRead(position, sizeof(UInt32));
UInt32 result;
unsafe
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- result = *((UInt32*)(pointer));
-#if ALIGN_ACCESS
- }
- else {
- result = (UInt32)( *pointer | *(pointer + 1) << 8 | *(pointer + 2) << 16 | *(pointer + 3) << 24 );
- }
-#endif
-
+ result = Unsafe.ReadUnaligned<UInt32>(pointer + _offset + position);
}
finally
{
[CLSCompliant(false)]
public UInt64 ReadUInt64(Int64 position)
{
- int sizeOfType = sizeof(UInt64);
- EnsureSafeToRead(position, sizeOfType);
+ EnsureSafeToRead(position, sizeof(UInt64));
UInt64 result;
unsafe
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- result = *((UInt64*)(pointer));
-#if ALIGN_ACCESS
- }
- else {
- UInt32 lo = (UInt32)( *pointer | *(pointer + 1) << 8 | *(pointer + 2) << 16 | *(pointer + 3) << 24 );
- UInt32 hi = (UInt32)( *(pointer + 4) | *(pointer + 5) << 8 | *(pointer + 6) << 16 | *(pointer + 7) << 24 );
- result = (UInt64)(((UInt64)hi << 32) | lo );
- }
-#endif
-
+ result = Unsafe.ReadUnaligned<UInt64>(pointer + _offset + position);
}
finally
{
public void Write(Int64 position, char value)
{
- int sizeOfType = sizeof(char);
- EnsureSafeToWrite(position, sizeOfType);
+ EnsureSafeToWrite(position, sizeof(char));
unsafe
{
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- *((char*)pointer) = value;
-#if ALIGN_ACCESS
- }
- else {
- *(pointer) = (byte)value;
- *(pointer+1) = (byte)(value >> 8);
- }
-#endif
+ Unsafe.WriteUnaligned<char>(pointer + _offset + position, value);
}
finally
{
public void Write(Int64 position, Int16 value)
{
- int sizeOfType = sizeof(Int16);
- EnsureSafeToWrite(position, sizeOfType);
+ EnsureSafeToWrite(position, sizeof(Int16));
unsafe
{
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- *((Int16*)pointer) = value;
-#if ALIGN_ACCESS
- }
- else {
- *(pointer) = (byte)value;
- *(pointer + 1) = (byte)(value >> 8);
- }
-#endif
+ Unsafe.WriteUnaligned<Int16>(pointer + _offset + position, value);
}
finally
{
public void Write(Int64 position, Int32 value)
{
- int sizeOfType = sizeof(Int32);
- EnsureSafeToWrite(position, sizeOfType);
+ EnsureSafeToWrite(position, sizeof(Int32));
unsafe
{
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- *((Int32*)pointer) = value;
-#if ALIGN_ACCESS
- }
- else {
- *(pointer) = (byte)value;
- *(pointer + 1) = (byte)(value >> 8);
- *(pointer + 2) = (byte)(value >> 16);
- *(pointer + 3) = (byte)(value >> 24);
- }
-#endif
+ Unsafe.WriteUnaligned<Int32>(pointer + _offset + position, value);
}
finally
{
public void Write(Int64 position, Int64 value)
{
- int sizeOfType = sizeof(Int64);
- EnsureSafeToWrite(position, sizeOfType);
+ EnsureSafeToWrite(position, sizeof(Int64));
unsafe
{
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- *((Int64*)pointer) = value;
-#if ALIGN_ACCESS
- }
- else {
- *(pointer) = (byte)value;
- *(pointer + 1) = (byte)(value >> 8);
- *(pointer + 2) = (byte)(value >> 16);
- *(pointer + 3) = (byte)(value >> 24);
- *(pointer + 4) = (byte)(value >> 32);
- *(pointer + 5) = (byte)(value >> 40);
- *(pointer + 6) = (byte)(value >> 48);
- *(pointer + 7) = (byte)(value >> 56);
- }
-#endif
+ Unsafe.WriteUnaligned<Int64>(pointer + _offset + position, value);
}
finally
{
}
}
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private unsafe void UnsafeWriteInt32(byte* pointer, Int32 value)
- {
- // check if pointer is aligned
- if (((int)pointer & (sizeof(Int32) - 1)) == 0)
- {
- *((Int32*)pointer) = value;
- }
- else
- {
- *(pointer) = (byte)value;
- *(pointer + 1) = (byte)(value >> 8);
- *(pointer + 2) = (byte)(value >> 16);
- *(pointer + 3) = (byte)(value >> 24);
- }
- }
-
public void Write(Int64 position, Decimal value)
{
- int sizeOfType = sizeof(Decimal);
- EnsureSafeToWrite(position, sizeOfType);
+ EnsureSafeToWrite(position, sizeof(Decimal));
unsafe
{
+ int* valuePtr = (int*)(&value);
+ int flags = *valuePtr;
+ int hi = *(valuePtr + 1);
+ int lo = *(valuePtr + 2);
+ int mid = *(valuePtr + 3);
+
byte* pointer = null;
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
- int* valuePtr = (int*)(&value);
- int flags = *valuePtr;
- int hi = *(valuePtr + 1);
- int lo = *(valuePtr + 2);
- int mid = *(valuePtr + 3);
-
- UnsafeWriteInt32(pointer, lo);
- UnsafeWriteInt32(pointer + 4, mid);
- UnsafeWriteInt32(pointer + 8, hi);
- UnsafeWriteInt32(pointer + 12, flags);
+
+ Unsafe.WriteUnaligned<Int32>(pointer + _offset + position, lo);
+ Unsafe.WriteUnaligned<Int32>(pointer + _offset + position + 4, mid);
+ Unsafe.WriteUnaligned<Int32>(pointer + _offset + position + 8, hi);
+ Unsafe.WriteUnaligned<Int32>(pointer + _offset + position + 12, flags);
}
finally
{
public void Write(Int64 position, Single value)
{
- int sizeOfType = sizeof(Single);
- EnsureSafeToWrite(position, sizeOfType);
+ EnsureSafeToWrite(position, sizeof(Single));
unsafe
{
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- *((int*)pointer) = BitConverter.SingleToInt32Bits(value);
-#if ALIGN_ACCESS
- }
- else {
- UInt32 tmpValue = *(UInt32*)&value;
- *(pointer) = (byte)tmpValue;
- *(pointer + 1) = (byte)(tmpValue >> 8);
- *(pointer + 2) = (byte)(tmpValue >> 16);
- *(pointer + 3) = (byte)(tmpValue >> 24);
-
- }
-#endif
+ Unsafe.WriteUnaligned<Single>(pointer + _offset + position, value);
}
finally
{
public void Write(Int64 position, Double value)
{
- int sizeOfType = sizeof(Double);
- EnsureSafeToWrite(position, sizeOfType);
+ EnsureSafeToWrite(position, sizeof(Double));
unsafe
{
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- *((long*)pointer) = BitConverter.DoubleToInt64Bits(value);
-#if ALIGN_ACCESS
- }
- else {
- UInt64 tmpValue = *(UInt64 *)&value;
- *(pointer) = (byte) tmpValue;
- *(pointer + 1) = (byte) (tmpValue >> 8);
- *(pointer + 2) = (byte) (tmpValue >> 16);
- *(pointer + 3) = (byte) (tmpValue >> 24);
- *(pointer + 4) = (byte) (tmpValue >> 32);
- *(pointer + 5) = (byte) (tmpValue >> 40);
- *(pointer + 6) = (byte) (tmpValue >> 48);
- *(pointer + 7) = (byte) (tmpValue >> 56);
-
- }
-#endif
+ Unsafe.WriteUnaligned<Double>(pointer + _offset + position, value);
}
finally
{
[CLSCompliant(false)]
public void Write(Int64 position, SByte value)
{
- int sizeOfType = sizeof(SByte);
- EnsureSafeToWrite(position, sizeOfType);
+ EnsureSafeToWrite(position, sizeof(SByte));
unsafe
{
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
- *((SByte*)pointer) = value;
+ *((SByte*)(pointer + _offset + position)) = value;
}
finally
{
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- *((UInt16*)pointer) = value;
-#if ALIGN_ACCESS
- }
- else {
- *(pointer) = (byte)value;
- *(pointer + 1) = (byte)(value >> 8);
- }
-#endif
+ Unsafe.WriteUnaligned<UInt16>(pointer + _offset + position, value);
}
finally
{
[CLSCompliant(false)]
public void Write(Int64 position, UInt32 value)
{
- int sizeOfType = sizeof(UInt32);
- EnsureSafeToWrite(position, sizeOfType);
+ EnsureSafeToWrite(position, sizeof(UInt32));
unsafe
{
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- *((UInt32*)pointer) = value;
-#if ALIGN_ACCESS
- }
- else {
- *(pointer) = (byte)value;
- *(pointer + 1) = (byte)(value >> 8);
- *(pointer + 2) = (byte)(value >> 16);
- *(pointer + 3) = (byte)(value >> 24);
- }
-#endif
-
+ Unsafe.WriteUnaligned<UInt32>(pointer + _offset + position, value);
}
finally
{
[CLSCompliant(false)]
public void Write(Int64 position, UInt64 value)
{
- int sizeOfType = sizeof(UInt64);
- EnsureSafeToWrite(position, sizeOfType);
+ EnsureSafeToWrite(position, sizeof(UInt64));
unsafe
{
try
{
_buffer.AcquirePointer(ref pointer);
- pointer += (_offset + position);
-#if ALIGN_ACCESS
- // check if pointer is aligned
- if (((int)pointer & (sizeOfType - 1)) == 0) {
-#endif
- *((UInt64*)pointer) = value;
-#if ALIGN_ACCESS
- }
- else {
- *(pointer) = (byte)value;
- *(pointer + 1) = (byte)(value >> 8);
- *(pointer + 2) = (byte)(value >> 16);
- *(pointer + 3) = (byte)(value >> 24);
- *(pointer + 4) = (byte)(value >> 32);
- *(pointer + 5) = (byte)(value >> 40);
- *(pointer + 6) = (byte)(value >> 48);
- *(pointer + 7) = (byte)(value >> 56);
- }
-#endif
-
+ Unsafe.WriteUnaligned<UInt64>(pointer + _offset + position, value);
}
finally
{
try
{
_buffer.AcquirePointer(ref pointer);
- result = *((byte*)(pointer + _offset + position));
+ result = *(pointer + _offset + position);
}
finally
{
try
{
_buffer.AcquirePointer(ref pointer);
- *((byte*)(pointer + _offset + position)) = value;
+ *(pointer + _offset + position) = value;
}
finally
{