Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix manual byte offsetting in memory/manager APIs
  • Loading branch information
Sergio0694 committed Apr 27, 2023
commit 76da6895cad4db7209495efb35f4d7025c7675a4
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ public override unsafe MemoryHandle Pin(int elementIndex = 0)
ThrowArgumentOutOfRangeExceptionForInvalidIndex();
}

int bytePrefix = this.offset * sizeof(TFrom);
int byteSuffix = elementIndex * sizeof(TTo);
int byteOffset = bytePrefix + byteSuffix;
nint bytePrefix = this.offset * sizeof(TFrom);
nint byteSuffix = elementIndex * sizeof(TTo);
nint byteOffset = bytePrefix + byteSuffix;

GCHandle handle = GCHandle.Alloc(this.array, GCHandleType.Pinned);

ref TFrom r0 = ref this.array.DangerousGetReference();
ref byte r1 = ref Unsafe.As<TFrom, byte>(ref r0);
ref byte r2 = ref Unsafe.Add(ref r1, byteOffset);
ref byte r2 = ref Unsafe.AddByteOffset(ref r1, byteOffset);
void* pi = Unsafe.AsPointer(ref r2);

return new(pi, handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public override unsafe MemoryHandle Pin(int elementIndex = 0)
ThrowArgumentOutOfRangeExceptionForInvalidIndex();
}

int bytePrefix = this.offset * sizeof(char);
int byteSuffix = elementIndex * sizeof(TTo);
int byteOffset = bytePrefix + byteSuffix;
nint bytePrefix = this.offset * sizeof(char);
nint byteSuffix = elementIndex * sizeof(TTo);
nint byteOffset = bytePrefix + byteSuffix;

GCHandle handle = GCHandle.Alloc(this.text, GCHandleType.Pinned);

ref char r0 = ref this.text.DangerousGetReference();
ref byte r1 = ref Unsafe.As<char, byte>(ref r0);
ref byte r2 = ref Unsafe.Add(ref r1, byteOffset);
ref byte r2 = ref Unsafe.AddByteOffset(ref r1, byteOffset);
void* pi = Unsafe.AsPointer(ref r2);

return new(pi, handle);
Expand Down
6 changes: 3 additions & 3 deletions src/CommunityToolkit.HighPerformance/Memory/Memory2D{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ namespace CommunityToolkit.HighPerformance;
private readonly object? instance;

/// <summary>
/// The initial offset within <see cref="instance"/>.
/// The initial byte offset within <see cref="instance"/>.
/// </summary>
private readonly IntPtr offset;
private readonly nint offset;

/// <summary>
/// The height of the specified 2D region.
Expand Down Expand Up @@ -603,7 +603,7 @@ public Span2D<T> Span
if (this.instance is MemoryManager<T> memoryManager)
{
ref T r0 = ref memoryManager.GetSpan().DangerousGetReference();
ref T r1 = ref Unsafe.Add(ref r0, this.offset);
ref T r1 = ref Unsafe.AddByteOffset(ref r0, this.offset);

return new(ref r1, this.height, this.width, this.pitch);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ namespace CommunityToolkit.HighPerformance;
private readonly object? instance;

/// <summary>
/// The initial offset within <see cref="instance"/>.
/// The initial byte offset within <see cref="instance"/>.
/// </summary>
private readonly IntPtr offset;
private readonly nint offset;

/// <summary>
/// The height of the specified 2D region.
Expand Down Expand Up @@ -615,7 +615,7 @@ public ReadOnlySpan2D<T> Span
if (this.instance is MemoryManager<T> memoryManager)
{
ref T r0 = ref memoryManager.GetSpan().DangerousGetReference();
ref T r1 = ref Unsafe.Add(ref r0, this.offset);
ref T r1 = ref Unsafe.AddByteOffset(ref r0, this.offset);

return new(in r1, this.height, this.width, this.pitch);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ public ref struct Enumerator
private readonly object? instance;

/// <summary>
/// The initial offset within <see cref="instance"/>.
/// The initial byte offset within <see cref="instance"/>.
/// </summary>
private readonly IntPtr offset;
private readonly nint offset;

/// <summary>
/// The height of the specified 2D region.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public readonly ref partial struct ReadOnlySpan2D<T>
private readonly object? instance;

/// <summary>
/// The initial offset within <see cref="instance"/>.
/// The initial byte offset within <see cref="instance"/>.
/// </summary>
private readonly IntPtr offset;
private readonly nint offset;

/// <summary>
/// The height of the specified 2D region.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ public ref struct Enumerator
private readonly object? instance;

/// <summary>
/// The initial offset within <see cref="instance"/>.
/// The initial byte offset within <see cref="instance"/>.
/// </summary>
private readonly IntPtr offset;
private readonly nint offset;

/// <summary>
/// The height of the specified 2D region.
Expand Down
4 changes: 2 additions & 2 deletions src/CommunityToolkit.HighPerformance/Memory/Span2D{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public readonly ref partial struct Span2D<T>
internal readonly object? Instance;

/// <summary>
/// The initial offset within <see cref="Instance"/>.
/// The initial byte offset within <see cref="Instance"/>.
/// </summary>
internal readonly IntPtr Offset;
internal readonly nint Offset;

/// <summary>
/// The height of the specified 2D region.
Expand Down