Struct NativeMemory<T>
Represents an allocated native memory buffer of elements of type T
public readonly struct NativeMemory<T> : IEquatable<ReadOnlyNativeMemory>, IEqualityOperators<NativeMemory<T>, ReadOnlyNativeMemory, bool>, IEquatable<NativeMemory<T>>, ISpanFormattable, IFormattable, IEqualityOperators<NativeMemory<T>, NativeMemory<T>, bool>, IEnumerable<T>, IEnumerable where T : unmanaged
Type Parameters
TThe type of the elements in the native memory buffer
- Implements
-
IEnumerable<T>
- Inherited Members
Properties
Empty
Gets an empty NativeMemory<T>
public static NativeMemory<T> Empty { get; }
Property Value
- NativeMemory<T>
An empty NativeMemory<T>
IsEmpty
Gets a value indicating whether the allocated memory buffer is empty
public bool IsEmpty { get; }
Property Value
- bool
A value indicating whether the allocated memory buffer is empty
- See Also
IsPinned
Gets a value indicating whether the underlying NativeMemoryManagerBase of this allocated memory buffer is pinned
[MemberNotNullWhen(true, new string[] { "mMemoryManager", "MemoryManager" })]
public bool IsPinned { get; }
Property Value
- bool
A value indicating whether the underlying NativeMemoryManagerBase of this allocated memory buffer is pinned
- See Also
IsValid
Gets a value indicating whether the allocated memory buffer is valid
public bool IsValid { get; }
Property Value
- bool
A value indicating whether the allocated memory buffer is valid
Remarks
A valid NativeMemory<T> might become invalid after the underlying NativeMemoryManager changed (e.g. by calling TryRealloc(ref NativeMemoryManager?, nuint) on it).
this[nuint]
Gets a reference to the element of type T at a specified index into the allocated memory buffer
public ref T this[nuint index] { get; }
Parameters
indexnuintThe index of the element of type
Tinto the allocated memory buffer to get a reference to
Property Value
- T
A reference to the element of type
Tat a specified index into the allocated memory buffer
Exceptions
- ArgumentOutOfRangeException
indexis greater or equal to Length- InvalidOperationException
The NativeMemory<T> instance is invalid (the underlying NativeMemoryManager might have changed)
Length
Gets the number of elements of type T in the allocated memory buffer
public nuint Length { get; }
Property Value
- nuint
The number of elements of type
Tin the allocated memory buffer
Pointer
Gets a pointer to the start of the allocated memory buffer
public nint Pointer { get; }
Property Value
- nint
A pointer to the start of the allocated memory buffer
Span
Gets the allocated memory buffer as a Span<T>
public Span<T> Span { get; }
Property Value
Examples
This example demonstrates how you could do a chunk-based approach with Span<T>s:
tempMemory = ...;
for ( span = tempMemory.; !span.; tempMemory = tempMemory.(()span.), span = tempMemory.)
{
// Do something with the memory in chunks using 'span'
}
Remarks
The returned Span<T> might not cover the whole memory region this NativeMemory<T> covers. This comes from a technical limitation where Span<T> uses int as the type for it's Length property where as NativeMemory<T> uses nuint as the type for it's Length property. Therefore, especially on 64-bit platforms, a NativeMemory<T> could cover a larger memory region than Span<T> ever could. If that's the case, you need to do a chunk-based approach, in order to cover the whole memory region with Span<T>s.
Exceptions
- InvalidOperationException
The NativeMemory<T> instance is invalid (the underlying NativeMemoryManager might have changed)
Methods
Equals(NativeMemory<T>)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(NativeMemory<T> other)
Parameters
otherNativeMemory<T>An object to compare with this object.
Returns
Equals(ReadOnlyNativeMemory)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(ReadOnlyNativeMemory other)
Parameters
otherReadOnlyNativeMemoryAn object to compare with this object.
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
- bool
true if
objand this instance are the same type and represent the same value; otherwise, false.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public NativeMemory<T>.Enumerator GetEnumerator()
Returns
- NativeMemory<T>.Enumerator
An enumerator that can be used to iterate through the collection.
Remarks
Note: This operation pins the given nativeMemory.
Exceptions
- InvalidOperationException
The NativeMemory<T> instance is invalid (the underlying NativeMemoryManager might have changed)
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
Pin()
Pins the underlying NativeMemoryManager
public NativeMemoryPin Pin()
Returns
- NativeMemoryPin
A pin pinning the underlying NativeMemoryManager
Slice(nuint)
Gets a slice of the allocate memory buffer starting at a specified element index
public NativeMemory<T> Slice(nuint start)
Parameters
startnuintThe index of an element of type
Twhere the resulting NativeMemory<T> should start
Returns
- NativeMemory<T>
A slice of the allocate memory buffer starting at a element index
start
Exceptions
- ArgumentOutOfRangeException
startis greater than Length- InvalidOperationException
The NativeMemory<T> instance is invalid (the underlying NativeMemoryManager might have changed)
Slice(nuint, nuint)
Gets a slice of the allocate memory buffer starting at a specified element index and containing a specified number of elements of type T
public NativeMemory<T> Slice(nuint start, nuint length)
Parameters
startnuintThe index of an element of type
Twhere the resulting NativeMemory<T> should startlengthnuintThe number of elements of type
Tthat the resulting NativeMemory<T> should contain
Returns
- NativeMemory<T>
A slice of the allocate memory buffer starting at a element index
startand containinglengthnumber of elements of typeT
Exceptions
- ArgumentOutOfRangeException
startis greater than Length- or -
start+lengthis greater than Length
- or -
- InvalidOperationException
The NativeMemory<T> instance is invalid (the underlying NativeMemoryManager might have changed)
ToString()
Returns the fully qualified type name of this instance.
public override string ToString()
Returns
- string
The fully qualified type name.
ToString(IFormatProvider?)
Formats the value of the current instance using the specified format.
public string ToString(IFormatProvider? formatProvider)
Parameters
formatProviderIFormatProviderThe provider to use to format the value.
-or-
A null reference (
Nothingin Visual Basic) to obtain the numeric format information from the current locale setting of the operating system.
Returns
- string
The value of the current instance in the specified format.
ToString(string?)
Formats the value of the current instance using the specified format.
public string ToString(string? format)
Parameters
formatstringThe format to use.
-or-
A null reference (
Nothingin Visual Basic) to use the default format defined for the type of the IFormattable implementation.
Returns
- string
The value of the current instance in the specified format.
ToString(string?, IFormatProvider?)
Formats the value of the current instance using the specified format.
public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
formatstringThe format to use.
-or-
A null reference (
Nothingin Visual Basic) to use the default format defined for the type of the IFormattable implementation.formatProviderIFormatProviderThe provider to use to format the value.
-or-
A null reference (
Nothingin Visual Basic) to obtain the numeric format information from the current locale setting of the operating system.
Returns
- string
The value of the current instance in the specified format.
TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)
Tries to format the value of the current instance into the provided span of characters.
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
Parameters
destinationSpan<char>The span in which to write this instance's value formatted as a span of characters.
charsWrittenintWhen this method returns, contains the number of characters that were written in
destination.formatReadOnlySpan<char>A span containing the characters that represent a standard or custom format string that defines the acceptable format for
destination.providerIFormatProviderAn optional object that supplies culture-specific formatting information for
destination.
Returns
Operators
operator ==(NativeMemory<T>, NativeMemory<T>)
Compares two values to determine equality.
public static bool operator ==(NativeMemory<T> left, NativeMemory<T> right)
Parameters
leftNativeMemory<T>The value to compare with
right.rightNativeMemory<T>The value to compare with
left.
Returns
operator ==(NativeMemory<T>, ReadOnlyNativeMemory)
Compares two values to determine equality.
public static bool operator ==(NativeMemory<T> left, ReadOnlyNativeMemory right)
Parameters
leftNativeMemory<T>The value to compare with
right.rightReadOnlyNativeMemoryThe value to compare with
left.
Returns
explicit operator NativeMemory<T>(NativeMemory)
Converts an allocated memory buffer of unspecified type to an allocated memory buffer of type T
public static explicit operator NativeMemory<T>(NativeMemory nativeMemory)
Parameters
nativeMemoryNativeMemoryThe allocated memory buffer of unspecified type to convert to an allocated memory buffer of type T
Returns
- NativeMemory<T>
An allocated memory buffer of type T spanning the same or a similar memory region as the given
nativeMemory
Remarks
The resulting allocated memory buffer of type T might not span all of the same memory region of the given nativeMemory.
Depending on the size of an instance of T, on how it aligns in the memory region given by nativeMemory, and on the offset from the originally allocated memory's starting point,
the resulting allocated memory buffer of type T might span a memory region which is a mere segment of the given nativeMemory's memory region.
Note: This might be still true, even if the given nativeMemory's Length is a multiple of the size of an instance of type T!
implicit operator NativeMemory(NativeMemory<T>)
Converts an allocated memory buffer of type T to an allocated memory buffer of unspecified type
public static implicit operator NativeMemory(NativeMemory<T> nativeMemory)
Parameters
nativeMemoryNativeMemory<T>The allocated memory buffer of type T to convert to an allocated memory buffer of unspecified type
Returns
- NativeMemory
An allocated memory buffer of unspecified type spanning the exact same memory region as the given
nativeMemory
implicit operator ReadOnlyNativeMemory(NativeMemory<T>)
Converts an allocated memory buffer of type T to an allocated read-only memory buffer of unspecified type
public static implicit operator ReadOnlyNativeMemory(NativeMemory<T> nativeMemory)
Parameters
nativeMemoryNativeMemory<T>The allocated memory buffer of type T to convert to an allocated read-only memory buffer of unspecified type
Returns
- ReadOnlyNativeMemory
An allocated read-only memory buffer of unspecified type spanning the exact same memory region as the given
nativeMemory
operator !=(NativeMemory<T>, NativeMemory<T>)
Compares two values to determine inequality.
public static bool operator !=(NativeMemory<T> left, NativeMemory<T> right)
Parameters
leftNativeMemory<T>The value to compare with
right.rightNativeMemory<T>The value to compare with
left.
Returns
operator !=(NativeMemory<T>, ReadOnlyNativeMemory)
Compares two values to determine inequality.
public static bool operator !=(NativeMemory<T> left, ReadOnlyNativeMemory right)
Parameters
leftNativeMemory<T>The value to compare with
right.rightReadOnlyNativeMemoryThe value to compare with
left.