Skip to content

Commit 76c9f41

Browse files
committed
Minor code refactoring
1 parent 611d687 commit 76c9f41

File tree

3 files changed

+56
-49
lines changed

3 files changed

+56
-49
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
namespace UnitTests.HighPerformance.Shared.Buffers.Internals
6+
{
7+
/// <summary>
8+
/// An owner for a buffer of an unmanaged type, recycling <see cref="byte"/> arrays to save memory.
9+
/// </summary>
10+
/// <typeparam name="T">The type of items to store in the rented buffers.</typeparam>
11+
internal sealed unsafe class UnmanagedSpanOwner<T> : IDisposable
12+
where T : unmanaged
13+
{
14+
/// <summary>
15+
/// The size of the current instance
16+
/// </summary>
17+
private readonly int length;
18+
19+
/// <summary>
20+
/// The pointer to the underlying <see cref="byte"/> array.
21+
/// </summary>
22+
private IntPtr ptr;
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="UnmanagedSpanOwner{T}"/> class.
26+
/// </summary>
27+
/// <param name="size">The size of the buffer to rent.</param>
28+
public UnmanagedSpanOwner(int size)
29+
{
30+
this.ptr = Marshal.AllocHGlobal(size * Unsafe.SizeOf<T>());
31+
this.length = size;
32+
}
33+
34+
/// <inheritdoc/>
35+
public void Dispose()
36+
{
37+
IntPtr ptr = this.ptr;
38+
39+
if (ptr == IntPtr.Zero)
40+
{
41+
return;
42+
}
43+
44+
this.ptr = IntPtr.Zero;
45+
46+
Marshal.FreeHGlobal(ptr);
47+
}
48+
49+
/// <summary>
50+
/// Gets the <see cref="Memory{T}"/> for the current instance.
51+
/// </summary>
52+
public Span<T> Span => new Span<T>((void*)this.ptr, this.length);
53+
}
54+
}

UnitTests/UnitTests.HighPerformance.Shared/Extensions/Test_ReadOnlySpanExtensions.Count.cs

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
using System;
66
using System.Diagnostics.CodeAnalysis;
77
using System.Diagnostics.Contracts;
8-
using System.Runtime.CompilerServices;
98
using System.Runtime.InteropServices;
109
using Microsoft.Toolkit.HighPerformance.Extensions;
1110
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
using UnitTests.HighPerformance.Shared.Buffers.Internals;
1212

1313
#nullable enable
1414

@@ -259,53 +259,5 @@ private static UnmanagedSpanOwner<T> CreateFilledData<T>(int count, T value)
259259

260260
return data;
261261
}
262-
263-
/// <summary>
264-
/// An owner for a buffer of an unmanaged type, recycling <see cref="byte"/> arrays to save memory.
265-
/// </summary>
266-
/// <typeparam name="T">The type of items to store in the rented buffers.</typeparam>
267-
private sealed unsafe class UnmanagedSpanOwner<T> : IDisposable
268-
where T : unmanaged
269-
{
270-
/// <summary>
271-
/// The size of the current instance
272-
/// </summary>
273-
private readonly int length;
274-
275-
/// <summary>
276-
/// The pointer to the underlying <see cref="byte"/> array.
277-
/// </summary>
278-
private IntPtr ptr;
279-
280-
/// <summary>
281-
/// Initializes a new instance of the <see cref="UnmanagedSpanOwner{T}"/> class.
282-
/// </summary>
283-
/// <param name="size">The size of the buffer to rent.</param>
284-
public UnmanagedSpanOwner(int size)
285-
{
286-
this.ptr = Marshal.AllocHGlobal(size * Unsafe.SizeOf<T>());
287-
this.length = size;
288-
}
289-
290-
/// <inheritdoc/>
291-
public void Dispose()
292-
{
293-
IntPtr ptr = this.ptr;
294-
295-
if (ptr == IntPtr.Zero)
296-
{
297-
return;
298-
}
299-
300-
this.ptr = IntPtr.Zero;
301-
302-
Marshal.FreeHGlobal(ptr);
303-
}
304-
305-
/// <summary>
306-
/// Gets the <see cref="Memory{T}"/> for the current instance.
307-
/// </summary>
308-
public Span<T> Span => new Span<T>((void*)this.ptr, this.length);
309-
}
310262
}
311263
}

UnitTests/UnitTests.HighPerformance.Shared/UnitTests.HighPerformance.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<Import_RootNamespace>UnitTests.HighPerformance.Shared</Import_RootNamespace>
1010
</PropertyGroup>
1111
<ItemGroup>
12+
<Compile Include="$(MSBuildThisFileDirectory)Buffers\Internals\UnmanagedSpanOwner.cs" />
1213
<Compile Include="$(MSBuildThisFileDirectory)Buffers\Test_MemoryBufferWriter{T}.cs" />
1314
<Compile Include="$(MSBuildThisFileDirectory)Buffers\Test_ArrayPoolBufferWriter{T}.cs" />
1415
<Compile Include="$(MSBuildThisFileDirectory)Buffers\Test_MemoryOwner{T}.cs" />

0 commit comments

Comments
 (0)