Class Memory
Low-level methods for direct memory access.
Namespace: DotNext.Runtime.InteropServices
Assembly: DotNext.dll
Syntax
public static class Memory : object
Remarks
Methods in this class doesn't perform any safety check. Incorrect usage of them may destabilize Common Language Runtime.
Methods
| Improve this Doc View SourceAddressOf<T>(T)
Returns address of the managed pointer to type T
.
Declaration
public static IntPtr AddressOf<T>(in T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | Managed pointer to convert into address. |
Returns
Type | Description |
---|---|
IntPtr | The address for the managed pointer. |
Type Parameters
Name | Description |
---|---|
T | Type of managed pointer. |
Remarks
This method converts managed pointer into address, not the address of the object itself.
AreSame<T>(T, T)
Indicates that two managed pointers are equal.
Declaration
public static bool AreSame<T>(in T first, in T second)
Parameters
Type | Name | Description |
---|---|---|
T | first | The first managed pointer. |
T | second | The second managed pointer. |
Returns
Type | Description |
---|---|
Boolean | true, if both managed pointers are equal; otherwise, false. |
Type Parameters
Name | Description |
---|---|
T | Type of managed pointer. |
AsRef<T>(TypedReference)
Converts typed reference into managed pointer.
Declaration
public static T AsRef<T>(this TypedReference reference)
Parameters
Type | Name | Description |
---|---|---|
TypedReference | reference | The typed reference. |
Returns
Type | Description |
---|---|
T | A managed pointer to the value represented by reference. |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
AsSpan<T>(T*)
Converts contiguous memory identified by the specified pointer
into
Declaration
public static Span<byte> AsSpan<T>(T*pointer)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
T* | pointer | The typed pointer. |
Returns
Type | Description |
---|---|
DotNext.Span<Byte> | The span of contiguous memory. |
Type Parameters
Name | Description |
---|---|
T | The type of the pointer. |
ClearBits(IntPtr, Int64)
Sets all bits of allocated memory to zero.
Declaration
public static void ClearBits(IntPtr address, long length)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | address | The pointer to the memory to be cleared. |
Int64 | length | The length of the memory to be cleared, in bytes. |
Remarks
This method has the same behavior as
ClearBits(Void*, Int64)
Sets all bits of allocated memory to zero.
Declaration
public static void ClearBits(void *address, long length)
Parameters
Type | Name | Description |
---|---|---|
Void* | address | The pointer to the memory to be cleared. |
Int64 | length | The length of the memory to be cleared, in bytes. |
Compare(IntPtr, IntPtr, Int64)
Bitwise comparison of two memory blocks.
Declaration
public static int Compare(IntPtr first, IntPtr second, long length)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | first | The pointer to the first memory block. |
IntPtr | second | The pointer to the second memory block. |
Int64 | length | The length of the first and second memory blocks. |
Returns
Type | Description |
---|---|
Int32 | Comparison result which has the semantics as return type of |
Compare(Void*, Void*, Int64)
Bitwise comparison of two memory blocks.
Declaration
public static int Compare(void *first, void *second, long length)
Parameters
Type | Name | Description |
---|---|---|
Void* | first | The pointer to the first memory block. |
Void* | second | The pointer to the second memory block. |
Int64 | length | The length of the first and second memory blocks. |
Returns
Type | Description |
---|---|
Int32 | Comparison result which has the semantics as return type of |
Copy(IntPtr, IntPtr, Int64)
Copies the specified number of bytes from one address in memory to another.
Declaration
public static void Copy(IntPtr source, IntPtr destination, long length)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | source | The address of the bytes to copy. |
IntPtr | destination | The target address. |
Int64 | length | The number of bytes to copy from source address to destination. |
Copy(Void*, Void*, Int64)
Copies the specified number of bytes from one address in memory to another.
Declaration
public static void Copy(void *source, void *destination, long length)
Parameters
Type | Name | Description |
---|---|---|
Void* | source | The address of the bytes to copy. |
Void* | destination | The target address. |
Int64 | length | The number of bytes to copy from source address to destination. |
Copy<T>(T*, T*)
Copies one value into another.
Declaration
public static void Copy<T>(T*input, T*output)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
T* | input | The reference to the source location. |
T* | output | The reference to the destination location. |
Type Parameters
Name | Description |
---|---|
T | The value type to copy. |
Copy<T>(T, out T)
Copies one value into another.
Declaration
public static void Copy<T>(in T input, out T output)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
T | input | The reference to the source location. |
T | output | The reference to the destination location. |
Type Parameters
Name | Description |
---|---|
T | The value type to copy. |
Copy<T>(ref T, ref T, UInt32)
Copies the specified number of elements from source address to the destination address.
Declaration
public static void Copy<T>(ref T source, ref T destination, uint count)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
T | source | The address of the bytes to copy. |
T | destination | The target address. |
UInt32 | count | The number of elements to copy. |
Type Parameters
Name | Description |
---|---|
T | The type of the element. |
Equals(IntPtr, IntPtr, Int64)
Computes equality between two blocks of memory.
Declaration
public static bool Equals(IntPtr first, IntPtr second, long length)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | first | A pointer to the first memory block. |
IntPtr | second | A pointer to the second memory block. |
Int64 | length | Length of first and second memory blocks, in bytes. |
Returns
Type | Description |
---|---|
Boolean | true, if both memory blocks have the same data; otherwise, false. |
Equals(Void*, Void*, Int64)
Computes equality between two blocks of memory.
Declaration
public static bool Equals(void *first, void *second, long length)
Parameters
Type | Name | Description |
---|---|---|
Void* | first | A pointer to the first memory block. |
Void* | second | A pointer to the second memory block. |
Int64 | length | Length of first and second memory blocks, in bytes. |
Returns
Type | Description |
---|---|
Boolean | true, if both memory blocks have the same data; otherwise, false. |
GetHashCode32(IntPtr, Int64, Boolean)
Computes 32-bit hash code for the block of memory.
Declaration
public static int GetHashCode32(IntPtr source, long length, bool salted = true)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | source | A pointer to the block of memory. |
Int64 | length | Length of memory block to be hashed, in bytes. |
Boolean | salted | true to include randomized salt data into hashing; false to use data from memory only. |
Returns
Type | Description |
---|---|
Int32 | Content hash code. |
Remarks
This method uses FNV-1a hash algorithm.
See Also
| Improve this Doc View SourceGetHashCode32(IntPtr, Int64, Int32, Func<Int32, Int32, Int32>, Boolean)
Computes 32-bit hash code for the block of memory.
Declaration
public static int GetHashCode32(IntPtr source, long length, int hash, Func<int, int, int> hashFunction, bool salted = true)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | source | A pointer to the block of memory. |
Int64 | length | Length of memory block to be hashed, in bytes. |
Int32 | hash | Initial value of the hash. |
DotNext.Func<Int32, Int32, Int32> | hashFunction | Hashing function. |
Boolean | salted | true to include randomized salt data into hashing; false to use data from memory only. |
Returns
Type | Description |
---|---|
Int32 | Hash code of the memory block. |
Remarks
This method may give different value each time you run the program for
the same data. To disable this behavior, pass false to salted
.
GetHashCode32(IntPtr, Int64, Int32, ValueFunc<Int32, Int32, Int32>, Boolean)
Computes 32-bit hash code for the block of memory.
Declaration
public static int GetHashCode32(IntPtr source, long length, int hash, in ValueFunc<int, int, int> hashFunction, bool salted = true)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | source | A pointer to the block of memory. |
Int64 | length | Length of memory block to be hashed, in bytes. |
Int32 | hash | Initial value of the hash. |
ValueFunc<Int32, Int32, Int32> | hashFunction | Hashing function. |
Boolean | salted | true to include randomized salt data into hashing; false to use data from memory only. |
Returns
Type | Description |
---|---|
Int32 | Hash code of the memory block. |
Remarks
This method may give different value each time you run the program for
the same data. To disable this behavior, pass false to salted
.
GetHashCode32(Void*, Int64, Boolean)
Computes 32-bit hash code for the block of memory.
Declaration
public static int GetHashCode32(void *source, long length, bool salted = true)
Parameters
Type | Name | Description |
---|---|---|
Void* | source | A pointer to the block of memory. |
Int64 | length | Length of memory block to be hashed, in bytes. |
Boolean | salted | true to include randomized salt data into hashing; false to use data from memory only. |
Returns
Type | Description |
---|---|
Int32 | Content hash code. |
Remarks
This method uses FNV-1a hash algorithm.
See Also
| Improve this Doc View SourceGetHashCode32(Void*, Int64, Int32, Func<Int32, Int32, Int32>, Boolean)
Computes 32-bit hash code for the block of memory.
Declaration
public static int GetHashCode32(void *source, long length, int hash, Func<int, int, int> hashFunction, bool salted = true)
Parameters
Type | Name | Description |
---|---|---|
Void* | source | A pointer to the block of memory. |
Int64 | length | Length of memory block to be hashed, in bytes. |
Int32 | hash | Initial value of the hash. |
DotNext.Func<Int32, Int32, Int32> | hashFunction | Hashing function. |
Boolean | salted | true to include randomized salt data into hashing; false to use data from memory only. |
Returns
Type | Description |
---|---|
Int32 | Hash code of the memory block. |
Remarks
This method may give different value each time you run the program for
the same data. To disable this behavior, pass false to salted
.
GetHashCode32(Void*, Int64, Int32, ValueFunc<Int32, Int32, Int32>, Boolean)
Computes 32-bit hash code for the block of memory.
Declaration
public static int GetHashCode32(void *source, long length, int hash, in ValueFunc<int, int, int> hashFunction, bool salted = true)
Parameters
Type | Name | Description |
---|---|---|
Void* | source | A pointer to the block of memory. |
Int64 | length | Length of memory block to be hashed, in bytes. |
Int32 | hash | Initial value of the hash. |
ValueFunc<Int32, Int32, Int32> | hashFunction | Hashing function. |
Boolean | salted | true to include randomized salt data into hashing; false to use data from memory only. |
Returns
Type | Description |
---|---|
Int32 | Hash code of the memory block. |
Remarks
This method may give different value each time you run the program for
the same data. To disable this behavior, pass false to salted
.
GetHashCode64(IntPtr, Int64, Boolean)
Computes 64-bit hash code for the block of memory.
Declaration
public static long GetHashCode64(IntPtr source, long length, bool salted = true)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | source | A pointer to the block of memory. |
Int64 | length | Length of memory block to be hashed, in bytes. |
Boolean | salted | true to include randomized salt data into hashing; false to use data from memory only. |
Returns
Type | Description |
---|---|
Int64 | Content hash code. |
Remarks
This method uses FNV-1a hash algorithm.
See Also
| Improve this Doc View SourceGetHashCode64(IntPtr, Int64, Int64, Func<Int64, Int64, Int64>, Boolean)
Computes 64-bit hash code for the block of memory, 64-bit version.
Declaration
public static long GetHashCode64(IntPtr source, long length, long hash, Func<long, long, long> hashFunction, bool salted = true)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | source | A pointer to the block of memory. |
Int64 | length | Length of memory block to be hashed, in bytes. |
Int64 | hash | Initial value of the hash. |
DotNext.Func<Int64, Int64, Int64> | hashFunction | Hashing function. |
Boolean | salted | true to include randomized salt data into hashing; false to use data from memory only. |
Returns
Type | Description |
---|---|
Int64 | Hash code of the memory block. |
Remarks
This method may give different value each time you run the program for
the same data. To disable this behavior, pass false to salted
.
GetHashCode64(IntPtr, Int64, Int64, ValueFunc<Int64, Int64, Int64>, Boolean)
Computes 64-bit hash code for the block of memory, 64-bit version.
Declaration
public static long GetHashCode64(IntPtr source, long length, long hash, in ValueFunc<long, long, long> hashFunction, bool salted = true)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | source | A pointer to the block of memory. |
Int64 | length | Length of memory block to be hashed, in bytes. |
Int64 | hash | Initial value of the hash. |
ValueFunc<Int64, Int64, Int64> | hashFunction | Hashing function. |
Boolean | salted | true to include randomized salt data into hashing; false to use data from memory only. |
Returns
Type | Description |
---|---|
Int64 | Hash code of the memory block. |
Remarks
This method may give different value each time you run the program for
the same data. To disable this behavior, pass false to salted
.
GetHashCode64(Void*, Int64, Boolean)
Computes 64-bit hash code for the block of memory.
Declaration
public static long GetHashCode64(void *source, long length, bool salted = true)
Parameters
Type | Name | Description |
---|---|---|
Void* | source | A pointer to the block of memory. |
Int64 | length | Length of memory block to be hashed, in bytes. |
Boolean | salted | true to include randomized salt data into hashing; false to use data from memory only. |
Returns
Type | Description |
---|---|
Int64 | Content hash code. |
Remarks
This method uses FNV-1a hash algorithm.
See Also
| Improve this Doc View SourceGetHashCode64(Void*, Int64, Int64, Func<Int64, Int64, Int64>, Boolean)
Computes 64-bit hash code for the block of memory, 64-bit version.
Declaration
public static long GetHashCode64(void *source, long length, long hash, Func<long, long, long> hashFunction, bool salted = true)
Parameters
Type | Name | Description |
---|---|---|
Void* | source | A pointer to the block of memory. |
Int64 | length | Length of memory block to be hashed, in bytes. |
Int64 | hash | Initial value of the hash. |
DotNext.Func<Int64, Int64, Int64> | hashFunction | Hashing function. |
Boolean | salted | true to include randomized salt data into hashing; false to use data from memory only. |
Returns
Type | Description |
---|---|
Int64 | Hash code of the memory block. |
Remarks
This method may give different value each time you run the program for
the same data. To disable this behavior, pass false to salted
.
GetHashCode64(Void*, Int64, Int64, ValueFunc<Int64, Int64, Int64>, Boolean)
Computes 64-bit hash code for the block of memory, 64-bit version.
Declaration
public static long GetHashCode64(void *source, long length, long hash, in ValueFunc<long, long, long> hashFunction, bool salted = true)
Parameters
Type | Name | Description |
---|---|---|
Void* | source | A pointer to the block of memory. |
Int64 | length | Length of memory block to be hashed, in bytes. |
Int64 | hash | Initial value of the hash. |
ValueFunc<Int64, Int64, Int64> | hashFunction | Hashing function. |
Boolean | salted | true to include randomized salt data into hashing; false to use data from memory only. |
Returns
Type | Description |
---|---|
Int64 | Hash code of the memory block. |
Remarks
This method may give different value each time you run the program for
the same data. To disable this behavior, pass false to salted
.
GetReadonlyRef<T>(T[], Int64)
Gets a reference to the array element with restricted mutability.
Declaration
public static T GetReadonlyRef<T>(this T[] array, long index)
Parameters
Type | Name | Description |
---|---|---|
T[] | array | The array object. |
Int64 | index | The index of the array element. |
Returns
Type | Description |
---|---|
T | The reference to the array element with restricted mutability. |
Type Parameters
Name | Description |
---|---|
T | The type of array elements. |
IsNull<T>(T)
Determines whether the specified managed pointer is null.
Declaration
public static bool IsNull<T>(in T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The managed pointer to check. |
Returns
Type | Description |
---|---|
Boolean | true, if the specified managed pointer is null; otherwise, false. |
Type Parameters
Name | Description |
---|---|
T | The type of the managed pointer. |
Read<T>(ref IntPtr)
Reads a value of type T
from the given location
and adjust pointer according with size of type T
.
Declaration
public static T Read<T>(ref IntPtr source)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
IntPtr | source | A pointer to block of memory. |
Returns
Type | Description |
---|---|
T | Dereferenced value. |
Type Parameters
Name | Description |
---|---|
T | Unmanaged type to dereference. |
ReadString(ref IntPtr)
Reads UTF-16 encoded string from the specified memory location and adjust the pointer.
Declaration
public static string ReadString(ref IntPtr source)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | source | The pointer to the memory location containing null-terminated characters. |
Returns
Type | Description |
---|---|
String | The string decoded from the memory. |
ReadUnaligned<T>(ref IntPtr)
Reads a value of type T
from the given location
without assuming architecture dependent alignment of the addresses;
and adjust pointer according with size of type T
.
Declaration
public static T ReadUnaligned<T>(ref IntPtr source)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
IntPtr | source | A pointer to block of memory. |
Returns
Type | Description |
---|---|
T | Dereferenced value. |
Type Parameters
Name | Description |
---|---|
T | Unmanaged type to dereference. |
Swap<T>(T*, T*)
Swaps two values.
Declaration
public static void Swap<T>(T*first, T*second)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
T* | first | The first value to be replaced with |
T* | second | The second value to be replaced with |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
Swap<T>(ref T, ref T)
Swaps two values.
Declaration
public static void Swap<T>(ref T first, ref T second)
Parameters
Type | Name | Description |
---|---|---|
T | first | The first value to be replaced with |
T | second | The second value to be replaced with |
Type Parameters
Name | Description |
---|---|
T | The type of the value. |
ThrowIfNull<T>(T)
Throws
Declaration
public static void ThrowIfNull<T>(in T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The managed pointer to check. |
Type Parameters
Name | Description |
---|---|
T | The type of the managed pointer. |
ToPointer<T>(IntPtr)
Converts the value of this instance to a pointer of the specified type.
Declaration
public static T*ToPointer<T>(this IntPtr source)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
IntPtr | source | The value to be converted into pointer. |
Returns
Type | Description |
---|---|
T* | The typed pointer. |
Type Parameters
Name | Description |
---|---|
T | The type of the pointer. |
ToPointer<T>(UIntPtr)
Converts the value of this instance to a pointer of the specified type.
Declaration
public static T*ToPointer<T>(this UIntPtr source)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
UIntPtr | source | The value to be converted into pointer. |
Returns
Type | Description |
---|---|
T* | The typed pointer. |
Type Parameters
Name | Description |
---|---|
T | The type of the pointer. |
Write<T>(ref IntPtr, T)
Writes a value into the address using aligned access
and adjust address according with size of
type T
.
Declaration
public static void Write<T>(ref IntPtr destination, T value)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
IntPtr | destination | Destination address. |
T | value | The value to write into the address. |
Type Parameters
Name | Description |
---|---|
T | Unmanaged type. |
WriteString(ref IntPtr, ReadOnlySpan<Char>)
Writes the characters in UTF-16 encoding to the specified address in the memory and adjust the pointer.
Declaration
public static void WriteString(ref IntPtr destination, ReadOnlySpan<char> str)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | destination | The location of in the memory. |
ReadOnlySpan<Char> | str | The characters to be written into the memory. |
Remarks
This method encodes the characters as null-terminated string.
WriteString(ref IntPtr, String)
Writes the string in UTF-16 encoding to the specified address in the memory and adjust the pointer.
Declaration
public static void WriteString(ref IntPtr destination, string str)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | destination | The location of in the memory. |
String | str | The string to be written into the memory. |
WriteUnaligned<T>(ref IntPtr, T)
Writes a value into the address using unaligned access
and adjust address according with size of
type T
.
Declaration
public static void WriteUnaligned<T>(ref IntPtr destination, T value)
where T : struct
Parameters
Type | Name | Description |
---|---|---|
IntPtr | destination | Destination address. |
T | value | The value to write into the address. |
Type Parameters
Name | Description |
---|---|
T | Unmanaged type. |