Gorgon
Show / Hide Table of Contents

Class GorgonNativeExtensions

Extension methods for native memory functionality.

Inheritance
object
GorgonNativeExtensions
Inherited Members
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
Namespace: Gorgon.Native
Assembly: Gorgon.Core.dll
Syntax
public static class GorgonNativeExtensions

Methods

| Edit this page View Source

CopyTo<T>(Stream, GorgonPtr<T>, int, int?)

Function to copy the contents of a stream into a GorgonPtr<T>.

Declaration
public static void CopyTo<T>(this Stream stream, GorgonPtr<T> pointer, int index = 0, int? count = null) where T : unmanaged
Parameters
Type Name Description
Stream stream

The stream to read from.

GorgonPtr<T> pointer

The pointer to copy the stream data into.

int index

[Optional] The index within the pointer to start writing at.

int? count

[Optional] The maximum number of items to read from the stream.

Type Parameters
Name Description
T

The type of value in the pointer. Must be an unmanaged value type.

Exceptions
Type Condition
ArgumentNullException

Thrown when the stream, or the pointer parameter is null.

ArgumentOutOfRangeException

Thrown when the index is less than 0.

ArgumentException

Thrown if the index, count, or both values added together will exceed the size of the pointer or the stream.

EndOfStreamException

Thrown when the stream is at its end.

IOException

Thrown when the stream is write only.

| Edit this page View Source

CopyTo<T>(ReadOnlyMemory<T>, GorgonPtr<T>, int)

Function to copy the contents of a memory type into a GorgonPtr<T>.

Declaration
public static void CopyTo<T>(this ReadOnlyMemory<T> memory, GorgonPtr<T> pointer, int pointerIndex = 0) where T : unmanaged
Parameters
Type Name Description
ReadOnlyMemory<T> memory

The memory type to copy from.

GorgonPtr<T> pointer

The pointer that will receive the data.

int pointerIndex

[Optional] The index in the pointer to start writing into.

Type Parameters
Name Description
T

The type of data in the memory type and pointer. Must be an unmanaged value type.

Exceptions
Type Condition
ArgumentNullException

Thrown when the pointer parameter is null.

ArgumentOutOfRangeException

Thrown when the pointerIndex parameter is less than 0.

ArgumentException

-or-

Thrown when the pointerIndex + memory type length is too big for the pointer.

| Edit this page View Source

CopyTo<T>(ReadOnlySpan<T>, GorgonPtr<T>, int)

Function to copy the contents of a span into a GorgonPtr<T>.

Declaration
public static void CopyTo<T>(this ReadOnlySpan<T> span, GorgonPtr<T> pointer, int pointerIndex = 0) where T : unmanaged
Parameters
Type Name Description
ReadOnlySpan<T> span

The span to copy from.

GorgonPtr<T> pointer

The pointer that will receive the data.

int pointerIndex

[Optional] The index in the pointer to start writing into.

Type Parameters
Name Description
T

The type of data in the span and pointer. Must be an unmanaged value type.

Exceptions
Type Condition
ArgumentNullException

Thrown when the pointer parameter is null.

ArgumentOutOfRangeException

Thrown when the pointerIndex parameter is less than 0.

ArgumentException

-or-

Thrown when the pointerIndex + span length is too big for the pointer.

| Edit this page View Source

CopyTo<T>(T[], GorgonPtr<T>, int, int?, int)

Function to copy the contents of an array into a GorgonPtr<T>.

Declaration
public static void CopyTo<T>(this T[] array, GorgonPtr<T> pointer, int arrayIndex = 0, int? count = null, int pointerIndex = 0) where T : unmanaged
Parameters
Type Name Description
T[] array

The array to copy from.

GorgonPtr<T> pointer

The pointer that will receive the data.

int arrayIndex

[Optional] The index in the array to start copying from.

int? count

[Optional] The number of items to copy.

int pointerIndex

[Optional] The index in the pointer to start writing into.

Type Parameters
Name Description
T

The type of data in the array and pointer. Must be an unmanaged value type.

Remarks

If the count parameter is ommitted, then the full length of the source pointer, minus the arrayIndex is used. Ensure that there is enough space in the pointer to accomodate the amount of data required.

Exceptions
Type Condition
ArgumentNullException

Thrown when the array, or pointer parameter is null.

ArgumentOutOfRangeException

Thrown when the arrayIndex, or the pointerIndex parameter is less than 0.

ArgumentException

Thrown when the arrayIndex + count is too big for the array.

-or-

Thrown when the pointerIndex + count is too big for the pointer.

| Edit this page View Source

ToNativeBuffer<T>(Stream, int?)

Function to copy the contents of a stream into a GorgonNativeBuffer<T>.

