Use LibHac's RentedArray<T> struct

This commit is contained in:
Xpl0itR 2020-08-08 17:47:50 +01:00
parent d138a52d06
commit 771663786d
No known key found for this signature in database
GPG key ID: 91798184109676AD

View file

@ -1,7 +1,7 @@
using System; using System;
using System.Buffers;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using LibHac.Common;
using LibHac.Fs; using LibHac.Fs;
using LibHac.Fs.Fsa; using LibHac.Fs.Fsa;
@ -74,7 +74,7 @@ namespace LibHac.FsSystem
public static void CopyTo(this IStorage input, IStorage output, IProgressReport progress = null) public static void CopyTo(this IStorage input, IStorage output, IProgressReport progress = null)
{ {
const int bufferSize = 81920; const int bufferSize = 81920;
input.GetSize(out long inputSize).ThrowIfFailure(); input.GetSize(out long inputSize).ThrowIfFailure();
output.GetSize(out long outputSize).ThrowIfFailure(); output.GetSize(out long outputSize).ThrowIfFailure();
@ -84,25 +84,18 @@ namespace LibHac.FsSystem
long pos = 0; long pos = 0;
byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize); using var buffer = new RentedArray<byte>(bufferSize);
try while (remaining > 0)
{ {
while (remaining > 0) int toCopy = (int)Math.Min(bufferSize, remaining);
{ Span<byte> buf = buffer.Span.Slice(0, toCopy);
int toCopy = (int)Math.Min(bufferSize, remaining); input.Read(pos, buf);
Span<byte> buf = buffer.AsSpan(0, toCopy); output.Write(pos, buf);
input.Read(pos, buf);
output.Write(pos, buf);
remaining -= toCopy; remaining -= toCopy;
pos += toCopy; pos += toCopy;
progress?.ReportAdd(toCopy); progress?.ReportAdd(toCopy);
}
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
} }
progress?.SetTotal(0); progress?.SetTotal(0);
@ -140,27 +133,20 @@ namespace LibHac.FsSystem
long pos = offset; long pos = offset;
byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize); using var buffer = new RentedArray<byte>(bufferSize);
try buffer.Span.Slice(0, (int)Math.Min(remaining, bufferSize)).Fill(value);
while (remaining > 0)
{ {
buffer.AsSpan(0, (int)Math.Min(remaining, bufferSize)).Fill(value); int toFill = (int)Math.Min(bufferSize, remaining);
Span<byte> buf = buffer.Span.Slice(0, toFill);
while (remaining > 0) input.Write(pos, buf);
{
int toFill = (int)Math.Min(bufferSize, remaining);
Span<byte> buf = buffer.AsSpan(0, toFill);
input.Write(pos, buf); remaining -= toFill;
pos += toFill;
remaining -= toFill; progress?.ReportAdd(toFill);
pos += toFill;
progress?.ReportAdd(toFill);
}
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
} }
progress?.SetTotal(0); progress?.SetTotal(0);
@ -205,15 +191,16 @@ namespace LibHac.FsSystem
const int bufferSize = 0x8000; const int bufferSize = 0x8000;
long remaining = length; long remaining = length;
long inOffset = 0; long inOffset = 0;
var buffer = new byte[bufferSize]; using var buffer = new RentedArray<byte>(bufferSize);
progress?.SetTotal(length); progress?.SetTotal(length);
while (remaining > 0) while (remaining > 0)
{ {
int toWrite = (int)Math.Min(buffer.Length, remaining); int toWrite = (int)Math.Min(bufferSize, remaining);
input.Read(inOffset, buffer.AsSpan(0, toWrite)); input.Read(inOffset, buffer.Span.Slice(0, toWrite));
output.Write(buffer, 0, toWrite); output.Write(buffer.Array, 0, toWrite);
remaining -= toWrite; remaining -= toWrite;
inOffset += toWrite; inOffset += toWrite;
progress?.ReportAdd(toWrite); progress?.ReportAdd(toWrite);