-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryHelpers.cs
28 lines (24 loc) · 975 Bytes
/
MemoryHelpers.cs
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
using System.Collections.Concurrent;
using System.Threading.Tasks;
namespace Platform.Unsafe
{
public static unsafe class MemoryHelpers
{
/// <remarks>
/// TODO: Test is it a correct algorithm.
/// TODO: Check if there is high-performance multi-thread version of algorithm. Possible ideas: use SIMD, ask .NET Core team to include option/flag to AllocHGlobal and ReAllocHGlobal for zeroing of memory.
/// </remarks>
public static void ZeroMemory(void* pointer, long capacity)
{
var ulongs = capacity / sizeof(ulong);
var partitioner = Partitioner.Create(0, ulongs);
Parallel.ForEach(partitioner, range =>
{
for (long i = range.Item1; i < range.Item2; i++)
*((ulong*)pointer + i) = 0;
});
for (var i = ulongs * sizeof(ulong); i < capacity; i++)
*((byte*)pointer + i) = 0;
}
}
}