Gorgon
Show / Hide Table of Contents

Struct GorgonPtr<T>

A value type representing a pointer to native (unmanaged) memory.

Implements
IEquatable<GorgonPtr<T>>
IComparable<GorgonPtr<T>>
Inherited Members
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetType()
Namespace: Gorgon.Native
Assembly: Gorgon.Core.dll
Syntax
public readonly struct GorgonPtr<T> : IEquatable<GorgonPtr<T>>, IComparable<GorgonPtr<T>> where T : unmanaged
Type Parameters
Name Description
T
Remarks

This is a pointer access type that allows safe access to pre-existing blocks of native memory. It does this by wrapping other the native pointer to the memory block and provides safety checks to ensure nothing goes out of bounds when accessing the memory pointed at by the pointer.

Developers may use this pointer type like a regular native pointer and increment or decrement it the usual way: gorPtr++, gorPtr--. And beyond that, many functions are available to allow copying of the memory pointed at by the pointer to another type of data.

This pointer type only wraps a native pointer to previously allocated memory, therefore it does not perform any memory allocation on its own. Gorgon includes the GorgonNativeBuffer<T> for that purpose. The GorgonNativeBuffer<T> will implicitly convert to this type, so it can be used in situations where this type is required.

information

This type is suitable when the type of data stored in native memory is known. If the type of data in memory is not known (e.g. void *), or refers to an opaque handle (e.g. HWND), an nint should be used instead.

important

This type is ~3x slower for access than a regular native pointer (x64). This is due to the safety features available to ensure the pointer does not cause a buffer over/underrun. For pure speed, nothing beats a native pointer (or nint) and if your code is sensitive to microsecond timings (i.e. it needs to be near realtime/blazing fast), then use a native pointer instead (developers can cast this type to a native pointer). But do so with the understanding that all safety is off and memory corruption is a very real possibility.

Before making a choice, ALWAYS profile your application with a profiler. Never assume that the fastest functionality is required when memory safety is on the line.

Constructors

| Edit this page View Source

GorgonPtr(GorgonNativeBuffer<T>, int, int?)

Initializes a new instance of the GorgonPtr<T> struct.

Declaration
public GorgonPtr(GorgonNativeBuffer<T> buffer, int index = 0, int? count = null)
Parameters
Type Name Description
GorgonNativeBuffer<T> buffer

The native buffer to wrap within this pointer.

int index

[Optional] The index within the native buffer to start at.

int? count

[Optional] The number of items within the native buffer to point at.

Exceptions
Type Condition
ArgumentNullException

Thrown when the buffer parameter is null.

ArgumentOutOfRangeException

Thrown when the index, or count parameter is less than 0.

ArgumentException

Thrown when the index plus the count is larger than the capacity of the buffer.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

GorgonPtr(GorgonPtr<T>)

Initializes a new instance of the GorgonPtr<T> struct by cloning another pointer.

Declaration
public GorgonPtr(GorgonPtr<T> dataPtr)
Parameters
Type Name Description
GorgonPtr<T> dataPtr

The pointer to clone.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

GorgonPtr(DataBuffer)

Initializes a new instance of the GorgonPtr<T> struct.

Declaration
public GorgonPtr(DataBuffer dataBuffer)
Parameters
Type Name Description
DataBuffer dataBuffer

The SharpDX buffer to wrap within this pointer.

Remarks

This constructor wraps the pointer provided by the SharpDX buffer dataBuffer.

Exceptions
Type Condition
ArgumentNullException

Thrown if the dataBuffer is NULL.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

GorgonPtr(DataPointer)

Initializes a new instance of the GorgonPtr<T> struct.

Declaration
public GorgonPtr(DataPointer dataPtr)
Parameters
Type Name Description
DataPointer dataPtr

The SharpDX pointer to wrap within this pointer.

Remarks

This constructor wraps the pointer provided by the SharpDX pointer dataPtr.

Exceptions
Type Condition
ArgumentNullException

Thrown if the dataPtr is NULL.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

GorgonPtr(DataStream)

Initializes a new instance of the GorgonPtr<T> struct.

Declaration
public GorgonPtr(DataStream dataStream)
Parameters
Type Name Description
DataStream dataStream

The SharpDX stream to wrap within this pointer.

Remarks

This constructor wraps the pointer provided by the SharpDX pointer dataStream. This pointer will not change the stream position when read, written, or incremented/decremented.

Exceptions
Type Condition
ArgumentNullException

Thrown if the dataStream is NULL.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

GorgonPtr(nint, int)

Initializes a new instance of the GorgonPtr<T> struct.

Declaration
public GorgonPtr(nint pointer, int count)
Parameters
Type Name Description
nint pointer

The pointer to memory to wrap with this pointer.

int count

The number of items of type T to wrap.

Remarks

important

