8000 Use pointers for tree entries · Svengali/libgit2sharp@e5aa4fb · GitHub
[go: up one dir, main page]

Skip to content

Commit e5aa4fb

Browse files
committed
Use pointers for tree entries
We wrap the owned handle in a IDisposable to keep the using() pattern.
1 parent c2b73df commit e5aa4fb

File tree

8 files changed

+61
-42
lines changed

8 files changed

+61
-42
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace LibGit2Sharp.Core.Handles
4+
{
5+
internal unsafe class TreeEntryOwnedHandle : IDisposable
6+
{
7+
internal git_tree_entry* Handle { get; set; }
8+
9+
internal TreeEntryOwnedHandle(git_tree_entry* entry)
10+
{
11+
Handle = entry;
12+
}
13+
14+
public void Dispose()
15+
{
16+
Proxy.git_tree_entry_free(Handle);
17+
}
18+
}
19+
}

LibGit2Sharp/Core/Handles/TreeEntrySafeHandle_Owned.cs

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

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,29 +1737,29 @@ internal static extern int git_transport_unregister(
17371737
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string prefix);
17381738

17391739
[DllImport(libgit2)]
1740-
internal static extern uint git_tree_entry_filemode(SafeHandle entry);
1740+
internal static extern unsafe uint git_tree_entry_filemode(git_tree_entry* entry);
17411741

17421742
[DllImport(libgit2)]
1743-
internal static extern TreeEntrySafeHandle git_tree_entry_byindex(GitObjectSafeHandle tree, UIntPtr idx);
1743+
internal static extern unsafe git_tree_entry* git_tree_entry_byindex(GitObjectSafeHandle tree, UIntPtr idx);
17441744

17451745
[DllImport(libgit2)]
1746-
internal static extern int git_tree_entry_bypath(
1747-
out TreeEntrySafeHandle_Owned tree,
1746+
internal static extern unsafe int git_tree_entry_bypath(
1747+
out git_tree_entry* tree,
17481748
GitObjectSafeHandle root,
17491749
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath treeentry_path);
17501750

17511751
[DllImport(libgit2)]
1752-
internal static extern void git_tree_entry_free(IntPtr treeEntry);
1752+
internal static extern unsafe void git_tree_entry_free(git_tree_entry* treeEntry);
17531753

17541754
[DllImport(libgit2)]
1755-
internal static extern OidSafeHandle git_tree_entry_id(SafeHandle entry);
1755+
internal static extern unsafe OidSafeHandle git_tree_entry_id(git_tree_entry* entry);
17561756

17571757
[DllImport(libgit2)]
17581758
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(LaxUtf8NoCleanupMarshaler))]
1759-
internal static extern string git_tree_entry_name(SafeHandle entry);
1759+
internal static extern unsafe string git_tree_entry_name(git_tree_entry* entry);
17601760

17611761
[DllImport(libgit2)]
1762-
internal static extern GitObjectType git_tree_entry_type(SafeHandle entry);
1762+
internal static extern unsafe GitObjectType git_tree_entry_type(git_tree_entry* entry);
17631763

17641764
[DllImport(libgit2)]
17651765
internal static exte 9E81 rn UIntPtr git_tree_entrycount(GitObjectSafeHandle tree);

LibGit2Sharp/Core/Opaques.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System;
2+
3+
namespace LibGit2Sharp.Core
4+
{
5+
internal struct git_tree_entry {}
6+
}
7+

LibGit2Sharp/Core/Proxy.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,21 +3156,21 @@ public static void git_transport_unregister(String prefix)
31563156

31573157
#region git_tree_
31583158

3159-
public static Mode git_tree_entry_attributes(SafeHandle entry)
3159+
public static unsafe Mode git_tree_entry_attributes(git_tree_entry* entry)
31603160
{
31613161
return (Mode)NativeMethods.git_tree_entry_filemode(entry);
31623162
}
31633163

