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;
@ -84,13 +84,11 @@ 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); int toCopy = (int)Math.Min(bufferSize, remaining);
Span<byte> buf = buffer.AsSpan(0, toCopy); Span<byte> buf = buffer.Span.Slice(0, toCopy);
input.Read(pos, buf); input.Read(pos, buf);
output.Write(pos, buf); output.Write(pos, buf);
@ -99,11 +97,6 @@ namespace LibHac.FsSystem
progress?.ReportAdd(toCopy); progress?.ReportAdd(toCopy);
} }
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
progress?.SetTotal(0); progress?.SetTotal(0);
} }
@ -140,15 +133,13 @@ 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);
{
buffer.AsSpan(0, (int)Math.Min(remaining, bufferSize)).Fill(value);
while (remaining > 0) while (remaining > 0)
{ {
int toFill = (int)Math.Min(bufferSize, remaining); int toFill = (int)Math.Min(bufferSize, remaining);
Span<byte> buf = buffer.AsSpan(0, toFill); Span<byte> buf = buffer.Span.Slice(0, toFill);
input.Write(pos, buf); input.Write(pos, buf);
@ -157,11 +148,6 @@ namespace LibHac.FsSystem
progress?.ReportAdd(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);