This takes a native memory pointer and wraps it for safety. It is important that the count is correct, otherwise memory access violations may occur when the pointer is used beyond the memory region that the original pointer is assigned to.

Exceptions
Type Condition
NullReferenceException

Thrown when the pointer is NULL.

ArgumentException

Thrown if the count parameter is less than 1.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

GorgonPtr(T*, int)

Initializes a new instance of the GorgonPtr<T> struct.

Declaration
public GorgonPtr(T* pointer, int count)
Parameters
Type Name Description
T* pointer

The pointer to memory to wrap with this pointer.

int count

The number of items of type T to wrap.

Remarks

important

This takes a native memory pointer and wraps it for safety. It is important that the count is correct, otherwise memory access violations may occur when the pointer is used beyond the memory region that the original pointer is assigned to.

Exceptions
Type Condition
NullReferenceException

Thrown when the pointer is NULL.

ArgumentException

Thrown if the count parameter is less than 1.

See Also
GorgonNativeBuffer<T>

Fields

| Edit this page View Source

Length

The number of items of type T stored within the memory block.

Declaration
public readonly int Length
Field Value
Type Description
int
See Also
GorgonNativeBuffer<T>
| Edit this page View Source

NullPtr

Represents a null pointer.

Declaration
public static readonly GorgonPtr<T> NullPtr
Field Value
Type Description
GorgonPtr<T>
See Also
GorgonNativeBuffer<T>

Properties

| Edit this page View Source

this[int]

Property to return a reference to the item located at the specified index.

Declaration
public ref T this[int index] { get; }
Parameters
Type Name Description
int index
Property Value
Type Description
T
Remarks

This property will return the value as a reference, and as such, it can be assigned to as well. For example:

GorgonPtr<int> ptr = ...;
int newValue = 123;

ptr[2] = newValue;

Exceptions
Type Condition
NullReferenceException

Thrown when this pointer is null.

IndexOutOfRangeException

Thrown if the index is less than 0, or greater than/equal to Length.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

SizeInBytes

Property to return the total size, in bytes, of the memory pointed at by this pointer.

Declaration
public int SizeInBytes { get; }
Property Value
Type Description
int
See Also
GorgonNativeBuffer<T>
| Edit this page View Source

TypeSize

Property to return the size, in bytes, of the type represented by T.

Declaration
public int TypeSize { get; }
Property Value
Type Description
int
See Also
GorgonNativeBuffer<T>

Methods

| Edit this page View Source

Add(in GorgonPtr<T>, int)

Function to increment the pointer by the given index offset.

Declaration
public static GorgonPtr<T> Add(in GorgonPtr<T> ptr, int indexOffset)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to increment.

int indexOffset

The number of indices to offset by.

Returns
Type Description
GorgonPtr<T>

A new GorgonPtr<T> starting at the updated index offset.

Remarks

If the pointer is incremented beyond the beginning, or end of the memory block that it points at, then the return value will be NullPtr rather than throw an exception. This is done this way for performance reasons. So users should check that their pointer is not null when iterating to ensure that the pointer is still valid.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

AsRef<Tc>(int)

Function to return the pointer as a reference value.

Declaration
public ref Tc AsRef<Tc>(int offset = 0) where Tc : unmanaged
Parameters
Type Name Description
int offset

[Optional] The offset, in bytes, within the memory pointed at this pointer to start at.

Returns
Type Description
Tc

The refernce to the value at the index.

Type Parameters
Name Description
Tc

The type of value. Must be an unmanaged value type, and can be different than T.

Remarks

This is meant for converting the data to another type while accessing memory. If the type of data specified by T is the same as Tc, then use the indexing property instead for better performance.

This value is returned as a reference, and as such, it can be assigned to as well. For example:

GorgonPtr<int> ptr = ...;
byte newValue = 123;

// This will write the byte value 123 at the 2nd byte in the first integer (since the pointer expects a int values). ptr.AsRef<byte>(1) = newValue;

Exceptions
Type Condition
NullReferenceException

Thrown when this pointer is null.

ArgumentOutOfRangeException

Thrown when the offset is less than 0, or greater than, or equal to, the SizeInBytes.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

CompareData(GorgonPtr<T>)

Function to compare the data pointed at by this pointer and another pointer.

Declaration
public bool CompareData(GorgonPtr<T> other)
Parameters
Type Name Description
GorgonPtr<T> other

The other pointer to compare with.

Returns
Type Description
bool

true if the data is the same, or false if not.

Remarks

This method is the equivalent of a memcmp in C/C++. It takes two pointers and compares the byte data in memory pointed at by both pointers. If any data is different at the byte level then the method will return false, otherwise, if all bytes are the same, then the method will return true. If both pointers point at the same memory address, then this method will always return true.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

CompareTo(GorgonPtr<T>)

Function to compare this pointer with another.

