8000 Get rid of the rest of MarshalAs · vrkcse2011/libgit2sharp@b55cd1c · GitHub
[go: up one dir, main page]

Skip to content

Commit b55cd1c

Browse files
committed
Get rid of the rest of MarshalAs
This removes the last of the instances where we copy the C struct into a managed struct, where we're just going to copy it to another managed struct. Instead copy straight from the unmanaged to memanged memory just once.
1 parent d3622c3 commit b55cd1c

26 files changed

+253
-185
lines changed

LibGit2Sharp/BlameHunk.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,34 @@ public class BlameHunk : IEquatable<BlameHunk>
1919
x => x.InitialSignature,
2020
x => x.InitialCommit);
2121

22-
23-
internal unsafe BlameHunk(IRepository repository, GitBlameHunk rawHunk)
22+
internal unsafe BlameHunk(IRepository repository, git_blame_hunk* rawHunk)
2423
{
25-
finalCommit = new Lazy<Commit>(() => repository.Lookup<Commit>(rawHunk.FinalCommitId));
26-
origCommit = new Lazy<Commit>(() => repository.Lookup<Commit>(rawHunk.OrigCommitId));
24+
var origId = ObjectId.BuildFromPtr(&rawHunk->orig_commit_id);
25+
var finalId = ObjectId.BuildFromPtr(&rawHunk->final_commit_id);
26+
27+
finalCommit = new Lazy<Commit>(() => repository.Lookup<Commit>(finalId));
28+
origCommit = new Lazy<Commit>(() => repository.Lookup<Commit>(origId));
29+
2730

28-
if (rawHunk.OrigPath != IntPtr.Zero)
31+
if (rawHunk->orig_path != null)
2932
{
30-
InitialPath = LaxUtf8Marshaler.FromNative(rawHunk.OrigPath);
33+
InitialPath = LaxUtf8Marshaler.FromNative(rawHunk->orig_path);
3134
}
32-
LineCount = rawHunk.LinesInHunk;
35+
LineCount = rawHunk->lines_in_hunk;
3336

3437
// Libgit2's line numbers are 1-based
35-
FinalStartLineNumber = rawHunk.FinalStartLineNumber - 1;
36-
InitialStartLineNumber = rawHunk.OrigStartLineNumber - 1;
38+
FinalStartLineNumber = rawHunk->final_start_line_number - 1;
39+
InitialStartLineNumber = rawHunk->orig_start_line_number - 1;
3740

3841
// Signature objects need to have ownership of their native pointers
39-
if (rawHunk.FinalSignature != null)
42+
if (rawHunk->final_signature != null)
4043
{
41-
FinalSignature = new Signature(Proxy.git_signature_dup(rawHunk.FinalSignature));
44+
FinalSignature = new Signature(rawHunk->final_signature);
4245
}
4346

44-
if (rawHunk.OrigSignature != null)
47+
if (rawHunk->orig_signature != null)
4548
{
46-
InitialSignature = new Signature(Proxy.git_signature_dup(rawHunk.OrigSignature));
49+
InitialSignature = new Signature(rawHunk->orig_signature);
4750
}
4851
}
4952

LibGit2Sharp/BlameHunkCollection.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Runtime.InteropServices;
56
using LibGit2Sharp.Core;
67
using LibGit2Sharp.Core.Handles;
78

@@ -20,26 +21,32 @@ public class BlameHunkCollection : IEnumerable<BlameHunk>
2021
/// </summary>
2122
protected BlameHunkCollection() { }
2223

