8000 git_strarray marshaling improvements · GiTechLab/libgit2sharp@18c57ac · GitHub
[go: up one dir, main page]

Skip to content

Commit 18c57ac

Browse files
phkelleynulltoken
authored andcommitted
git_strarray marshaling improvements
1 parent 2378a56 commit 18c57ac

14 files changed

+283
-261
lines changed

LibGit2Sharp/Core/GitCheckoutOpts.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ internal struct GitCheckoutOpts
138138
public progress_cb progress_cb;
139139
public IntPtr progress_payload;
140140

141-
public GitStrArrayIn paths;
141+
public GitStrArray paths;
142142

143143
public IntPtr baseline;
144144
public IntPtr target_directory;

LibGit2Sharp/Core/GitCheckoutOptsWrapper.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public GitCheckoutOptsWrapper(IConvertableToGitCheckoutOpts options, FilePath[]
2222

2323
if (paths != null)
2424
{
25-
PathArray = GitStrArrayIn.BuildFrom(paths);
25+
PathArray = GitStrArrayManaged.BuildFrom(paths);
2626
}
2727

2828
Options = new GitCheckoutOpts
@@ -32,7 +32,7 @@ public GitCheckoutOptsWrapper(IConvertableToGitCheckoutOpts options, FilePath[]
3232
progress_cb = Callbacks.CheckoutProgressCallback,
3333
notify_cb = Callbacks.CheckoutNotifyCallback,
3434
notify_flags = options.CheckoutNotifyFlags,
35-
paths = PathArray,
35+
paths = PathArray.Array,
3636
};
3737
}
3838

@@ -50,23 +50,11 @@ public GitCheckoutOptsWrapper(IConvertableToGitCheckoutOpts options, FilePath[]
5050
/// <summary>
5151
/// Keep the paths around so we can dispose them.
5252
/// </summary>
53-
private GitStrArrayIn PathArray;
53+
private GitStrArrayManaged PathArray;
5454

5555
public void Dispose()
5656
{
57-
Dispose(true);
58-
}
59-
60-
private void Dispose(bool disposing)
61-
{
62-
if (disposing)
63-
{
64-
if (PathArray != null)
65-
{
66-
PathArray.Dispose();
67-
PathArray = null;
68-
}
69-
}
57+
PathArray.Dispose();
7058
}
7159

7260
/// <summary>

LibGit2Sharp/Core/GitDiff.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ internal class GitDiffOptions : IDisposable
189189
/* options controlling which files are in the diff */
190190

191191
public SubmoduleIgnore IgnoreSubmodules;
192-
public GitStrArrayIn PathSpec;
192+
public GitStrArrayManaged PathSpec;
193193
public diff_notify_cb NotifyCallback;
194194
public IntPtr NotifyPayload;
195195

@@ -204,11 +204,6 @@ internal class GitDiffOptions : IDisposable
204204

205205
public void Dispose()
206206
{
207-
if (PathSpec == null)
208-
{
209-
return;
210-
}
211-
212207
PathSpec.Dispose();
213208
}
214209
}

LibGit2Sharp/Core/GitStatusOptions.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,10 @@ internal class GitStatusOptions : IDisposable
1414
public GitStatusShow Show;
1515
public GitStatusOptionFlags Flags;
1616

17-
GitStrArrayIn PathSpec;
17+
GitStrArrayManaged PathSpec;
1818

1919
public void Dispose()
2020
{
21-
if (PathSpec == null)
22-
{
23-
return;
24-
}
25-
2621
PathSpec.Dispose();
2722
}
2823
}

LibGit2Sharp/Core/GitStrArray.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.InteropServices;
4+
using System.Text;
5+
6+
namespace LibGit2Sharp.Core
7+
{
8+
[StructLayout(LayoutKind.Sequential)]
9+
internal struct GitStrArray
10+
{
11+
/// <summary>
12+
/// A pointer to an array of null-terminated strings.
13+
/// </summary>
14+
public IntPtr Strings;
15+
16+
/// <summary>
17+
/// The number of strings in the array.
18+
/// </summary>
19+
public UIntPtr Count;
20+
21+
/// <summary>
22+
/// Resets the GitStrArray to default values.
23+
/// </summary>
24+
public void Reset()
25+
{
26+
Strings = IntPtr.Zero;
27+
Count = UIntPtr.Zero;
28+
}
29+
}
30+
}