Declaration
public int CompareTo(GorgonPtr<T> other)
Parameters
Type Name Description
GorgonPtr<T> other

The other pointer to compare to.

Returns
Type Description
int

0 if the two pointers point at the same memory address, -1 if the this pointer address is less than the other pointer address and 1 if this pointer address is greater than the other pointer address.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

CopyTo(GorgonNativeBuffer<T>, int, int?, int)

Function to copy the contents of this buffer into other.

Declaration
public void CopyTo(GorgonNativeBuffer<T> destination, int sourceIndex = 0, int? count = null, int destIndex = 0)
Parameters
Type Name Description
GorgonNativeBuffer<T> destination

The destination buffer that will receive the data.

int sourceIndex

[Optional] The first index to start copying from.

int? count

[Optional] The number of items to copy.

int destIndex

[Optional] The destination index in the destination buffer to start copying into.

Remarks

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

Exceptions
Type Condition
NullReferenceException

Thrown when this pointer is null.

ArgumentNullException

Thrown when the destination parameter is null.

ArgumentOutOfRangeException

Thrown when the sourceIndex, or the destIndex parameter is less than 0.

ArgumentException

Thrown when the sourceIndex + count is too big for this buffer.

-or-

Thrown when the destIndex + count is too big for the destination buffer.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

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

Function to copy the memory pointed at by this pointer into another pointer.

Declaration
public void CopyTo(in GorgonPtr<T> destination, int sourceIndex = 0, int? count = null, int destIndex = 0)
Parameters
Type Name Description
GorgonPtr<T> destination

The destination pointer that will receive the data.

int sourceIndex

[Optional] The first index to start copying from.

int? count

[Optional] The number of items to copy.

int destIndex

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

Remarks

If the count parameter is ommitted, then the full length of the memory block, minus the sourceIndex is used. Ensure that there is enough space in the destination memory block to accomodate the amount of data required.

Exceptions
Type Condition
NullReferenceException

Thrown when this pointer is null.

ArgumentNullException

Thrown when the destination parameter is null.

ArgumentOutOfRangeException

Thrown when the sourceIndex, or the destIndex parameter is less than 0.

ArgumentException

Thrown when the sourceIndex + count is too big for the memory block.

-or-

Thrown when the destIndex + count is too big for the memory block.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

CopyTo(Stream, int, int?)

Function to copy the contents of the memory pointed at this pointer into a stream.

Declaration
public void CopyTo(Stream stream, int startIndex = 0, int? count = null)
Parameters
Type Name Description
Stream stream

The stream to write into.

int startIndex

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

int? count

[Optional] The maximum number of items to read.

Remarks

If the count parameter is ommitted, then the full length of the memory block, minus the startIndex is used. Ensure that there is enough space in the stream to accomodate the amount of data required.

Exceptions
Type Condition
NullReferenceException

Thrown when this pointer is null.

ArgumentNullException

Thrown when the stream parameter is null.

IOException

Thrown when the stream is read only.

ArgumentOutOfRangeException

Thrown when the startIndex is less than 0.

ArgumentException

Thrown when the startIndex + count are equal to or greater than the Length.

See Also
ToStream(int, int?)
| Edit this page View Source

CopyTo(Memory<T>, int)

Function to copy the contents of this buffer into a span.

Declaration
public void CopyTo(Memory<T> memory, int srcOffset = 0)
Parameters
Type Name Description
Memory<T> memory

The span to write into.

int srcOffset

[Optional] The offset within the buffer to start reading from.

Exceptions
Type Condition
ArgumentOutOfRangeException

Thrown when the srcOffset is less than 0.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

CopyTo(Span<T>, int)

Function to copy the contents of the memory pointed at by this pointer into a span.

Declaration
public void CopyTo(Span<T> span, int index = 0)
Parameters
Type Name Description
Span<T> span

The span to write into.

int index

[Optional] The index within the pointer to start reading from.

Exceptions
Type Condition
NullReferenceException

Thrown when this pointer is null.

ArgumentOutOfRangeException

Thrown when the index is less than 0.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

CopyTo(T[], int, int?, int)

Function to copy the contents of memory pointed at by this pointer into an array of type T.

Declaration
public void CopyTo(T[] destination, int sourceIndex = 0, int? count = null, int destIndex = 0)
Parameters
Type Name Description
T[] destination

The destination array that will receive the data.

int sourceIndex

[Optional] The first index to start copying from.

int? count

[Optional] The number of items to copy.

int destIndex

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

Remarks

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

Exceptions
Type Condition
NullReferenceException

Thrown when this pointer is null.

ArgumentNullException

Thrown when the destination parameter is null.

ArgumentOutOfRangeException

Thrown when the sourceIndex, or the destIndex parameter is less than 0.

ArgumentException

Thrown when the sourceIndex + count is too big for this memory block.

-or-

Thrown when the destIndex + count is too big for the destination memory block.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

