Table of Contents

Class Mem

Namespace
Hi3Helper.Plugin.Core.Utility
Assembly
Hi3Helper.Plugin.Core.dll

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

count int

Count of elements to allocate.

zeroed bool

Whether the memory is zeroed on allocation or not.

Returns

T

The pointer of the T type on native memory.

Type Parameters

T

The 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

count int

Count of elements to allocate.

zeroed bool

Whether the memory is zeroed on allocation or not.

Returns

T*

The pointer of the T type on native memory.

Type Parameters

T

The 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

ptr nint

The safe pointer to cast.

Returns

T*

An unmanaged pointer of the T type.

Type Parameters

T

The 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

source T

The ref of the object.

Returns

T*

An unmanaged pointer of the T type.

Type Parameters

T

The 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

ptr nint

The safe pointer to cast into.

Returns

T

.NET object ref of the T type.

Type Parameters

T

The 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

ptr void*

Unmanaged pointer to cast into.

Returns

T

.NET object ref of the T type.

Type Parameters

T

The 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

baseUrl ReadOnlySpan<char>

The base of the URL string.

segments ReadOnlySpan<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

baseUrl string

The base of the URL string.

segments ReadOnlySpan<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

source T

The .NET object ref of T to copy from.

Returns

T*

An unmanaged native memory pointer to the struct.

Type Parameters

T

The type of T struct 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

memory PluginDisposableMemory<T>

A plugin memory to be converted from.

Returns

Span<T>

A modifiable span of the string.

Type Parameters

T

The type of character (either char (UTF-16) or byte (UTF-8/ANSI) string)

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

ptr void*

A pointer of the unmanaged native memory.

Returns

Span<T>

A modifiable span of the string.

Type Parameters

T

The type of character (either char (UTF-16) or byte (UTF-8/ANSI) string)

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

memory PluginDisposableMemory<T>

A struct of PluginDisposableMemory<T>.

Returns

string

A managed .NET string.

Type Parameters

T

The type of character (either char (UTF-16) or byte (UTF-8/ANSI) string)

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

source T*

An unmanaged native memory pointer of T type.

Returns

string

A managed .NET string.

Type Parameters

T

The type of character (either char (UTF-16) or byte (UTF-8/ANSI) string)

Free(nint)

Free native memory.

public static void Free(this nint ptr)

Parameters

ptr nint

The pointer of the memory.

Free(void*)

Free native memory.

public static void Free(void* ptr)

Parameters

ptr void*

The pointer of the memory.

GetPinnableStringPointer(string?)

Get pinnable pointer of the string.

public static char* GetPinnableStringPointer(this string? str)

Parameters

str string

A 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

str string

A 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

str string

The 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

span ReadOnlySpan<char>

A Span of char to create the string from.

Returns

byte*

An unmanaged UTF-8 (null-terminated) string stored into native memory.