23-
internal BlameHunkCollection(Repository repo, RepositoryHandle repoHandle, string path, BlameOptions options)
24+
internal unsafe BlameHunkCollection(Repository repo, RepositoryHandle repoHandle, string path, BlameOptions options)
2425
{
2526
this.repo = repo;
2627

27-
var rawopts = new GitBlameOptions
28+
var rawopts = new git_blame_options
2829
{
2930
version = 1,
3031
flags = options.Strategy.ToGitBlameOptionFlags(),
31-
MinLine = (uint)options.MinLine,
32-
MaxLine = (uint)options.MaxLine,
32+
min_line = (uint)options.MinLine,
33+
max_line = (uint)options.MaxLine,
3334
};
3435

3536
if (options.StartingAt != null)
3637
{
37-
rawopts.NewestCommit = repo.Committish(options.StartingAt).Oid;
38+
fixed (byte* p = rawopts.newest_commit.Id)
39+
{
40+
Marshal.Copy(repo.Committish(options.StartingAt).Oid.Id, 0, new IntPtr(p), git_oid.Size);
41+
}
3842
}
3943

4044
if (options.StoppingAt != null)
4145
{
42-
rawopts.OldestCommit = repo.Committish(options.StoppingAt).Oid;
46+
fixed (byte* p = rawopts.oldest_commit.Id)
47+
{
48+
Marshal.Copy(repo.Committish(options.StoppingAt).Oid.Id, 0, new IntPtr(p), git_oid.Size);
49+
}
4350
}
4451

4552
using (var blameHandle = Proxy.git_blame_file(repoHandle, path, rawopts))

LibGit2Sharp/CertificateSsh.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using LibGit2Sharp.Core;
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using LibGit2Sharp.Core;
24

35
namespace LibGit2Sharp
46
{
@@ -37,17 +39,29 @@ protected CertificateSsh()
3739
/// True if we have the SHA1 hostkey hash from the server
3840
/// </summary>public readonly bool HasSHA1;
3941

40-
internal CertificateSsh(GitCertificateSsh cert)
42+
internal unsafe CertificateSsh(git_certificate_ssh* cert)
4143
{
4244

43-
HasMD5 = cert.type.HasFlag(GitCertificateSshType.MD5);
44-
HasSHA1 = cert.type.HasFlag(GitCertificateSshType.SHA1);
45+
HasMD5 = cert->type.HasFlag(GitCertificateSshType.MD5);
46+
HasSHA1 = cert->type.HasFlag(GitCertificateSshType.SHA1);
4547

4648
HashMD5 = new byte[16];
47-
cert.HashMD5.CopyTo(HashMD5, 0);
49+
fixed (byte* p = &HashMD5[0])
50+
{
51+
for (var i = 0; i < HashMD5.Length; i++)
52+
{
53+
HashMD5[i] = p[i];
54+
}
55+
}
4856

4957
HashSHA1 = new byte[20];
50-
cert.HashSHA1.CopyTo(HashSHA1, 0);
58+
fixed (byte* p = &HashSHA1[0])
59+
{
60+
for (var i = 0; i < HashSHA1.Length; i++)
61+
{
62+
HashMD5[i] = p[i];
63+
}
64+
}
5165
}
5266
}
5367
}

LibGit2Sharp/CertificateX509.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Runtime.InteropServices;
1+
using System;
2+
using System.Runtime.InteropServices;
23
using System.Security.Cryptography.X509Certificates;
34
using LibGit2Sharp.Core;
45

@@ -21,11 +22,11 @@ protected CertificateX509()
2122
/// </summary>
2223
public virtual X509Certificate Certificate { get; private set; }
2324

24-
internal CertificateX509(GitCertificateX509 cert)
25+
internal unsafe CertificateX509(git_certificate_x509* cert)
2526
{
26-
int len = checked((int) cert.len.ToUInt32());
27+
int len = checked((int) cert->len.ToUInt32());
2728
byte[] data = new byte[len];
28-
Marshal.Copy(cert.data, data, 0, len);
29+
Marshal.Copy(new IntPtr(cert->data), data, 0, len);
2930
Certificate = new X509Certificate(data);
3031
}
3132
}

LibGit2Sharp/Core/GitBlame.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,32 @@ internal enum GitBlameOptionFlags
4141
}
4242

4343
[StructLayout(LayoutKind.Sequential)]
44-
internal class GitBlameOptions
44+
internal class git_blame_options
4545
{
4646
public uint version = 1;
4747
public GitBlameOptionFlags flags;
48-
public UInt16 MinMatchCharacters;
49-
public GitOid NewestCommit;
50-
public GitOid OldestCommit;
51-
public uint MinLine;
52-
public uint MaxLine;
48+
public UInt16 min_match_characters;
49+
public git_oid newest_commit;
50+
public git_oid oldest_commit;
51+
public uint min_line;
52+
public uint max_line;
5353
}
5454

5555
[StructLayout(LayoutKind.Sequential)]
56-
internal unsafe class GitBlameHunk
56+
internal unsafe struct git_blame_hunk
5757
{
58-
public ushort LinesInHunk;
58+
public ushort lines_in_hunk;
5959

60-
public GitOid FinalCommitId;
61-
public ushort FinalStartLineNumber;
62-
public git_signature* FinalSignature;
60+
public git_oid final_commit_id;
61+
public ushort final_start_line_number;
62+
public git_signature* final_signature;
6363

64-
public GitOid OrigCommitId;
65-
public IntPtr OrigPath;
66-
public ushort OrigStartLineNumber;
67-
public git_signature* OrigSignature;
64+
public git_oid orig_commit_id;
65+
public char* orig_path;
66+
public ushort orig_start_line_number;
67+
public git_signature* orig_signature;
6868

69-
public byte Boundary;
69+
public byte boundary;
7070
}
7171

7272
internal static class BlameStrategyExtensions