Equals(GorgonPtr<T>)

Indicates whether the current object is equal to another object of the same type.

Declaration
public bool Equals(GorgonPtr<T> other)
Parameters
Type Name Description
GorgonPtr<T> other

An object to compare with this object.

Returns
Type Description
bool

true if the current object is equal to the other parameter; otherwise, false.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

Equals(object)

Determines whether the specified object is equal to this instance.

Declaration
public override bool Equals(object obj)
Parameters
Type Name Description
object obj

The object to compare with the current instance.

Returns
Type Description
bool

true if the specified object is equal to this instance; otherwise, false.

Overrides
ValueType.Equals(object)
See Also
GorgonNativeBuffer<T>
| Edit this page View Source

Fill(byte)

Function to fill the memory pointed at by this pointer with a specific value.

Declaration
public void Fill(byte clearValue)
Parameters
Type Name Description
byte clearValue

The value used to fill the memory.

Exceptions
Type Condition
NullReferenceException

Thrown when this pointer is null.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

GetHashCode()

Returns a hash code for this instance.

Declaration
public override int GetHashCode()
Returns
Type Description
int

A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.

Overrides
ValueType.GetHashCode()
See Also
GorgonNativeBuffer<T>
| Edit this page View Source

Subtract(in GorgonPtr<T>, in GorgonPtr<T>)

Function to subtract two pointers to return the number of bytes between them.

Declaration
public static long Subtract(in GorgonPtr<T> left, in GorgonPtr<T> right)
Parameters
Type Name Description
GorgonPtr<T> left

The left pointer to subtract.

GorgonPtr<T> right

The right pointer to subtract.

Returns
Type Description
long

The difference in bytes between the two pointers.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

Subtract(in GorgonPtr<T>, int)

Function to decrement the pointer by the given index offset.

Declaration
public static GorgonPtr<T> Subtract(in GorgonPtr<T> ptr, int indexOffset)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to decrement.

int indexOffset

The number of indices to offset by.

Returns
Type Description
GorgonPtr<T>

A new GorgonPtr<T> starting at the updated index offset.

Remarks

If the pointer is incremented beyond the beginning, or end of the memory block that it points at, then the return value will be NullPtr rather than throw an exception. This is done this way for performance reasons. So users should check that their pointer is not null when iterating to ensure that the pointer is still valid.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToBytePointer(int)

Function to convert this pointer into a byte based pointer.

Declaration
public GorgonPtr<byte> ToBytePointer(int offset = 0)
Parameters
Type Name Description
int offset

[Optional] The number of bytes to offset by.

Returns
Type Description
GorgonPtr<byte>

A new byte pointer starting at the pointer address with the offset applied.

Exceptions
Type Condition
ArgumentOutOfRangeException

Thrown if the offset would cause a buffer overrun.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToDataBuffer(GorgonPtr<T>)

Function to return a SharpDX DataBuffer wrapping this pointer.

