Skip to content

Commit

Permalink
Added SafeAllocatedMemoryHandleBase.CopyTo methods to copy memory seg…
Browse files Browse the repository at this point in the history
…ments to pointers and other SafeAllocatedMemoryHandleBase instances
  • Loading branch information
David Hall committed Jan 21, 2025
1 parent 3ffeefa commit d9c11bd
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Core/InteropServices/SafeMemoryHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,43 @@ public virtual int CompareTo(IReadOnlyList<byte>? other)
return ret;
}

/// <summary>Copies memory from this allocation to an allocated memory pointer.</summary>
/// <param name="dest">A pointer to allocated memory that must be at least <paramref name="length"/> bytes.</param>
/// <param name="length">The number of bytes to copy.</param>
public void CopyTo(IntPtr dest, SizeT length) => CallLocked(p => p.CopyTo(dest, length));

/// <summary>Copies memory from this allocation to an allocated memory handle.</summary>
/// <param name="dest">A safe handle to allocated memory.</param>
/// <param name="destOffset">The offset within <paramref name="dest"/> at which to start copying.</param>
/// <exception cref="System.ArgumentNullException">dest</exception>
/// <exception cref="System.ArgumentOutOfRangeException">destOffset</exception>
public void CopyTo(SafeAllocatedMemoryHandleBase dest, SizeT destOffset = default)
{
if (dest is null) throw new ArgumentNullException(nameof(dest));
if (dest.Size < destOffset + Size) throw new ArgumentOutOfRangeException(nameof(destOffset), "The destination buffer is not large enough.");
CallLocked(p => p.CopyTo(dest.handle, Size));
}

/// <summary>Copies memory from this allocation to an allocated memory pointer.</summary>
/// <param name="start">The starting offset within this allocation at which to start copying.</param>
/// <param name="dest">A pointer to allocated memory that must be at least <paramref name="length" /> bytes.</param>
/// <param name="length">The number of bytes to copy.</param>
public void CopyTo(SizeT start, IntPtr dest, SizeT length) => CallLocked(p => p.CopyTo(start, dest, length));

/// <summary>Copies memory from this allocation to an allocated memory handle.</summary>
/// <param name="start">The starting offset within this allocation at which to start copying.</param>
/// <param name="length">The number of bytes to copy.</param>
/// <param name="dest">A safe handle to allocated memory.</param>
/// <param name="destOffset">The offset within <paramref name="dest"/> at which to start copying.</param>
/// <exception cref="System.ArgumentNullException">dest</exception>
/// <exception cref="System.ArgumentOutOfRangeException">destOffset - The destination buffer is not large enough.</exception>
public void CopyTo(SizeT start, SizeT length, SafeAllocatedMemoryHandleBase dest, SizeT destOffset = default)
{
if (dest is null) throw new ArgumentNullException(nameof(dest));
if (dest.Size < destOffset + length - start) throw new ArgumentOutOfRangeException(nameof(destOffset), "The destination buffer is not large enough.");
CallLocked(p => p.CopyTo(start, dest.handle.Offset(destOffset), length));
}

/// <inheritdoc/>
public bool Equals(SafeAllocatedMemoryHandleBase? other) => CompareTo(other) == 0;

Expand Down

0 comments on commit d9c11bd

Please sign in to comment.