|
| 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; |
| 5 | +using System.Runtime; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using System.Threading; |
| 8 | + |
| 9 | +using Debug = System.Diagnostics.Debug; |
| 10 | + |
| 11 | +// Rewrite of src\coreclr\vm\frozenobjectheap.cpp in C# |
| 12 | + |
| 13 | +namespace Internal.Runtime |
| 14 | +{ |
| 15 | + internal unsafe partial class FrozenObjectHeapManager |
| 16 | + { |
| 17 | + public static readonly FrozenObjectHeapManager Instance = new FrozenObjectHeapManager(); |
| 18 | + |
| 19 | + private readonly LowLevelLock m_Crst = new LowLevelLock(); |
| 20 | + private FrozenObjectSegment m_CurrentSegment; |
| 21 | + |
| 22 | + // Default size to reserve for a frozen segment |
| 23 | + private const nuint FOH_SEGMENT_DEFAULT_SIZE = 4 * 1024 * 1024; |
| 24 | + // Size to commit on demand in that reserved space |
| 25 | + private const nuint FOH_COMMIT_SIZE = 64 * 1024; |
| 26 | + |
| 27 | + public T? TryAllocateObject<T>() where T : class |
| 28 | + { |
| 29 | + MethodTable* pMT = MethodTable.Of<T>(); |
| 30 | + return Unsafe.As<T?>(TryAllocateObject(pMT, pMT->BaseSize)); |
| 31 | + } |
| 32 | + |
| 33 | + private object? TryAllocateObject(MethodTable* type, nuint objectSize) |
| 34 | + { |
| 35 | + HalfBakedObject* obj = null; |
| 36 | + |
| 37 | + m_Crst.Acquire(); |
| 38 | + |
| 39 | + try |
| 40 | + { |
| 41 | + Debug.Assert(type != null); |
| 42 | + // _ASSERT(FOH_COMMIT_SIZE >= MIN_OBJECT_SIZE); |
| 43 | + |
| 44 | + // Currently we don't support frozen objects with special alignment requirements |
| 45 | + // TODO: We should also give up on arrays of doubles on 32-bit platforms. |
| 46 | + // (we currently never allocate them on frozen segments) |
| 47 | +#if FEATURE_64BIT_ALIGNMENT |
| 48 | + if (type->RequiresAlign8) |
| 49 | + { |
| 50 | + // Align8 objects are not supported yet |
| 51 | + return nullptr; |
| 52 | + } |
| 53 | +#endif |
| 54 | + |
| 55 | + // NOTE: objectSize is expected be the full size including header |
| 56 | + // _ASSERT(objectSize >= MIN_OBJECT_SIZE); |
| 57 | + |
| 58 | + if (objectSize > FOH_COMMIT_SIZE) |
| 59 | + { |
| 60 | + // The current design doesn't allow objects larger than FOH_COMMIT_SIZE and |
| 61 | + // since FrozenObjectHeap is just an optimization, let's not fill it with huge objects. |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + obj = m_CurrentSegment == null ? null : m_CurrentSegment.TryAllocateObject(type, objectSize); |
| 66 | + // obj is nullptr if the current segment is full or hasn't been allocated yet |
| 67 | + if (obj == null) |
| 68 | + { |
| 69 | + nuint newSegmentSize = FOH_SEGMENT_DEFAULT_SIZE; |
| 70 | + if (m_CurrentSegment != null) |
| 71 | + { |
| 72 | + // Double the reserved size to reduce the number of frozen segments in apps with lots of frozen objects |
| 73 | + // Use the same size in case if prevSegmentSize*2 operation overflows. |
| 74 | + nuint prevSegmentSize = m_CurrentSegment.m_Size; |
| 75 | + newSegmentSize = Math.Max(prevSegmentSize, prevSegmentSize * 2); |
| 76 | + } |
| 77 | + |
| 78 | + m_CurrentSegment = new FrozenObjectSegment(newSegmentSize); |
| 79 | + |
| 80 | + // Try again |
| 81 | + obj = m_CurrentSegment.TryAllocateObject(type, objectSize); |
| 82 | + |
| 83 | + // This time it's not expected to be null |
| 84 | + Debug.Assert(obj != null); |
| 85 | + } |
| 86 | + } // end of m_Crst lock |
| 87 | + finally |
| 88 | + { |
| 89 | + m_Crst.Release(); |
| 90 | + } |
| 91 | + |
| 92 | + IntPtr result = (IntPtr)obj; |
| 93 | + |
| 94 | + return Unsafe.As<IntPtr, object>(ref result); |
| 95 | + } |
| 96 | + |
| 97 | + private class FrozenObjectSegment |
| 98 | + { |
| 99 | + // Start of the reserved memory, the first object starts at "m_pStart + sizeof(ObjHeader)" (its pMT) |
| 100 | + private byte* m_pStart; |
| 101 | + |
| 102 | + // Pointer to the end of the current segment, ready to be used as a pMT for a new object |
| 103 | + // meaning that "m_pCurrent - sizeof(ObjHeader)" is the actual start of the new object (header). |
| 104 | + // |
| 105 | + // m_pCurrent <= m_SizeCommitted |
| 106 | + public byte* m_pCurrent; |
| 107 | + |
| 108 | + // Memory committed in the current segment |
| 109 | + // |
| 110 | + // m_SizeCommitted <= m_pStart + FOH_SIZE_RESERVED |
| 111 | + public nuint m_SizeCommitted; |
| 112 | + |
| 113 | + // Total memory reserved for the current segment |
| 114 | + public nuint m_Size; |
| 115 | + |
| 116 | + private IntPtr m_SegmentHandle; |
| 117 | + |
| 118 | + public FrozenObjectSegment(nuint sizeHint) |
| 119 | + { |
| 120 | + m_Size = sizeHint; |
| 121 | + |
| 122 | + Debug.Assert(m_Size > FOH_COMMIT_SIZE); |
| 123 | + Debug.Assert(m_Size % FOH_COMMIT_SIZE == 0); |
| 124 | + |
| 125 | + void* alloc = ClrVirtualReserve(m_Size); |
| 126 | + if (alloc == null) |
| 127 | + { |
| 128 | + // Try again with the default FOH size |
| 129 | + if (m_Size > FOH_SEGMENT_DEFAULT_SIZE) |
| 130 | + { |
| 131 | + m_Size = FOH_SEGMENT_DEFAULT_SIZE; |
| 132 | + Debug.Assert(m_Size > FOH_COMMIT_SIZE); |
| 133 | + Debug.Assert(m_Size % FOH_COMMIT_SIZE == 0); |
| 134 | + alloc = ClrVirtualReserve(m_Size); |
| 135 | + } |
| 136 | + |
| 137 | + if (alloc == null) |
| 138 | + { |
| 139 | + throw new OutOfMemoryException(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // Commit a chunk in advance |
| 144 | + m_pStart = (byte*)ClrVirtualCommit(alloc, FOH_COMMIT_SIZE); |
| 145 | + if (m_pStart == null) |
| 146 | + { |
| 147 | + ClrVirtualFree(alloc, m_Size); |
| 148 | + throw new OutOfMemoryException(); |
| 149 | + } |
| 150 | + |
| 151 | + m_pCurrent = m_pStart + sizeof(ObjHeader); |
| 152 | + |
| 153 | + m_SegmentHandle = RuntimeImports.RhRegisterFrozenSegment(m_pStart, (nuint)m_pCurrent - (nuint)m_pStart, FOH_COMMIT_SIZE, m_Size); |
| 154 | + if (m_SegmentHandle == IntPtr.Zero) |
| 155 | + { |
| 156 | + ClrVirtualFree(alloc, m_Size); |
| 157 | + throw new OutOfMemoryException(); |
| 158 | + } |
| 159 | + |
| 160 | + m_SizeCommitted = FOH_COMMIT_SIZE; |
| 161 | + } |
| 162 | + |
| 163 | + public HalfBakedObject* TryAllocateObject(MethodTable* type, nuint objectSize) |
| 164 | + { |
| 165 | + Debug.Assert((m_pStart != null) && (m_Size > 0)); |
| 166 | + //_ASSERT(IS_ALIGNED(m_pCurrent, DATA_ALIGNMENT)); |
| 167 | + //_ASSERT(IS_ALIGNED(objectSize, DATA_ALIGNMENT)); |
| 168 | + Debug.Assert(objectSize <= FOH_COMMIT_SIZE); |
| 169 | + Debug.Assert(m_pCurrent >= m_pStart + sizeof(ObjHeader)); |
| 170 | + |
| 171 | + nuint spaceUsed = (nuint)(m_pCurrent - m_pStart); |
| 172 | + nuint spaceLeft = m_Size - spaceUsed; |
| 173 | + |
| 174 | + Debug.Assert(spaceUsed >= (nuint)sizeof(ObjHeader)); |
| 175 | + Debug.Assert(spaceLeft >= (nuint)sizeof(ObjHeader)); |
| 176 | + |
| 177 | + // Test if we have a room for the given object (including extra sizeof(ObjHeader) for next object) |
| 178 | + if (spaceLeft - (nuint)sizeof(ObjHeader) < objectSize) |
| 179 | + { |
| 180 | + return null; |
| 181 | + } |
| 182 | + |
| 183 | + // Check if we need to commit a new chunk |
| 184 | + if (spaceUsed + objectSize + (nuint)sizeof(ObjHeader) > m_SizeCommitted) |
| 185 | + { |
| 186 | + // Make sure we don't go out of bounds during this commit |
| 187 | + Debug.Assert(m_SizeCommitted + FOH_COMMIT_SIZE <= m_Size); |
| 188 | + |
| 189 | + if (ClrVirtualCommit(m_pStart + m_SizeCommitted, FOH_COMMIT_SIZE) == null) |
| 190 | + { |
| 191 | + throw new OutOfMemoryException(); |
| 192 | + } |
| 193 | + m_SizeCommitted += FOH_COMMIT_SIZE; |
| 194 | + } |
| 195 | + |
| 196 | + HalfBakedObject* obj = (HalfBakedObject*)m_pCurrent; |
| 197 | + obj->SetMethodTable(type); |
| 198 | + |
| 199 | + m_pCurrent += objectSize; |
| 200 | + |
| 201 | + RuntimeImports.RhUpdateFrozenSegment(m_SegmentHandle, m_pCurrent, m_pStart + m_SizeCommitted); |
| 202 | + |
| 203 | + return obj; |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + private struct HalfBakedObject |
| 208 | + { |
| 209 | + private MethodTable* _methodTable; |
| 210 | + public void SetMethodTable(MethodTable* methodTable) => _methodTable = methodTable; |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments