Skip to content

Commit b1f1f47

Browse files
committed
Use a pointer to retrieve the library's error
As the first step to moving away from marshalling data into managed memory when we can avoid it, we start with a simple one which we only read from.
1 parent f8c944b commit b1f1f47

File tree

7 files changed

+20
-37
lines changed

7 files changed

+20
-37
lines changed

LibGit2Sharp/Core/EncodingMarshaler.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ public static void Cleanup(IntPtr pNativeData)
9393

9494
public static unsafe string FromNative(Encoding encoding, IntPtr pNativeData)
9595
{
96-
if (pNativeData == IntPtr.Zero)
96+
return FromNative(encoding, (byte*)pNativeData);
97+
}
98+
99+
public static unsafe string FromNative(Encoding encoding, byte* pNativeData)
100+
{
101+
if (pNativeData == null)
97102
{
98103
return null;
99104
}
@@ -112,7 +117,7 @@ public static unsafe string FromNative(Encoding encoding, IntPtr pNativeData)
112117
return String.Empty;
113118
}
114119

115-
return new String((sbyte*)pNativeData.ToPointer(), 0, (int)(walk - start), encoding);
120+
return new String((sbyte*)pNativeData, 0, (int)(walk - start), encoding);
116121
}
117122

118123
public static unsafe string FromNative(Encoding encoding, IntPtr pNativeData, int length)

LibGit2Sharp/Core/Ensure.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,25 +130,19 @@ private static readonly Dictionary<GitErrorCode, Func<string, GitErrorCode, GitE
130130
{ GitErrorCode.Peel, (m, r, c) => new PeelException(m, r, c) },
131131
};
132132

133-
private static void HandleError(int result)
133+
private static unsafe void HandleError(int result)
134134
{
135135
string errorMessage;
136-
GitError error = null;
137-
var errHandle = NativeMethods.giterr_last();
138-
139-
if (errHandle != null && !errHandle.IsInvalid)
140-
{
141-
error = errHandle.MarshalAsGitError();
142-
}
136+
GitErrorCategory errorCategory = GitErrorCategory.Unknown;
137+
GitError* error = NativeMethods.giterr_last();
143138

144139
if (error == null)
145140
{
146-
error = new GitError { Category = GitErrorCategory.Unknown, Message = IntPtr.Zero };
147141
errorMessage = "No error message has been provided by the native library";
148142
}
149143
else
150144
{
151-
errorMessage = LaxUtf8Marshaler.FromNative(error.Message);
145+
errorMessage = LaxUtf8Marshaler.FromNative(error->Message);
152146
}
153147

154148
Func<string, GitErrorCode, GitErrorCategory, LibGit2SharpException> exceptionBuilder;
@@ -157,7 +151,7 @@ private static void HandleError(int result)
157151
exceptionBuilder = (m, r, c) => new LibGit2SharpException(m, r, c);
158152
}
159153

160-
throw exceptionBuilder(errorMessage, (GitErrorCode)result, error.Category);
154+
throw exceptionBuilder(errorMessage, (GitErrorCode)result, errorCategory);
161155
}
162156

163157
/// <summary>

LibGit2Sharp/Core/GitError.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
namespace LibGit2Sharp.Core
55
{
66
[StructLayout(LayoutKind.Sequential)]
7-
internal class GitError
7+
internal unsafe struct GitError
88
{
9-
public IntPtr Message;
9+
public char* Message;
1010
public GitErrorCategory Category;
1111
}
1212
}

LibGit2Sharp/Core/Handles/GitErrorSafeHandle.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static NativeMethods()
7979
}
8080

8181
[DllImport(libgit2)]
82-
internal static extern GitErrorSafeHandle giterr_last();
82+
internal static extern unsafe GitError* giterr_last();
8383

8484
[DllImport(libgit2)]
8585
internal static extern void giterr_set_str(

LibGit2Sharp/Core/Utf8Marshaler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ public override IntPtr MarshalManagedToNative(object managedObj)
112112

113113
#endregion
114114

115+
public static unsafe string FromNative(char* pNativeData)
116+
{
117+
return FromNative(Encoding, (byte*)pNativeData);
118+
}
119+
115120
public static string FromNative(IntPtr pNativeData)
116121
{
117122
return FromNative(Encoding, pNativeData);

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,6 @@
280280
<Compile Include="Core\GitErrorCategory.cs" />
281281
<Compile Include="Core\GitOdbBackend.cs" />
282282
<Compile Include="Core\GitOdbBackendStream.cs" />
283-
<Compile Include="Core\Handles\GitErrorSafeHandle.cs" />
284283
<Compile Include="Core\Handles\NoteSafeHandle.cs" />
285284
<Compile Include="Core\Handles\ObjectDatabaseSafeHandle.cs" />
286285
<Compile Include="Core\Handles\DiffSafeHandle.cs" />

0 commit comments

Comments
 (0)