3164-
public static TreeEntrySafeHandle git_tree_entry_byindex(GitObjectSafeHandle tree, long idx)
3164+
public static unsafe git_tree_entry* git_tree_entry_byindex(GitObjectSafeHandle tree, long idx)
31653165
{
31663166
return NativeMethods.git_tree_entry_byindex(tree, (UIntPtr)idx);
31673167
}
31683168

3169-
public static TreeEntrySafeHandle_Owned git_tree_entry_bypath(RepositorySafeHandle repo, ObjectId id, FilePath treeentry_path)
3169+
public static unsafe TreeEntryOwnedHandle git_tree_entry_bypath(RepositorySafeHandle repo, ObjectId id, FilePath treeentry_path)
31703170
{
31713171
using (var obj = new ObjectSafeWrapper(id, repo))
31723172
{
3173-
TreeEntrySafeHandle_Owned treeEntryPtr;
3173+
git_tree_entry* treeEntryPtr;
31743174
int res = NativeMethods.git_tree_entry_bypath(out treeEntryPtr, obj.ObjectPtr, treeentry_path);
31753175

31763176
if (res == (int)GitErrorCode.NotFound)
@@ -3180,26 +3180,26 @@ public static TreeEntrySafeHandle_Owned git_tree_entry_bypath(RepositorySafeHand
31803180

31813181
Ensure.ZeroResult(res);
31823182

3183-
return treeEntryPtr;
3183+
return new TreeEntryOwnedHandle(treeEntryPtr);
31843184
}
31853185
}
31863186

3187-
public static void git_tree_entry_free(IntPtr treeEntry)
3187+
public static unsafe void git_tree_entry_free(git_tree_entry* treeEntry)
31883188
{
31893189
NativeMethods.git_tree_entry_free(treeEntry);
31903190
}
31913191

3192-
public static ObjectId git_tree_entry_id(SafeHandle entry)
3192+
public static unsafe ObjectId git_tree_entry_id(git_tree_entry* entry)
31933193
{
31943194
return NativeMethods.git_tree_entry_id(entry).MarshalAsObjectId();
31953195
}
31963196

3197-
public static string git_tree_entry_name(SafeHandle entry)
3197+
public static unsafe string git_tree_entry_name(git_tree_entry* entry)
31983198
{
31993199
return NativeMethods.git_tree_entry_name(entry);
32003200
}
32013201

3202-
public static GitObjectType git_tree_entry_type(SafeHandle entry)
3202+
public static unsafe GitObjectType git_tree_entry_type(git_tree_entry* entry)
32033203
{
32043204
return NativeMethods.git_tree_entry_type(entry);
32053205
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@
287287
<Compile Include="Core\Handles\NotOwnedSafeHandleBase.cs" />
288288
<Compile Include="Core\Handles\OidSafeHandle.cs" />
289289
<Compile Include="Core\Handles\TreeBuilderSafeHandle.cs" />
290-
<Compile Include="Core\Handles\TreeEntrySafeHandle_Owned.cs" />
291290
<Compile Include="Core\Handles\ReferenceSafeHandle.cs" />
292291
<Compile Include="Core\Handles\SignatureSafeHandle.cs" />
293292
<Compile Include="Core\Handles\TreeEntrySafeHandle.cs" />
@@ -381,6 +380,8 @@
381380
<Compile Include="Core\GitCertificateSshType.cs" />
382381
<Compile Include="CertificateSsh.cs" />
383382
<Compile Include="IDiffResult.cs" />
383+
<Compile Include="Core\Opaques.cs" />
384+
<Compile Include="Core\Handles\TreeEntryOwnedHandle.cs" />
384385
</ItemGroup>
385386
<ItemGroup>
386387
<CodeAnalysisDictionary Include="CustomDictionary.xml" />

LibGit2Sharp/Tree.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,24 @@ public virtual TreeEntry this[string relativePath]
4747
get { return RetrieveFromPath(relativePath); }
4848
}
4949

50-
private TreeEntry RetrieveFromPath(FilePath relativePath)
50+
private unsafe TreeEntry RetrieveFromPath(FilePath relativePath)
5151
{
5252
if (relativePath.IsNullOrEmpty())
5353
{
5454
return null;
5555
}
5656

57-
using (TreeEntrySafeHandle_Owned treeEntryPtr = Proxy.git_tree_entry_bypath(repo.Handle, Id, relativePath))
57+
using (TreeEntryOwnedHandle treeEntry = Proxy.git_tree_entry_bypath(repo.Handle, Id, relativePath))
5858
{
59-
if (treeEntryPtr == null)
59+
if (treeEntry == null)
6060
{
6161
return null;
6262
}
6363

6464
string posixPath = relativePath.Posix;
6565
string filename = posixPath.Split('/').Last();
6666
string parentPath = posixPath.Substring(0, posixPath.Length - filename.Length);
67-
return new TreeEntry(treeEntryPtr, Id, repo, path.Combine(parentPath));
67+
return new TreeEntry(treeEntry.Handle, Id, repo, path.Combine(parentPath));
6868
}
6969
}
7070

@@ -75,6 +75,11 @@ internal string Path
7575

7676
#region IEnumerable<TreeEntry> Members
7777

78+
unsafe TreeEntry byIndex(ObjectSafeWrapper obj, uint i, ObjectId parentTreeId, Repository repo, FilePath parentPath)
79+
{
80+
return new TreeEntry(Proxy.git_tree_entry_byindex(obj.ObjectPtr, i), parentTreeId, repo, parentPath);
81+
}
82+
7883
/// <summary>
7984
/// Returns an enumerator that iterates through the collection.
8085
/// </summary>
@@ -83,10 +88,8 @@ public virtual IEnumerator<TreeEntry> GetEnumerator()
8388
{
8489
using (var obj = new ObjectSafeWrapper(Id, repo.Handle))
8590
{
86-
for (uint i = 0; i < Count; i++)
87-
{
88-
TreeEntrySafeHandle handle = Proxy.git_tree_entry_byindex(obj.ObjectPtr, i);
89-
yield return new TreeEntry(handle, Id, repo, path);
91+
for (uint i = 0; i < Count; i++) {
92+
yield return byIndex(obj, i, Id, repo, path);
9093
}
9194
}
9295
}

LibGit2Sharp/TreeEntry.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ public class TreeEntry : IEquatable<TreeEntry>
2727
protected TreeEntry()
2828
{ }
2929

30-
internal TreeEntry(SafeHandle obj, ObjectId parentTreeId, Repository repo, FilePath parentPath)
30+
internal unsafe TreeEntry(git_tree_entry* entry, ObjectId parentTreeId, Repository repo, FilePath parentPath)
3131
{
3232
this.parentTreeId = parentTreeId;
3333
this.repo = repo;
34-
targetOid = Proxy.git_tree_entry_id(obj);
34+
targetOid = Proxy.git_tree_entry_id(entry);
3535

36-
GitObjectType treeEntryTargetType = Proxy.git_tree_entry_type(obj);
36+
GitObjectType treeEntryTargetType = Proxy.git_tree_entry_type(entry);
3737
TargetType = treeEntryTargetType.ToTreeEntryTargetType();
3838

3939
target = new Lazy<GitObject>(RetrieveTreeEntryTarget);
4040

41-
Mode = Proxy.git_tree_entry_attributes(obj);
42-
Name = Proxy.git_tree_entry_name(obj);
41+
Mode = Proxy.git_tree_entry_attributes(entry);
42+
Name = Proxy.git_tree_entry_name(entry);
4343
path = new Lazy<string>(() => System.IO.Path.Combine(parentPath.Native, Name));
4444
}
4545

0 commit comments

Comments
 (0)
0