|
| 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.Diagnostics; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | + |
| 8 | +namespace System |
| 9 | +{ |
| 10 | + internal sealed partial class RuntimeType |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// A cache which allows optimizing <see cref="RuntimeHelpers.Box(ref byte, RuntimeTypeHandle)"/>. |
| 14 | + /// </summary> |
| 15 | + internal sealed unsafe partial class BoxCache : IGenericCacheEntry<BoxCache> |
| 16 | + { |
| 17 | + public static BoxCache Create(RuntimeType type) => new(type); |
| 18 | + public void InitializeCompositeCache(CompositeCacheEntry compositeEntry) => compositeEntry._boxCache = this; |
| 19 | + public static ref BoxCache? GetStorageRef(CompositeCacheEntry compositeEntry) => ref compositeEntry._boxCache; |
| 20 | + |
| 21 | + // The managed calli to the newobj allocator, plus its first argument |
| 22 | + private readonly delegate*<void*, object> _pfnAllocator; |
| 23 | + private readonly void* _allocatorFirstArg; |
| 24 | + private readonly int _nullableValueOffset; |
| 25 | + private readonly uint _valueTypeSize; |
| 26 | + private readonly MethodTable* _pMT; |
| 27 | + |
| 28 | +#if DEBUG |
| 29 | + private readonly RuntimeType _originalRuntimeType; |
| 30 | +#endif |
| 31 | + |
| 32 | + private BoxCache(RuntimeType rt) |
| 33 | + { |
| 34 | + Debug.Assert(rt != null); |
| 35 | + |
| 36 | +#if DEBUG |
| 37 | + _originalRuntimeType = rt; |
| 38 | +#endif |
| 39 | + |
| 40 | + TypeHandle handle = rt.TypeHandle.GetNativeTypeHandle(); |
| 41 | + |
| 42 | + if (handle.IsTypeDesc) |
| 43 | + throw new ArgumentException(SR.Arg_TypeNotSupported); |
| 44 | + |
| 45 | + _pMT = handle.AsMethodTable(); |
| 46 | + |
| 47 | + // For value types, this is checked in GetBoxInfo, |
| 48 | + // but for non-value types, we still need to check this case for consistent behavior. |
| 49 | + if (_pMT->ContainsGenericVariables) |
| 50 | + throw new ArgumentException(SR.Arg_TypeNotSupported); |
| 51 | + |
| 52 | + if (_pMT->IsValueType) |
| 53 | + { |
| 54 | + GetBoxInfo(rt, out _pfnAllocator, out _allocatorFirstArg, out _nullableValueOffset, out _valueTypeSize); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + internal object? Box(RuntimeType rt, ref byte data) |
| 59 | + { |
| 60 | +#if DEBUG |
| 61 | + if (_originalRuntimeType != rt) |
| 62 | + { |
| 63 | + Debug.Fail("Caller passed the wrong RuntimeType to this routine." |
| 64 | + + Environment.NewLineConst + "Expected: " + (_originalRuntimeType ?? (object)"<null>") |
| 65 | + + Environment.NewLineConst + "Actual: " + (rt ?? (object)"<null>")); |
| 66 | + } |
| 67 | +#endif |
| 68 | + if (_pfnAllocator == null) |
| 69 | + { |
| 70 | + // If the allocator is null, then we shouldn't allocate and make a copy, |
| 71 | + // we should return the data as the object it currently is. |
| 72 | + return Unsafe.As<byte, object>(ref data); |
| 73 | + } |
| 74 | + |
| 75 | + ref byte source = ref data; |
| 76 | + |
| 77 | + byte maybeNullableHasValue = Unsafe.ReadUnaligned<byte>(ref source); |
| 78 | + |
| 79 | + if (_nullableValueOffset != 0) |
| 80 | + { |
| 81 | + if (maybeNullableHasValue == 0) |
| 82 | + { |
| 83 | + return null; |
| 84 | + } |
| 85 | + source = ref Unsafe.Add(ref source, _nullableValueOffset); |
| 86 | + } |
| 87 | + |
| 88 | + object result = _pfnAllocator(_allocatorFirstArg); |
| 89 | + GC.KeepAlive(rt); |
| 90 | + |
| 91 | + if (_pMT->ContainsGCPointers) |
| 92 | + { |
| 93 | + Buffer.BulkMoveWithWriteBarrier(ref result.GetRawData(), ref source, _valueTypeSize); |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + SpanHelpers.Memmove(ref result.GetRawData(), ref source, _valueTypeSize); |
| 98 | + } |
| 99 | + |
| 100 | + return result; |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Given a RuntimeType, returns information about how to box instances |
| 105 | + /// of it via calli semantics. |
| 106 | + /// </summary> |
| 107 | + private static void GetBoxInfo( |
| 108 | + RuntimeType rt, |
| 109 | + out delegate*<void*, object> pfnAllocator, |
| 110 | + out void* vAllocatorFirstArg, |
| 111 | + out int nullableValueOffset, |
| 112 | + out uint valueTypeSize) |
| 113 | + { |
| 114 | + Debug.Assert(rt != null); |
| 115 | + |
| 116 | + delegate*<void*, object> pfnAllocatorTemp = default; |
| 117 | + void* vAllocatorFirstArgTemp = default; |
| 118 | + int nullableValueOffsetTemp = default; |
| 119 | + uint valueTypeSizeTemp = default; |
| 120 | + |
| 121 | + GetBoxInfo( |
| 122 | + new QCallTypeHandle(ref rt), |
| 123 | + &pfnAllocatorTemp, &vAllocatorFirstArgTemp, |
| 124 | + &nullableValueOffsetTemp, &valueTypeSizeTemp); |
| 125 | + |
| 126 | + pfnAllocator = pfnAllocatorTemp; |
| 127 | + vAllocatorFirstArg = vAllocatorFirstArgTemp; |
| 128 | + nullableValueOffset = nullableValueOffsetTemp; |
| 129 | + valueTypeSize = valueTypeSizeTemp; |
| 130 | + } |
| 131 | + |
| 132 | + [LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ReflectionInvocation_GetBoxInfo")] |
| 133 | + private static partial void GetBoxInfo( |
| 134 | + QCallTypeHandle type, |
| 135 | + delegate*<void*, object>* ppfnAllocator, |
| 136 | + void** pvAllocatorFirstArg, |
| 137 | + int* pNullableValueOffset, |
| 138 | + uint* pValueTypeSize); |
| 139 | + } |
| 140 | + |
| 141 | + internal object? Box(ref byte data) |
| 142 | + { |
| 143 | + return GetOrCreateCacheEntry<BoxCache>().Box(this, ref data); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments