Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions SngTool/SngLib/LargeFile.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.IO;
using System.Threading.Tasks;
using System.Buffers;
using Cysharp.Collections;

Expand Down
31 changes: 31 additions & 0 deletions SngTool/SngLib/NativeByteArray/NativeByteArray.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#nullable enable

#if NET6_0_OR_GREATER
#define WITH_INTEROPSERVICES_NATIVEMEMORY
#endif

using System;
using System.Buffers;
using System.Collections.Generic;
Expand Down Expand Up @@ -57,11 +61,22 @@ public NativeByteArray(long length, bool skipZeroClear = false, bool addMemoryPr
{
if (skipZeroClear)
{
#if WITH_INTEROPSERVICES_NATIVEMEMORY
buffer = (byte*)NativeMemory.Alloc(checked((nuint)length), 1);
#else
int byteCount = (int)checked((uint)length) * 1;
buffer = (byte*)Marshal.AllocHGlobal(byteCount);
#endif
}
else
{
#if WITH_INTEROPSERVICES_NATIVEMEMORY
buffer = (byte*)NativeMemory.AllocZeroed(checked((nuint)length), 1);
#else
int byteCount = (int)checked((uint)length) * 1;
buffer = (byte*)Marshal.AllocHGlobal(byteCount);
Unsafe.InitBlockUnaligned(buffer, 0, (uint)byteCount);
#endif
}

if (addMemoryPressure)
Expand Down Expand Up @@ -109,7 +124,19 @@ public bool Resize(long newLength)
// This vastly reduces the total number of re-allocations
// with the drawback of increasing memory usage somewhat
newLength = NextPO2(newLength);
#if WITH_INTEROPSERVICES_NATIVEMEMORY
buffer = (byte*)NativeMemory.Realloc(buffer, (nuint)newLength);
#else
int byteCount = (int)newLength;
byte* newBuffer = (byte*)Marshal.AllocHGlobal(byteCount);

Span<byte> src = new Span<byte>(buffer, (int)length);
Span<byte> dst = new Span<byte>(newBuffer, (int)byteCount);
src.CopyTo(dst);

Marshal.FreeHGlobal((IntPtr)buffer);
buffer = newBuffer;
#endif

allocatedLength = newLength;
length = newLength;
Expand Down Expand Up @@ -291,7 +318,11 @@ void DisposeCore()
isDisposed = true;
if (Unsafe.IsNullRef(ref Unsafe.AsRef<byte>(buffer))) return;

#if WITH_INTEROPSERVICES_NATIVEMEMORY
NativeMemory.Free(buffer);
#else
Marshal.FreeHGlobal((IntPtr)buffer);
#endif
if (addMemoryPressure)
{
GC.RemoveMemoryPressure(length * Unsafe.SizeOf<byte>());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#nullable enable

#if !NETSTANDARD2_0 && !UNITY_2019_1_OR_NEWER
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER

using System;
using System.IO;
Expand Down
5 changes: 3 additions & 2 deletions SngTool/SngLib/SngFile.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Cysharp.Collections;

Expand All @@ -11,8 +12,8 @@ public class SngFile : IDisposable

public bool metadataAvailable;

public Dictionary<string, string> Metadata = new();
public Dictionary<string, NativeByteArray?> Files = new();
public Dictionary<string, string> Metadata = new Dictionary<string, string>();
public Dictionary<string, NativeByteArray?> Files = new Dictionary<string, NativeByteArray?>();

public void AddFile(string fileName, NativeByteArray? data)
{
Expand Down
13 changes: 13 additions & 0 deletions SngTool/SngLib/SngSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BinaryEx;
using Cysharp.Collections;

Expand Down Expand Up @@ -75,7 +79,16 @@ private static void DoMaskData(Span<byte> data, long filePos)
Vector<byte> dataVector = new Vector<byte>(data.Slice(byteIndex));

Vector<byte> maskedData = dataVector ^ dataIndexVectors![lookupIndex & lookupSizeMask];
#if NET5_0_OR_GREATER
maskedData.CopyTo(data.Slice(byteIndex));
#else
var destination = data.Slice(byteIndex);
if (destination.Length < Vector<byte>.Count)
{
throw new ArgumentException("Destination is too short");
}
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(data.Slice(byteIndex)), maskedData);
#endif
}

long endOfVecFilePos = filePos + totalVecElements;
Expand Down