Declaration
public static GorgonNativeBuffer<T> ToNativeBuffer<T>(this Stream stream, int? count = null) where T : unmanaged
Parameters
Type Name Description
Stream stream

The stream to read from.

int? count

[Optional] The maximum number of items to read from the stream.

Returns
Type Description
GorgonNativeBuffer<T>

A GorgonNativeBuffer<T> containing the contents of the stream.

Type Parameters
Name Description
T

The type of value in the buffer. Must be an unmanaged value type.

Exceptions
Type Condition
ArgumentNullException

Thrown when the stream parameter is null.

EndOfStreamException

Thrown when the stream is at its end.

IOException

Thrown when the stream is write only.

| Edit this page View Source

ToNativeBuffer<T>(ReadOnlyMemory<T>)

Function to copy the contents of a memory type to a GorgonNativeBuffer<T>.

Declaration
public static GorgonNativeBuffer<T> ToNativeBuffer<T>(this ReadOnlyMemory<T> memory) where T : unmanaged
Parameters
Type Name Description
ReadOnlyMemory<T> memory

The memory type to copy from.

Returns
Type Description
GorgonNativeBuffer<T>

A native buffer containing the contents of the memory type.

Type Parameters
Name Description
T

The type of data in the array. Must be an unmanaged value type.

| Edit this page View Source

ToNativeBuffer<T>(ReadOnlySpan<T>)

Function to copy the contents of a span slice to a GorgonNativeBuffer<T>.

Declaration
public static GorgonNativeBuffer<T> ToNativeBuffer<T>(this ReadOnlySpan<T> span) where T : unmanaged
Parameters
Type Name Description
ReadOnlySpan<T> span

The span to copy from.

Returns
Type Description
GorgonNativeBuffer<T>

A native buffer containing the contents of the span.

Type Parameters
Name Description
T

The type of data in the array. Must be an unmanaged value type.

| Edit this page View Source

ToNativeBuffer<T>(T[], int, int?)

Function to convert an array into a GorgonNativeBuffer<T>.

Declaration
public static GorgonNativeBuffer<T> ToNativeBuffer<T>(this T[] array, int index = 0, int? count = null) where T : unmanaged
Parameters
Type Name Description
T[] array

The array to turn into a native buffer.

int index

[Optional] The index in the array that represents the beginning of the native buffer.

int? count

[Optional] The number of items in the array that will be contained in the buffer.

Returns
Type Description
GorgonNativeBuffer<T>

A new GorgonNativeBuffer<T> containing the array data.

Type Parameters
Name Description
T

The type of data in the array. Must be an unmanaged value type.

Remarks

This method copies the contents of an array into a GorgonNativeBuffer<T> so that the data can be used with native code.

If the index is not supplied, then the beginning of the array is used as the start of the buffer, and if the count parameter is not supplied, then the length of the array (minus the index) is used.

warning

This method copies the data from the array into the buffer. This may have a negative impact on performance and memory usage.

Exceptions
Type Condition
ArgumentNullException

Thrown when the array parameter is null.

ArgumentOutOfRangeException

Thrown when the index is less than 0.

ArgumentException

Thrown when the index + count are equal to or greater than the length of array.

| Edit this page View Source

ToPinned<T>(T[], int, int?)

Function to pin an array and return a GorgonNativeBuffer<T> containing the pinned data.

Declaration
public static GorgonNativeBuffer<T> ToPinned<T>(this T[] array, int index = 0, int? count = null) where T : unmanaged
Parameters
Type Name Description
T[] array

The array to turn into a native buffer.

int index

[Optional] The index in the array that represents the beginning of the native buffer.

int? count

[Optional] The number of items in the array that will be contained in the buffer.

Returns
Type Description
GorgonNativeBuffer<T>

A new GorgonNativeBuffer<T> containing the array data.

Type Parameters
Name Description
T

The type of data in the array. Must be an unmanaged value type.

Remarks

This method pins the supplied array and returns a new GorgonNativeBuffer<T> containing the pinned data.

If the index is not supplied, then the beginning of the array is used as the start of the buffer, and if the count parameter is not supplied, then the length of the array (minus the index) is used.

warning

This method pins the array, which can cause performance issues with the garbage collector. Applications should only pin their objects for a very short time for best performance.

Exceptions
Type Condition
ArgumentNullException

Thrown when the array parameter is null.

ArgumentOutOfRangeException

Thrown when the index is less than 0.

ArgumentException

Thrown when the index + count are equal to or greater than the length of array.

  • Edit this page
  • View Source
In this article
Back to top Copyright 2023 - Licensed under the MIT license by Michael Winsor (Tape_Worm).
Send comments on this topic to the author