Unsafe
Contains generic, low-level functionality for manipulating pointers.
using System.Runtime.Versioning;
namespace System.Runtime.CompilerServices
{
public static class Unsafe
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static T Read<T>(void* source)
{
return *(T*)source;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static T ReadUnaligned<T>(void* source)
{
return *(T*)source;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static T ReadUnaligned<T>(ref byte source)
{
return *(T*)(&source);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void Write<T>(void* destination, T value)
{
*(T*)destination = value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void WriteUnaligned<T>(void* destination, T value)
{
*(T*)destination = value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void WriteUnaligned<T>(ref byte destination, T value)
{
*(T*)(&destination) = value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void Copy<T>(void* destination, ref T source)
{
*(T*)destination = source;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void Copy<T>(ref T destination, void* source)
{
destination = *(T*)source;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void* AsPointer<T>(ref T value)
{
return &value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SkipInit<T>(out T value)
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static int SizeOf<T>()
{
return sizeof(T);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.Versioning.NonVersionable]
public unsafe static void CopyBlock(void* destination, void* source, uint byteCount)
{
memcpy(destination, source, byteCount);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.Versioning.NonVersionable]
public static void CopyBlock(ref byte destination, ref byte source, uint byteCount)
{
memcpy(ref destination, ref source, byteCount);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.Versioning.NonVersionable]
public unsafe static void CopyBlockUnaligned(void* destination, void* source, uint byteCount)
{
memcpy(destination, source, byteCount);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.Versioning.NonVersionable]
public static void CopyBlockUnaligned(ref byte destination, ref byte source, uint byteCount)
{
memcpy(ref destination, ref source, byteCount);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.Versioning.NonVersionable]
public unsafe static void InitBlock(void* startAddress, byte value, uint byteCount)
{
memset(startAddress, value, byteCount);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.Versioning.NonVersionable]
public static void InitBlock(ref byte startAddress, byte value, uint byteCount)
{
memset(ref startAddress, value, byteCount);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.Versioning.NonVersionable]
public unsafe static void InitBlockUnaligned(void* startAddress, byte value, uint byteCount)
{
memset(startAddress, value, byteCount);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.Versioning.NonVersionable]
public static void InitBlockUnaligned(ref byte startAddress, byte value, uint byteCount)
{
memset(ref startAddress, value, byteCount);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T As<T>(object o) where T : class
{
return (T)o;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static ref T AsRef<T>(void* source)
{
return ref *(T*)source;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T AsRef<T>([System.Runtime.CompilerServices.IsReadOnly] ref T source)
{
return ref source;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static ref TTo As<TFrom, TTo>(ref TFrom source)
{
return ref *(TTo*)(&source);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T Unbox<T>(object box)
{
return ref (T)box;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static ref T Add<T>(ref T source, int elementOffset)
{
return ref *(T*)(ref source + (long)(IntPtr)(void*)((long)elementOffset * (long)sizeof(T)));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void* Add<T>(void* source, int elementOffset)
{
return (byte*)source + (long)elementOffset * (long)sizeof(T);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static ref T Add<T>(ref T source, IntPtr elementOffset)
{
return ref *(T*)(ref source + (long)(IntPtr)(void*)((long)elementOffset * (long)sizeof(T)));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static ref T AddByteOffset<T>(ref T source, IntPtr byteOffset)
{
return ref *(T*)(ref source + (long)byteOffset);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static ref T Subtract<T>(ref T source, int elementOffset)
{
return ref *(T*)(ref source - (long)(IntPtr)(void*)((long)elementOffset * (long)sizeof(T)));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void* Subtract<T>(void* source, int elementOffset)
{
return (byte*)source - (long)elementOffset * (long)sizeof(T);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static ref T Subtract<T>(ref T source, IntPtr elementOffset)
{
return ref *(T*)(ref source - (long)(IntPtr)(void*)((long)elementOffset * (long)sizeof(T)));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static ref T SubtractByteOffset<T>(ref T source, IntPtr byteOffset)
{
return ref *(T*)(ref source - (long)byteOffset);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IntPtr ByteOffset<T>(ref T origin, ref T target)
{
return (IntPtr)(ref target - ref origin);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool AreSame<T>(ref T left, ref T right)
{
return ref left == ref right;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsAddressGreaterThan<T>(ref T left, ref T right)
{
return ref left > ref right;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsAddressLessThan<T>(ref T left, ref T right)
{
return ref left < ref right;
}
}
}