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

Free each allocation with the matching Free(void*) or AlignedFree(void*) method.

Allocation and deallocation are thread-safe. Synchronizing access to the allocated memory is the caller's responsibility.

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.

Exceptions

ArgumentOutOfRangeException

Thrown when len is negative or alignment is not a positive power of two.

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.

Exceptions

ArgumentOutOfRangeException

Thrown when num is negative or alignment is not a positive power of two.

OverflowException

Thrown when the requested byte count exceeds the native address space.

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.

Exceptions

ArgumentOutOfRangeException

Thrown when len is negative.

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.

Exceptions

ArgumentOutOfRangeException

Thrown when num is negative.

OverflowException

Thrown when the requested byte count exceeds the native address space.

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.

Exceptions

ArgumentOutOfRangeException

Thrown when len is negative.