Class MemoryUtilities
Provides a set of static utility methods for memory management.
public static class MemoryUtilities
- Inheritance
-
objectMemoryUtilities
Methods
Allocate(int, int)
Allocates an aligned memory buffer of the requested size.
public static nint Allocate(int sizeInBytes, int alignment = 16)
Parameters
sizeInBytesintThe size of the buffer to allocate.
alignmentintThe memory alignment. It must be a positive power of two value. Defaults to 16 bytes.
Returns
- nint
A pointer to the allocated aligned buffer.
Remarks
To free the buffer allocated by this method, use the Free(nint) method
Exceptions
- ArgumentException
The
alignmentparameter is not a positive power of two value.
AllocateCleared(int, byte, int)
Allocates an aligned memory buffer of the requested size, and clears its contents.
public static nint AllocateCleared(int sizeInBytes, byte clearValue = 0, int alignment = 16)
Parameters
sizeInBytesintThe size of the buffer to allocate.
clearValuebyteThe value to use to clear the buffer. Default is 0.
alignmentintThe memory alignment. It must be a positive power of two value. Defaults to 16 bytes.
Returns
- nint
A pointer to the allocated aligned buffer.
Remarks
To free the buffer allocated by this method, use the Free(nint) method
Clear(ref byte, uint)
Clears the contents of memory at a provided address, zeroing it out.
public static void Clear(ref byte startAddress, uint byteCount)
Parameters
startAddressbyteA reference to the starting byte of the memory to clear.
byteCountuintThe number of bytes to clear.
Clear(nint, uint)
Clears the contents of memory at a provided address, zeroing it out.
public static void Clear(nint startAddress, uint byteCount)
Parameters
startAddressnintThe starting address of the memory to clear.
byteCountuintThe number of bytes to clear.
Clear(void*, uint)
Clears the contents of memory at a provided address, zeroing it out.
public static void Clear(void* startAddress, uint byteCount)
Parameters
startAddressvoid*The starting address of the memory to clear.
byteCountuintThe number of bytes to clear.
CopyWithAlignmentFallback(ref byte, ref readonly byte, uint)
Copies bytes from the source address to the destination address.
public static void CopyWithAlignmentFallback(ref byte destination, ref readonly byte source, uint byteCount)
Parameters
destinationbyteThe managed pointer corresponding to the destination address to copy to.
sourcebyteThe managed pointer corresponding to the source address to copy from.
byteCountuintThe number of bytes to copy.
Remarks
Some platform architectures do not support arbitrary unaligned memory reads or writes. This method checks whether unaligned memory access is safe on the current architecture and uses the most efficient method available.
Use this method instead of other memory copying methods if you are not sure whether the pointers or references you pass in are aligned.
CopyWithAlignmentFallback(nint, nint, uint)
Copies bytes from the source address to the destination address.
public static void CopyWithAlignmentFallback(nint destination, nint source, uint byteCount)
Parameters
destinationnintThe managed pointer corresponding to the destination address to copy to.
sourcenintThe managed pointer corresponding to the source address to copy from.
byteCountuintThe number of bytes to copy.
Remarks
Some platform architectures do not support arbitrary unaligned memory reads or writes. This method checks whether unaligned memory access is safe on the current architecture and uses the most efficient method available.
Use this method instead of other memory copying methods if you are not sure whether the pointers or references you pass in are aligned.
CopyWithAlignmentFallback(void*, void*, uint)
Copies bytes from the source address to the destination address.
public static void CopyWithAlignmentFallback(void* destination, void* source, uint byteCount)
Parameters
destinationvoid*The managed pointer corresponding to the destination address to copy to.
sourcevoid*The managed pointer corresponding to the source address to copy from.
byteCountuintThe number of bytes to copy.
Remarks
Some platform architectures do not support arbitrary unaligned memory reads or writes. This method checks whether unaligned memory access is safe on the current architecture and uses the most efficient method available.
Use this method instead of other memory copying methods if you are not sure whether the pointers or references you pass in are aligned.
Free(nint)
Frees an aligned memory buffer.
public static void Free(nint alignedBuffer)
Parameters
alignedBuffernintThe aligned buffer to free.
Remarks
The buffer must have been allocated with AllocateMemory.
IsAligned(ref byte, int)
Determines whether the specified memory reference is aligned.
public static bool IsAligned(ref byte memoryRef, int alignment = 16)
Parameters
memoryRefbyteThe memory reference.
alignmentintThe memory alignment. It must be a positive power of two value. Defaults to 16 bytes.
Returns
Exceptions
- ArgumentException
The
alignmentparameter is not a positive power of two value.
IsAligned(nint, int)
Determines whether the specified memory address is aligned.
public static bool IsAligned(nint memoryAddress, int alignment = 16)
Parameters
memoryAddressnintThe memory address.
alignmentintThe memory alignment. It must be a positive power of two value. Defaults to 16 bytes.
Returns
Exceptions
- ArgumentException
The
alignmentparameter is not a positive power of two value.
IsAligned(Memory<byte>, int)
Determines whether the specified memory is aligned.
public static bool IsAligned(Memory<byte> memory, int alignment = 16)
Parameters
memoryMemory<byte>The memory memory.
alignmentintThe memory alignment. It must be a positive power of two value. Defaults to 16 bytes.
Returns
Exceptions
- ArgumentException
The
alignmentparameter is not a positive power of two value.
IsAligned(ReadOnlyMemory<byte>, int)
Determines whether the specified memory is aligned.
public static bool IsAligned(ReadOnlyMemory<byte> memory, int alignment = 16)
Parameters
memoryReadOnlyMemory<byte>The memory.
alignmentintThe memory alignment. It must be a positive power of two value. Defaults to 16 bytes.
Returns
Exceptions
- ArgumentException
The
alignmentparameter is not a positive power of two value.
IsAligned(ReadOnlySpan<byte>, int)
Determines whether the specified span is aligned.
public static bool IsAligned(ReadOnlySpan<byte> span, int alignment = 16)
Parameters
spanReadOnlySpan<byte>The memory span.
alignmentintThe memory alignment. It must be a positive power of two value. Defaults to 16 bytes.
Returns
Exceptions
- ArgumentException
The
alignmentparameter is not a positive power of two value.
IsAligned(Span<byte>, int)
Determines whether the specified span is aligned.
public static bool IsAligned(Span<byte> span, int alignment = 16)
Parameters
spanSpan<byte>The memory span.
alignmentintThe memory alignment. It must be a positive power of two value. Defaults to 16 bytes.
Returns
Exceptions
- ArgumentException
The
alignmentparameter is not a positive power of two value.
IsAligned(void*, int)
Determines whether the specified memory pointer is aligned.
public static bool IsAligned(void* memoryPtr, int alignment = 16)
Parameters
memoryPtrvoid*The memory pointer.
alignmentintThe memory alignment. It must be a positive power of two value. Defaults to 16 bytes.
Returns
Exceptions
- ArgumentException
The
alignmentparameter is not a positive power of two value.
Swap<T>(ref T, ref T)
Swaps two values.
public static void Swap<T>(ref T left, ref T right)
Parameters
leftTThe left value.
rightTThe right value.
Type Parameters
TThe type of the values to swap.