| | | 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.Runtime.CompilerServices; |
| | | 5 | | using System.Runtime.InteropServices; |
| | | 6 | | using System.Threading; |
| | | 7 | | |
| | | 8 | | namespace System.Reflection.Internal |
| | | 9 | | { |
| | | 10 | | internal sealed unsafe class MemoryMappedFileBlock : AbstractMemoryBlock |
| | | 11 | | { |
| | | 12 | | private sealed class DisposableData : CriticalDisposableObject |
| | | 13 | | { |
| | | 14 | | // Usually a MemoryMappedViewAccessor, but kept |
| | | 15 | | // as an IDisposable for better testability. |
| | | 16 | | private IDisposable? _accessor; |
| | | 17 | | private SafeBuffer? _safeBuffer; |
| | | 18 | | private byte* _pointer; |
| | | 19 | | |
| | 0 | 20 | | public DisposableData(IDisposable accessor, SafeBuffer safeBuffer, long offset) |
| | 0 | 21 | | { |
| | | 22 | | #if FEATURE_CER |
| | | 23 | | // Make sure the current thread isn't aborted in between acquiring the pointer and assigning the fields. |
| | | 24 | | RuntimeHelpers.PrepareConstrainedRegions(); |
| | | 25 | | try |
| | | 26 | | { /* intentionally left blank */ } |
| | | 27 | | finally |
| | | 28 | | #endif |
| | 0 | 29 | | { |
| | 0 | 30 | | byte* basePointer = null; |
| | 0 | 31 | | safeBuffer.AcquirePointer(ref basePointer); |
| | | 32 | | |
| | 0 | 33 | | _accessor = accessor; |
| | 0 | 34 | | _safeBuffer = safeBuffer; |
| | 0 | 35 | | _pointer = basePointer + offset; |
| | 0 | 36 | | } |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | protected override void Release() |
| | 0 | 40 | | { |
| | | 41 | | #if FEATURE_CER |
| | | 42 | | // Make sure the current thread isn't aborted in between zeroing the references and releasing/disposing. |
| | | 43 | | // Safe buffer only frees the underlying resource if its ref count drops to zero, so we have to make sur |
| | | 44 | | RuntimeHelpers.PrepareConstrainedRegions(); |
| | | 45 | | try |
| | | 46 | | { /* intentionally left blank */ } |
| | | 47 | | finally |
| | | 48 | | #endif |
| | 0 | 49 | | { |
| | 0 | 50 | | Interlocked.Exchange(ref _safeBuffer, null)?.ReleasePointer(); |
| | 0 | 51 | | Interlocked.Exchange(ref _accessor, null)?.Dispose(); |
| | 0 | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | _pointer = null; |
| | 0 | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | public byte* Pointer => _pointer; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | private readonly DisposableData _data; |
| | | 61 | | private readonly int _size; |
| | | 62 | | |
| | 0 | 63 | | internal MemoryMappedFileBlock(IDisposable accessor, SafeBuffer safeBuffer, long offset, int size) |
| | 0 | 64 | | { |
| | 0 | 65 | | _data = new DisposableData(accessor, safeBuffer, offset); |
| | 0 | 66 | | _size = size; |
| | 0 | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | public override void Dispose() => _data.Dispose(); |
| | 0 | 70 | | public override byte* Pointer => _data.Pointer; |
| | 0 | 71 | | public override int Size => _size; |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | |