Class MemoryHelper
Provides helper methods for allocating and managing unmanaged memory.
public static class MemoryHelper
- Inheritance
-
MemoryHelper
- Inherited Members
Remarks
Safety: All pointers returned by allocation methods must be freed using the corresponding Free(void*) or AlignedFree(void*) method. Failure to do so will result in memory leaks.
Threading: These methods are thread-safe, but the caller is responsible for synchronizing access to the allocated memory.
Methods
AlignedAllocateHeap(int, int)
Allocates a block of aligned unmanaged memory of the specified length in bytes.
public static void* AlignedAllocateHeap(int len, int alignment)
Parameters
lenintThe length of the memory block to allocate, in bytes.
alignmentintThe memory alignment in bytes (must be a power of 2).
Returns
- void*
A pointer to the allocated memory block.
Remarks
Safety: The caller must free the returned pointer using AlignedFree(void*). The memory is not zero-initialized.
AlignedAllocateHeap<T>(int, int)
Allocates a block of aligned unmanaged memory for an array of the specified type.
public static T* AlignedAllocateHeap<T>(int num, int alignment) where T : unmanaged
Parameters
numintThe number of elements to allocate memory for.
alignmentintThe memory alignment in bytes (must be a power of 2).
Returns
- T*
A pointer to the allocated memory block.
Type Parameters
TThe unmanaged type of the elements to allocate memory for.
Remarks
Safety: The caller must free the returned pointer using AlignedFree(void*). The memory is not zero-initialized.
AlignedFree(void*)
Frees a block of aligned unmanaged memory previously allocated.
public static void AlignedFree(void* ptr)
Parameters
ptrvoid*A pointer to the aligned memory block to free.
AllocateHeap(int)
Allocates a block of unmanaged memory of the specified length in bytes.
public static void* AllocateHeap(int len)
Parameters
lenintThe length of the memory block to allocate, in bytes.
Returns
- void*
A pointer to the allocated memory block.
Remarks
Safety: The caller must free the returned pointer using Free(void*). The memory is not zero-initialized.
AllocateHeap<T>(int)
Allocates a block of unmanaged memory for an array of the specified type.
public static T* AllocateHeap<T>(int num) where T : unmanaged
Parameters
numintThe number of elements to allocate memory for.
Returns
- T*
A pointer to the allocated memory block.
Type Parameters
TThe unmanaged type of the elements to allocate memory for.
Remarks
Safety: The caller must free the returned pointer using Free<T>(T*). The memory is not zero-initialized.
Free(void*)
Frees a block of unmanaged memory previously allocated.
public static void Free(void* ptr)
Parameters
ptrvoid*A pointer to the memory block to free.
Free<T>(T*)
Frees a block of unmanaged memory previously allocated for an array of the specified type.
public static void Free<T>(T* ptr) where T : unmanaged
Parameters
ptrT*A pointer to the memory block to free.
Type Parameters
TThe unmanaged type of the elements in the memory block.
MemSet(void*, int)
Zeros out unmanaged memory.
public static void MemSet(void* buffer, int len)