LibGit2Sharp/Core/GitStrArrayIn.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace LibGit2Sharp.Core
5+
{
6+
/// <summary>
7+
/// A git_strarray where the string array and strings themselves were allocated
8+
/// with LibGit2Sharp's allocator (Marshal.AllocHGlobal).
9+
/// </summary>
10+
[StructLayout(LayoutKind.Sequential)]
11+
internal struct GitStrArrayManaged : IDisposable
12+
{
13+
public GitStrArray Array;
14+
15+
public static GitStrArrayManaged BuildFrom(string[] strings)
16+
{
17+
return BuildFrom(strings, StrictUtf8Marshaler.FromManaged);
18+
}
19+
20+
public static GitStrArrayManaged BuildFrom(FilePath[] paths)
21+
{
22+
return BuildFrom(paths, StrictFilePathMarshaler.FromManaged);
23+
}
24+
25+
private static GitStrArrayManaged BuildFrom<T>(T[] input, Func<T, IntPtr> marshaler)
26+
{
27+
var pointers = new IntPtr[input.Length];
28+
29+
for (int i = 0; i < input.Length; i++)
30+
{
31+
var item = input[i];
32+
pointers[i] = marshaler(item);
33+
}
34+
35+
var toReturn = new GitStrArrayManaged();
36+
37+
toReturn.Array.Strings = Marshal.AllocHGlobal(checked(IntPtr.Size * input.Length));
38+
Marshal.Copy(pointers, 0, toReturn.Array.Strings, input.Length);
39+
toReturn.Array.Count = new UIntPtr((uint)input.Length);
40+
41+
return toReturn;
42+
}
43+
44+
public void Dispose()
45+
{
46+
var count = checked((int)Array.Count.ToUInt32());
47+
48+
for (int i = 0; i < count; i++)
49+
{
50+
EncodingMarshaler.Cleanup(Marshal.ReadIntPtr(Array.Strings, i * IntPtr.Size));
51+
}
52+
53+
if (Array.Strings != IntPtr.Zero)
54+
{
55+
Marshal.FreeHGlobal(Array.Strings);
56+
}
57+
58+
// Now that we've freed the memory, zero out the structure.
59+
Array.Reset();
60+
}
61+
}
62+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.InteropServices;
4+
5+
namespace LibGit2Sharp.Core
6+
{
7+
/// <summary>
8+
/// A git_strarray where the string array and strings themselves were allocated
9+
/// with libgit2's allocator. Only libgit2 can free this git_strarray.
10+
/// </summary>
11+
[StructLayout(LayoutKind.Sequential)]
12+
internal struct GitStrArrayNative : IDisposable
13+
{
14+
public GitStrArray Array;
15+
16+
/// <summary>
17+
/// Enumerates each string from the array using the UTF-8 marshaler.
18+
/// </summary>
19+
public String[] ReadStrings()
20+
{
21+
var count = checked((int)Array.Count.ToUInt32());
22+
23+
String[] toReturn = new String[count];
24+
25+
for (int i = 0; i < count; i++)
26+
{
27+
toReturn[i] = LaxUtf8Marshaler.FromNative(Marshal.ReadIntPtr(Array.Strings, i * IntPtr.Size));
28+
}
29+
30+
return toReturn;
31+
}
32+
33+
public void Dispose()
34+
{
35+
if (Array.Strings != IntPtr.Zero)
36+
{
37+
NativeMethods.git_strarray_free(ref Array);
38+
}
39+
40+
// Now that we've freed the memory, zero out the structure.
41+
Array.Reset();
42+
}
43+
}
44+
}

LibGit2Sharp/Core/GitStrArrayOut.cs

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

0 commit comments

Comments
 (0)
0