public bool HasPointer => _pointer != null;
/// <summary>
- /// Adds an offset to the pinned pointer.
- /// </summary>
- /// <exception cref="System.ArgumentNullException">
- /// Throw when pinned pointer is null.
- /// </exception>
- internal void AddOffset(int offset)
- {
- if (_pointer == null)
- {
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.pointer);
- }
- else
- {
- _pointer = (void*)((byte*)_pointer + offset);
- }
- }
-
- /// <summary>
/// Frees the pinned handle and releases IRetainable.
/// </summary>
public void Dispose()
/// <summary>
/// Returns a handle for the array that has been pinned and hence its address can be taken
/// </summary>
- public abstract MemoryHandle Pin();
+ public abstract MemoryHandle Pin(int offset = 0);
/// <summary>
/// Returns an array segment.
{
if (_index < 0)
{
- memoryHandle = ((OwnedMemory<T>)_object).Pin();
- memoryHandle.AddOffset((_index & RemoveOwnedFlagBitMask) * Unsafe.SizeOf<T>());
+ memoryHandle = ((OwnedMemory<T>)_object).Pin((_index & RemoveOwnedFlagBitMask) * Unsafe.SizeOf<T>());
}
else if (typeof(T) == typeof(char) && _object is string s)
{
{
if (_index < 0)
{
- memoryHandle = ((OwnedMemory<T>)_object).Pin();
- memoryHandle.AddOffset((_index & RemoveOwnedFlagBitMask) * Unsafe.SizeOf<T>());
+ memoryHandle = ((OwnedMemory<T>)_object).Pin((_index & RemoveOwnedFlagBitMask) * Unsafe.SizeOf<T>());
}
else if (typeof(T) == typeof(char) && _object is string s)
{