LibGit2Sharp/Core/GitCertificate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace LibGit2Sharp.Core
44
{
55
[StructLayout(LayoutKind.Sequential)]
6-
internal struct GitCertificate
6+
internal struct git_certificate
77
{
88
public GitCertificateType type;
99
}

LibGit2Sharp/Core/GitCertificateSsh.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@
33
namespace LibGit2Sharp.Core
44
{
55
[StructLayout(LayoutKind.Sequential)]
6-
internal struct GitCertificateSsh
6+
internal unsafe struct git_certificate_ssh
77
{
88
public GitCertificateType cert_type;
99
public GitCertificateSshType type;
1010

1111
/// <summary>
1212
/// The MD5 hash (if appropriate)
1313
/// </summary>
14-
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
15-
public byte[] HashMD5;
14+
public unsafe fixed byte HashMD5[16];
1615

1716
/// <summary>
1817
/// The MD5 hash (if appropriate)
1918
/// </summary>
20-
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
21-
public byte[] HashSHA1;
19+
public unsafe fixed byte HashSHA1[20];
2220
}
2321
}

LibGit2Sharp/Core/GitCertificateX509.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace LibGit2Sharp.Core
55
{
66
[StructLayout(LayoutKind.Sequential)]
7-
internal struct GitCertificateX509
7+
internal unsafe struct git_certificate_x509
88
{
99
/// <summary>
1010
/// Type of the certificate, in this case, GitCertificateType.X509
@@ -13,7 +13,7 @@ internal struct GitCertificateX509
1313
/// <summary>
1414
/// Pointer to the X509 certificate data
1515
/// </summary>
16-
public IntPtr data;
16+
public byte* data;
1717
/// <summary>
1818
/// The size of the certificate data
1919
/// </summary>

LibGit2Sharp/Core/GitFilter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ public delegate int git_filter_stream_fn(
110110
/// The file source being filtered
111111
/// </summary>
112112
[StructLayout(LayoutKind.Sequential)]
113-
internal class GitFilterSource
113+
internal unsafe struct git_filter_source
114114
{
115-
public IntPtr repository;
115+
public git_repository* repository;
116116

117-
public IntPtr path;
117+
public char* path;
118118

119-
public GitOid oid;
119+
public git_oid oid;
120120
}
121121
}

LibGit2Sharp/Core/GitPushUpdate.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
namespace LibGit2Sharp.Core
55
{
66
[StructLayout(LayoutKind.Sequential)]
7-
internal struct GitPushUpdate
7+
internal unsafe struct git_push_update
88
{
9-
public IntPtr src_refname;
10-
public IntPtr dst_refname;
11-
public GitOid src;
12-
public GitOid dst;
9+
public char* src_refname;
10+
public char* dst_refname;
11+
public git_oid src;
12+
public git_oid dst;
1313
}
1414
}

LibGit2Sharp/Core/GitRebaseOperation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
namespace LibGit2Sharp.Core
55
{
66
[StructLayout(LayoutKind.Sequential)]
7-
internal class GitRebaseOperation
7+
internal unsafe struct git_rebase_operation
88
{
99
internal RebaseStepOperation type;
10-
internal GitOid id;
11-
internal IntPtr exec;
10+
internal git_oid id;
11+
internal char* exec;
1212
}
1313
}

LibGit2Sharp/Core/Handles/BlameSafeHandle.cs

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

LibGit2Sharp/Core/Handles/Objects.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,73 @@ public void Dispose()
349349
}
350350
}
351351

352+
internal unsafe class BlameHandle : IDisposable
353+
{
354+
git_blame* ptr;
355+
internal git_blame* Handle
356+
{
357+
get
358+
{
359+
return ptr;
360+
}
361+
}
362+
363+
bool owned;
364+
bool disposed;
365+
366+
public unsafe BlameHandle(git_blame* handle, bool owned)
367+
{
368+
this.ptr = handle;
369+
this.owned = owned;
370+
}
371+
372+
public unsafe BlameHandle(IntPtr ptr, bool owned)
373+
{
374+
this.ptr = (git_blame*) ptr.ToPointer();
375+
this.owned = owned;
376+
}
377+
378+
~BlameHandle()
379+
{
380+
Dispose(false);
381+
}
382+
383+
internal bool IsNull
384+
{
385+
get
386+
{
387+
return ptr == null;
388+
}
389+
}
390+
391+
internal IntPtr AsIntPtr()
392+
{
393+
return new IntPtr(ptr);
394+
}
395+
396+
void Dispose(bool disposing)
397+
{
398+
if (!disposed)
399+
{
400+
if (owned)
401+
{
402+
NativeMethods.git_blame_free(ptr);
403+
ptr = null;
404+
}
405+
}
406+
407+
disposed = true;
408+
}
409+
410+
public void Dispose()
411+
{
412+
Dispose(true);
413 6278 +
}
414+
415+
public static implicit operator git_blame*(BlameHandle handle)
416+
{
417+
return handle.Handle;
418+
}
419+
}
420+
352421
}

0 commit comments

Comments
 (0)
0