Skip to content

Commit

Permalink
公开 ToManagedMemory API
Browse files Browse the repository at this point in the history
  • Loading branch information
lc6464 committed Aug 29, 2023
1 parent 8ca41ad commit b1ea556
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,24 @@ public static long DecodeLength(long dataLength, long offset = 0) {
}


private static ReadOnlySpan<T> CopyToManagedMemory<T>(this ReadOnlySpan<T> data) {
/// <summary>
/// 将 <paramref name="data"/>(可能在非托管内存中)复制到托管内存中。
/// </summary>
/// <typeparam name="T"><paramref name="data"/> 的类型</typeparam>
/// <param name="data">要复制的数据</param>
/// <returns>复制结果</returns>
public static ReadOnlySpan<T> CopyToManagedMemory<T>(this ReadOnlySpan<T> data) {
Span<T> result = new(new T[data.Length]);
data.CopyTo(result);
return result;
}

private static unsafe ReadOnlySpan<byte> MoveFromUnmanagedMemoryToManagedMemory(this ReadOnlySpan<byte> data) {
/// <summary>
/// 将非托管内存中的 <paramref name="data"/> 移动到托管内存中。
/// </summary>
/// <param name="data">要移动的数据</param>
/// <returns>移动结果</returns>
public static unsafe ReadOnlySpan<byte> MoveFromUnmanagedMemoryToManagedMemory(this ReadOnlySpan<byte> data) {
var result = data.CopyToManagedMemory();
fixed (byte* ptr = data) {
Marshal.FreeHGlobal((nint)ptr);
Expand Down

0 comments on commit b1ea556

Please sign in to comment.