This repository was archived by the owner on Feb 3, 2023. It is now read-only.
forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathGitStrArrayIn.cs
More file actions
62 lines (49 loc) · 1.58 KB
/
GitStrArrayIn.cs
File metadata and controls
62 lines (49 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Runtime.InteropServices;
namespace LibGit2Sharp.Core
{
[StructLayout(LayoutKind.Sequential)]
internal class GitStrArrayIn : IDisposable
{
public IntPtr strings;
public uint size;
public static GitStrArrayIn BuildFrom(string[] strings)
{
return BuildFrom(strings, StrictUtf8Marshaler.FromManaged);
}
public static GitStrArrayIn BuildFrom(FilePath[] paths)
{
return BuildFrom(paths, StrictFilePathMarshaler.FromManaged);
}
private static GitStrArrayIn BuildFrom<T>(T[] input, Func<T, IntPtr> marshaler)
{
var count = input.Length;
var pointers = new IntPtr[count];
for (int i = 0; i < count; i++)
{
var item = input[i];
pointers[i] = marshaler(item);
}
int dim = IntPtr.Size * count;
IntPtr arrayPtr = Marshal.AllocHGlobal(dim);
Marshal.Copy(pointers, 0, arrayPtr, count);
return new GitStrArrayIn { strings = arrayPtr, size = (uint)count };
}
public void Dispose()
{
if (size == 0)
{
return;
}
var count = (int)size;
var pointers = new IntPtr[count];
Marshal.Copy(strings, pointers, 0, count);
for (int i = 0; i < count; i++)
{
EncodingMarshaler.Cleanup(pointers[i]);
}
Marshal.FreeHGlobal(strings);
size = 0;
}
}
}