Class MemoryHelper
Helper static class containing functions that aid dealing with unmanaged memory to managed memory conversions.
Inherited Members
Namespace: SharpAssimp
Assembly: SharpAssimp.dll
Syntax
public static class MemoryHelper
Methods
| Edit this page View SourceAllocateMemory(int, int)
Allocates unmanaged memory. This memory should only be freed by this helper.
Declaration
public static nint AllocateMemory(int sizeInBytes, int alignment = 16)
Parameters
Type | Name | Description |
---|---|---|
int | sizeInBytes | Size to allocate |
int | alignment | Alignment of the memory, by default aligned along 16-byte boundary. |
Returns
Type | Description |
---|---|
nint | Pointer to the allocated unmanaged memory. |
AsRef<T>(nint)
Casts the pointer into a by-ref value of the specified type.
Declaration
public static ref T AsRef<T>(nint pSrc) where T : struct
Parameters
Type | Name | Description |
---|---|---|
nint | pSrc | Memory location. |
Returns
Type | Description |
---|---|
T | By-ref value. |
Type Parameters
Name | Description |
---|---|
T | Struct type. |
ClearMemory(nint, int)
Clears the memory to zero.
Declaration
public static void ClearMemory(nint memoryPtr, int sizeInBytesToClear)
Parameters
Type | Name | Description |
---|---|---|
nint | memoryPtr | Pointer to the memory. |
int | sizeInBytesToClear | Number of bytes, starting from the memory pointer, to clear. |
CopyMemory(nint, nint, int)
Performs a memcopy that copies data from the memory pointed to by the source pointer to the memory pointer by the destination pointer.
Declaration
public static void CopyMemory(nint pDest, nint pSrc, int sizeInBytesToCopy)
Parameters
Type | Name | Description |
---|---|---|
nint | pDest | Destination memory location |
nint | pSrc | Source memory location |
int | sizeInBytesToCopy | Number of bytes to copy |
FreeMemory(nint)
Frees unmanaged memory that was allocated by this helper.
Declaration
public static void FreeMemory(nint memoryPtr)
Parameters
Type | Name | Description |
---|---|---|
nint | memoryPtr | Pointer to unmanaged memory to free. |
FreeNativeArray<T>(nint, int, FreeNativeDelegate, bool)
Frees an unmanaged array and performs cleanup for each value. Optionally can free an array of pointers. This can be used on any type that can be marshaled into unmanaged memory.
Declaration
public static void FreeNativeArray<T>(nint nativeArray, int length, FreeNativeDelegate action, bool arrayOfPointers = false) where T : struct
Parameters
Type | Name | Description |
---|---|---|
nint | nativeArray | Pointer to unmanaged memory |
int | length | Number of elements to free |
FreeNativeDelegate | action | Delegate that performs the necessary cleanup |
bool | arrayOfPointers | True if the pointer is an array of pointers, false otherwise. |
Type Parameters
Name | Description |
---|---|
T | Struct type |
FromNativeArray<T>(nint, int)
Marshals an array of blittable structs from a c-style unmanaged array (void*). This should not be used on non-blittable types that require marshaling by the runtime (e.g. has MarshalAs attributes).
Declaration
public static T[] FromNativeArray<T>(nint nativeArray, int length) where T : struct
Parameters
Type | Name | Description |
---|---|---|
nint | nativeArray | Pointer to unmanaged memory |
int | length | Number of elements to read |
Returns
Type | Description |
---|---|
T[] | Managed array |
Type Parameters
Name | Description |
---|---|
T | Struct type |
FromNativeArray<Managed, Native>(nint, int, bool)
Marshals an array of managed values from a c-style unmanaged array (void*). This also can optionally marshal from an unmanaged array of pointers (void**).
Declaration
public static Managed[] FromNativeArray<Managed, Native>(nint nativeArray, int length, bool arrayOfPointers = false) where Managed : class, IMarshalable<Managed, Native>, new() where Native : struct
Parameters
Type | Name | Description |
---|---|---|
nint | nativeArray | Pointer to unmanaged memory |
int | length | Number of elements to marshal |
bool | arrayOfPointers | True if the pointer is an array of pointers, false otherwise. |
Returns
Type | Description |
---|---|
Managed[] | Marshaled managed values |
Type Parameters
Name | Description |
---|---|
Managed | Managed type |
Native | Native type |
FromNativePointer<Managed, Native>(nint)
Marshals a managed value from unmanaged memory.
Declaration
public static Managed? FromNativePointer<Managed, Native>(nint ptr) where Managed : class, IMarshalable<Managed, Native>, new() where Native : struct
Parameters
Type | Name | Description |
---|---|---|
nint | ptr | Pointer to unmanaged memory |
Returns
Type | Description |
---|---|
Managed | The marshaled managed value |
Type Parameters
Name | Description |
---|---|
Managed | Managed type |
Native | Unmanaged type |
MarshalPointer<T>(in T, nint)
Convienence method for marshaling a structure to a pointer. Only use if the type is not blittable, otherwise use the write methods for blittable types.
Declaration
public static void MarshalPointer<T>(in T value, nint ptr) where T : struct
Parameters
Type | Name | Description |
---|---|---|
T | value | Struct to marshal |
nint | ptr | Pointer to unmanaged chunk of memory which must be allocated prior to this call |
Type Parameters
Name | Description |
---|---|
T | Struct type |
MarshalSizeOf<T>()
Computes the size of the struct type using Marshal SizeOf. Only use if the type is not blittable, thus requiring marshaling by the runtime, (e.g. has MarshalAs attributes), otherwise use the SizeOf methods for blittable types.
Declaration
public static int MarshalSizeOf<T>() where T : struct
Returns
Type | Description |
---|---|
int | Size of the struct in bytes. |
Type Parameters
Name | Description |
---|---|
T | Struct type |
MarshalStructure<T>(nint)
Convienence method for marshaling a pointer to a structure. Only use if the type is not blittable, otherwise use the read methods for blittable types.
Declaration
public static T MarshalStructure<T>(nint ptr) where T : struct
Parameters
Type | Name | Description |
---|---|---|
nint | ptr | Pointer to marshal |
Returns
Type | Description |
---|---|
T | The marshaled structure |
Type Parameters
Name | Description |
---|---|
T | Struct type |
MarshalStructure<T>(nint, out T)
Convienence method for marshaling a pointer to a structure. Only use if the type is not blittable, otherwise use the read methods for blittable types.
Declaration
public static void MarshalStructure<T>(nint ptr, out T value) where T : struct
Parameters
Type | Name | Description |
---|---|---|
nint | ptr | Pointer to marshal |
T | value | The marshaled structure |
Type Parameters
Name | Description |
---|---|
T | Struct type |
ReadStreamFully(Stream, int)
Reads a stream until the end is reached into a byte array. Based on Jon Skeet's implementation. It is up to the caller to dispose of the stream.
Declaration
public static byte[] ReadStreamFully(Stream stream, int initialLength)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | Stream to read all bytes from |
int | initialLength | Initial buffer length, default is 32K |
Returns
Type | Description |
---|---|
byte[] | The byte array containing all the bytes from the stream |
Read<T>(nint)
Reads a single element from the memory location.
Declaration
public static T Read<T>(nint pSrc) where T : struct
Parameters
Type | Name | Description |
---|---|---|
nint | pSrc | Pointer to memory location |
Returns
Type | Description |
---|---|
T | The read value |
Type Parameters
Name | Description |
---|---|
T | Struct type |
Read<T>(nint, out T)
Reads a single element from the memory location.
Declaration
public static void Read<T>(nint pSrc, out T value) where T : struct
Parameters
Type | Name | Description |
---|---|---|
nint | pSrc | Pointer to memory location |
T | value | The read value. |
Type Parameters
Name | Description |
---|---|
T | Struct type |
Read<T>(nint, T[], int, int)
Reads data from the memory location into the array.
Declaration
public static void Read<T>(nint pSrc, T[] data, int startIndexInArray, int count) where T : struct
Parameters
Type | Name | Description |
---|---|---|
nint | pSrc | Pointer to memory location |
T[] | data | Array to store the copied data |
int | startIndexInArray | Zero-based element index to start writing data to in the element array. |
int | count | Number of elements to copy |
Type Parameters
Name | Description |
---|---|
T | Struct type |
SizeOf<T>()
Computes the size of the struct type.
Declaration
public static int SizeOf<T>() where T : struct
Returns
Type | Description |
---|---|
int | Size of the struct in bytes. |
Type Parameters
Name | Description |
---|---|
T | Struct type |
SizeOf<T>(T[])
Computes the size of the struct array.
Declaration
public static int SizeOf<T>(T[] array) where T : struct
Parameters
Type | Name | Description |
---|---|---|
T[] | array | Array of structs |
Returns
Type | Description |
---|---|
int | Total size, in bytes, of the array's contents. |
Type Parameters
Name | Description |
---|---|
T | Struct type |
ToNativeArray<T>(List<T>)
Marshals a list of blittable structs to a c-style unmanaged array (void*). This should not be used on non-blittable types that require marshaling by the runtime (e.g. has MarshalAs attributes).
Declaration
public static nint ToNativeArray<T>(List<T> managedArray) where T : struct
Parameters
Type | Name | Description |
---|---|---|
List<T> | managedArray | Managed list of structs |
Returns
Type | Description |
---|---|
nint | Pointer to unmanaged memory |
Type Parameters
Name | Description |
---|---|
T | Struct type |
ToNativeArray<T>(T[]?)
Marshals an array of blittable structs to a c-style unmanaged array (void*). This should not be used on non-blittable types that require marshaling by the runtime (e.g. has MarshalAs attributes).
Declaration
public static nint ToNativeArray<T>(T[]? managedArray) where T : struct
Parameters
Type | Name | Description |
---|---|---|
T[] | managedArray | Managed array of structs |
Returns
Type | Description |
---|---|
nint | Pointer to unmanaged memory |
Type Parameters
Name | Description |
---|---|
T | Struct type |
ToNativeArray<Managed, Native>(IReadOnlyCollection<Managed>?, bool)
Marshals an array of managed values to a c-style unmanaged array (void*). This also can optionally marshal to an unmanaged array of pointers (void**).
Declaration
public static nint ToNativeArray<Managed, Native>(IReadOnlyCollection<Managed>? managedColl, bool arrayOfPointers = false) where Managed : class, IMarshalable<Managed, Native>, new() where Native : struct
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyCollection<Managed> | managedColl | Collection of managed values |
bool | arrayOfPointers | True if the pointer is an array of pointers, false otherwise. |
Returns
Type | Description |
---|---|
nint | Pointer to unmanaged memory |
Type Parameters
Name | Description |
---|---|
Managed | Managed type |
Native | Native type |
ToNativePointer<Managed, Native>(Managed)
Marshals a managed value to unmanaged memory.
Declaration
public static nint ToNativePointer<Managed, Native>(Managed managedValue) where Managed : class, IMarshalable<Managed, Native>, new() where Native : struct
Parameters
Type | Name | Description |
---|---|---|
Managed | managedValue | Managed value to marshal |
Returns
Type | Description |
---|---|
nint | Pointer to unmanaged memory |
Type Parameters
Name | Description |
---|---|
Managed | Managed type |
Native | Unmanaged type |
Write<T>(nint, List<T>, int, int)
Writes data from the list to the memory location.
Declaration
public static void Write<T>(nint pDest, List<T> data, int startIndexInArray, int count) where T : struct
Parameters
Type | Name | Description |
---|---|---|
nint | pDest | Pointer to memory location |
List<T> | data | List containing data to write |
int | startIndexInArray | Zero-based element index to start reading data from in the element array. |
int | count | Number of elements to copy |
Type Parameters
Name | Description |
---|---|
T | Struct type |
Write<T>(nint, in T)
Writes a single element to the memory location.
Declaration
public static void Write<T>(nint pDest, in T data) where T : struct
Parameters
Type | Name | Description |
---|---|---|
nint | pDest | Pointer to memory location |
T | data | The value to write |
Type Parameters
Name | Description |
---|---|
T | Struct type |
Write<T>(nint, T[], int, int)
Writes data from the array to the memory location.
Declaration
public static void Write<T>(nint pDest, T[] data, int startIndexInArray, int count) where T : struct
Parameters
Type | Name | Description |
---|---|---|
nint | pDest | Pointer to memory location |
T[] | data | Array containing data to write |
int | startIndexInArray | Zero-based element index to start reading data from in the element array. |
int | count | Number of elements to copy |
Type Parameters
Name | Description |
---|---|
T | Struct type |