Table of Contents

Class MemoryHelper

Namespace
Jitter2.Unmanaged
Assembly
Jitter2.dll

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

len int

The length of the memory block to allocate, in bytes.

alignment int

The 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

num int

The number of elements to allocate memory for.

alignment int

The memory alignment in bytes (must be a power of 2).

Returns

T*

A pointer to the allocated memory block.

Type Parameters

T

The 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

ptr void*

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

len int

The 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

num int

The number of elements to allocate memory for.

Returns

T*

A pointer to the allocated memory block.

Type Parameters

T

The 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

ptr void*

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

ptr T*

A pointer to the memory block to free.

Type Parameters

T

The unmanaged type of the elements in the memory block.

MemSet(void*, int)

Zeros out unmanaged memory.

public static void MemSet(void* buffer, int len)

Parameters

buffer void*

A pointer to the memory block to zero out.

len int

The length of the memory block to zero out, in bytes.