Declaration
public static DataBuffer ToDataBuffer(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr
Returns
Type Description
DataBuffer

A new SharpDX DataBuffer which will wrap the contents of the pointer.

Remarks

This method takes the unmanaged memory used by the buffer and wraps it in a SharpDX DataBuffer. The SharpDX DataBuffer returned is not a copy of the memory used by the buffer, so it is important to ensure that the SharpDX DataBuffer lifetime is managed in conjunction with the lifetime of the buffer. Disposing of the buffer and using the returned SharpDX DataBuffer will result in undefined behavior and potential memory access violations.

Exceptions
Type Condition
ArgumentNullException

Thrown when the ptr parameter is null.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToDataBuffer(int, int?)

Function to return a SharpDX DataBuffer wrapping the memory pointed at by this pointer.

Declaration
public DataBuffer ToDataBuffer(int index = 0, int? count = null)
Parameters
Type Name Description
int index

[Optional] The index in the buffer to map to the beginning of the SharpDX DataBuffer.

int? count

[Optional] The number of items to wrap in the SharpDX DataBuffer.

Returns
Type Description
DataBuffer

A new SharpDX DataBuffer which will wrap the contents of the memory block.

Remarks

This method takes the unmanaged memory pointed at by this pointer and wraps it in a SharpDX DataBuffer. The SharpDX DataBuffer returned is not a copy of the memory, so it is important to ensure that the SharpDX DataBuffer lifetime is managed in conjunction with the lifetime of the memory. Disposing of the memory and using the returned SharpDX DataBuffer will result in undefined behavior and potential memory access violations.

A portion of the memory block can be wrapped by the SharpDX DataBuffer by supplying a value for index and/or count. If the count is omitted, all data from index up to the end of the memory block is wrapped by the SharpDX DataBuffer. Likewise, if index is omitted, all data from the beginning of the memory block up to the count is wrapped. If no parameters are supplied, then the entire memory block is wrapped.

Exceptions
Type Condition
NullReferenceException

Thrown when this pointer is null.

ArgumentOutOfRangeException

Thrown when the index, or the count parameter is less than 0.

ArgumentException

Thrown when the index plus the count exceeds the size of the memory block.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToDataPointer(GorgonPtr<T>)

Function to return a SharpDX DataPointer wrapping the memory pointed at by this pointer.

Declaration
public static DataPointer ToDataPointer(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to wrap.

Returns
Type Description
DataPointer

A new SharpDX DataPointer which will wrap the contents of the memory block.

Remarks

This method takes the unmanaged memory pointed at by this pointer and wraps it in a SharpDX DataPointer. The SharpDX DataPointer returned is not a copy of the memory, so it is important to ensure that the SharpDX DataPointer lifetime is managed in conjunction with the lifetime of the memory. Disposing of the memory and using the returned SharpDX DataPointer will result in undefined behavior and potential memory access violations.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToDataPointer(int, int?)

Function to return a SharpDX DataPointer wrapping the memory pointed at by this pointer.

Declaration
public DataPointer ToDataPointer(int index = 0, int? count = null)
Parameters
Type Name Description
int index

[Optional] The index in the buffer to map to the beginning of the SharpDX DataPointer.

int? count

[Optional] The number of items to wrap in the SharpDX DataPointer.

Returns
Type Description
DataPointer

A new SharpDX DataPointer which will wrap the contents of the memory block.

Remarks

This method takes the unmanaged memory pointed at by this pointer and wraps it in a SharpDX DataPointer. The SharpDX DataPointer returned is not a copy of the memory, so it is important to ensure that the SharpDX DataPointer lifetime is managed in conjunction with the lifetime of the memory. Disposing of the memory and using the returned SharpDX DataPointer will result in undefined behavior and potential memory access violations.

A portion of the memory block can be wrapped by the SharpDX DataPointer by supplying a value for index and/or count. If the count is omitted, all data from index up to the end of the memory block is wrapped by the SharpDX DataPointer. Likewise, if index is omitted, all data from the beginning of the memory block up to the count is wrapped. If no parameters are supplied, then the entire memory block is wrapped.

Exceptions
Type Condition
NullReferenceException

Thrown when this pointer is null.

ArgumentOutOfRangeException

Thrown when the index, or the count parameter is less than 0.

ArgumentException

Thrown when the index plus the count exceeds the size of the memory block.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToDataStream(GorgonPtr<T>)

Function to return a SharpDX DataStream wrapping the memory pointed at by this pointer.

Declaration
public static DataStream ToDataStream(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to wrap.

Returns
Type Description
DataStream

A new SharpDX DataStream which will wrap the contents of the memory block.

Remarks

This method takes the unmanaged memory pointed at by this pointer and wraps it in a SharpDX DataStream. The SharpDX DataStream returned is not a copy of the memory, so it is important to ensure that the SharpDX DataStream lifetime is managed in conjunction with the lifetime of the memory. Disposing of the memory and using the returned SharpDX DataStream will result in undefined behavior and potential memory access violations.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToDataStream(int, int?)

Function to return a SharpDX DataStream wrapping the memory pointed at by this pointer.

Declaration
public DataStream ToDataStream(int index = 0, int? count = null)
Parameters
Type Name Description
int index

[Optional] The index in the buffer to map to the beginning of the SharpDX DataStream.

int? count

[Optional] The number of items to wrap in the SharpDX DataStream.

Returns
Type Description
DataStream

A new SharpDX DataStream which will wrap the contents of the memory block.

Remarks

This method takes the unmanaged memory pointed at by this pointer and wraps it in a SharpDX DataStream. The SharpDX DataStream returned is not a copy of the memory, so it is important to ensure that the SharpDX DataStream lifetime is managed in conjunction with the lifetime of the memory. Disposing of the memory and using the returned SharpDX DataStream will result in undefined behavior and potential memory access violations.

A portion of the memory block can be wrapped by the SharpDX DataStream by supplying a value for index and/or count. If the count is omitted, all data from index up to the end of the memory block is wrapped by the SharpDX DataStream. Likewise, if index is omitted, all data from the beginning of the memory block up to the count is wrapped. If no parameters are supplied, then the entire memory block is wrapped.

Exceptions
Type Condition
NullReferenceException

Thrown when this pointer is null.

ArgumentOutOfRangeException

Thrown when the index, or the count parameter is less than 0.

ArgumentException

Thrown when the index plus the count exceeds the size of the memory block.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToLong(GorgonPtr<T>)

Function to convert a pointer to a long value representing the memory address of the block of memory being pointed at.

Declaration
public static long ToLong(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to convert.

Returns
Type Description
long

The memory address as a long value.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToPointer(GorgonPtr<T>)

Function to convert a pointer to a native pointer.

Declaration
public static T* ToPointer(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to convert.

Returns
Type Description
T*

The native pointer.

Remarks

warning

This operator returns the pointer to the memory address of this pointer. Developers should only use this for interop scenarios where a native call needs a pointer. Manipulation of this pointer is not advisable and may cause harm.

No safety checks are done on this pointer, and as such, memory corruption is possible if the pointer is used without due care.

Use this at your own risk.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToReadOnlySpan(GorgonPtr<T>)

Function to access native data as a read only span slice.

Declaration
public static ReadOnlySpan<T> ToReadOnlySpan(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr
Returns
Type Description
ReadOnlySpan<T>

A span for the pointer.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToReadOnlySpan(int, int?)

Function to access native data as a read only span slice.

Declaration
public ReadOnlySpan<T> ToReadOnlySpan(int index, int? count = null)
Parameters
Type Name Description
int index

The index of the item to start slicing at.

int? count

[Optional] The number of items to slice.

Returns
Type Description
ReadOnlySpan<T>

A span for the pointer.

Exceptions
Type Condition
NullReferenceException

Thrown when this pointer is null.

ArgumentOutOfRangeException

Thrown when the index, or the count parameter is less than 0.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToSpan(GorgonPtr<T>)

Function to access native data as a span slice.

Declaration
public static Span<T> ToSpan(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr
Returns
Type Description
Span<T>

A span for the pointer.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToSpan(int, int?)

Function to access native data as a span slice.

Declaration
public Span<T> ToSpan(int index = 0, int? count = null)
Parameters
Type Name Description
int index

The index of the item to start slicing at.

int? count

[Optional] The number of items to slice.

Returns
Type Description
Span<T>

A span for the pointer.

Exceptions
Type Condition
NullReferenceException

Thrown when this pointer is null.

ArgumentOutOfRangeException

Thrown when the index, or the count parameter is less than 0.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToStream(int, int?)

Function to return a stream wrapping this pointer.

Declaration
public Stream ToStream(int index = 0, int? count = null)
Parameters
Type Name Description
int index

[Optional] The index in the pointer memory to map to the beginning of the stream.

int? count

[Optional] The number of items to wrap in the stream.

Returns
Type Description
Stream

A new Stream which will wrap the contents of the memory pointed at by this pointer.

Remarks

This method takes the unmanaged memory pointed at by this pointer and wraps it in a Stream. The stream returned is not a copy of the memory, so it is important to ensure that the stream lifetime is managed in conjunction with the lifetime of the memory. Disposing of the memory and using the returned stream will result in undefined behavior and potential memory access violations.

A portion of the buffer can be wrapped by the stream by supplying a value for index and/or count. If the count is omitted, all data from index up to the end of the memory block is wrapped by the stream. Likewise, if index is omitted, all data from the beginning of the memory block up to the count is wrapped. If no parameters are supplied, then the entire memory block is wrapped.

Exceptions
Type Condition
NullReferenceException

Thrown when this pointer is null.

ArgumentOutOfRangeException

Thrown when the index, or the count parameter is less than 0.

ArgumentException

Thrown when the index plus the count exceeds the size of the memory block.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToString()

Returns a string that represents this instance.

Declaration
public override string ToString()
Returns
Type Description
string

A string that represents this instance.

Overrides
ValueType.ToString()
See Also
GorgonNativeBuffer<T>
| Edit this page View Source

ToUnsignedLong(GorgonPtr<T>)

Function to convert a pointer to a ulong value representing the memory address of the block of memory being pointed at.

Declaration
public static ulong ToUnsignedLong(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to convert.

Returns
Type Description
ulong

The memory address as a ulong value.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

To<Tc>()

Function to cast this pointer to another pointer type.

Declaration
public GorgonPtr<Tc> To<Tc>() where Tc : unmanaged
Returns
Type Description
GorgonPtr<Tc>

The casted pointer.

Type Parameters
Name Description
Tc

The type to convert to. Must be an unmanaged value type.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

Tonint(GorgonPtr<T>)

Function to convert pointer to a native pointer.

Declaration
public static nint Tonint(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to convert.

Returns
Type Description
nint

The native pointer.

Remarks

warning

This operator returns the pointer to the memory address of this pointer. Developers should only use this for interop scenarios where a native call needs a pointer. Manipulation of this pointer is not advisable and may cause harm.

No safety checks are done on this pointer, and as such, memory corruption is possible if the pointer is used without due care.

Use this at your own risk.

See Also
GorgonNativeBuffer<T>

Operators

| Edit this page View Source

operator +(in GorgonPtr<T>, int)

Operator to increment the pointer by the given index offset.

Declaration
public static GorgonPtr<T> operator +(in GorgonPtr<T> ptr, int indexOffset)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to increment.

int indexOffset

The number of indices to offset by.

Returns
Type Description
GorgonPtr<T>

A new GorgonPtr<T> starting at the updated index offset.

Remarks

If the pointer is incremented beyond the beginning, or end of the memory block that it points at, then the return value will be NullPtr rather than throw an exception. This is done this way for performance reasons. So users should check that their pointer is not null when iterating to ensure that the pointer is still valid.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

operator --(in GorgonPtr<T>)

Operator to decrement the pointer by one index.

Declaration
public static GorgonPtr<T> operator --(in GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to decrement.

Returns
Type Description
GorgonPtr<T>

A new GorgonPtr<T> starting at the updated index offset.

Remarks

If the pointer is incremented beyond the beginning, or end of the memory block that it points at, then the return value will be NullPtr rather than throw an exception. This is done this way for performance reasons. So users should check that their pointer is not null when iterating to ensure that the pointer is still valid.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

operator ==(GorgonPtr<T>, GorgonPtr<T>)

Operator compare two pointer addresses for equality.

Declaration
public static bool operator ==(GorgonPtr<T> left, GorgonPtr<T> right)
Parameters
Type Name Description
GorgonPtr<T> left

The left side pointer to compare.

GorgonPtr<T> right

The right side pointer to compare.

Returns
Type Description
bool

true if the two pointers point at the same address, false if not.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

explicit operator long(GorgonPtr<T>)

Operator to convert this pointer to a long value representing the memory address of the block of memory being pointed at.

Declaration
public static explicit operator long(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to convert.

Returns
Type Description
long

The memory address as a long value.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

explicit operator ulong(GorgonPtr<T>)

Operator to convert this pointer to a ulong value representing the memory address of the block of memory being pointed at.

Declaration
public static explicit operator ulong(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to convert.

Returns
Type Description
ulong

The memory address as a ulong value.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

operator >(GorgonPtr<T>, GorgonPtr<T>)

Operator to determine if one pointer address is greater than another.

Declaration
public static bool operator >(GorgonPtr<T> left, GorgonPtr<T> right)
Parameters
Type Name Description
GorgonPtr<T> left

The left side pointer to compare.

GorgonPtr<T> right

The right side pointer to compare.

Returns
Type Description
bool

true if left is greater than right, false if not.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

operator >=(GorgonPtr<T>, GorgonPtr<T>)

Operator to determine if one pointer address is greater than or equal to another.

Declaration
public static bool operator >=(GorgonPtr<T> left, GorgonPtr<T> right)
Parameters
Type Name Description
GorgonPtr<T> left

The left side pointer to compare.

GorgonPtr<T> right

The right side pointer to compare.

Returns
Type Description
bool

true if left is greater than or equal to right, false if not.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

implicit operator DataBuffer(GorgonPtr<T>)

Operator to convert this buffer to a SharpDX data buffer.

Declaration
public static implicit operator DataBuffer(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to convert.

Returns
Type Description
DataBuffer

A SharpDX DataBuffer wrapping the buffer data.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

implicit operator DataPointer(GorgonPtr<T>)

Operator to convert this pointer to a SharpDX data pointer.

Declaration
public static implicit operator DataPointer(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to convert.

Returns
Type Description
DataPointer

A SharpDX DataPointer wrapping the pointer data.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

implicit operator DataStream(GorgonPtr<T>)

Operator to convert this pointer to a SharpDX data stream.

Declaration
public static implicit operator DataStream(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to convert.

Returns
Type Description
DataStream

The SharpDX DataStream wrapping the pointer data.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

implicit operator nint(GorgonPtr<T>)

Operator to convert this pointer to a native pointer.

Declaration
public static implicit operator nint(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to convert.

Returns
Type Description
nint

A native pointer.

Remarks

warning

This operator returns the pointer to the memory address of this pointer. Developers should only use this for interop scenarios where a native call needs a pointer. Manipulation of this pointer is not advisable and may cause harm.

No safety checks are done on this pointer, and as such, memory corruption is possible if the pointer is used without due care.

Use this at your own risk.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

implicit operator ReadOnlySpan<T>(GorgonPtr<T>)

Operator to implicitly convert this pointer to a span.

Declaration
public static implicit operator ReadOnlySpan<T>(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The buffer to convert.

Returns
Type Description
ReadOnlySpan<T>
See Also
GorgonNativeBuffer<T>
| Edit this page View Source

implicit operator Span<T>(GorgonPtr<T>)

Operator to implicitly convert this pointer to a span.

Declaration
public static implicit operator Span<T>(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The buffer to convert.

Returns
Type Description
Span<T>
See Also
GorgonNativeBuffer<T>
| Edit this page View Source

implicit operator void*(GorgonPtr<T>)

Operator to convert this pointer to a native pointer.

Declaration
public static implicit operator void*(GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to convert.

Returns
Type Description
void*

A native pointer.

Remarks

warning

This operator returns the pointer to the memory address of this pointer. Developers should only use this for interop scenarios where a native call needs a pointer. Manipulation of this pointer is not advisable and may cause harm.

No safety checks are done on this pointer, and as such, memory corruption is possible if the pointer is used without due care.

Use this at your own risk.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

implicit operator GorgonPtr<T>(DataBuffer)

Operator to convert this SharpDX data buffer into a GorgonPtr<T>.

Declaration
public static implicit operator GorgonPtr<T>(DataBuffer buffer)
Parameters
Type Name Description
DataBuffer buffer

The data buffer to convert.

Returns
Type Description
GorgonPtr<T>

A pointer wrapping the buffer data.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

implicit operator GorgonPtr<T>(DataPointer)

Operator to convert this SharpDX data pointer.

Declaration
public static implicit operator GorgonPtr<T>(DataPointer ptr)
Parameters
Type Name Description
DataPointer ptr

The pointer to convert.

Returns
Type Description
GorgonPtr<T>

A pointer wrapping the SharpDX Pointer data.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

implicit operator GorgonPtr<T>(DataStream)

Operator to convert this SharpDX data stream into a GorgonPtr<T>..

Declaration
public static implicit operator GorgonPtr<T>(DataStream stream)
Parameters
Type Name Description
DataStream stream

The data stream convert.

Returns
Type Description
GorgonPtr<T>

The pointer wrapping the stream data.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

operator ++(in GorgonPtr<T>)

Operator to increment the pointer by one index.

Declaration
public static GorgonPtr<T> operator ++(in GorgonPtr<T> ptr)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to increment.

Returns
Type Description
GorgonPtr<T>

A new GorgonPtr<T> starting at the updated index offset.

Remarks

If the pointer is incremented beyond the beginning, or end of the memory block that it points at, then the return value will be NullPtr rather than throw an exception. This is done this way for performance reasons. So users should check that their pointer is not null when iterating to ensure that the pointer is still valid.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

operator !=(GorgonPtr<T>, GorgonPtr<T>)

Operator compare two pointer addresses for inequality.

Declaration
public static bool operator !=(GorgonPtr<T> left, GorgonPtr<T> right)
Parameters
Type Name Description
GorgonPtr<T> left

The left side pointer to compare.

GorgonPtr<T> right

The right side pointer to compare.

Returns
Type Description
bool

true if the two pointers do not point at the same address, false if they do.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

operator <(GorgonPtr<T>, GorgonPtr<T>)

Operator to determine if one pointer address is less than another.

Declaration
public static bool operator <(GorgonPtr<T> left, GorgonPtr<T> right)
Parameters
Type Name Description
GorgonPtr<T> left

The left side pointer to compare.

GorgonPtr<T> right

The right side pointer to compare.

Returns
Type Description
bool

true if left is less than right, false if not.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

operator <=(GorgonPtr<T>, GorgonPtr<T>)

Operator to determine if one pointer address is less than or equal to another.

Declaration
public static bool operator <=(GorgonPtr<T> left, GorgonPtr<T> right)
Parameters
Type Name Description
GorgonPtr<T> left

The left side pointer to compare.

GorgonPtr<T> right

The right side pointer to compare.

Returns
Type Description
bool

true if left is less than or equal to right, false if not.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

operator -(in GorgonPtr<T>, in GorgonPtr<T>)

Function to subtract two pointers to return the number of bytes between them.

Declaration
public static long operator -(in GorgonPtr<T> left, in GorgonPtr<T> right)
Parameters
Type Name Description
GorgonPtr<T> left

The left pointer to subtract.

GorgonPtr<T> right

The right pointer to subtract.

Returns
Type Description
long

The difference in bytes between the two pointers.

See Also
GorgonNativeBuffer<T>
| Edit this page View Source

operator -(in GorgonPtr<T>, int)

Operator to decrement the pointer by the given index offset.

Declaration
public static GorgonPtr<T> operator -(in GorgonPtr<T> ptr, int indexOffset)
Parameters
Type Name Description
GorgonPtr<T> ptr

The pointer to decrement.

int indexOffset

The number of indices to offset by.

Returns
Type Description
GorgonPtr<T>

A new GorgonPtr<T> starting at the updated index offset.

Remarks

If the pointer is incremented beyond the beginning, or end of the memory block that it points at, then the return value will be NullPtr rather than throw an exception. This is done this way for performance reasons. So users should check that their pointer is not null when iterating to ensure that the pointer is still valid.

See Also
GorgonNativeBuffer<T>

Implements

IEquatable<T>
IComparable<T>

Extension Methods

GorgonNullExtensions.AsNullable<T>(object)
GorgonNullExtensions.IfNull<T>(object, T)
GorgonNullExtensions.IsNull(object)

See Also

GorgonNativeBuffer<T>
  • 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