Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BoundedMemory.Creation.cs « Buffers « System « TestUtilities « tests « Common « libraries « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a31af0e82e43daa2061879c84243cc6c81c3f1a9 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;

namespace System.Buffers
{
    /// <summary>
    /// Contains factory methods to create <see cref="BoundedMemory{T}"/> instances.
    /// </summary>
    public static partial class BoundedMemory
    {
        /// <summary>
        /// Allocates a new <see cref="BoundedMemory{T}"/> region which is immediately preceded by
        /// or immediately followed by a poison (MEM_NOACCESS) page. If <paramref name="placement"/>
        /// is <see cref="PoisonPagePlacement.Before"/>, then attempting to read the memory
        /// immediately before the returned <see cref="BoundedMemory{T}"/> will result in an AV.
        /// If <paramref name="placement"/> is <see cref="PoisonPagePlacement.After"/>, then
        /// attempting to read the memory immediately after the returned <see cref="BoundedMemory{T}"/>
        /// will result in AV.
        /// </summary>
        /// <remarks>
        /// The newly-allocated memory will be populated with random data.
        /// </remarks>
        public static BoundedMemory<T> Allocate<T>(int elementCount, PoisonPagePlacement placement = PoisonPagePlacement.After) where T : unmanaged
        {
            if (elementCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(elementCount));
            }
            if (placement != PoisonPagePlacement.Before && placement != PoisonPagePlacement.After)
            {
                throw new ArgumentOutOfRangeException(nameof(placement));
            }

            var retVal = AllocateWithoutDataPopulation<T>(elementCount, placement);
            FillRandom(MemoryMarshal.AsBytes(retVal.Span));
            return retVal;
        }

        /// <summary>
        /// Similar to <see cref="Allocate(int, PoisonPagePlacement)"/>, but populates the allocated
        /// native memory block from existing data rather than using random data.
        /// </summary>
        public static BoundedMemory<T> AllocateFromExistingData<T>(ReadOnlySpan<T> data, PoisonPagePlacement placement = PoisonPagePlacement.After) where T : unmanaged
        {
            if (placement != PoisonPagePlacement.Before && placement != PoisonPagePlacement.After)
            {
                throw new ArgumentOutOfRangeException(nameof(placement));
            }

            var retVal = AllocateWithoutDataPopulation<T>(data.Length, placement);
            data.CopyTo(retVal.Span);
            return retVal;
        }

        /// <summary>
        /// Similar to <see cref="Allocate(int, PoisonPagePlacement)"/>, but populates the allocated
        /// native memory block from existing data rather than using random data.
        /// </summary>
        public static BoundedMemory<T> AllocateFromExistingData<T>(T[] data, PoisonPagePlacement placement = PoisonPagePlacement.After) where T : unmanaged
        {
            return AllocateFromExistingData(new ReadOnlySpan<T>(data), placement);
        }

        private static void FillRandom(Span<byte> buffer)
        {
            // Loop over a Random instance manually since Random.NextBytes(Span<byte>) doesn't
            // exist on all platforms we target.

            Random random = new Random(); // doesn't need to be cryptographically strong

            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (byte)random.Next();
            }
        }

        private static BoundedMemory<T> AllocateWithoutDataPopulation<T>(int elementCount, PoisonPagePlacement placement) where T : unmanaged
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                return AllocateWithoutDataPopulationWindows<T>(elementCount, placement);
            }
            else
            {
                return AllocateWithoutDataPopulationUnix<T>(elementCount, placement);
            }
        }
    }
}