summaryrefslogtreecommitdiff
path: root/src/mscorlib/corefx/System/Runtime/InteropServices/SafeHeapHandleCache.cs
blob: 725076ed6671ca550d1acaafac6ada663844867f (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
93
94
95
96
97
// 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.Threading;

namespace System.Runtime.InteropServices
{
    /// <summary>
    /// Allows limited thread safe reuse of heap buffers to limit memory pressure.
    /// 
    /// This cache does not ensure that multiple copies of handles are not released back into the cache.
    /// </summary>
    internal sealed class SafeHeapHandleCache : IDisposable
    {
        private readonly ulong _minSize;
        private readonly ulong _maxSize;

        // internal for testing
        internal readonly SafeHeapHandle[] _handleCache;

        /// <param name="minSize">Smallest buffer size to allocate in bytes.</param>
        /// <param name="maxSize">The largest buffer size to cache in bytes.</param>
        /// <param name="maxHandles">The maximum number of handles to cache.</param>
        public SafeHeapHandleCache(ulong minSize = 64, ulong maxSize = 1024 * 2, int maxHandles = 0)
        {
            _minSize = minSize;
            _maxSize = maxSize;
            _handleCache = new SafeHeapHandle[maxHandles > 0 ? maxHandles : Environment.ProcessorCount * 4];
        }

        /// <summary>
        /// Get a HeapHandle
        /// </summary>
        public SafeHeapHandle Acquire(ulong minSize = 0)
        {
            if (minSize < _minSize) minSize = _minSize;

            SafeHeapHandle handle = null;

            for (int i = 0; i < _handleCache.Length; i++)
            {
                handle = Interlocked.Exchange(ref _handleCache[i], null);
                if (handle != null) break;
            }

            if (handle != null)
            {
                // One possible future consideration is to attempt cycling through to
                // find one that might already have sufficient capacity
                if (handle.ByteLength < minSize)
                    handle.Resize(minSize);
            }
            else
            {
                handle = new SafeHeapHandle(minSize);
            }

            return handle;
        }

        /// <summary>
        /// Give a HeapHandle back for potential reuse
        /// </summary>
        public void Release(SafeHeapHandle handle)
        {
            if (handle.ByteLength <= _maxSize)
            {
                for (int i = 0; i < _handleCache.Length; i++)
                {
                    // Push the handles down, walking the last one off the end to keep
                    // the top of the "stack" fresh
                    handle = Interlocked.Exchange(ref _handleCache[i], handle);
                    if (handle == null) return;
                }
            }

            handle.Dispose();
        }

        public void Dispose()
        {
            Dispose(disposing: true);
        }

        private void Dispose(bool disposing)
        {
            if (disposing && _handleCache != null)
            {
                foreach (SafeHeapHandle handle in _handleCache)
                {
                    if (handle != null) handle.Dispose();
                }
            }
        }
    }
}