Class Mem
Contains tools for Memory management, including allocation, freeing and marshalling.
public static class Mem
- Inheritance
-
Mem
- Inherited Members
Remarks
Mem, mem-mem... Mem mem-mem!!
Mem-mem mem, mem mem-mem mem-mem.
Mem mem-mem, mem-mem mem mem-mem~
- Mem
Methods
AllocAsRef<T>(int, bool)
Allocate native memory using UCRT's malloc and use it as .NET ref.
public static ref T AllocAsRef<T>(int count = 1, bool zeroed = true) where T : unmanaged
Parameters
countintCount of elements to allocate.
zeroedboolWhether the memory is zeroed on allocation or not.
Returns
- T
The pointer of the
Ttype on native memory.
Type Parameters
TThe type of struct to allocate.
Alloc<T>(int, bool)
Allocate native memory using UCRT's malloc.
public static T* Alloc<T>(int count = 1, bool zeroed = true) where T : unmanaged
Parameters
countintCount of elements to allocate.
zeroedboolWhether the memory is zeroed on allocation or not.
Returns
- T*
The pointer of the
Ttype on native memory.
Type Parameters
TThe type of struct to allocate.
AsPointer<T>(nint)
Cast the safe pointer as unmanaged pointer of the T type.
public static T* AsPointer<T>(this nint ptr) where T : unmanaged
Parameters
ptrnintThe safe pointer to cast.
Returns
- T*
An unmanaged pointer of the
Ttype.
Type Parameters
TThe type of the struct to cast onto.
AsPointer<T>(scoped ref T)
Cast the .NET object ref as unmanaged pointer of the T type.
public static T* AsPointer<T>(this scoped ref T source) where T : unmanaged
Parameters
sourceTThe ref of the object.
Returns
- T*
An unmanaged pointer of the
Ttype.
Type Parameters
TThe type of the struct to cast onto.
AsRef<T>(nint)
Cast safe pointer into .NET object ref of the T type.
public static ref T AsRef<T>(this nint ptr) where T : unmanaged
Parameters
ptrnintThe safe pointer to cast into.
Returns
- T
.NET object ref of the
Ttype.
Type Parameters
TThe type of the struct to cast onto.
AsRef<T>(void*)
Cast any unmanaged pointer into .NET object ref of the T type.
public static ref T AsRef<T>(void* ptr) where T : unmanaged
Parameters
ptrvoid*Unmanaged pointer to cast into.
Returns
- T
.NET object ref of the
Ttype.
Type Parameters
TThe type of the struct to cast onto.
CombineUrlFromString(ReadOnlySpan<char>, params ReadOnlySpan<string?>)
Combines string of URL and path into one combined string.
public static string CombineUrlFromString(ReadOnlySpan<char> baseUrl, params ReadOnlySpan<string?> segments)
Parameters
baseUrlReadOnlySpan<char>The base of the URL string.
segmentsReadOnlySpan<string>Segments of path to combine with.
Returns
- string
Combined URL string.
CombineUrlFromString(string?, params ReadOnlySpan<string?>)
Combines string of URL and path into one combined string.
public static string CombineUrlFromString(this string? baseUrl, params ReadOnlySpan<string?> segments)
Parameters
baseUrlstringThe base of the URL string.
segmentsReadOnlySpan<string>Segments of path to combine with.
Returns
- string
Combined URL string.
CopyStructToUnmanaged<T>(scoped ref T)
Copy managed struct and allocate it into unmanaged native memory.
public static T* CopyStructToUnmanaged<T>(this scoped ref T source) where T : unmanaged
Parameters
sourceTThe .NET object ref of
Tto copy from.
Returns
- T*
An unmanaged native memory pointer to the struct.
Type Parameters
TThe type of
Tstruct to copy from.
CreateSpanFromNullTerminated<T>(PluginDisposableMemory<T>)
Creates a modifiable Span<T> by scanning the end of the null character of either char (UTF-16) or byte (UTF-8/ANSI) string from PluginDisposableMemory<T>.
public static Span<T> CreateSpanFromNullTerminated<T>(this PluginDisposableMemory<T> memory) where T : unmanaged
Parameters
memoryPluginDisposableMemory<T>A plugin memory to be converted from.
Returns
- Span<T>
A modifiable span of the string.
Type Parameters
CreateSpanFromNullTerminated<T>(void*)
Creates a modifiable Span<T> by scanning the end of the null character of either char (UTF-16) or byte (UTF-8/ANSI) string from a pointer of unmanaged native memory.
public static Span<T> CreateSpanFromNullTerminated<T>(void* ptr) where T : unmanaged
Parameters
ptrvoid*A pointer of the unmanaged native memory.
Returns
- Span<T>
A modifiable span of the string.
Type Parameters
CreateStringFromNullTerminated<T>(ref PluginDisposableMemory<T>)
Creates a managed string from PluginDisposableMemory<T>
public static string? CreateStringFromNullTerminated<T>(this ref PluginDisposableMemory<T> memory) where T : unmanaged
Parameters
memoryPluginDisposableMemory<T>A struct of PluginDisposableMemory<T>.
Returns
Type Parameters
CreateStringFromNullTerminated<T>(T*)
Creates a managed string from the pointer of unmanaged native memory of T type.
public static string CreateStringFromNullTerminated<T>(T* source) where T : unmanaged
Parameters
sourceT*An unmanaged native memory pointer of
Ttype.
Returns
Type Parameters
Free(nint)
Free native memory.
public static void Free(this nint ptr)
Parameters
ptrnintThe pointer of the memory.
Free(void*)
Free native memory.
public static void Free(void* ptr)
Parameters
ptrvoid*The pointer of the memory.
GetPinnableStringPointer(string?)
Get pinnable pointer of the string.
public static char* GetPinnableStringPointer(this string? str)
Parameters
strstringA string to get its pointer from.
Returns
- char*
A pinned pointer of the string.
GetPinnableStringPointerSafe(string?)
Get pinnable pointer of the string.
public static nint GetPinnableStringPointerSafe(this string? str)
Parameters
strstringA string to get its pointer from.
Returns
- nint
A pinned pointer of the string.
NormalizePathInplace(string)
Normalize path separator character based on what OS the environment is currently running on.
public static void NormalizePathInplace(this string str)
Parameters
strstringThe string path to be normalized.
Utf16SpanToUtf8Unmanaged(ReadOnlySpan<char>)
Creates an unmanaged UTF-8 (null-terminated) string from .NET Managed ReadOnlySpan<T> of char (or string)
public static byte* Utf16SpanToUtf8Unmanaged(this ReadOnlySpan<char> span)
Parameters
spanReadOnlySpan<char>A Span of char to create the string from.
Returns
- byte*
An unmanaged UTF-8 (null-terminated) string stored into native memory.