8000 Use custom marshaling · libgit2/libgit2sharp@8f07d9c · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f07d9c

Browse files
committed
Use custom marshaling
1 parent fffc5a6 commit 8f07d9c

File tree

4 files changed

+332
-340
lines changed

4 files changed

+332
-340
lines changed

LibGit2Sharp/Core/EncodingMarshaler.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,7 @@ public static unsafe string FromNative(Encoding encoding, byte* pNativeData)
117117
return String.Empty;
118118
}
119119

120-
#if DESKTOP
121120
return new String((sbyte*)pNativeData, 0, (int)(walk - start), encoding);
122-
#else
123-
return encoding.GetString(pNativeData, (int)(walk - start));
124-
#endif
125121
}
126122

127123
public static unsafe string FromNative(Encoding encoding, IntPtr pNativeData, int length)
@@ -136,11 +132,7 @@ public static unsafe string FromNative(Encoding encoding, IntPtr pNativeData, in
136132
return String.Empty;
137133
}
138134

139-
#if DESKTOP
140135
return new String((sbyte*)pNativeData.ToPointer(), 0, length, encoding);
141-
#else
142-
return encoding.GetString((byte*)pNativeData.ToPointer(), length);
143-
#endif
144136
}
145137

146138
public static string FromBuffer(Encoding encoding, byte[] buffer)

LibGit2Sharp/Core/FilePathMarshaler.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ namespace LibGit2Sharp.Core
1010
/// free the native pointer after conversion, because the memory is owned by libgit2.
1111
///
1212
/// Use this marshaler for return values, for example:
13-
/// [return: CustomMarshaler(typeof(LaxFilePathNoCleanupMarshaler), typeof(FilePath))]
13+
/// [return: MarshalAs(UnmanagedType.CustomMarshaler,
14+
/// MarshalCookie = UniqueId.UniqueIdentifier,
15+
/// MarshalTypeRef = typeof(LaxFilePathNoCleanupMarshaler))]
1416
/// </summary>
1517
internal class LaxFilePathNoCleanupMarshaler : LaxFilePathMarshaler
1618
{
1719
private static readonly LaxFilePathNoCleanupMarshaler staticInstance = new LaxFilePathNoCleanupMarshaler();
1820

19-
public new static ICustomMarshaler GetInstance()
21+
public new static ICustomMarshaler GetInstance(string cookie)
2022
{
2123
return staticInstance;
2224
}
@@ -39,15 +41,16 @@ public override void CleanUpNativeData(IntPtr pNativeData)
3941
///
4042
/// Use this marshaler for function parameters, for example:
4143
/// [DllImport(libgit2)]
42-
/// private static extern unsafe int git_index_remove_bypath(
43-
/// git_index* index,
44-
/// [CustomMarshaler(typeof(StrictFilePathMarshaler), typeof(FilePath))] byte* path);
44+
/// internal static extern int git_index_open(out IndexSafeHandle index,
45+
/// [MarshalAs(UnmanagedType.CustomMarshaler,
46+
/// MarshalCookie = UniqueId.UniqueIdentifier,
47+
/// MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath indexpath);
4548
/// </summary>
4649
internal class StrictFilePathMarshaler : StrictUtf8Marshaler
4750
{
4851
private static readonly StrictFilePathMarshaler staticInstance = new StrictFilePathMarshaler();
4952

50-
public new static ICustomMarshaler GetInstance()
53+
public new static ICustomMarshaler GetInstance(string cookie)
5154
{
5255
return staticInstance;
5356
}
@@ -95,7 +98,7 @@ internal class LaxFilePathMarshaler : LaxUtf8Marshaler
9598
{
9699
private static readonly LaxFilePathMarshaler staticInstance = new LaxFilePathMarshaler();
97100

98-
public new static ICustomMarshaler GetInstance()
101+
public new static ICustomMarshaler GetInstance(string cookie)
99102
{
100103
return staticInstance;
101104
}

0 commit comments

Comments
 (0)
0