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
publicstaticunsafestringFromNative(Encodingencoding,IntPtrpNativeData)
9595
{
96-
if(pNativeData==IntPtr.Zero)
96+
returnFromNative(encoding,(byte*)pNativeData);
97+
}
98+
99+
publicstaticunsafestringFromNative(Encodingencoding,byte*pNativeData)
100+
{
101+
if(pNativeData==null)
97102
{
98103
returnnull;
99104
}
@@ -112,7 +117,7 @@ public static unsafe string FromNative(Encoding encoding, IntPtr pNativeData)
112117
returnString.Empty;
113118
}
114119

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

118123
publicstaticunsafestringFromNative(Encodingencoding,IntPtrpNativeData,intlength)

‎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)=>newPeelException(m,r,c)},
131131
};
132132

133-
privatestaticvoidHandleError(intresult)
133+
privatestaticunsafevoidHandleError(intresult)
134134
{
135135
stringerrorMessage;
136-
GitErrorerror=null;
137-
varerrHandle=NativeMethods.giterr_last();
138-
139-
if(errHandle!=null&&!errHandle.IsInvalid)
140-
{
141-
error=errHandle.MarshalAsGitError();
142-
}
136+
GitErrorCategoryerrorCategory=GitErrorCategory.Unknown;
137+
GitError*error=NativeMethods.giterr_last();
143138

144139
if(error==null)
145140
{
146-
error=newGitError{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)=>newLibGit2SharpException(m,r,c);
158152
}
159153

160-
throwexceptionBuilder(errorMessage,(GitErrorCode)result,error.Category);
154+
throwexceptionBuilder(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
namespaceLibGit2Sharp.Core
55
{
66
[StructLayout(LayoutKind.Sequential)]
7-
internalclassGitError
7+
internalunsafestructGitError
88
{
9-
publicIntPtrMessage;
9+
publicchar*Message;
1010
publicGitErrorCategoryCategory;
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-
internalstaticexternGitErrorSafeHandlegiterr_last();
82+
internalstaticexternunsafeGitError*giterr_last();
8383

8484
[DllImport(libgit2)]
8585
internalstaticexternvoidgiterr_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+
publicstaticunsafestringFromNative(char*pNativeData)
116+
{
117+
returnFromNative(Encoding,(byte*)pNativeData);
118+
}
119+
115120
publicstaticstringFromNative(IntPtrpNativeData)
116121
{
117122
returnFromNative(Encoding,pNativeData);

‎LibGit2Sharp/LibGit2Sharp.csproj‎

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

0 commit comments

Comments
(0)