Skip to content

Commit

Permalink
Merge pull request #548 from qmfrederik/fixes/dispose-housekeeping
Browse files Browse the repository at this point in the history
Managed Git: .Dispose() housekeeping
  • Loading branch information
AArnott committed Dec 7, 2020
2 parents 90d7e39 + 75d370f commit a44c1fa
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/NerdBank.GitVersioning/GitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ private protected static void FindGitPaths(string path, out string gitDirectory,
}
}

/// <inheritdoc />
/// <summary>
/// Disposes of native and managed resources associated by this object.
/// </summary>
/// <param name="disposing"><see langword="true" /> to dispose managed and native resources; <see langword="false" /> to only dispose of native resources.</param>
protected virtual void Dispose(bool disposing)
{
}
Expand Down
11 changes: 11 additions & 0 deletions src/NerdBank.GitVersioning/Managed/ManagedGitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ public override string GetShortUniqueCommitId(int minLength)
return this.Repository.ShortenObjectId(this.Commit.Value.Sha, minLength);
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (disposing)
{
this.Repository.Dispose();
}

base.Dispose(disposing);
}

/// <summary>
/// Encodes a commit from history in a <see cref="Version"/>
/// so that the original commit can be found later.
Expand Down
1 change: 1 addition & 0 deletions src/NerdBank.GitVersioning/ManagedGit/GitPack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ public void Dispose()

this.accessor.Dispose();
this.packFile.Dispose();
this.cache.Dispose();
}

private long? GetOffset(GitObjectId objectId)
Expand Down
18 changes: 17 additions & 1 deletion src/NerdBank.GitVersioning/ManagedGit/GitPackCache.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable

using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
Expand All @@ -12,7 +13,7 @@ namespace Nerdbank.GitVersioning.ManagedGit
/// data from a <see cref="GitPack"/> can be potentially expensive: the data is
/// compressed and can be deltified.
/// </summary>
public abstract class GitPackCache
public abstract class GitPackCache : IDisposable
{
/// <summary>
/// Attempts to retrieve a Git object from cache.
Expand Down Expand Up @@ -51,5 +52,20 @@ public abstract class GitPackCache
/// A <see cref="Stream"/> which represents the cached entry.
/// </returns>
public abstract Stream Add(long offset, Stream stream);

/// <inheritdoc/>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Disposes of native and managed resources associated by this object.
/// </summary>
/// <param name="disposing"><see langword="true" /> to dispose managed and native resources; <see langword="false" /> to only dispose of native resources.</param>
protected virtual void Dispose(bool disposing)
{
}
}
}
18 changes: 18 additions & 0 deletions src/NerdBank.GitVersioning/ManagedGit/GitPackMemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;

namespace Nerdbank.GitVersioning.ManagedGit
Expand Down Expand Up @@ -55,5 +56,22 @@ public override void GetCacheStatistics(StringBuilder builder)
{
builder.AppendLine($"{this.cache.Count} items in cache");
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (disposing)
{
while (this.cache.Count > 0)
{
var key = this.cache.Keys.First();
var stream = this.cache[key];
stream.Dispose();
this.cache.Remove(key);
}
}

base.Dispose(disposing);
}
}
}
11 changes: 11 additions & 0 deletions src/NerdBank.GitVersioning/ManagedGit/GitPackMemoryCacheStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ public override void Write(byte[] buffer, int offset, int count)
throw new NotSupportedException();
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
this.stream.Dispose();
this.cacheStream.Dispose();
}

base.Dispose(disposing);
}

private void DisposeStreamIfRead()
{
if (this.cacheStream.Length == this.stream.Length)
Expand Down
28 changes: 28 additions & 0 deletions src/NerdBank.GitVersioning/ManagedGit/MemoryMappedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public unsafe class MemoryMappedStream : Stream
private readonly long length;
private long position;
private byte* ptr;
private bool disposed;

/// <summary>
/// Initializes a new instance of the <see cref="MemoryMappedStream"/> class.
Expand Down Expand Up @@ -57,6 +58,11 @@ public override void Flush()
/// <inheritdoc/>
public override int Read(byte[] buffer, int offset, int count)
{
if (this.disposed)
{
throw new ObjectDisposedException(nameof(MemoryMappedStream));
}

int read = (int)Math.Min(count, this.length - this.position);

new Span<byte>(this.ptr + this.position, read)
Expand All @@ -70,6 +76,11 @@ public override int Read(byte[] buffer, int offset, int count)
/// <inheritdoc/>
public override int Read(Span<byte> buffer)
{
if (this.disposed)
{
throw new ObjectDisposedException(nameof(MemoryMappedStream));
}

int read = (int)Math.Min(buffer.Length, this.length - this.position);

new Span<byte>(this.ptr + this.position, read)
Expand All @@ -83,6 +94,11 @@ public override int Read(Span<byte> buffer)
/// <inheritdoc/>
public override long Seek(long offset, SeekOrigin origin)
{
if (this.disposed)
{
throw new ObjectDisposedException(nameof(MemoryMappedStream));
}

long newPosition = this.position;

switch (origin)
Expand Down Expand Up @@ -124,5 +140,17 @@ public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (disposing)
{
this.accessor.SafeMemoryMappedViewHandle.ReleasePointer();
this.disposed = true;
}

base.Dispose(disposing);
}
}
}

0 comments on commit a44c1fa

Please sign in to comment.