Class MemoryUtil
- Namespace
- SharpPluginLoader.Core.Memory
- Assembly
- SharpPluginLoader.Core.dll
Provides low-level memory reading and writing methods.
public static class MemoryUtil
- Inheritance
-
MemoryUtil
- Inherited Members
Methods
AddressOf<T>(ref T)
Gets the address of a given reference.
public static nint AddressOf<T>(ref T value) where T : unmanaged
Parameters
valueTThe reference to convert
Returns
- nint
An address that points to
value
Type Parameters
TThe type of the value
Alloc(long)
Allocates a block of native memory of the specified size.
public static nint Alloc(long size)
Parameters
sizelongThe size of the block to allocate
Returns
- nint
The address of the allocated block
Remarks
Any memory allocated using this method must be freed using either Free(nint) or Free(void*). to avoid memory leaks.
Alloc<T>(long)
Allocates an array of type T[] of the specified size in native memory.
public static T* Alloc<T>(long count = 1) where T : unmanaged
Parameters
countlongThe size of the array
Returns
- T*
The address of the allocated array
Type Parameters
TThe type of the array
Remarks
Generally, if you need to allocate an array of native memory, you should use Create(int, IAllocator?). If you do use this method, you must free the memory using Free(nint) or Free(void*).
AsPointer<T>(ref T)
Converts a given reference to a pointer.
public static T* AsPointer<T>(ref T value) where T : unmanaged
Parameters
valueTThe reference to convert
Returns
- T*
A pointer of type
T* that points tovalue
Type Parameters
TThe type of the value
AsRef<T>(T*)
Converts a given pointer to a reference.
public static ref T AsRef<T>(T* address) where T : unmanaged
Parameters
addressT*The pointer to convert
Returns
- T
A reference of type
Tthat points toaddress
Type Parameters
TThe type of the value
AsSpan<T>(long, int)
Creates a span around a native array from a given address and count.
public static Span<T> AsSpan<T>(long address, int count) where T : unmanaged
Parameters
Returns
- Span<T>
A new span that represents the native array
Type Parameters
TThe type of the array
AsSpan<T>(nint, int)
Creates a span around a native array from a given address and count.
public static Span<T> AsSpan<T>(nint address, int count) where T : unmanaged
Parameters
Returns
- Span<T>
A new span that represents the native array
Type Parameters
TThe type of the array
Copy(long, long, long)
Copies a block of memory from one address to another.
public static void Copy(long source, long destination, long count)
Parameters
sourcelongThe source address
destinationlongThe destination address
countlongThe number of bytes to copy
Copy(nint, nint, long)
Copies a block of memory from one address to another.
public static void Copy(nint source, nint destination, long count)
Parameters
sourcenintThe source address
destinationnintThe destination address
countlongThe number of bytes to copy
Copy(void*, void*, long)
Copies a block of memory from one address to another.
public static void Copy(void* source, void* destination, long count)
Parameters
sourcevoid*The source address
destinationvoid*The destination address
countlongThe number of bytes to copy
Copy<T>(T*, T*, int)
Copies count elements of type T from one address to another.
public static void Copy<T>(T* source, T* destination, int count) where T : unmanaged
Parameters
sourceT*The source address
destinationT*The destination address
countintThe number of elements to copy
Type Parameters
TThe type of the elements
Free(nint)
Frees a block of native memory.
public static void Free(nint address)
Parameters
addressnintThe address of the block to free
Free(void*)
Frees a block of native memory.
public static void Free(void* address)
Parameters
addressvoid*The address of the block to free
GetRef<T>(long)
Gets a reference to a value of type T from a given address.
public static ref T GetRef<T>(long address) where T : unmanaged
Parameters
addresslongThe address to read from
Returns
- T
A reference to the value read
Type Parameters
TThe type of the value
GetRef<T>(nint)
Gets a reference to a value of type T from a given address.
public static ref T GetRef<T>(nint address) where T : unmanaged
Parameters
addressnintThe address to read from
Returns
- T
A reference to the value read
Type Parameters
TThe type of the value
ReadArray<T>(long, int)
Reads an array of type T[] from a given address.
public static T[] ReadArray<T>(long address, int count = 1) where T : unmanaged
Parameters
Returns
- T[]
The array read
Type Parameters
TThe type of the array
Remarks
This method copies the data into managed memory.
ReadArray<T>(nint, int)
Reads an array of type T[] from a given address.
public static T[] ReadArray<T>(nint address, int count = 1) where T : unmanaged
Parameters
Returns
- T[]
The array read
Type Parameters
TThe type of the array
Remarks
This method copies the data into managed memory.
ReadBytes(nint, int)
Reads a specified number of bytes from a given address.
public static byte[] ReadBytes(nint address, int count)
Parameters
Returns
- byte[]
The bytes read
ReadPointer<T>(long)
Reads a pointer of type T* from a given address.
public static T* ReadPointer<T>(long address) where T : unmanaged
Parameters
addresslongThe address to read from
Returns
- T*
The pointer read
Type Parameters
TThe type of the pointer
ReadPointer<T>(nint)
Reads a pointer of type T* from a given address.
public static T* ReadPointer<T>(nint address) where T : unmanaged
Parameters
addressnintThe address to read from
Returns
- T*
The pointer read
Type Parameters
TThe type of the pointer
ReadString(byte*, Encoding?)
Reads a null-terminated string from a given address.
public static string ReadString(byte* address, Encoding? encoding = null)
Parameters
addressbyte*The address of the string
encodingEncodingThe encoding to use, or UTF8 if no encoding is given
Returns
- string
The string read
ReadString(long, Encoding?)
Reads a null-terminated string from a given address.
public static string ReadString(long address, Encoding? encoding = null)
Parameters
addresslongThe address of the string
encodingEncodingThe encoding to use, or UTF8 if no encoding is given
Returns
- string
The string read
ReadString(nint, Encoding?)
Reads a null-terminated string from a given address.
public static string ReadString(nint address, Encoding? encoding = null)
Parameters
addressnintThe address of the string
encodingEncodingThe encoding to use, or UTF8 if no encoding is given
Returns
- string
The string read
ReadStructArray<T>(long, int)
Reads an array of structs of type T[] from a given address.
public static T[] ReadStructArray<T>(long address, int count = 1) where T : struct
Parameters
Returns
- T[]
The array read
Type Parameters
TThe type of the struct
Remarks
This method copies the data into managed memory.
ReadStructArray<T>(nint, int)
Reads an array of structs of type T[] from a given address.
public static T[] ReadStructArray<T>(nint address, int count = 1) where T : struct
Parameters
Returns
- T[]
The array read
Type Parameters
TThe type of the struct
Remarks
This method copies the data into managed memory.
ReadStruct<T>(long)
Reads a struct of type T from a given address.
public static T ReadStruct<T>(long address) where T : struct
Parameters
addresslongThe address to read from
Returns
- T
The struct read
Type Parameters
TThe type of the struct
Remarks
This method copies the data into managed memory.
ReadStruct<T>(nint)
Reads a struct of type T from a given address.
public static T ReadStruct<T>(nint address) where T : struct
Parameters
addressnintThe address to read from
Returns
- T
The struct read
Type Parameters
TThe type of the struct
Remarks
This method copies the data into managed memory.
Read<T>(long)
Reads a value of type T from a given address.
public static T Read<T>(long address) where T : unmanaged
Parameters
addresslongThe address to read from
Returns
- T
The value read
Type Parameters
TThe type of the value
Read<T>(nint)
Reads a value of type T from a given address.
public static T Read<T>(nint address) where T : unmanaged
Parameters
addressnintThe address to read from
Returns
- T
The value read
Type Parameters
TThe type of the value
Realloc(nint, long)
Reallocates a block of native memory to the specified size.
public static nint Realloc(nint address, long newSize)
Parameters
Returns
- nint
The address of the reallocated block
Realloc<T>(T*, long)
Reallocates an array of type T[] to the specified size in native memory.
public static T* Realloc<T>(T* address, long count) where T : unmanaged
Parameters
addressT*The address of the array to reallocate
countlongThe new size of the array
Returns
- T*
The address of the reallocated array
Type Parameters
TThe type of the array
Remarks
Generally, if you need an array in native memory, you should use a NativeArray<T> with Resize(int).
StringLength(byte*)
Gets the length of a null-terminated string at a given address.
public static int StringLength(byte* address)
Parameters
addressbyte*The address of the string
Returns
- int
The length of the string
Remarks
This method counts the number of bytes, not the number of characters.
StringLength(long)
Gets the length of a null-terminated string at a given address.
public static int StringLength(long address)
Parameters
addresslongThe address of the string
Returns
- int
The length of the string
Remarks
This method counts the number of bytes, not the number of characters.
StringLength(nint)
Gets the length of a null-terminated string at a given address.
public static int StringLength(nint address)
Parameters
addressnintThe address of the string
Returns
- int
The length of the string
Remarks
This method counts the number of bytes, not the number of characters.
WriteBytes(nint, byte[])
Writes a specified number of bytes to a given address.
public static void WriteBytes(nint address, byte[] bytes)
Parameters
Remarks
Do not write to EXE regions using this method. Use WriteBytesSafe(nint, byte[]) instead.
WriteBytesSafe(nint, byte[])
Writes a specified number of bytes to a given address.
public static void WriteBytesSafe(nint address, byte[] bytes)
Parameters
Remarks
This method changes the memory protection of the page prior to writing, and is thus safe to use on EXE regions.