Fixes for memory mapping issues

This commit is contained in:
Timothy Baldridge 2020-03-09 14:38:35 -06:00
parent 1bafe1f13c
commit 3ef75a248d
9 changed files with 21 additions and 16 deletions

View File

@ -99,6 +99,7 @@ namespace Compression.BSA.Test
TestContext.WriteLine($"Reading {bsa}");
string tempFile = Path.Combine("tmp.bsa");
var size = File.GetSize(bsa);
using (var a = BSADispatch.OpenRead(bsa))
{
await a.Files.PMap(Queue, file =>
@ -120,7 +121,7 @@ namespace Compression.BSA.Test
Console.WriteLine($"Building {bsa}");
using (var w = ViaJson(a.State).MakeBuilder())
using (var w = ViaJson(a.State).MakeBuilder(size))
{
var streams = await a.Files.PMap(Queue, file =>
{

View File

@ -27,10 +27,10 @@ namespace Compression.BSA
private List<IFileBuilder> _entries = new List<IFileBuilder>();
private DiskSlabAllocator _slab;
public BA2Builder(BA2StateObject state)
public BA2Builder(BA2StateObject state, long size)
{
_state = state;
_slab = new DiskSlabAllocator();
_slab = new DiskSlabAllocator(size);
}
public void Dispose()

View File

@ -125,9 +125,9 @@ namespace Compression.BSA
public EntryType Type { get; set; }
public string HeaderMagic { get; set; }
public uint Version { get; set; }
public override IBSABuilder MakeBuilder()
public override IBSABuilder MakeBuilder(long size)
{
return new BA2Builder(this);
return new BA2Builder(this, size);
}
}

View File

@ -25,14 +25,14 @@ namespace Compression.BSA
internal uint _version;
internal DiskSlabAllocator _slab;
public BSABuilder()
public BSABuilder(long size)
{
_fileId = Encoding.ASCII.GetBytes("BSA\0");
_offset = 0x24;
_slab = new DiskSlabAllocator();
_slab = new DiskSlabAllocator(size);
}
public BSABuilder(BSAStateObject bsaStateObject) : this()
public BSABuilder(BSAStateObject bsaStateObject, long size) : this(size)
{
_version = bsaStateObject.Version;
_fileFlags = bsaStateObject.FileFlags;

View File

@ -163,9 +163,9 @@ namespace Compression.BSA
}
public override IBSABuilder MakeBuilder()
public override IBSABuilder MakeBuilder(long size)
{
return new BSABuilder(this);
return new BSABuilder(this, size);
}
public string Magic { get; set; }

View File

@ -22,7 +22,7 @@ namespace Compression.BSA
public class ArchiveStateObject
{
public virtual IBSABuilder MakeBuilder()
public virtual IBSABuilder MakeBuilder(long size)
{
throw new NotImplementedException();
}

View File

@ -87,7 +87,7 @@ namespace Compression.BSA
public uint HashOffset { get; set; }
public uint VersionNumber { get; set; }
public override IBSABuilder MakeBuilder()
public override IBSABuilder MakeBuilder(long size)
{
return new TES3Builder(this);
}

View File

@ -14,11 +14,13 @@ namespace Wabbajack.Common
private MemoryMappedFile _mmap;
private long _head = 0;
private string _name;
private FileStream _fileStream;
public DiskSlabAllocator()
public DiskSlabAllocator(long size)
{
_name = Guid.NewGuid().ToString();
_mmap = MemoryMappedFile.CreateNew(null, (long)1 << 34);
_file = new TempFile();
_fileStream = _file.File.Open(FileMode.Create, FileAccess.ReadWrite);
_mmap = MemoryMappedFile.CreateFromFile(_fileStream, null, size, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false);
}
public Stream Allocate(long size)
@ -34,6 +36,8 @@ namespace Wabbajack.Common
public void Dispose()
{
_mmap?.Dispose();
_fileStream?.Dispose();
_file?.Dispose();
}
}
}

View File

@ -237,7 +237,7 @@ namespace Wabbajack.Lib
Status($"Building {bsa.To}");
var sourceDir = Path.Combine(OutputFolder, Consts.BSACreationDir, bsa.TempID);
using (var a = bsa.State.MakeBuilder())
using (var a = bsa.State.MakeBuilder(bsa.Size))
{
var streams = await bsa.FileStates.PMap(Queue, state =>
{