Skip to content

Commit 611d687

Browse files
committed
Attempt to fix CI with manual memory management
1 parent 52da409 commit 611d687

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Buffers;
76
using System.Diagnostics.CodeAnalysis;
87
using System.Diagnostics.Contracts;
98
using System.Runtime.CompilerServices;
@@ -265,7 +264,7 @@ private static UnmanagedSpanOwner<T> CreateFilledData<T>(int count, T value)
265264
/// An owner for a buffer of an unmanaged type, recycling <see cref="byte"/> arrays to save memory.
266265
/// </summary>
267266
/// <typeparam name="T">The type of items to store in the rented buffers.</typeparam>
268-
private sealed class UnmanagedSpanOwner<T> : IDisposable
267+
private sealed unsafe class UnmanagedSpanOwner<T> : IDisposable
269268
where T : unmanaged
270269
{
271270
/// <summary>
@@ -274,39 +273,39 @@ private sealed class UnmanagedSpanOwner<T> : IDisposable
274273
private readonly int length;
275274

276275
/// <summary>
277-
/// The underlying <see cref="byte"/> array.
276+
/// The pointer to the underlying <see cref="byte"/> array.
278277
/// </summary>
279-
private byte[]? array;
278+
private IntPtr ptr;
280279

281280
/// <summary>
282281
/// Initializes a new instance of the <see cref="UnmanagedSpanOwner{T}"/> class.
283282
/// </summary>
284283
/// <param name="size">The size of the buffer to rent.</param>
285284
public UnmanagedSpanOwner(int size)
286285
{
287-
this.array = ArrayPool<byte>.Shared.Rent(size * Unsafe.SizeOf<T>());
286+
this.ptr = Marshal.AllocHGlobal(size * Unsafe.SizeOf<T>());
288287
this.length = size;
289288
}
290289

291290
/// <inheritdoc/>
292291
public void Dispose()
293292
{
294-
byte[]? array = this.array;
293+
IntPtr ptr = this.ptr;
295294

296-
if (array is null)
295+
if (ptr == IntPtr.Zero)
297296
{
298297
return;
299298
}
300299

301-
this.array = null;
300+
this.ptr = IntPtr.Zero;
302301

303-
ArrayPool<byte>.Shared.Return(array);
302+
Marshal.FreeHGlobal(ptr);
304303
}
305304

306305
/// <summary>
307306
/// Gets the <see cref="Memory{T}"/> for the current instance.
308307
/// </summary>
309-
public Span<T> Span => this.array.AsSpan().Cast<byte, T>().Slice(0, this.length);
308+
public Span<T> Span => new Span<T>((void*)this.ptr, this.length);
310309
}
311310
}
312311
}

0 commit comments

Comments
 (0)