Table of Contents

Class MemoryUtilities

Namespace
Stride.Core
Assembly
Stride.Core.dll

Provides a set of static utility methods for memory management.

public static class MemoryUtilities
Inheritance
object
MemoryUtilities

Methods

Allocate(int, int)

Allocates an aligned memory buffer of the requested size.

public static nint Allocate(int sizeInBytes, int alignment = 16)

Parameters

sizeInBytes int

The size of the buffer to allocate.

alignment int

The 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 alignment parameter 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

sizeInBytes int

The size of the buffer to allocate.

clearValue byte

The value to use to clear the buffer. Default is 0.

alignment int

The 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

startAddress byte

A reference to the starting byte of the memory to clear.

byteCount uint

The 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

startAddress nint

The starting address of the memory to clear.

byteCount uint

The 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

startAddress void*

The starting address of the memory to clear.

byteCount uint

The 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

destination byte

The managed pointer corresponding to the destination address to copy to.

source byte

The managed pointer corresponding to the source address to copy from.

byteCount uint

The 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

destination nint

The managed pointer corresponding to the destination address to copy to.

source nint

The managed pointer corresponding to the source address to copy from.

byteCount uint

The 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

destination void*

The managed pointer corresponding to the destination address to copy to.

source void*

The managed pointer corresponding to the source address to copy from.

byteCount uint

The 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

alignedBuffer nint

The 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

memoryRef byte

The memory reference.

alignment int

The memory alignment. It must be a positive power of two value. Defaults to 16 bytes.

Returns

bool

true if the specified memoryRef is aligned to the given alignment; otherwise, false.

Exceptions

ArgumentException

The alignment parameter 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

memoryAddress nint

The memory address.

alignment int

The memory alignment. It must be a positive power of two value. Defaults to 16 bytes.

Returns

bool

true if the specified memoryAddress is aligned to the given alignment; otherwise, false.

Exceptions

ArgumentException

The alignment parameter 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

memory Memory<byte>

The memory memory.

alignment int

The memory alignment. It must be a positive power of two value. Defaults to 16 bytes.

Returns

bool

true if the specified memory is aligned to the given alignment; otherwise, false.

Exceptions

ArgumentException

The alignment parameter 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

memory ReadOnlyMemory<byte>

The memory.

alignment int

The memory alignment. It must be a positive power of two value. Defaults to 16 bytes.

Returns

bool

true if the specified memory is aligned to the given alignment; otherwise, false.

Exceptions

ArgumentException

The alignment parameter 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

span ReadOnlySpan<byte>

The memory span.

alignment int

The memory alignment. It must be a positive power of two value. Defaults to 16 bytes.

Returns

bool

true if the specified span is aligned to the given alignment; otherwise, false.

Exceptions

ArgumentException

The alignment parameter 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

span Span<byte>

The memory span.

alignment int

The memory alignment. It must be a positive power of two value. Defaults to 16 bytes.

Returns

bool

true if the specified span is aligned to the given alignment; otherwise, false.

Exceptions

ArgumentException

The alignment parameter 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

memoryPtr void*

The memory pointer.

alignment int

The memory alignment. It must be a positive power of two value. Defaults to 16 bytes.

Returns

bool

true if the specified memoryPtr is aligned to the given alignment; otherwise, false.

Exceptions

ArgumentException

The alignment parameter 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

left T

The left value.

right T

The right value.

Type Parameters

T

The type of the values to swap.