Skip to content

Make OdbBackend.Read[Prefix]() methods return an UnmanagedMemoryStream #715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2014
Merged
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
4 changes: 2 additions & 2 deletions LibGit2Sharp.Tests/OdbBackendFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ protected override OdbBackendOperations SupportedOperations
}
}

public override int Read(ObjectId oid, out Stream data, out ObjectType objectType)
public override int Read(ObjectId oid, out UnmanagedMemoryStream data, out ObjectType objectType)
{
data = null;
objectType = default(ObjectType);
Expand All @@ -264,7 +264,7 @@ public override int Read(ObjectId oid, out Stream data, out ObjectType objectTyp
return (int)ReturnCode.GIT_OK;
}

public override int ReadPrefix(string shortSha, out ObjectId id, out Stream data, out ObjectType objectType)
public override int ReadPrefix(string shortSha, out ObjectId id, out UnmanagedMemoryStream data, out ObjectType objectType)
{
id = null;
data = null;
Expand Down
60 changes: 36 additions & 24 deletions LibGit2Sharp/OdbBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,45 @@ protected abstract OdbBackendOperations SupportedOperations
/// Call this method from your implementations of Read and ReadPrefix to allocate a buffer in
/// which to return the object's data.
/// </summary>
/// <param name="bytes">Number of bytes to allocate</param>
/// <returns>An Stream for you to write to and then return. Do not dispose this object before returning it.</returns>
protected unsafe Stream Allocate(long bytes)
/// <param name="bytes">The bytes to be copied to the stream.</param>
/// <returns>
/// A Stream already filled with the content of provided the byte array.
/// Do not dispose this object before returning it.
/// </returns>
protected UnmanagedMemoryStream AllocateAndBuildFrom(byte[] bytes)
{
if (bytes < 0 ||
(UIntPtr.Size == sizeof(int) && bytes > int.MaxValue))
var stream = Allocate(bytes.Length);

stream.Write(bytes, 0, bytes.Length);

return stream;
}

/// <summary>
/// Call this method from your implementations of Read and ReadPrefix to allocate a buffer in
/// which to return the object's data.
/// </summary>
/// <param name="size">Number of bytes to allocate</param>
/// <returns>A Stream for you to write to and then return. Do not dispose this object before returning it.</returns>
protected unsafe UnmanagedMemoryStream Allocate(long size)
{
if (size < 0 ||
(UIntPtr.Size == sizeof(int) && size > int.MaxValue))
{
throw new ArgumentOutOfRangeException("bytes");
throw new ArgumentOutOfRangeException("size");
}

IntPtr buffer = Proxy.git_odb_backend_malloc(this.GitOdbBackendPointer, new UIntPtr((ulong)bytes));
IntPtr buffer = Proxy.git_odb_backend_malloc(this.GitOdbBackendPointer, new UIntPtr((ulong)size));

return new UnmanagedMemoryStream((byte*)buffer, 0, bytes, FileAccess.ReadWrite);
return new UnmanagedMemoryStream((byte*)buffer, 0, size, FileAccess.ReadWrite);
}

/// <summary>
/// Requests that this backend read an object.
/// </summary>
public abstract int Read(
ObjectId id,
out Stream data,
out UnmanagedMemoryStream data,
out ObjectType objectType);

/// <summary>
Expand All @@ -65,7 +83,7 @@ public abstract int Read(
public abstract int ReadPrefix(
string shortSha,
out ObjectId oid,
out Stream data,
out UnmanagedMemoryStream data,
out ObjectType objectType);

/// <summary>
Expand Down Expand Up @@ -242,21 +260,18 @@ private unsafe static int Read(
return (int)GitErrorCode.Error;
}

Stream dataStream = null;
UnmanagedMemoryStream memoryStream = null;

try
{
ObjectType objectType;
int toReturn = odbBackend.Read(new ObjectId(oid), out dataStream, out objectType);
int toReturn = odbBackend.Read(new ObjectId(oid), out memoryStream, out objectType);

if (toReturn != 0)
{
return toReturn;
}

// Caller is expected to give us back a stream created with the Allocate() method.
var memoryStream = dataStream as UnmanagedMemoryStream;

if (memoryStream == null)
{
return (int)GitErrorCode.Error;
Expand All @@ -276,9 +291,9 @@ private unsafe static int Read(
}
finally
{
if (dataStream != null)
if (memoryStream != null)
{
dataStream.Dispose();
memoryStream.Dispose();
}
}

Expand All @@ -305,7 +320,7 @@ private unsafe static int ReadPrefix(
return (int)GitErrorCode.Error;
}

Stream dataStream = null;
UnmanagedMemoryStream memoryStream = null;

try
{
Expand All @@ -314,16 +329,13 @@ private unsafe static int ReadPrefix(
ObjectId oid;
ObjectType objectType;

int toReturn = odbBackend.ReadPrefix(shortSha, out oid, out dataStream, out objectType);
int toReturn = odbBackend.ReadPrefix(shortSha, out oid, out memoryStream, out objectType);

if (toReturn != 0)
{
return toReturn;
}

// Caller is expected to give us back a stream created with the Allocate() method.
var memoryStream = dataStream as UnmanagedMemoryStream;

if (memoryStream == null)
{
return (int)GitErrorCode.Error;
Expand All @@ -343,9 +355,9 @@ private unsafe static int ReadPrefix(
}
finally
{
if (null != dataStream)
if (memoryStream != null)
{
dataStream.Dispose();
memoryStream.Dispose();
}
}

Expand Down