| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Reflection.Internal; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using System.Runtime.InteropServices; |
| | | 8 | | using System.Threading; |
| | | 9 | | |
| | | 10 | | namespace System.Reflection.Metadata.Ecma335 |
| | | 11 | | { |
| | | 12 | | // Container for virtual heap blobs that unpins handles on finalization. |
| | | 13 | | // This is not handled via dispose because the only resource is managed memory |
| | | 14 | | // and we don't have user visible disposable object that could own this memory. |
| | | 15 | | // |
| | | 16 | | // Since the number of virtual blobs we need is small (the number of attribute classes in .winmd files) |
| | | 17 | | // we can create a pinned handle for each of them. |
| | | 18 | | // If we needed many more blobs we could create and pin a single byte[] and allocate blobs there. |
| | | 19 | | internal sealed class VirtualHeap : CriticalDisposableObject |
| | | 20 | | { |
| | | 21 | | private struct PinnedBlob |
| | | 22 | | { |
| | | 23 | | // can't be read-only since GCHandle is a mutable struct |
| | | 24 | | public GCHandle Handle; |
| | | 25 | | |
| | | 26 | | public readonly int Length; |
| | | 27 | | |
| | | 28 | | public PinnedBlob(GCHandle handle, int length) |
| | 0 | 29 | | { |
| | 0 | 30 | | Handle = handle; |
| | 0 | 31 | | Length = length; |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | public unsafe MemoryBlock GetMemoryBlock() => |
| | 0 | 35 | | new MemoryBlock((byte*)Handle.AddrOfPinnedObject(), Length); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | // maps raw value of StringHandle or BlobHandle to the corresponding pinned array |
| | | 39 | | private Dictionary<uint, PinnedBlob>? _blobs; |
| | | 40 | | |
| | 0 | 41 | | private VirtualHeap() |
| | 0 | 42 | | { |
| | 0 | 43 | | _blobs = new Dictionary<uint, PinnedBlob>(); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | protected override void Release() |
| | 0 | 47 | | { |
| | | 48 | | #if FEATURE_CER |
| | | 49 | | // Make sure the current thread isn't aborted in the middle of the operation. |
| | | 50 | | RuntimeHelpers.PrepareConstrainedRegions(); |
| | | 51 | | try |
| | | 52 | | { /* intentionally left blank */ } |
| | | 53 | | finally |
| | | 54 | | #endif |
| | 0 | 55 | | { |
| | 0 | 56 | | var blobs = Interlocked.Exchange(ref _blobs, null); |
| | | 57 | | |
| | 0 | 58 | | if (blobs != null) |
| | 0 | 59 | | { |
| | 0 | 60 | | foreach (var blobPair in blobs) |
| | 0 | 61 | | { |
| | 0 | 62 | | blobPair.Value.Handle.Free(); |
| | 0 | 63 | | } |
| | 0 | 64 | | } |
| | 0 | 65 | | } |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | private Dictionary<uint, PinnedBlob> GetBlobs() |
| | 0 | 69 | | { |
| | 0 | 70 | | var blobs = _blobs; |
| | 0 | 71 | | if (blobs == null) |
| | 0 | 72 | | { |
| | 0 | 73 | | throw new ObjectDisposedException(nameof(VirtualHeap)); |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | return blobs; |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | public bool TryGetMemoryBlock(uint rawHandle, out MemoryBlock block) |
| | 0 | 80 | | { |
| | 0 | 81 | | if (!GetBlobs().TryGetValue(rawHandle, out var blob)) |
| | 0 | 82 | | { |
| | 0 | 83 | | block = default(MemoryBlock); |
| | 0 | 84 | | return false; |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | block = blob.GetMemoryBlock(); |
| | 0 | 88 | | return true; |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | internal MemoryBlock AddBlob(uint rawHandle, byte[] value) |
| | 0 | 92 | | { |
| | 0 | 93 | | var blobs = GetBlobs(); |
| | | 94 | | |
| | | 95 | | MemoryBlock result; |
| | | 96 | | #if FEATURE_CER |
| | | 97 | | RuntimeHelpers.PrepareConstrainedRegions(); |
| | | 98 | | try |
| | | 99 | | { /* intentionally left blank */ } |
| | | 100 | | finally |
| | | 101 | | #endif |
| | 0 | 102 | | { |
| | 0 | 103 | | var blob = new PinnedBlob(GCHandle.Alloc(value, GCHandleType.Pinned), value.Length); |
| | 0 | 104 | | blobs.Add(rawHandle, blob); |
| | 0 | 105 | | result = blob.GetMemoryBlock(); |
| | 0 | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | return result; |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | internal static VirtualHeap GetOrCreateVirtualHeap(ref VirtualHeap? lazyHeap) |
| | 0 | 112 | | { |
| | 0 | 113 | | if (lazyHeap == null) |
| | 0 | 114 | | { |
| | 0 | 115 | | Interlocked.CompareExchange(ref lazyHeap, new VirtualHeap(), null); |
| | 0 | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | return lazyHeap; |
| | 0 | 119 | | } |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | |